Skip to content

Commit

Permalink
Refactor keys and handle rejected sign (#13)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
aashutoshrathi authored Aug 1, 2024
1 parent 40b007e commit 8484e27
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 42 deletions.
2 changes: 1 addition & 1 deletion client/game/tickRecorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class TickRecorder {
gameId: getFromStore(StorageKey.GAME_ID),
timestamp: Date.now(),
score,
ticks: this.serializedTicks(),
gameInputs: this.serializedTicks(),
};

await endGame(payload);
Expand Down
3 changes: 2 additions & 1 deletion client/rpc/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getAddress } from "viem";
import { addToStore, getFromStore, StorageKey } from "./storage";
import { getWalletClient } from "./wallet";

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

const fetchMruInfo = async () => {
const response = await fetch(`${API_URL}/info`);
Expand Down Expand Up @@ -33,6 +33,7 @@ const submitAction = async (transition: string, inputs: any) => {
});
} catch (e) {
console.error("Error signing message", e);
return;
}

const response = await fetch(`${API_URL}/${transition}`, {
Expand Down
50 changes: 14 additions & 36 deletions rollup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import {
} from "@stackr/sdk";
import { HDNodeWallet, Wallet } from "ethers";
import express from "express";
import { readFileSync } from "fs";
import { stackrConfig } from "./stackr.config";
import { StartGameSchema, EndGameSchema } from "./stackr/action";
import { EndGameSchema, StartGameSchema } from "./stackr/action";
import { machine, MACHINE_ID } from "./stackr/machine";

const PORT = process.env.PORT || 3210;
Expand Down Expand Up @@ -39,36 +38,6 @@ const mru = await MicroRollup({
stfSchemaMap,
});

const notMain = async () => {
await mru.init();

const filePath = "./ticks.json";
let jsonData;
try {
const data = readFileSync(filePath, "utf8");
jsonData = JSON.parse(data);
} catch (error) {
console.error("Error reading or parsing the file:", error);
return null;
}

console.log("Found", jsonData.keypresses.length, "ticks");
const inputs = {
gameId: 1,
...jsonData,
};

const signature = await signMessage(wallet, EndGameSchema, inputs);
const incrementAction = EndGameSchema.actionFrom({
inputs,
signature,
msgSender: wallet.address,
});

const ack = await mru.submitAction("endGame", incrementAction);
// console.log(ack);
};

const main = async () => {
await mru.init();

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

app.get("/leaderboard", async (_req, res) => {
const { state } = stateMachine;
const topTen = [...state.games]
.sort((a, b) => b.score - a.score)
const sortedScores = [...state.games].sort((a, b) => b.score - a.score);
// make sure to return one entry per player
const players = new Set<string>();

// TODO: store this in app instance later
const topTen = sortedScores
.filter((game) => {
if (players.has(game.player)) {
return false;
}
players.add(game.player);
return true;
})
.slice(0, 10);

const leaderboard = topTen.map((game) => ({
Expand Down Expand Up @@ -167,5 +147,3 @@ const main = async () => {
};

main();

// notMain();
2 changes: 1 addition & 1 deletion rollup/stackr/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const EndGameSchema = new ActionSchema("endGame", {
gameId: SolidityType.UINT,
timestamp: SolidityType.UINT, // nonce
score: SolidityType.UINT,
ticks: [
gameInputs: [
{
v: SolidityType.STRING,
},
Expand Down
6 changes: 3 additions & 3 deletions rollup/stackr/transitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type CreateGame = {
export type ValidateGameInput = {
gameId: number;
score: number;
ticks: { v: string }[];
gameInputs: { v: string }[];
};

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

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

const world = new World(0);
const gameMode = new GameMode(world);
for (const t of ticks) {
for (const t of gameInputs) {
gameMode.deserializeAndUpdate(1 / 60, t);
}

Expand Down

0 comments on commit 8484e27

Please sign in to comment.