@icpctools/contest-api 0.0.0-stage → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,230 @@
1
+ /**
2
+ * Copyright later.
3
+ */
4
+ import type { ContestAPI } from './contest-api.js';
5
+ import { parseRelTime } from './contest-time-util.js';
6
+ import type { Access, FileReference, Problem, Submission } from './contest-types.js';
7
+
8
+ export function findById<Type extends { id: string }>(
9
+ arr: Array<Type> | undefined,
10
+ id: string | undefined
11
+ ): Type | undefined {
12
+ if (!arr || arr.length === 0 || !id) {
13
+ return undefined;
14
+ }
15
+
16
+ for (let i = 0; i < arr.length; i++) {
17
+ if (id === arr[i].id) {
18
+ return arr[i];
19
+ }
20
+ }
21
+ return undefined;
22
+ }
23
+
24
+ export function findManyById<Type extends { id: string }>(
25
+ arr: Array<Type> | undefined,
26
+ ids: string[] | undefined
27
+ ): Array<Type> | undefined {
28
+ if (!arr || arr.length === 0 || !ids || ids.length == 0) {
29
+ return undefined;
30
+ }
31
+
32
+ const list = [];
33
+ for (let j = 0; j < ids.length; j++) {
34
+ for (let i = 0; i < arr.length; i++) {
35
+ if (ids[j] === arr[i].id) {
36
+ list.push(arr[i]);
37
+ }
38
+ }
39
+ }
40
+ return list;
41
+ }
42
+
43
+ export function findManyBySubmissionId<Type extends { submission_id: string }>(
44
+ arr: Array<Type> | undefined,
45
+ id: string | undefined
46
+ ): Array<Type> | undefined {
47
+ if (!arr || arr.length === 0 || !id) {
48
+ return undefined;
49
+ }
50
+
51
+ const list = [];
52
+ for (let i = 0; i < arr.length; i++) {
53
+ if (arr[i].submission_id === id) {
54
+ list.push(arr[i]);
55
+ }
56
+ }
57
+ return list;
58
+ }
59
+
60
+ export function getOppositeTag(tag: string): string | undefined {
61
+ if ('light' === tag) {
62
+ return 'dark';
63
+ } else if ('dark' === tag) {
64
+ return 'light';
65
+ }
66
+ return undefined;
67
+ }
68
+
69
+ export function getFileByTag(files: FileReference[] | undefined, tag: string): FileReference | undefined {
70
+ if (!files || files.length == 0) {
71
+ return undefined;
72
+ }
73
+
74
+ if (files.length === 1) {
75
+ return files[0];
76
+ }
77
+
78
+ // look for a file that has the given tag
79
+ for (const file of files) {
80
+ if (file.tags) {
81
+ for (const tag2 of file.tags) {
82
+ if (tag2 === tag) {
83
+ return file;
84
+ }
85
+ }
86
+ }
87
+ }
88
+
89
+ return undefined;
90
+ }
91
+
92
+ export function bestLogo(
93
+ logos: FileReference[] | undefined,
94
+ width: number,
95
+ height: number,
96
+ tag?: string
97
+ ): FileReference | undefined {
98
+ if (!logos || logos.length === 0 || width < 1 || height < 1) {
99
+ return undefined;
100
+ }
101
+
102
+ if (logos.length === 1) {
103
+ return logos[0];
104
+ }
105
+
106
+ const matchingTags = [];
107
+ const nonOppositeTags = [];
108
+ // filter looking for files with matching tag
109
+ if (tag) {
110
+ for (const logo of logos) {
111
+ if (logo.tags) {
112
+ let found = false;
113
+ for (const tag2 of logo.tags) {
114
+ if (tag2 === tag) {
115
+ matchingTags.push(logo);
116
+ found = true;
117
+ break;
118
+ } else if (tag2 === getOppositeTag(tag)) {
119
+ found = true;
120
+ break;
121
+ }
122
+ }
123
+ if (!found) {
124
+ nonOppositeTags.push(logo);
125
+ }
126
+ } else {
127
+ nonOppositeTags.push(logo);
128
+ }
129
+ }
130
+ } else {
131
+ for (const logo of logos) {
132
+ if (!logo.tags || logo.tags.length === 0) {
133
+ matchingTags.push(logo);
134
+ }
135
+ }
136
+ }
137
+
138
+ let logos2 = logos;
139
+ if (matchingTags.length > 0) {
140
+ // If we have at least one file with the tag, use it
141
+ logos2 = matchingTags;
142
+ } else if (nonOppositeTags.length > 0) {
143
+ // Otherwise, use the list of files without any tag, if not empty
144
+ logos2 = nonOppositeTags;
145
+ }
146
+
147
+ // return an svg if possible
148
+ for (const logo of logos2) {
149
+ if ('image/svg+xml' === logo.mime) {
150
+ return logo;
151
+ }
152
+ }
153
+
154
+ let best: FileReference | undefined;
155
+ for (const ref of logos2) {
156
+ if (!best) {
157
+ best = ref;
158
+ } else {
159
+ if (best.width && best.width < width && best.height && best.height < height) {
160
+ // current best image is too small - is this one better (larger than current)?
161
+ if ((ref.width && best.width && ref.width > best.width) || (ref.height && ref.height > best.height)) best = ref;
162
+ else if (best.width > width && best.height > height) {
163
+ // current image is too big - is this one better (smaller but still big enough)?
164
+ if ((ref.width && ref.width < best.width) || (ref.height && ref.height < best.height)) {
165
+ if ((ref.width && ref.width >= width) || (ref.height && ref.height >= height)) best = ref;
166
+ }
167
+ }
168
+ }
169
+ }
170
+ }
171
+ return best;
172
+ }
173
+
174
+ export function isFirstToSolve(contest: ContestAPI, submission: Submission): boolean {
175
+ const problem_id = submission.problem_id;
176
+ const submissions = contest.getSubmissions();
177
+ if (!submissions) {
178
+ return false;
179
+ }
180
+
181
+ for (let i = 0; i < submissions.length; i++) {
182
+ const time: number | string | undefined = parseRelTime(submissions[i].contest_time);
183
+ if (time && time >= 0 && submissions[i].problem_id == problem_id) {
184
+ // TODO: should we check if this is a public team too?
185
+ const judgements = findManyBySubmissionId(contest.getJudgements(), submissions[i].id);
186
+ if (judgements && judgements.length > 0) {
187
+ const jt = findById(contest.getJudgementTypes(), judgements[judgements.length - 1].judgement_type_id);
188
+ if (jt && jt.solved) {
189
+ return submission == submissions[i];
190
+ }
191
+ }
192
+ }
193
+ }
194
+ return false;
195
+ }
196
+
197
+ export function sortProblems(problems: Problem[]): Problem[] {
198
+ return problems.sort((a, b) => (a.ordinal > b.ordinal ? 1 : b.ordinal > a.ordinal ? -1 : 0));
199
+ }
200
+
201
+ export function hasEndpoint(acc: Access | undefined, endpoint: string): boolean {
202
+ if (!acc || !acc.endpoints || !endpoint) {
203
+ return false;
204
+ }
205
+
206
+ for (const ep of acc.endpoints) {
207
+ if (endpoint === ep.type) return true;
208
+ }
209
+
210
+ return false;
211
+ }
212
+
213
+ export function hasEndpointProperty(acc: Access | undefined, endpoint: string, property: string): boolean {
214
+ if (!acc || !acc.endpoints || !endpoint || !property) {
215
+ return false;
216
+ }
217
+
218
+ for (const ep of acc.endpoints) {
219
+ if (endpoint === ep.type) {
220
+ for (const p of ep.properties) {
221
+ if (property === p) {
222
+ return true;
223
+ }
224
+ }
225
+ return false;
226
+ }
227
+ }
228
+
229
+ return false;
230
+ }
@@ -0,0 +1,78 @@
1
+ import type { Contest } from './contest-types.js';
2
+ import { ContestAPI, type Credentials } from './contest-api.js';
3
+ import { fetchOptions } from './fetch-utils.js';
4
+
5
+ export class Contests {
6
+ contests: Contest[] | undefined;
7
+ contestObjs: Contest[] | undefined;
8
+ baseURL: string;
9
+ credentials?: Credentials;
10
+ proxyURL?: string;
11
+
12
+ constructor(baseURL: string, credentials?: Credentials, proxyURL?: string) {
13
+ if (!baseURL.endsWith('/')) {
14
+ baseURL += '/';
15
+ }
16
+ this.baseURL = baseURL;
17
+ this.credentials = credentials;
18
+ this.proxyURL = proxyURL;
19
+ console.log('Contest API URL: ' + this.baseURL);
20
+ }
21
+
22
+ public async loadContests(): Promise<void> {
23
+ const url = this.baseURL + 'contests';
24
+ const startTime = performance.now();
25
+ try {
26
+ const response = await fetch(url, fetchOptions(this.credentials));
27
+ if (!response.ok) {
28
+ throw new Error(`HTTP error ${response.status} loading contests: ${response.statusText}`);
29
+ }
30
+ this.contests = JSON.parse(await response.text()) as Contest[];
31
+ const endTime = performance.now();
32
+ console.log(`Fetched ${this.baseURL} in ${(endTime - startTime).toFixed(1)}ms`);
33
+ } catch (error: unknown) {
34
+ if (error instanceof Error) {
35
+ throw new Error(`Error loading contests: ${error.message}`, { cause: error });
36
+ }
37
+ throw new Error(`Unexpected error loading contests: ${error}`, { cause: error });
38
+ }
39
+ }
40
+
41
+ getBaseURL(): string {
42
+ return this.baseURL;
43
+ }
44
+
45
+ getContests(): Contest[] | undefined {
46
+ return this.contests;
47
+ }
48
+
49
+ getContest(contestId?: string): ContestAPI | undefined {
50
+ if (!this.contests || this.contests.length === 0) {
51
+ return undefined;
52
+ }
53
+ let contestURL = this.baseURL + 'contests/';
54
+ if (contestId && contestId.length > 0) {
55
+ contestURL += contestId;
56
+ } else {
57
+ contestURL += this.contests[0].id;
58
+ }
59
+ return new ContestAPI(contestURL, this.credentials, this.proxyURL);
60
+ }
61
+
62
+ getContestObjs(): Contest[] | undefined {
63
+ if (this.contestObjs != null) return this.contestObjs;
64
+
65
+ this.contests = [];
66
+ /*for (var i = 0; i < this.contests.length; i++) {
67
+ let c = new Contest(this.baseURL, this.contests[i].id);
68
+ c.info = this.contests[i];
69
+ contests.push(c);
70
+ }*/
71
+ //this.contestObjs = contests;
72
+ return this.contestObjs;
73
+ }
74
+
75
+ clear(): void {
76
+ this.contests = [];
77
+ }
78
+ }
@@ -0,0 +1,18 @@
1
+ import type { Credentials } from './contest-api.js';
2
+
3
+ // Accept self-signed certificates — contest servers commonly use them (Node.js only)
4
+ declare const process: { env: Record<string, string | undefined> } | undefined;
5
+ if (typeof process !== 'undefined' && process?.env) {
6
+ process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
7
+ }
8
+
9
+ export function fetchOptions(credentials?: Credentials, timeoutMs = 10000): RequestInit {
10
+ const headers: Record<string, string> = {};
11
+ if (credentials?.user) {
12
+ headers['Authorization'] = 'Basic ' + btoa(credentials.user + ':' + (credentials.password ?? ''));
13
+ }
14
+ return {
15
+ headers,
16
+ signal: AbortSignal.timeout(timeoutMs)
17
+ };
18
+ }
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ export * from './contest-api.js';
2
+ export type * from './contest-types.js';
3
+ export * from './contest-time-util.js';
4
+ export * from './contest-util.js';
5
+ export * from './contests.js';
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.