Skip to content

Commit 364ff51

Browse files
feat: add ens naming with caching (#23)
* feat: add ens naming with caching * ui: replace headings * fix: use RPC url if provided in .env * refactor getAddressOrEns --------- Co-authored-by: Aashutosh Rathi <aashutosh@stackrlabs.xyz>
1 parent 0cde2dc commit 364ff51

File tree

5 files changed

+44
-32
lines changed

5 files changed

+44
-32
lines changed

client/game/demoMode.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Screen } from './screen';
2-
import { Collisions } from './collisions';
3-
import { World } from './world';
1+
import { Screen } from "./screen";
2+
import { Collisions } from "./collisions";
3+
import { World } from "./world";
44
import { IGameState, VirtualInput } from "../comets";
55

66
export class DemoMode implements IGameState {
@@ -78,12 +78,12 @@ export class DemoMode implements IGameState {
7878
screen.draw.oneCoinOnePlay();
7979
// screen.draw.highscore(this.world.highscore);
8080
screen.draw.stackr();
81-
screen.draw.copyright();
81+
screen.draw.gameTitle();
8282
}
8383

8484
private drawPushStart(screen: Screen) {
8585
if (this.showPushStart) {
8686
screen.draw.pushStart();
8787
}
8888
}
89-
}
89+
}

client/game/draw.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const VectorLine = "rgba(255,255,255,.8)";
88
const TextColor = "rgba(255,255,255,.8)";
99
const Y_START = 20;
1010
const DefaultLineWidth = 2;
11-
const CR = String.fromCharCode(169);
1211

1312
export function magenta(opacity: number = 1) {
1413
return `rgba(255,0,255, ${opacity})`;
@@ -328,8 +327,8 @@ export class Draw {
328327
});
329328
}
330329

331-
stackr() {
332-
this.text2("Stackr", this.screen.font.small, (width) => {
330+
gameTitle() {
331+
this.text2("Comets", this.screen.font.small, (width) => {
333332
return {
334333
x: this.screen.width2 - width / 2,
335334
y: Y_START,
@@ -373,8 +372,8 @@ export class Draw {
373372
});
374373
}
375374

376-
copyright() {
377-
this.text2(CR + " 1979 atari inc", this.screen.font.small, (width) => {
375+
stackr() {
376+
this.text2("Powered by Stackr Labs", this.screen.font.small, (width) => {
378377
return {
379378
x: this.screen.width2 - width / 2,
380379
y: this.screen.height - this.screen.font.small,

client/game/gameMode.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export class GameMode extends EventSource implements IGameState {
129129

130130
private renderStatic(screen: Screen) {
131131
screen.draw.background();
132-
screen.draw.copyright();
132+
screen.draw.gameTitle();
133133
screen.draw.stackr();
134134
screen.draw.scorePlayer1(this.world.score);
135135
screen.draw.drawExtraLives(this.world.lives);

client/game/highScoreMode.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import { IGameState } from "../comets";
22
import { Highscores } from "./highscores";
33
import { Screen } from "./screen";
4+
import { isAddress } from "viem";
5+
46
const formatAddress = (address: string) => {
5-
return `${address.slice(0, 6)}...${address.slice(-4)}`;
7+
if (isAddress(address)) {
8+
return `${address.slice(0, 6)}...${address.slice(-4)}`;
9+
}
10+
// Is an ENS name then
11+
return address.padEnd(14, " ");
612
};
713

814
export class HighScoreMode implements IGameState {
@@ -31,7 +37,7 @@ export class HighScoreMode implements IGameState {
3137
screen.draw.stackr();
3238
screen.draw.scorePlayer1(this.score);
3339
screen.draw.oneCoinOnePlay();
34-
screen.draw.copyright();
40+
screen.draw.gameTitle();
3541
}
3642

3743
private drawHighScores(screen: Screen) {

rollup/index.ts

+26-19
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
import {
22
ActionConfirmationStatus,
33
ActionSchema,
4-
AllowedInputTypes,
54
MicroRollup,
65
} from "@stackr/sdk";
7-
import { HDNodeWallet, Wallet } from "ethers";
6+
import { getDefaultProvider } from "ethers";
87
import express from "express";
98
import { stackrConfig } from "./stackr.config";
109
import { EndGameSchema, StartGameSchema } from "./stackr/action";
1110
import { machine, MACHINE_ID } from "./stackr/machine";
1211

1312
const PORT = process.env.PORT || 3210;
14-
const wallet = new Wallet(stackrConfig.operator.accounts[0].privateKey);
15-
16-
const signMessage = async (
17-
wallet: HDNodeWallet,
18-
schema: ActionSchema,
19-
payload: AllowedInputTypes
20-
) => {
21-
const signature = await wallet.signTypedData(
22-
schema.domain,
23-
schema.EIP712TypedData.types,
24-
payload
25-
);
26-
return signature;
13+
14+
const ensCache = new Map<string, string>();
15+
16+
const getAddressOrEns = async (address: string) => {
17+
if (ensCache.has(address)) {
18+
return ensCache.get(address)!;
19+
}
20+
try {
21+
const ens = await getDefaultProvider(process.env.API_URL).lookupAddress(
22+
address
23+
);
24+
const name = ens || address;
25+
ensCache.set(address, name);
26+
return name;
27+
} catch (e) {
28+
console.error(e);
29+
return address;
30+
}
2731
};
2832

2933
const stfSchemaMap: Record<string, ActionSchema> = {
@@ -97,10 +101,12 @@ const main = async () => {
97101
})
98102
.slice(0, 10);
99103

100-
const leaderboard = topTen.map((game) => ({
101-
address: game.player,
102-
score: game.score,
103-
}));
104+
const leaderboard = await Promise.all(
105+
topTen.map(async (game) => ({
106+
address: await getAddressOrEns(game.player),
107+
score: game.score,
108+
}))
109+
);
104110

105111
return res.json(leaderboard);
106112
});
@@ -146,4 +152,5 @@ const main = async () => {
146152
});
147153
};
148154

155+
149156
main();

0 commit comments

Comments
 (0)