-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
ctrl.ts
100 lines (92 loc) · 2.28 KB
/
ctrl.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { Auth } from './auth';
import { GameCtrl } from './game';
import { Page } from './interfaces';
import { Stream } from './ndJsonStream';
import { formData } from './util';
import OngoingGames from './ongoingGames';
import { SeekCtrl } from './seek';
import ChallengeCtrl from './challenge';
import TvCtrl from './tv';
export class Ctrl {
auth: Auth = new Auth();
stream?: Stream;
page: Page = 'home';
games = new OngoingGames();
game?: GameCtrl;
seek?: SeekCtrl;
challenge?: ChallengeCtrl;
tv?: TvCtrl;
constructor(readonly redraw: () => void) {}
openHome = async () => {
this.page = 'home';
if (this.auth.me) {
await this.stream?.close();
this.games.empty();
this.stream = await this.auth.openStream('/api/stream/event', {}, msg => {
switch (msg.type) {
case 'gameStart':
this.games.onStart(msg.game);
break;
case 'gameFinish':
this.games.onFinish(msg.game);
break;
default:
console.warn(`Unprocessed message of type ${msg.type}`, msg);
}
this.redraw();
});
}
this.redraw();
};
openGame = async (id: string) => {
this.page = 'game';
this.game = undefined;
this.redraw();
this.game = await GameCtrl.open(this, id);
this.redraw();
};
playAi = async () => {
this.game = undefined;
this.page = 'game';
this.redraw();
await this.auth.fetchBody('/api/challenge/ai', {
method: 'post',
body: formData({
level: 1,
'clock.limit': 60 * 3,
'clock.increment': 2,
}),
});
};
playPool = async (minutes: number, increment: number) => {
this.seek = await SeekCtrl.make(
{
rated: true,
time: minutes,
increment,
},
this
);
this.page = 'seek';
this.redraw();
};
playMaia = async (minutes: number, increment: number) => {
this.challenge = await ChallengeCtrl.make(
{
username: 'maia1',
rated: false,
'clock.limit': minutes * 60,
'clock.increment': increment,
},
this
);
this.page = 'challenge';
this.redraw();
};
watchTv = async () => {
this.page = 'tv';
this.redraw();
this.tv = await TvCtrl.open(this);
this.redraw();
};
}