Skip to content

Commit 3d5970f

Browse files
added more precise types to LichessApi event types
1 parent 09cfc1e commit 3d5970f

File tree

7 files changed

+233
-299
lines changed

7 files changed

+233
-299
lines changed

RaspberryPi-WebServer/backend/src/controllers/Streams/Stream.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ export default class Stream {
3131
const dataStr = data.toString().trim();
3232
if (dataStr.length > 0) {
3333
dataStr.split("\n").forEach((element: string) => {
34-
this.dataFunction(JSON.parse(element));
34+
const parsedObject = JSON.parse(element);
35+
if(this.#isLichessError(parsedObject)) {
36+
this.errorFunction(parsedObject.error);
37+
} else {
38+
this.dataFunction(parsedObject);
39+
}
3540
});
3641
}
3742
});
@@ -56,4 +61,9 @@ export default class Stream {
5661
getName() {
5762
return this.streamName;
5863
}
64+
65+
#isLichessError(obj: any): obj is LichessError {
66+
return typeof obj === "object" && typeof obj?.error === "string";
67+
}
68+
5969
}

RaspberryPi-WebServer/backend/src/controllers/WebSocketController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export default class WebSocketController {
6060
});
6161
}
6262

63-
#handleIncomingData(message) {
63+
#handleIncomingData(message: any) {
6464
switch (message.type) {
6565
case "challengeAccepted":
6666
return LichessChallengeController.acceptChallenge(message.data.id);

RaspberryPi-WebServer/backend/tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
}, /* Specify a set of entries that re-map imports to additional lookup locations. */
3737
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
3838
"typeRoots": [
39-
"./types",
39+
"./types/**/*",
4040
"./node_modules/@types"
4141
], /* Specify multiple folders that act like './node_modules/@types'. */
4242
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
@@ -115,5 +115,5 @@
115115
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
116116
"skipLibCheck": true /* Skip type checking all .d.ts files. */
117117
},
118-
"include": ["src/**/*", "types"]
118+
"include": ["src/**/*", "types/**/*"]
119119
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
type GameStreamEvent = GameFullEvent | GameStateEvent | ChatLineEvent | OpponentGoneEvent;
3+
4+
interface GameFullEvent {
5+
type: "gameFull",
6+
id: string,
7+
variant: Variant,
8+
clock: {
9+
initial: number,
10+
increment: number
11+
},
12+
speed: Speed,
13+
perf: {
14+
name: string
15+
},
16+
rated: boolean,
17+
createdAt: number,
18+
white: GameEventPlayer,
19+
black: GameEventPlayer,
20+
initialFen: string,
21+
state: GameStateEvent
22+
tournamentId: string,
23+
}
24+
25+
interface GameStateEvent {
26+
type: "gameState",
27+
moves: string,
28+
wtime: number,
29+
btime: number,
30+
winc: number,
31+
binc: number,
32+
status: GameStatus,
33+
winner: string,
34+
wdraw: boolean,
35+
bdraw: boolean,
36+
wtakeback: boolean,
37+
btakeback: boolean,
38+
}
39+
40+
interface ChatLineEvent {
41+
type: "chatLine",
42+
room: "player" | "spectator",
43+
username: string,
44+
text: string,
45+
}
46+
47+
interface OpponentGoneEvent {
48+
type: "opponentGone",
49+
gone: boolean,
50+
claimWinInSeconds: number,
51+
}
52+
53+
interface GameEventPlayer {
54+
aiLevel: number,
55+
id: string,
56+
name: string,
57+
title: Title | null
58+
rating: number,
59+
provisional: boolean,
60+
}
61+
62+
type GameStatus = | "created" | "started" | "aborted" | "mate" | "resign" | "stalemate" | "timeout" | "draw" | "outoftime" | "cheat" | "noStart" | "unknownFinish" | "variantEnd";
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
2+
type MainEventStreamEvent = GameStartEvent | GameFinishEvent | ChallengeEvent | ChallengeCanceledEvent | ChallengeDeclinedEvent;
3+
4+
interface GameStartEvent {
5+
type: "gameStart",
6+
game: GameEventInfo
7+
}
8+
9+
interface GameFinishEvent {
10+
type: "gameFinish",
11+
game: GameEventInfo
12+
}
13+
14+
interface ChallengeEvent {
15+
type: "challenge",
16+
challenge: ChallengeJson,
17+
compat: GameCompat,
18+
}
19+
20+
interface ChallengeCanceledEvent {
21+
type: "challengeCanceled",
22+
challenge: ChallengeJson
23+
}
24+
25+
interface ChallengeDeclinedEvent {
26+
type: "challengeDeclined",
27+
challenge: ChallengeDeclinedJson
28+
}
29+
30+
interface GameEventInfo {
31+
fullId: string,
32+
gameId: string,
33+
fen: string,
34+
color: Color
35+
lastMove: string,
36+
source: GameSource
37+
status: EventStatus
38+
variant: {
39+
key: string,
40+
name: string
41+
},
42+
speed: Speed,
43+
perf: string,
44+
rated: boolean,
45+
hasMoved: boolean,
46+
opponent: {
47+
id: string,
48+
username: string,
49+
rating: number
50+
},
51+
isMyTurn: boolean,
52+
secondsLeft: number,
53+
compat: GameCompat
54+
id: string
55+
}
56+
57+
interface ChallengeJson {
58+
id: string,
59+
url: string,
60+
status: ChallengeStatus,
61+
challenger: ChallengeUser,
62+
destUser: ChallengeUser | null,
63+
variant: Variant,
64+
rated: boolean,
65+
speed: Speed
66+
timeControl: RealTimeTimeControl | CorrespondenceTimeControl | UnlimitedTimeControl,
67+
color: Color | "random",
68+
finalColor: Color,
69+
perf: {
70+
icon: string,
71+
name: string,
72+
},
73+
direction: "in" | "out",
74+
initialFen: string,
75+
}
76+
77+
interface ChallengeDeclinedJson {
78+
declineReason: string,
79+
declineReasonKey: DeclineReasonKey,
80+
id: string,
81+
url: string,
82+
status: ChallengeStatus,
83+
challenger: ChallengeUser,
84+
destUser: ChallengeUser | null,
85+
variant: Variant,
86+
rated: boolean,
87+
speed: Speed,
88+
timeControl: RealTimeTimeControl | CorrespondenceTimeControl | UnlimitedTimeControl,
89+
color: Color | "random",
90+
finalColor: Color,
91+
perf: {
92+
icon: string,
93+
name: string
94+
},
95+
direction: "in" | "out",
96+
initialFen: string
97+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
interface LichessError {
2+
error: string
3+
}
4+
5+
interface RealTimeTimeControl {
6+
type: "clock",
7+
limit: number,
8+
increment: number,
9+
show: string
10+
}
11+
12+
interface CorrespondenceTimeControl {
13+
type: "correspondence",
14+
daysPerTurn: number
15+
}
16+
17+
interface UnlimitedTimeControl {
18+
type: "unlimited"
19+
}
20+
21+
type GameSource = "lobby" | "friend" | "ai" | "api" | "tournament" | "position" | "import" | "importlive" | "simul" | "relay" | "pool" | "swiss";
22+
type Color = "white" | "black";
23+
24+
interface EventStatus {
25+
id: 10 | 20 | 25 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 60,
26+
name: "created" | "started" | "aborted" | "mate" | "resign" | "stalemate" | "timeout" | "draw" | "outoftime" | "cheat" | "noStart" | "unknownFinish" | "variantEnd",
27+
}
28+
29+
type Speed = "ultraBullet" | "bullet" | "blitz" | "rapid" | "classical" | "correspondence";
30+
31+
type ChallengeStatus = "created" | "offline" | "canceled" | "declined" | "accepted";
32+
33+
type Title = "GM" | "WGM" | "IM" | "WIM" | "FM" | "WFM" | "NM" | "CM" | "WCM" | "WNM" | "LM" | "BOT";
34+
35+
interface ChallengeUser {
36+
id: string,
37+
name: string,
38+
rating: number,
39+
title: Title | null,
40+
flair: string,
41+
patron: boolean
42+
provisional: boolean,
43+
online: boolean,
44+
lag: number
45+
}
46+
47+
type VariantKey = "standard" | "chess960" | "crazyhouse" | "antichess" | "atomic" | "horde" | "kingOfTheHill" | "racingKings" | "threeCheck" | "fromPosition";
48+
49+
interface Variant {
50+
key: VariantKey
51+
name: string,
52+
short: string
53+
}
54+
55+
type DeclineReasonKey = "generic" | "later" | "tooFast" | "tooSlow" | "timeControl" | "rated" | "casual" | "standard" | "variant" | "noBot" | "onlyBot";
56+
57+
interface GameCompat {
58+
bot: boolean,
59+
board: boolean
60+
}

0 commit comments

Comments
 (0)