Skip to content

Commit 8484e27

Browse files
Refactor keys and handle rejected sign (#13)
* rm: initialmode, fetch highscores from server and more * fix some race conditions * fix: prevent actions from from popping up post sign cancel * return when sign rejected * fix: send gameInputs * feat: show unique address in leaderboard * use hosted api
1 parent 40b007e commit 8484e27

File tree

5 files changed

+21
-42
lines changed

5 files changed

+21
-42
lines changed

client/game/tickRecorder.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export class TickRecorder {
6666
gameId: getFromStore(StorageKey.GAME_ID),
6767
timestamp: Date.now(),
6868
score,
69-
ticks: this.serializedTicks(),
69+
gameInputs: this.serializedTicks(),
7070
};
7171

7272
await endGame(payload);

client/rpc/api.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { getAddress } from "viem";
22
import { addToStore, getFromStore, StorageKey } from "./storage";
33
import { getWalletClient } from "./wallet";
44

5-
const API_URL = "http://localhost:3210";
5+
const API_URL = "https://api.comets.stf.xyz";
66

77
const fetchMruInfo = async () => {
88
const response = await fetch(`${API_URL}/info`);
@@ -33,6 +33,7 @@ const submitAction = async (transition: string, inputs: any) => {
3333
});
3434
} catch (e) {
3535
console.error("Error signing message", e);
36+
return;
3637
}
3738

3839
const response = await fetch(`${API_URL}/${transition}`, {

rollup/index.ts

+14-36
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ import {
66
} from "@stackr/sdk";
77
import { HDNodeWallet, Wallet } from "ethers";
88
import express from "express";
9-
import { readFileSync } from "fs";
109
import { stackrConfig } from "./stackr.config";
11-
import { StartGameSchema, EndGameSchema } from "./stackr/action";
10+
import { EndGameSchema, StartGameSchema } from "./stackr/action";
1211
import { machine, MACHINE_ID } from "./stackr/machine";
1312

1413
const PORT = process.env.PORT || 3210;
@@ -39,36 +38,6 @@ const mru = await MicroRollup({
3938
stfSchemaMap,
4039
});
4140

42-
const notMain = async () => {
43-
await mru.init();
44-
45-
const filePath = "./ticks.json";
46-
let jsonData;
47-
try {
48-
const data = readFileSync(filePath, "utf8");
49-
jsonData = JSON.parse(data);
50-
} catch (error) {
51-
console.error("Error reading or parsing the file:", error);
52-
return null;
53-
}
54-
55-
console.log("Found", jsonData.keypresses.length, "ticks");
56-
const inputs = {
57-
gameId: 1,
58-
...jsonData,
59-
};
60-
61-
const signature = await signMessage(wallet, EndGameSchema, inputs);
62-
const incrementAction = EndGameSchema.actionFrom({
63-
inputs,
64-
signature,
65-
msgSender: wallet.address,
66-
});
67-
68-
const ack = await mru.submitAction("endGame", incrementAction);
69-
// console.log(ack);
70-
};
71-
7241
const main = async () => {
7342
await mru.init();
7443

@@ -113,8 +82,19 @@ const main = async () => {
11382

11483
app.get("/leaderboard", async (_req, res) => {
11584
const { state } = stateMachine;
116-
const topTen = [...state.games]
117-
.sort((a, b) => b.score - a.score)
85+
const sortedScores = [...state.games].sort((a, b) => b.score - a.score);
86+
// make sure to return one entry per player
87+
const players = new Set<string>();
88+
89+
// TODO: store this in app instance later
90+
const topTen = sortedScores
91+
.filter((game) => {
92+
if (players.has(game.player)) {
93+
return false;
94+
}
95+
players.add(game.player);
96+
return true;
97+
})
11898
.slice(0, 10);
11999

120100
const leaderboard = topTen.map((game) => ({
@@ -167,5 +147,3 @@ const main = async () => {
167147
};
168148

169149
main();
170-
171-
// notMain();

rollup/stackr/action.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const EndGameSchema = new ActionSchema("endGame", {
88
gameId: SolidityType.UINT,
99
timestamp: SolidityType.UINT, // nonce
1010
score: SolidityType.UINT,
11-
ticks: [
11+
gameInputs: [
1212
{
1313
v: SolidityType.STRING,
1414
},

rollup/stackr/transitions.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export type CreateGame = {
1111
export type ValidateGameInput = {
1212
gameId: number;
1313
score: number;
14-
ticks: { v: string }[];
14+
gameInputs: { v: string }[];
1515
};
1616

1717
const startGame: STF<AppState, ValidateGameInput> = {
@@ -37,7 +37,7 @@ const startGame: STF<AppState, ValidateGameInput> = {
3737

3838
const endGame: STF<AppState, ValidateGameInput> = {
3939
handler: ({ state, inputs, msgSender }) => {
40-
const { ticks, gameId, score } = inputs;
40+
const { gameInputs, gameId, score } = inputs;
4141
const { games } = state;
4242
if (!games[gameId]) {
4343
throw new Error("Game not found");
@@ -53,7 +53,7 @@ const endGame: STF<AppState, ValidateGameInput> = {
5353

5454
const world = new World(0);
5555
const gameMode = new GameMode(world);
56-
for (const t of ticks) {
56+
for (const t of gameInputs) {
5757
gameMode.deserializeAndUpdate(1 / 60, t);
5858
}
5959

0 commit comments

Comments
 (0)