@icpctools/contest-api 0.8.0 → 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/README.md +135 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
# @icpctools/contest-api
|
|
2
|
+
|
|
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
|