@icpctools/contest-api 0.0.0-stage → 0.9.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.
- package/LICENSE +21 -0
- package/README.md +134 -2
- package/dist/contest-api.d.ts +120 -0
- package/dist/contest-api.js +795 -0
- package/dist/contest-api.js.map +1 -0
- package/dist/contest-time-util.d.ts +24 -0
- package/dist/contest-time-util.js +182 -0
- package/dist/contest-time-util.js.map +1 -0
- package/dist/contest-types.d.ts +315 -0
- package/dist/contest-types.js +12 -0
- package/dist/contest-types.js.map +1 -0
- package/dist/contest-util.d.ts +21 -0
- package/dist/contest-util.js +195 -0
- package/dist/contest-util.js.map +1 -0
- package/dist/contests.d.ts +16 -0
- package/dist/contests.js +72 -0
- package/dist/contests.js.map +1 -0
- package/dist/fetch-utils.d.ts +2 -0
- package/dist/fetch-utils.js +14 -0
- package/dist/fetch-utils.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/package.json +36 -3
- package/src/contest-api.ts +924 -0
- package/src/contest-time-util.ts +219 -0
- package/src/contest-types.ts +381 -0
- package/src/contest-util.ts +230 -0
- package/src/contests.ts +78 -0
- package/src/fetch-utils.ts +18 -0
- package/src/index.ts +5 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 ICPC Foundation
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,135 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @icpctools/contest-api
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A TypeScript library for accessing ICPC contest data via the
|
|
4
|
+
[Contest API](https://ccs-specs.icpc.io). Supports servers implementing the 2023-06 and 2026-01
|
|
5
|
+
specifications.
|
|
6
|
+
|
|
7
|
+
It loads contest data over REST, keeps it up to date by following the event feed, and notifies
|
|
8
|
+
listeners when anything changes. It is used by [Team View](https://github.com/icpctools/team-view)
|
|
9
|
+
and [@icpctools/contest-ui](https://www.npmjs.com/package/@icpctools/contest-ui).
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install @icpctools/contest-api
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
The library uses the standard `fetch` API and runs in Node.js 18+ or modern browsers.
|
|
18
|
+
|
|
19
|
+
> **Note:** Contest servers commonly use self-signed certificates, so in Node.js this library sets
|
|
20
|
+
> `NODE_TLS_REJECT_UNAUTHORIZED=0` when it is loaded. This disables TLS certificate verification
|
|
21
|
+
> for the whole process.
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
### Finding a contest
|
|
26
|
+
|
|
27
|
+
Use `Contests` to list the contests on a server and get a `ContestAPI` for one of them:
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import { Contests } from '@icpctools/contest-api';
|
|
31
|
+
|
|
32
|
+
const contests = new Contests('https://cds.example.com/api/', {
|
|
33
|
+
user: 'admin',
|
|
34
|
+
password: 'secret'
|
|
35
|
+
});
|
|
36
|
+
await contests.loadContests();
|
|
37
|
+
|
|
38
|
+
console.log(contests.getContests()); // Contest[]
|
|
39
|
+
|
|
40
|
+
// Pass a contest id, or omit it to use the first contest
|
|
41
|
+
const contest = contests.getContest('finals');
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
You can also create a `ContestAPI` directly if you already know the contest URL:
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
import { ContestAPI } from '@icpctools/contest-api';
|
|
48
|
+
|
|
49
|
+
const contest = new ContestAPI('https://cds.example.com/api/contests/finals', {
|
|
50
|
+
user: 'admin',
|
|
51
|
+
password: 'secret'
|
|
52
|
+
});
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Loading data
|
|
56
|
+
|
|
57
|
+
Each endpoint has a `load*()` method that fetches it, and a matching `get*()` method that returns
|
|
58
|
+
the cached value. Pass `true` to force a reload.
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
await contest.loadContest();
|
|
62
|
+
await contest.loadProblems();
|
|
63
|
+
await contest.loadTeams();
|
|
64
|
+
await contest.loadScoreboard();
|
|
65
|
+
|
|
66
|
+
console.log(contest.getContest()?.name);
|
|
67
|
+
console.log(contest.getProblems().map((p) => p.label));
|
|
68
|
+
console.log(contest.getScoreboard()?.rows);
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Available endpoints include contest, state, access, languages, judgement types, problems, groups,
|
|
72
|
+
organizations, teams, persons, accounts, submissions, judgements, runs, clarifications,
|
|
73
|
+
commentary, scoreboard, awards, and map info.
|
|
74
|
+
|
|
75
|
+
### Watching for live updates
|
|
76
|
+
|
|
77
|
+
`watch()` loads the full contest and then follows the event feed, keeping all data current and
|
|
78
|
+
automatically reconnecting if the connection drops. Register a listener to be notified of
|
|
79
|
+
changes:
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
contest.addContestListener((event) => {
|
|
83
|
+
// e.g. { type: 'submissions', id: '123' }
|
|
84
|
+
console.log(`Changed: ${event.type} ${event.id ?? ''}`);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
await contest.watch(); // resolves once the initial contest data has loaded
|
|
88
|
+
|
|
89
|
+
// later
|
|
90
|
+
contest.unwatch();
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
The scoreboard is not part of the event feed, so it is reloaded automatically (throttled) when
|
|
94
|
+
relevant events arrive.
|
|
95
|
+
|
|
96
|
+
### Utilities
|
|
97
|
+
|
|
98
|
+
The package also exports helpers for working with contest data:
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
import {
|
|
102
|
+
findById,
|
|
103
|
+
sortProblems,
|
|
104
|
+
bestLogo,
|
|
105
|
+
isFirstToSolve,
|
|
106
|
+
getContestTime,
|
|
107
|
+
getRemainingContestTime,
|
|
108
|
+
formatContestTime,
|
|
109
|
+
parseRelTime
|
|
110
|
+
} from '@icpctools/contest-api';
|
|
111
|
+
|
|
112
|
+
const team = findById(contest.getTeams(), '42');
|
|
113
|
+
const org = findById(contest.getOrganizations(), team?.organization_id);
|
|
114
|
+
const logo = bestLogo(org?.logo, 64, 64); // pick the best-sized file reference
|
|
115
|
+
|
|
116
|
+
const elapsed = getContestTime(contest.getContest(), contest.getState());
|
|
117
|
+
console.log(formatContestTime(elapsed)); // e.g. "2:15:07"
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
All Contest API object types (`Contest`, `Team`, `Problem`, `Submission`, `Scoreboard`, etc.) are
|
|
121
|
+
exported as TypeScript types.
|
|
122
|
+
|
|
123
|
+
### Proxying file references
|
|
124
|
+
|
|
125
|
+
If your client cannot reach the contest server directly (e.g. a browser with CORS restrictions),
|
|
126
|
+
pass a proxy URL as the third constructor argument. Server-relative file references (logos,
|
|
127
|
+
photos, video streams) will then be rewritten to go through the proxy:
|
|
128
|
+
|
|
129
|
+
```ts
|
|
130
|
+
const contest = new ContestAPI(url, credentials, '/proxy');
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## License
|
|
134
|
+
|
|
135
|
+
MIT © ICPC Foundation
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import type { Access, Account, Award, Clarification, Commentary, Contest, ContestState, ContestType, FileReference, Group, Id, Judgement, JudgementType, Language, MapInfo, Notification, Organization, Person, Problem, Run, Scoreboard, StartStatus, Submission, Team, Version } from './contest-types.js';
|
|
2
|
+
export interface Credentials {
|
|
3
|
+
user?: string;
|
|
4
|
+
password?: string;
|
|
5
|
+
}
|
|
6
|
+
export type ContestEvent = {
|
|
7
|
+
type: ContestType;
|
|
8
|
+
id?: Id;
|
|
9
|
+
};
|
|
10
|
+
export type ContestListener = (event: ContestEvent) => void;
|
|
11
|
+
export type ContestModifier = (event: Notification) => boolean;
|
|
12
|
+
export declare class ContestAPI {
|
|
13
|
+
private version?;
|
|
14
|
+
private contest?;
|
|
15
|
+
private access?;
|
|
16
|
+
private state?;
|
|
17
|
+
private organizations?;
|
|
18
|
+
private groups?;
|
|
19
|
+
private teams?;
|
|
20
|
+
private persons?;
|
|
21
|
+
private accounts?;
|
|
22
|
+
private account?;
|
|
23
|
+
private languages?;
|
|
24
|
+
private judgementTypes?;
|
|
25
|
+
private problems?;
|
|
26
|
+
private submissions?;
|
|
27
|
+
private judgements?;
|
|
28
|
+
private runs?;
|
|
29
|
+
private clarifications?;
|
|
30
|
+
private commentary?;
|
|
31
|
+
private awards?;
|
|
32
|
+
private startStatus?;
|
|
33
|
+
private scoreboard?;
|
|
34
|
+
private mapInfo?;
|
|
35
|
+
private id;
|
|
36
|
+
private contestURL;
|
|
37
|
+
private baseURL;
|
|
38
|
+
private serverURL;
|
|
39
|
+
private proxyURL?;
|
|
40
|
+
private credentials?;
|
|
41
|
+
private timeDelta;
|
|
42
|
+
private watching;
|
|
43
|
+
private scoreboardReloadTimeout;
|
|
44
|
+
private static readonly SCOREBOARD_RELOAD_THROTTLE_MS;
|
|
45
|
+
private unknownTypes;
|
|
46
|
+
private contestListeners;
|
|
47
|
+
private contestModifiers;
|
|
48
|
+
private shouldReconnect;
|
|
49
|
+
constructor(contestURL: string, credentials?: Credentials, proxyURL?: string);
|
|
50
|
+
getURL(type: string, id?: string): string;
|
|
51
|
+
loadObject(type: ContestType): Promise<any>;
|
|
52
|
+
loadVersion(force?: boolean): Promise<void>;
|
|
53
|
+
loadContest(force?: boolean): Promise<void>;
|
|
54
|
+
loadAccess(force?: boolean): Promise<void>;
|
|
55
|
+
loadState(force?: boolean): Promise<void>;
|
|
56
|
+
loadStartStatus(force?: boolean): Promise<void>;
|
|
57
|
+
loadLanguages(force?: boolean): Promise<void>;
|
|
58
|
+
loadJudgementTypes(force?: boolean): Promise<void>;
|
|
59
|
+
private sortProblems;
|
|
60
|
+
loadProblems(force?: boolean): Promise<void>;
|
|
61
|
+
loadGroups(force?: boolean): Promise<void>;
|
|
62
|
+
loadOrganizations(force?: boolean): Promise<void>;
|
|
63
|
+
private sortTeams;
|
|
64
|
+
loadTeams(force?: boolean): Promise<void>;
|
|
65
|
+
loadPersons(force?: boolean): Promise<void>;
|
|
66
|
+
loadAccounts(force?: boolean): Promise<void>;
|
|
67
|
+
loadAccount(force?: boolean): Promise<void>;
|
|
68
|
+
loadSubmissions(force?: boolean): Promise<void>;
|
|
69
|
+
loadJudgements(force?: boolean): Promise<void>;
|
|
70
|
+
loadRuns(force?: boolean): Promise<void>;
|
|
71
|
+
loadClarifications(force?: boolean): Promise<void>;
|
|
72
|
+
loadCommentary(force?: boolean): Promise<void>;
|
|
73
|
+
loadScoreboard(force?: boolean): Promise<void>;
|
|
74
|
+
loadAwards(force?: boolean): Promise<void>;
|
|
75
|
+
loadMapInfo(force?: boolean): Promise<void>;
|
|
76
|
+
getContestURL(): string;
|
|
77
|
+
getVersion(): Version | undefined;
|
|
78
|
+
getContest(): Contest | undefined;
|
|
79
|
+
getAccess(): Access | undefined;
|
|
80
|
+
getState(): ContestState | undefined;
|
|
81
|
+
getStartStatus(): StartStatus[] | undefined;
|
|
82
|
+
getLanguages(): Language[];
|
|
83
|
+
getJudgementTypes(): JudgementType[];
|
|
84
|
+
getProblems(): Problem[];
|
|
85
|
+
getGroups(): Group[];
|
|
86
|
+
getTeams(): Team[];
|
|
87
|
+
getOrganizations(): Organization[];
|
|
88
|
+
getPersons(): Person[];
|
|
89
|
+
getAccounts(): Account[];
|
|
90
|
+
getAccount(): Account | undefined;
|
|
91
|
+
getSubmissions(): Submission[];
|
|
92
|
+
getJudgements(): Judgement[];
|
|
93
|
+
getRuns(): Run[];
|
|
94
|
+
getClarifications(): Clarification[];
|
|
95
|
+
getCommentary(): Commentary[];
|
|
96
|
+
getScoreboard(): Scoreboard | undefined;
|
|
97
|
+
getAwards(): Award[];
|
|
98
|
+
getMapInfo(): MapInfo | undefined;
|
|
99
|
+
getTimeDelta(): number;
|
|
100
|
+
resolveClientURL(ref: FileReference): string | undefined;
|
|
101
|
+
private isFileReference;
|
|
102
|
+
private processFileReferences;
|
|
103
|
+
getAuth(): string;
|
|
104
|
+
private processNotificationSingleton;
|
|
105
|
+
private processData;
|
|
106
|
+
processNotification(n: Notification): void;
|
|
107
|
+
private readLines;
|
|
108
|
+
connectToFeed(): Promise<boolean>;
|
|
109
|
+
readEventFeed(): Promise<void>;
|
|
110
|
+
private reset;
|
|
111
|
+
watch(): Promise<void>;
|
|
112
|
+
unwatch(): void;
|
|
113
|
+
private scheduleScoreboardReload;
|
|
114
|
+
addContestListener(listener: ContestListener): void;
|
|
115
|
+
removeContestListener(listener: ContestListener): void;
|
|
116
|
+
private fireChange;
|
|
117
|
+
addContestModifier(modifier: ContestModifier): void;
|
|
118
|
+
removeContestModifier(modifier: ContestModifier): void;
|
|
119
|
+
private modify;
|
|
120
|
+
}
|