-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol.ts
More file actions
167 lines (144 loc) · 4.91 KB
/
Copy pathprotocol.ts
File metadata and controls
167 lines (144 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**
* Wire protocol shared by the game client and the multiplayer server.
*
* Kept in one file so a change to an event payload is a type error on both
* sides rather than a silent runtime mismatch. The server re-exports these
* types; the frontend imports them directly.
*
* Scope discipline: nothing here carries economic meaning. Positions, presence
* and chat only. Ownership, balances, crops and trades live on-chain and are
* never taken from a socket message.
*/
export const PROTOCOL_VERSION = 1
export interface PlayerPresence {
/** Server-assigned session id. Not stable across reconnects. */
id: string
/** Verified wallet address, lowercase, or null for a guest. */
address: string | null
username: string
x: number
y: number
zone: string
isGuest: boolean
/** Server clock, ms since epoch. */
joinedAt: number
}
export interface ChatMessage {
id: string
senderId: string
senderName: string
senderAddress: string | null
content: string
timestamp: number
scope: ChatScope
zone: string
}
export type ChatScope = 'global' | 'nearby' | 'system'
export interface AuthChallenge {
nonce: string
/** The exact string the client must sign. */
message: string
expiresAt: number
}
// --- client -> server -----------------------------------------------------
export interface ClientToServerEvents {
/** Step 1 of wallet auth: ask for a nonce to sign. */
'auth:challenge': (
payload: { address: string },
ack: (response: Result<AuthChallenge>) => void
) => void
/** Step 2: present the signature. Server verifies it recovers to `address`. */
'player:join': (
payload: JoinPayload,
ack: (response: Result<{ self: PlayerPresence; players: PlayerPresence[] }>) => void
) => void
'player:move': (payload: MovePayload) => void
'player:leave': () => void
'chat:send': (payload: ChatSendPayload, ack?: (response: Result<{ id: string }>) => void) => void
'players:sync': (ack: (response: Result<PlayerPresence[]>) => void) => void
'heartbeat': (ack?: (serverTime: number) => void) => void
}
export interface JoinPayload {
/** Omit for guest play. */
address?: string
/** Signature over the challenge message. Required when `address` is present. */
signature?: string
nonce?: string
username?: string
x?: number
y?: number
}
export interface MovePayload {
x: number
y: number
/** Facing, for remote animation. */
facing?: Facing
moving?: boolean
}
export type Facing = 'up' | 'down' | 'left' | 'right'
export interface ChatSendPayload {
content: string
scope: Exclude<ChatScope, 'system'>
}
// --- server -> client -----------------------------------------------------
export interface ServerToClientEvents {
'player:joined': (player: PlayerPresence) => void
'player:moved': (payload: { id: string; x: number; y: number; facing?: Facing; moving?: boolean }) => void
'player:left': (payload: { id: string }) => void
'players:snapshot': (players: PlayerPresence[]) => void
'chat:message': (message: ChatMessage) => void
'zone:changed': (payload: { id: string; zone: string }) => void
'server:error': (payload: { code: ServerErrorCode; message: string }) => void
'server:info': (payload: { onlineCount: number; serverTime: number }) => void
}
export type ServerErrorCode =
| 'RATE_LIMITED'
| 'INVALID_PAYLOAD'
| 'AUTH_FAILED'
| 'NOT_JOINED'
| 'ALREADY_CONNECTED'
| 'INTERNAL'
export type Result<T> = { ok: true; data: T } | { ok: false; code: ServerErrorCode; message: string }
// --- world constants ------------------------------------------------------
export const WORLD = {
/** Tile dimensions of the shared map. */
width: 64,
height: 64,
/** Manhattan radius, in tiles, for "nearby" chat. */
nearbyRadius: 12,
/** Zones are square blocks of this many tiles. */
zoneSize: 16,
} as const
export const LIMITS = {
maxChatLength: 280,
maxUsernameLength: 24,
/** Movement updates the server will accept per second. */
moveRatePerSecond: 20,
chatPerTenSeconds: 8,
/** Max tiles a player may move in a single update before it is rejected. */
maxMoveDelta: 6,
heartbeatIntervalMs: 20_000,
/** Sessions with no traffic for this long are reaped. */
idleTimeoutMs: 90_000,
} as const
export function zoneIdForPosition(x: number, y: number): string {
const zx = Math.floor(clampCoord(x) / WORLD.zoneSize)
const zy = Math.floor(clampCoord(y) / WORLD.zoneSize)
return `${zx}:${zy}`
}
export function clampCoord(value: number): number {
if (!Number.isFinite(value)) return 0
return Math.max(0, Math.min(WORLD.width - 1, Math.floor(value)))
}
/** The exact message a client signs to prove wallet ownership. */
export function buildAuthMessage(address: string, nonce: string): string {
return [
'Sign in to Web3 Farming Game',
'',
`Wallet: ${address.toLowerCase()}`,
`Nonce: ${nonce}`,
'',
'This signature proves you own this wallet.',
'It is free, and does not authorise any transaction.',
].join('\n')
}