Skip to content

Commit d9e4bed

Browse files
feat: working state parsing bot
1 parent 32944e2 commit d9e4bed

1 file changed

Lines changed: 40 additions & 32 deletions

File tree

  • packages/example-bot/src

packages/example-bot/src/bot.ts

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,60 @@
11
import "dotenv/config";
22
import cliProgress from "cli-progress";
33
import { http, fallback, webSocket, createPublicClient } from "viem";
4-
import { Entity } from "../../client/src/utils/game/entityLib";
4+
55
import { forwardStateTo, LiveState, parseSyncStateGivenTables } from "../../client/src/utils/sync";
6+
import { EntityType } from "../../client/src/utils/game/entityLib";
67
import { GameConfig } from "../../client/src/utils/game/configLib";
7-
88
import { stash } from "../../client/src/mud/stash";
9-
import { getRecord, getTable, Stash } from "@latticexyz/stash/internal";
10-
import { SyncProgress, syncToStash } from "@latticexyz/store-sync/internal";
9+
import { timeWad } from "../../client/src/utils/timeLib";
1110
import { ODYSSEY_CHAIN } from "../../client/src/utils/chains";
11+
12+
import { syncToStash } from "@latticexyz/store-sync/internal";
13+
import { Stash } from "@latticexyz/stash/internal";
14+
1215
import Worlds from "../../contracts/worlds.json";
1316

1417
const chain = ODYSSEY_CHAIN;
1518
const WORLD_ADDRESS = Worlds[chain.id]?.address as `0x${string}`;
1619
if (!WORLD_ADDRESS) throw new Error(`No world address found for chain ${chain.id}`);
1720
const START_BLOCK = Worlds[chain.id]!.blockNumber!;
1821

19-
const publicClient = createPublicClient({
20-
chain,
21-
transport: fallback([webSocket(), http()]),
22-
pollingInterval: 100,
23-
cacheTime: 100,
24-
});
25-
26-
const progressBar = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic);
27-
28-
function botDecision(
29-
liveState: LiveState,
30-
gameConfig: GameConfig,
31-
wadTime: bigint
32-
): [string, string] {
33-
// TODO
34-
return ["up", "right"]; // [vertical, horizontal]
22+
function onBlock(liveState: LiveState, gameConfig: GameConfig, wadTime: bigint) {
23+
const { lines, gameState } = liveState;
24+
25+
// <Do something with the processed game state here>
26+
27+
// Demo: Visualize each line by printing players with their usernames.
28+
lines.forEach((line, idx) => {
29+
const players = line
30+
.filter(
31+
(entity) => entity.etype === EntityType.ALIVE && gameState.usernames.has(entity.entityId)
32+
)
33+
.map((entity) => {
34+
return gameState.usernames.get(entity.entityId);
35+
});
36+
37+
console.log(
38+
`Line ${idx} has players: ${players.length > 0 ? players.join(", ") : "No players"}`
39+
);
40+
});
3541
}
3642

3743
export async function main() {
44+
const progressBar = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic);
3845
const { storedBlockLogs$ } = await syncToStash({
3946
stash: stash as Stash,
4047
startSync: true,
4148
address: WORLD_ADDRESS,
4249
startBlock: BigInt(START_BLOCK ?? 0),
43-
publicClient: publicClient as any,
50+
publicClient: createPublicClient({
51+
chain,
52+
transport: fallback([webSocket(), http()]),
53+
pollingInterval: 100,
54+
cacheTime: 100,
55+
}),
4456
indexerUrl: chain.indexerUrl,
4557
});
46-
47-
// Initialize progress bar
4858
progressBar.start(100, 0);
4959

5060
storedBlockLogs$.subscribe(() => {
@@ -54,34 +64,32 @@ export async function main() {
5464
if (progressBar.isActive) {
5565
progressBar.update(100);
5666
progressBar.stop();
57-
console.log("Caught up!");
67+
console.log("\n✅Caught up!");
5868
}
5969

60-
const { gameConfig, lines, lineStates, gameState } = data;
61-
6270
const syncedState = {
63-
lastSyncedTime,
71+
lastSyncedTime: performance.now(),
6472
lastProcessedTime: -1n,
6573
lines: data.lines,
6674
lineStates: data.lineStates,
6775
gameState: data.gameState,
6876
};
6977

70-
const liveState = forwardStateTo(syncedState, gameConfig, false, null, {
71-
stopAtIteration: 99999999999999,
78+
const liveState = forwardStateTo(syncedState, data.gameConfig, false, null, {
79+
stopAtIteration: null,
7280
stopAtTimestampWad: null,
7381
});
7482

75-
console.log("Got block!", Number(syncProgress.latestBlockNumber));
83+
console.log("\n📥 Got block:", Number(syncProgress.latestBlockNumber), "\n");
7684

77-
const [vertical, horizontal] = botDecision(liveState, gameConfig, wadTime);
85+
onBlock(liveState, data.gameConfig, timeWad());
7886
} else {
7987
if (syncProgress.step === "snapshot") progressBar.update(0);
8088
else progressBar.update(Math.round(syncProgress.percentage));
8189
}
8290
});
8391
}
8492

85-
console.log("Starting bot... (this may take a couple seconds)", WORLD_ADDRESS, START_BLOCK);
93+
console.log("🤖 Starting bot... (this may take a couple seconds)", WORLD_ADDRESS, START_BLOCK);
8694
console.log();
8795
await main();

0 commit comments

Comments
 (0)