Skip to content

修复竞赛接口问题 #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@
"eslint-plugin-prettier": "^3.4.0",
"glob": "^7.1.6",
"husky": "^6.0.0",
"mocha": "^7.1.2",
"mocha": "^10.4.0",
"prettier": "^2.3.0",
"typescript": "^4.3.2",
"vscode-test": "^1.3.0"
Expand All @@ -358,12 +358,12 @@
"acorn": "^8.0.4",
"algm": "^0.1.6",
"ast-types": "^0.14.2",
"axios": "^0.21.1",
"axios": "^1.7.2",
"cheerio": "^1.0.0-rc.3",
"compressing": "^1.5.1",
"fs-extra": "^9.1.0",
"highlight.js": "^11.0.0",
"markdown-it": "^10.0.0",
"markdown-it": "^14.1.0",
"pretty-object-string": "2.0.0-beta.3",
"pretty-tag": "^0.1.1",
"recast": "^0.20.4",
Expand Down
40 changes: 36 additions & 4 deletions src/api/api.cn.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios, { AxiosError, AxiosRequestConfig } from 'axios'
import axios, { AxiosError, AxiosRequestConfig, all } from 'axios'
import { cache } from '../cache'
import {
ConciseQuestion,
Expand All @@ -9,6 +9,7 @@ import {
CheckResponse,
SubmitOptions,
SubmitResponse,
ContestStatus,
TagData,
QuestionTranslationData,
TodayRecordData,
Expand Down Expand Up @@ -120,7 +121,7 @@ async function graphql<T>(data: GraphqlRequestData): Promise<T> {
method: 'POST',
headers: {
origin: DomainCN,
referer: `${DomainCN}/problemset/all/`,
referer: `${DomainCN}/contest/`,
...headers,
},
}).then((res) => {
Expand Down Expand Up @@ -195,6 +196,12 @@ const config = {
method: 'GET',
}
},
getContestStatus(titleSlug: string): AxiosRequestConfig {
return {
url: `${DomainCN}/contest/api/myranking/${titleSlug}/?region=local`,
method: 'GET',
}
},
getQuestionDetail(titleSlug: string) {
return {
operationName: 'questionData',
Expand Down Expand Up @@ -311,6 +318,22 @@ const config = {
},
}

function filterContest(allContests) {
// 过滤title相同的竞赛
const times = new Set()
const N = allContests.length
const ans: any[] = []
for (let i = 0; i < N; i++) {
const startTime = allContests[i].startTime
if (times.has(startTime)) {
continue
}
times.add(startTime)
ans.push(allContests[i])
}
return ans
}

export const api = {
refreshQuestions,
getAllQuestions,
Expand All @@ -321,14 +344,17 @@ export const api = {
return request<Problems>(config.getQuestionsByCategory(categorie))
},
fetchContests() {
return graphql<ContestData>(config.contests).then((data) => data.allContests)
return graphql<ContestData>(config.contests).then((data) => filterContest(data.allContests))
},
fetchTags(): Promise<TagData> {
return request<TagData>(config.tags)
},
fetchContest(titleSlug: string) {
return request<ContestDetail>(config.getContest(titleSlug))
},
fetchContestStatus(titleSlug: string) {
return request<ContestStatus>(config.getContestStatus(titleSlug))
},
fetchTodayRecord(): Promise<TodayRecord[]> {
return graphql<TodayRecordData>(config.todayRecord).then((data) => data.todayRecord)
},
Expand All @@ -352,7 +378,13 @@ export const api = {
return request<string>(config.getQuestionContest(titleSlug, weekname))
},
submit(options: SubmitOptions): Promise<SubmitResponse> {
return request<SubmitResponse>(config.getSubmit(options))
return request<SubmitResponse>(config.getSubmit(options)).then((data) => {
if (data && data.submission_id) {

// window.showInformationMessage(`提交通过,id: ${data.submission_id}`)
}
return data
})
},
submitContest(options: SubmitContestOptions): Promise<SubmitResponse> {
return request<SubmitResponse>(config.getSubmitContest(options))
Expand Down
2 changes: 1 addition & 1 deletion src/common/website.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Lang } from '../model/common'
import { config, DomainCN, DomainEN } from '../config'
import { config } from '../config'
export enum Website {
Leetcode = 'https://leetcode.com',
LeetcodeCn = 'https://leetcode.cn',
Expand Down
2 changes: 1 addition & 1 deletion src/model/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export function checkParams<T, R extends keyof T>(obj: T, attrs: R[]): asserts o
const verify = attrs.every((attr) => !!obj[attr])
if (!verify) {
const attr = attrs.find((attr) => !obj[attr])!
throw new Error(`options error, ${attr} is ${obj[attr]}`)
throw new Error(`options error, ${String(attr)} is ${obj[attr]}`)
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/model/question.cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ export interface SubmitContestOptions extends SubmitOptions {
}
export interface SubmitResponse {
submission_id: number

}
export interface ContestStatus {
my_solved: string[]
registered: boolean
}
export interface CheckResponse {
status_code: number
Expand Down
8 changes: 8 additions & 0 deletions src/provider/resolver.cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { ResolverType } from './resolver'
import { normalizeQuestions } from '../common/util'


export const resolverCn: ResolverType = {
Query() {
//'Company', 'Favorite',
Expand Down Expand Up @@ -143,6 +144,12 @@ export const resolverCn: ResolverType = {
}
const data = await api.fetchContest(titleSlug)
const questions = data.questions

const data2 = await api.fetchContestStatus(titleSlug)
const keys = new Set()
for (let i = 0; i < data2.my_solved.length; i++) {
keys.add(data2.my_solved[i])
}
return questions.map((question) => ({
type: 'QuestionContest',
label: question.title,
Expand All @@ -152,6 +159,7 @@ export const resolverCn: ResolverType = {
titleSlug: question.title_slug,
weekname: titleSlug,
},
isAC: keys.has(question.question_id),
}))
},
},
Expand Down
8 changes: 5 additions & 3 deletions src/webview/questionPreview.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as path from 'path'
import * as vscode from 'vscode'
import { api } from '../api/index'
import { config } from '../config'
import { DomainCN, DomainEN, config } from '../config'
import { writeFile, parseHtml } from '../common/util'
import { preprocessCode, shouldAskForImport, askForImport } from '../util'
import { CodeLang, enNameLangs, isAlgorithm, langMap } from '../common/langConfig'
Expand Down Expand Up @@ -265,9 +265,11 @@ export async function createQuestionPanelCommand(extensionPath: string, param: P
const { codeSnippets, questionFrontendId, title, content, translatedContent, translatedTitle, titleSlug } =
question
//preview
let previewText = `# ${title}\n` + content
let previewText = ''
if (config.lang === 'cn') {
previewText = `# ${translatedTitle}\n` + translatedContent
previewText = `# [${translatedTitle}](${DomainCN}/problems/${titleSlug}/description)\n` + translatedContent
} else {
previewText = `# [${title}](${DomainEN}/problems/${titleSlug}/description)\n` + content
}

const codeSnippet = getCodeSnippet(codeSnippets)
Expand Down