Skip to content

Commit 40b007e

Browse files
rm initialmode, fetch highscores from mru, fix sending ticks (#12)
* rm: initialmode, fetch highscores from server and more * fix some race conditions * cleanup * fix: prevent actions from from popping up post sign cancel * add workflow for deploying
1 parent d29aabf commit 40b007e

20 files changed

+248
-294
lines changed

.github/workflows/pages.yml

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Update Deployment
2+
3+
on:
4+
push:
5+
branches:
6+
- "main"
7+
8+
jobs:
9+
publish:
10+
permissions:
11+
contents: write
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Check out
15+
uses: actions/checkout@v4
16+
17+
- name: Setup Node.js
18+
uses: actions/setup-node@v2
19+
with:
20+
node-version: '20'
21+
22+
- name: Install dependencies
23+
run: cd client && npm install
24+
25+
- name: Remove .gitignore
26+
run: rm -rf client/.gitignore
27+
28+
- name: Generate your content
29+
run: npm run build:client
30+
31+
- name: Publish current workdir (which contains generated content) to GitHub Pages
32+
uses: rayluo/github-pages-overwriter@v1.3
33+
34+
with:
35+
source-directory: client
36+
target-branch: gh-pages

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
**/*.sqlite
22
**/*.env
3+
node_modules/

client/game/attractMode.ts

+17-33
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { IGameState } from "../comets";
2-
import { startGame } from "../rpc/api";
2+
import { fetchMruInfo, startGame } from "../rpc/api";
33
import { addToStore, getFromStore, StorageKey } from "../rpc/storage";
44
import { DemoMode } from "./demoMode";
55
import { EventSource } from "./events";
66
import { HighScoreMode } from "./highScoreMode";
7-
import { Highscores } from "./highscores";
87
import { Key } from "./keys";
98
import { Screen } from "./screen";
109
import { Sound } from "./sounds";
@@ -14,18 +13,16 @@ const ATTRACT_TIME = 15;
1413

1514
// combines DemoMode and HighscoreMode to attract people to part with their quarters
1615
export class AttractMode extends EventSource implements IGameState {
17-
private demoTimer = 0;
1816
private currentMode: IGameState;
1917
private modes: IGameState[];
20-
private index: number = 0;
2118
private isStarting = false;
2219

2320
constructor(world: World, lastScore: number) {
2421
super();
2522

2623
this.modes = [
2724
new HighScoreMode(lastScore),
28-
new DemoMode(world || new World(Highscores.top.score)),
25+
new DemoMode(world || new World()),
2926
];
3027

3128
this.currentMode = this.modes[0];
@@ -36,43 +33,30 @@ export class AttractMode extends EventSource implements IGameState {
3633

3734
update(step: number) {
3835
this.currentMode.update(step);
39-
// since page is reloaded on update for now, we need to check if game is already started
40-
if (getFromStore(StorageKey.GAME_ID)) {
41-
this.trigger("done");
42-
}
4336

4437
if (Key.isAnyPressed()) {
45-
if (getFromStore(StorageKey.GAME_ID)) {
46-
this.trigger("done");
47-
} else if (!this.isStarting) {
38+
if (!this.isStarting) {
4839
this.isStarting = true;
49-
startGame().then((res) => {
50-
if (res?.error || !res.isOk) {
51-
console.error(res.error);
40+
startGame()
41+
.then((res) => {
42+
console.log("Game started", res.logs[0].value);
43+
addToStore(StorageKey.GAME_ID, res.logs[0].value);
44+
this.isStarting = false;
45+
this.trigger("done");
46+
})
47+
.catch((e) => {
48+
console.error("Error starting game", e.message);
49+
})
50+
.finally(() => {
5251
this.isStarting = false;
53-
return;
54-
}
55-
console.log("Game started", res.logs[0].value);
56-
addToStore(StorageKey.GAME_ID, res.logs[0].value);
57-
this.isStarting = false;
58-
});
52+
// clears the keys to prevent the game from starting again
53+
Key.clear();
54+
});
5955
}
60-
} else {
61-
this.updateAttractTimer(step);
6256
}
6357
}
6458

6559
render(screen: Screen, dt?: number) {
6660
this.currentMode.render(screen, dt);
6761
}
68-
69-
updateAttractTimer(step: number) {
70-
this.demoTimer += step;
71-
72-
if (this.demoTimer >= ATTRACT_TIME) {
73-
this.demoTimer = 0;
74-
this.index = 1 - this.index;
75-
this.currentMode = this.modes[this.index];
76-
}
77-
}
7862
}

client/game/comets.ts

+23-22
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import { IGameState } from "../comets";
2-
import { fetchMruInfo } from "../rpc/api";
2+
import { fetchLeaderboard, fetchMruInfo } from "../rpc/api";
33
import { removeFromStore, StorageKey } from "../rpc/storage";
44
import { getWalletClient } from "../rpc/wallet";
55
import { AttractMode } from "./attractMode";
66
import { GameMode } from "./gameMode";
77
import Global from "./global";
8-
import { Highscores } from "./highscores";
9-
import { InitialsMode } from "./initialsMode";
108
import { Key, Keys } from "./keys";
119
import { loop } from "./loop";
1210
import { Screen } from "./screen";
@@ -18,46 +16,49 @@ export class Comets {
1816
private lastScore = 0;
1917
private attractMode: AttractMode;
2018
private gameMode: GameMode;
21-
private initialsMode: InitialsMode;
2219
private currentMode: IGameState;
2320
private tickRecorder: TickRecorder;
2421
private screen: Screen;
22+
private isSendingTicks = false;
2523

2624
constructor() {
2725
this.init();
2826
}
2927

3028
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);
3530
this.screen = new Screen();
3631
this.currentMode = this.attractMode;
3732
this.tickRecorder = new TickRecorder();
3833

3934
const setGameMode = () => {
40-
this.gameMode = new GameMode(new World(Highscores.top.score));
35+
this.gameMode = new GameMode(new World());
4136
this.currentMode = this.gameMode;
4237
this.tickRecorder.reset();
4338

4439
this.gameMode.on("done", async (source, world) => {
4540
// Send ticks in the form of an action to MRU
4641
// And wait for C1 to confirm score
4742
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+
});
6060
}
61+
// restart in attract mode
6162
console.log("Game over");
6263
});
6364
};
@@ -115,7 +116,7 @@ const game = new Comets();
115116

116117
// setup things
117118
(async () => {
118-
await fetchMruInfo();
119+
await Promise.all([fetchMruInfo(), fetchLeaderboard()]);
119120
await getWalletClient();
120121
setTimeout(() => {
121122
loop(game);

client/game/demoMode.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ export class DemoMode implements IGameState {
7676
screen.draw.background();
7777
screen.draw.scorePlayer1(this.world.score);
7878
screen.draw.oneCoinOnePlay();
79-
screen.draw.highscore(this.world.highscore);
79+
// screen.draw.highscore(this.world.highscore);
80+
screen.draw.stackr();
8081
screen.draw.copyright();
8182
}
8283

client/game/draw.ts

+9
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,15 @@ export class Draw {
328328
});
329329
}
330330

331+
stackr() {
332+
this.text2("Stackr", this.screen.font.small, (width) => {
333+
return {
334+
x: this.screen.width2 - width / 2,
335+
y: Y_START,
336+
};
337+
});
338+
}
339+
331340
oneCoinOnePlay() {
332341
this.text2("1 coin 1 play", this.screen.font.medium, (width) => {
333342
return {

client/game/gameMode.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ export class GameMode extends EventSource implements IGameState {
107107
private renderStatic(screen: Screen) {
108108
screen.draw.background();
109109
screen.draw.copyright();
110+
screen.draw.stackr();
110111
screen.draw.scorePlayer1(this.world.score);
111-
screen.draw.highscore(this.world.highscore);
112112
screen.draw.drawExtraLives(this.world.lives);
113113

114114
// remaining shields

0 commit comments

Comments
 (0)