| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
import { agentById, sightBlocked } from "./abilities.ts"; |
| 14 |
import { runBots } from "./bots.ts"; |
| 15 |
import { recordResult } from "./db.ts"; |
| 16 |
import { fallsBetween, marksOf, trimFeed, type Fall, type Mark } from "./killfeed.ts"; |
| 17 |
import type { Point } from "./map.ts"; |
| 18 |
import { |
| 19 |
buyAbility, |
| 20 |
buyShield, |
| 21 |
buyWeapon, |
| 22 |
newMatch, |
| 23 |
pickAgent, |
| 24 |
setInput, |
| 25 |
tick, |
| 26 |
visible, |
| 27 |
} from "./match.ts"; |
| 28 |
import type { MatchState, Player } from "./state.ts"; |
| 29 |
import { isWeaponId, WEAPONS } from "./weapons.ts"; |
| 30 |
|
| 31 |
export const TICK = 1 / 30; |
| 32 |
const BROADCAST_EVERY = 2; |
| 33 |
|
| 34 |
export type Seat = { playerId: string; token: string; address: string; send: (data: string) => void }; |
| 35 |
|
| 36 |
let state: MatchState = newMatch(); |
| 37 |
const seats = new Map<string, Seat>(); |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
const addressSeats = new Map<string, string>(); |
| 44 |
let ticks = 0; |
| 45 |
let lastResult: string | null = null; |
| 46 |
|
| 47 |
|
| 48 |
let marks: Mark[] = marksOf(state.players); |
| 49 |
let feed: Fall[] = []; |
| 50 |
|
| 51 |
export const match = (): MatchState => state; |
| 52 |
|
| 53 |
export const findPlayer = (id: string): Player | undefined => state.players.find((p) => p.id === id); |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
function freeName(want: string, exceptId: string): string { |
| 65 |
const taken = new Set(state.players.filter((p) => p.id !== exceptId).map((p) => p.name)); |
| 66 |
if (!taken.has(want)) return want; |
| 67 |
for (let n = 2; n < 100; n++) { |
| 68 |
const tried = `${want.slice(0, 13)} ${n}`; |
| 69 |
if (!taken.has(tried)) return tried; |
| 70 |
} |
| 71 |
return want; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
export function addressCapped(address: string, session: string): boolean { |
| 80 |
const holder = addressSeats.get(address); |
| 81 |
return holder !== undefined && holder !== session; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
export function takeSeat(address: string, session: string, name: string): Player | null { |
| 95 |
const existing = seats.get(session); |
| 96 |
const held = existing ? findPlayer(existing.playerId) : undefined; |
| 97 |
if (held) { |
| 98 |
held.name = freeName(name, held.id); |
| 99 |
return held; |
| 100 |
} |
| 101 |
if (addressCapped(address, session)) return null; |
| 102 |
const humans = { red: 0, blue: 0 }; |
| 103 |
for (const player of state.players) if (!player.bot) humans[player.team] += 1; |
| 104 |
const wanted = humans.red <= humans.blue ? "red" : "blue"; |
| 105 |
const seat = |
| 106 |
state.players.find((p) => p.bot && p.team === wanted) ?? state.players.find((p) => p.bot); |
| 107 |
if (!seat) return null; |
| 108 |
seat.bot = false; |
| 109 |
seat.name = freeName(name, seat.id); |
| 110 |
seat.think = 0; |
| 111 |
seat.input = { mx: 0, my: 0, angle: seat.angle, fire: false, use: false, reload: false, cast: null }; |
| 112 |
addressSeats.set(address, session); |
| 113 |
seats.set(session, { playerId: seat.id, token: "", address, send: () => {} }); |
| 114 |
return seat; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
export function attach(session: string, playerId: string, address: string, token: string, send: (data: string) => void): void { |
| 119 |
seats.set(session, { playerId, token, address, send }); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
export function release(session: string, token: string): void { |
| 130 |
const seat = seats.get(session); |
| 131 |
if (!seat || seat.token !== token) return; |
| 132 |
seats.delete(session); |
| 133 |
if (addressSeats.get(seat.address) === session) addressSeats.delete(seat.address); |
| 134 |
const player = findPlayer(seat.playerId); |
| 135 |
if (player && !stillHeld(seat.playerId)) { |
| 136 |
player.bot = true; |
| 137 |
player.think = 0; |
| 138 |
player.input.fire = false; |
| 139 |
player.input.use = false; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
export function releaseAddress(address: string): void { |
| 151 |
const session = addressSeats.get(address); |
| 152 |
if (session !== undefined) release(session, seats.get(session)?.token ?? ""); |
| 153 |
} |
| 154 |
|
| 155 |
const stillHeld = (playerId: string): boolean => |
| 156 |
[...seats.values()].some((s) => s.playerId === playerId); |
| 157 |
|
| 158 |
export const seatCount = (): number => seats.size; |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
const spectators = new Map<string, (data: string) => void>(); |
| 165 |
|
| 166 |
export function spectatorSnapshot(): string { |
| 167 |
return JSON.stringify({ |
| 168 |
t: "s", |
| 169 |
spectator: true, |
| 170 |
phase: state.phase, |
| 171 |
clock: round2(Math.max(0, state.clock)), |
| 172 |
round: state.round, |
| 173 |
score: state.score, |
| 174 |
attackers: state.attackers, |
| 175 |
result: state.result, |
| 176 |
events: state.events.slice(0, 5).map((e) => e.text), |
| 177 |
}); |
| 178 |
} |
| 179 |
|
| 180 |
export function watch(token: string, send: (data: string) => void): void { |
| 181 |
spectators.set(token, send); |
| 182 |
send(spectatorSnapshot()); |
| 183 |
} |
| 184 |
|
| 185 |
export function unwatch(token: string): void { |
| 186 |
spectators.delete(token); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
export type Command = { t: string; [key: string]: unknown }; |
| 192 |
|
| 193 |
const num = (value: unknown): number | undefined => |
| 194 |
typeof value === "number" && Number.isFinite(value) ? value : undefined; |
| 195 |
|
| 196 |
|
| 197 |
export function command(playerId: string, message: Command): void { |
| 198 |
const player = findPlayer(playerId); |
| 199 |
if (!player || player.bot) return; |
| 200 |
switch (message.t) { |
| 201 |
case "i": |
| 202 |
setInput(player, { |
| 203 |
mx: num(message.mx), |
| 204 |
my: num(message.my), |
| 205 |
angle: num(message.a), |
| 206 |
fire: typeof message.f === "boolean" ? message.f : undefined, |
| 207 |
use: typeof message.u === "boolean" ? message.u : undefined, |
| 208 |
reload: typeof message.r === "boolean" ? message.r : undefined, |
| 209 |
cast: message.c === "c" || message.c === "q" || message.c === "e" || message.c === "x" ? message.c : undefined, |
| 210 |
}); |
| 211 |
break; |
| 212 |
case "buy": { |
| 213 |
const what = String(message.what ?? ""); |
| 214 |
if (isWeaponId(what)) buyWeapon(state, player, what); |
| 215 |
else if (what === "light" || what === "heavy") buyShield(state, player, what); |
| 216 |
else if (what === "c" || what === "q") buyAbility(state, player, what); |
| 217 |
break; |
| 218 |
} |
| 219 |
case "agent": |
| 220 |
if (typeof message.id === "string") pickAgent(state, player, message.id); |
| 221 |
break; |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
const round2 = (n: number): number => Math.round(n * 10) / 10; |
| 228 |
|
| 229 |
export function snapshot(viewerId: string): string { |
| 230 |
const me = findPlayer(viewerId) ?? state.players[0]!; |
| 231 |
const agent = agentById(me.agent); |
| 232 |
const seen = state.players.filter((p) => visible(state, me, p)); |
| 233 |
const canSee = (point: Point): boolean => !sightBlocked(me, point, state.zones); |
| 234 |
|
| 235 |
return JSON.stringify({ |
| 236 |
t: "s", |
| 237 |
phase: state.phase, |
| 238 |
clock: round2(Math.max(0, state.clock)), |
| 239 |
round: state.round, |
| 240 |
score: state.score, |
| 241 |
attackers: state.attackers, |
| 242 |
result: state.result, |
| 243 |
me: { |
| 244 |
id: me.id, |
| 245 |
side: me.side, |
| 246 |
team: me.team, |
| 247 |
agent: me.agent, |
| 248 |
alive: me.alive, |
| 249 |
x: round2(me.x), |
| 250 |
y: round2(me.y), |
| 251 |
angle: round2(me.angle), |
| 252 |
hp: Math.round(me.hp), |
| 253 |
armour: Math.round(me.armour), |
| 254 |
credits: me.credits, |
| 255 |
weapon: me.weapon, |
| 256 |
ammo: me.ammo, |
| 257 |
reloading: me.reloadIn > 0, |
| 258 |
charges: me.charges, |
| 259 |
cooldown: { c: round2(me.cooldown.c), q: round2(me.cooldown.q), e: round2(me.cooldown.e), x: 0 }, |
| 260 |
ult: me.ult, |
| 261 |
ultNeeds: agent.abilities.x.points, |
| 262 |
blind: round2(me.blind), |
| 263 |
slow: round2(me.slow), |
| 264 |
haste: round2(me.haste), |
| 265 |
spike: me.spike, |
| 266 |
planting: round2(me.planting), |
| 267 |
bot: me.bot, |
| 268 |
}, |
| 269 |
players: seen.map((p) => ({ |
| 270 |
id: p.id, |
| 271 |
name: p.name, |
| 272 |
agent: p.agent, |
| 273 |
x: round2(p.x), |
| 274 |
y: round2(p.y), |
| 275 |
angle: round2(p.angle), |
| 276 |
hp: Math.round(p.hp), |
| 277 |
armour: Math.round(p.armour), |
| 278 |
alive: p.alive, |
| 279 |
mate: p.side === me.side, |
| 280 |
spike: p.spike, |
| 281 |
blind: p.blind > 0, |
| 282 |
revealed: p.revealed > 0, |
| 283 |
})), |
| 284 |
zones: state.zones |
| 285 |
.filter((z) => (z.kind === "trip" ? z.side === me.side : true)) |
| 286 |
.map((z) => ({ k: z.kind, x: round2(z.x), y: round2(z.y), r: z.r, f: round2(z.fuse), l: round2(z.life) })), |
| 287 |
spike: { |
| 288 |
planted: state.spike.planted, |
| 289 |
carried: state.spike.carried !== null, |
| 290 |
fuse: round2(state.spike.fuse), |
| 291 |
defuse: round2(state.spike.defuse), |
| 292 |
checkpoint: state.spike.checkpoint, |
| 293 |
|
| 294 |
x: state.spike.planted || me.side === "atk" ? round2(state.spike.x) : null, |
| 295 |
y: state.spike.planted || me.side === "atk" ? round2(state.spike.y) : null, |
| 296 |
}, |
| 297 |
orbs: state.orbs.filter((o) => !o.taken).map((o) => ({ x: o.x, y: o.y })), |
| 298 |
shots: state.shots.filter((s) => canSee({ x: s.x1, y: s.y1 }) || canSee({ x: s.x2, y: s.y2 })), |
| 299 |
board: state.players.map((p) => ({ |
| 300 |
id: p.id, |
| 301 |
name: p.name, |
| 302 |
team: p.team, |
| 303 |
side: p.side, |
| 304 |
agent: p.agent, |
| 305 |
bot: p.bot, |
| 306 |
alive: p.alive, |
| 307 |
kills: p.kills, |
| 308 |
deaths: p.deaths, |
| 309 |
damage: Math.round(p.damageDone), |
| 310 |
credits: p.team === me.team ? p.credits : null, |
| 311 |
ult: p.ult, |
| 312 |
ultNeeds: agentById(p.agent).abilities.x.points, |
| 313 |
})), |
| 314 |
events: state.events.slice(0, 5).map((e) => e.text), |
| 315 |
|
| 316 |
|
| 317 |
feed: feed.map((f) => ({ |
| 318 |
k: f.killer, |
| 319 |
v: f.victim, |
| 320 |
w: f.weapon ? WEAPONS[f.weapon].name : null, |
| 321 |
h: f.headshot, |
| 322 |
t: round2(Math.max(0, state.now - f.at)), |
| 323 |
})), |
| 324 |
}); |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
function readFalls(): void { |
| 333 |
const after = marksOf(state.players); |
| 334 |
const shooters = new Set<string>(); |
| 335 |
for (const shot of state.shots) { |
| 336 |
if (!shot.hit || shot.age > 0) continue; |
| 337 |
for (const player of state.players) |
| 338 |
if (Math.hypot(player.x - shot.x1, player.y - shot.y1) < 2) shooters.add(player.id); |
| 339 |
} |
| 340 |
const falls = fallsBetween(marks, after, { |
| 341 |
at: state.now, |
| 342 |
events: state.events.slice(0, 6).map((e) => e.text), |
| 343 |
shooters, |
| 344 |
}); |
| 345 |
marks = after; |
| 346 |
if (falls.length) feed = [...feed, ...falls]; |
| 347 |
feed = trimFeed(feed, state.now); |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
function step(): void { |
| 353 |
runBots(state, TICK); |
| 354 |
tick(state, TICK); |
| 355 |
readFalls(); |
| 356 |
if (state.result && state.result !== lastResult) { |
| 357 |
lastResult = state.result; |
| 358 |
recordResult(state.result, state.score.red, state.score.blue, state.round); |
| 359 |
} |
| 360 |
if (state.result === null) lastResult = null; |
| 361 |
ticks += 1; |
| 362 |
if (ticks % BROADCAST_EVERY !== 0) return; |
| 363 |
for (const seat of seats.values()) { |
| 364 |
try { |
| 365 |
seat.send(snapshot(seat.playerId)); |
| 366 |
} catch { |
| 367 |
|
| 368 |
|
| 369 |
} |
| 370 |
} |
| 371 |
for (const send of spectators.values()) { |
| 372 |
try { |
| 373 |
send(spectatorSnapshot()); |
| 374 |
} catch { |
| 375 |
|
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
let timer: ReturnType<typeof setInterval> | null = null; |
| 381 |
|
| 382 |
export function startArena(): void { |
| 383 |
if (timer) return; |
| 384 |
timer = setInterval(step, TICK * 1000); |
| 385 |
|
| 386 |
(timer as unknown as { unref?: () => void }).unref?.(); |
| 387 |
} |
| 388 |
|
| 389 |
export function stopArena(): void { |
| 390 |
if (timer) clearInterval(timer); |
| 391 |
timer = null; |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
export function advance(seconds: number): void { |
| 396 |
for (let i = 0; i < Math.round(seconds / TICK); i++) step(); |
| 397 |
} |