1
1
import { IGameState } from "../comets" ;
2
- import { fetchMruInfo } from "../rpc/api" ;
2
+ import { fetchLeaderboard , fetchMruInfo } from "../rpc/api" ;
3
3
import { removeFromStore , StorageKey } from "../rpc/storage" ;
4
4
import { getWalletClient } from "../rpc/wallet" ;
5
5
import { AttractMode } from "./attractMode" ;
6
6
import { GameMode } from "./gameMode" ;
7
7
import Global from "./global" ;
8
- import { Highscores } from "./highscores" ;
9
- import { InitialsMode } from "./initialsMode" ;
10
8
import { Key , Keys } from "./keys" ;
11
9
import { loop } from "./loop" ;
12
10
import { Screen } from "./screen" ;
@@ -18,46 +16,49 @@ export class Comets {
18
16
private lastScore = 0 ;
19
17
private attractMode : AttractMode ;
20
18
private gameMode : GameMode ;
21
- private initialsMode : InitialsMode ;
22
19
private currentMode : IGameState ;
23
20
private tickRecorder : TickRecorder ;
24
21
private screen : Screen ;
22
+ private isSendingTicks = false ;
25
23
26
24
constructor ( ) {
27
25
this . init ( ) ;
28
26
}
29
27
30
28
init ( ) {
31
- this . attractMode = new AttractMode (
32
- new World ( Highscores . top . score ) ,
33
- this . lastScore
34
- ) ;
29
+ this . attractMode = new AttractMode ( new World ( ) , this . lastScore ) ;
35
30
this . screen = new Screen ( ) ;
36
31
this . currentMode = this . attractMode ;
37
32
this . tickRecorder = new TickRecorder ( ) ;
38
33
39
34
const setGameMode = ( ) => {
40
- this . gameMode = new GameMode ( new World ( Highscores . top . score ) ) ;
35
+ this . gameMode = new GameMode ( new World ( ) ) ;
41
36
this . currentMode = this . gameMode ;
42
37
this . tickRecorder . reset ( ) ;
43
38
44
39
this . gameMode . on ( "done" , async ( source , world ) => {
45
40
// Send ticks in the form of an action to MRU
46
41
// And wait for C1 to confirm score
47
42
this . lastScore = world . score ;
48
- await this . tickRecorder . sendTicks ( this . lastScore ) ;
49
-
50
- if ( Highscores . qualifies ( world . score ) ) {
51
- this . initialsMode = new InitialsMode ( world . score ) ;
52
- this . currentMode = this . initialsMode ;
53
-
54
- this . initialsMode . on ( "done" , ( ) => {
55
- this . init ( ) ;
56
- } ) ;
57
- } else {
58
- // restart in attract mode
59
- this . init ( ) ;
43
+ if ( ! this . isSendingTicks ) {
44
+ this . isSendingTicks = true ;
45
+ await this . tickRecorder
46
+ . sendTicks ( this . lastScore )
47
+ . then ( ( ) => {
48
+ console . log ( "Sent ticks" ) ;
49
+ } )
50
+ . catch ( ( e ) => {
51
+ console . error ( "Error sending ticks" , e . message ) ;
52
+ } )
53
+ . finally ( ( ) => {
54
+ removeFromStore ( StorageKey . GAME_ID ) ;
55
+ this . isSendingTicks = false ;
56
+ this . init ( ) ;
57
+ // Reload page
58
+ window . location . reload ( ) ;
59
+ } ) ;
60
60
}
61
+ // restart in attract mode
61
62
console . log ( "Game over" ) ;
62
63
} ) ;
63
64
} ;
@@ -115,7 +116,7 @@ const game = new Comets();
115
116
116
117
// setup things
117
118
( async ( ) => {
118
- await fetchMruInfo ( ) ;
119
+ await Promise . all ( [ fetchMruInfo ( ) , fetchLeaderboard ( ) ] ) ;
119
120
await getWalletClient ( ) ;
120
121
setTimeout ( ( ) => {
121
122
loop ( game ) ;
0 commit comments