-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.mjs
More file actions
410 lines (387 loc) · 16.6 KB
/
server.mjs
File metadata and controls
410 lines (387 loc) · 16.6 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/**
* Pokemon Battle Arena — DAP World Agent
* Gen random battles powered by @pkmn/sim, integrated with the DAP network.
*
* Run: WORLD_ID=pokemon-arena PEER_PORT=9099 DATA_DIR=/tmp/pokemon-world node server.mjs
* Open: http://localhost:9099/
*/
import fs from "fs"
import path from "path"
import crypto from "node:crypto"
import { createRequire } from "node:module"
import { fileURLToPath } from "node:url"
import { createWorldServer } from "@resciencelab/agent-world-sdk"
const require = createRequire(import.meta.url)
const { Dex, BattleStreams, Teams } = require("@pkmn/sim")
const { TeamGenerators } = require("@pkmn/randoms")
Teams.setGeneratorFactory(TeamGenerators)
const PORT = parseInt(process.env.PORT ?? process.env.PEER_PORT ?? "9099")
const WORLD_ID = process.env.WORLD_ID ?? "pokemon-arena"
const WORLD_NAME = process.env.WORLD_NAME ?? "Pokemon Battle Arena"
const DATA_DIR = process.env.DATA_DIR ?? "/tmp/pokemon-demo"
const TEAM_SIZE = parseInt(process.env.TEAM_SIZE ?? "3")
const GEN = parseInt(process.env.GEN ?? "5")
const FORMAT = `gen${GEN}randombattle`
const TURN_DELAY = parseInt(process.env.TURN_DELAY ?? "3000")
const __dirname = path.dirname(fileURLToPath(import.meta.url))
// ---------------------------------------------------------------------------
// Type effectiveness chart
// ---------------------------------------------------------------------------
const TYPE_CHART = {
Normal:{Rock:0.5,Ghost:0},Fire:{Fire:0.5,Water:0.5,Grass:2,Ice:2,Bug:2,Rock:0.5,Dragon:0.5},
Water:{Fire:2,Water:0.5,Grass:0.5,Ground:2,Rock:2,Dragon:0.5},
Grass:{Fire:0.5,Water:2,Grass:0.5,Poison:0.5,Ground:2,Flying:0.5,Bug:0.5,Rock:2,Dragon:0.5},
Electric:{Water:2,Grass:0.5,Electric:0.5,Ground:0,Flying:2,Dragon:0.5},
Ice:{Fire:0.5,Water:0.5,Grass:2,Ice:0.5,Ground:2,Flying:2,Dragon:2},
Fighting:{Normal:2,Ice:2,Poison:0.5,Flying:0.5,Psychic:0.5,Bug:0.5,Rock:2,Ghost:0},
Poison:{Grass:2,Poison:0.5,Ground:0.5,Bug:2,Rock:0.5,Ghost:0.5},
Ground:{Fire:2,Electric:2,Grass:0.5,Poison:2,Flying:0,Bug:0.5,Rock:2},
Flying:{Grass:2,Electric:0.5,Fighting:2,Bug:2,Rock:0.5},
Psychic:{Fighting:2,Poison:2,Psychic:0.5},
Bug:{Fire:0.5,Grass:2,Fighting:0.5,Poison:2,Flying:0.5,Psychic:2,Ghost:0.5},
Rock:{Fire:2,Ice:2,Fighting:0.5,Ground:0.5,Flying:2,Bug:2},
Ghost:{Normal:0,Ghost:2,Psychic:0},Dragon:{Dragon:2},
Dark:{Fighting:0.5,Psychic:2,Ghost:2,Dark:0.5},Steel:{},Fairy:{}
}
function getEffectiveness(moveType, defTypes) {
let mult = 1
for (const dt of defTypes) { mult *= (TYPE_CHART[moveType]?.[dt] ?? 1) }
return mult
}
// ---------------------------------------------------------------------------
// DemoBattle — two AI agents with visible thinking
// ---------------------------------------------------------------------------
class DemoBattle {
constructor() {
this.battleId = crypto.randomUUID()
this.battleOver = false
this.winner = null
this.turn = 0
this.log = []
this.protocolLog = []
this.thinking = { p1: [], p2: [] }
this.lastChoice = { p1: null, p2: null }
this.p1 = { name: "Agent Alpha", alias: "Alpha", team: null, request: null, active: null }
this.p2 = { name: "Agent Beta", alias: "Beta", team: null, request: null, active: null }
this.startedAt = Date.now()
this.autoRunning = false
this._initBattle()
}
_initBattle() {
const stream = new BattleStreams.BattleStream()
this.allStreams = BattleStreams.getPlayerStreams(stream)
this._listenOmniscient(this.allStreams.omniscient)
const t1 = Teams.generate(FORMAT).slice(0, TEAM_SIZE)
const t2 = Teams.generate(FORMAT).slice(0, TEAM_SIZE)
this.p1.team = t1
this.p2.team = t2
this._listenStream("p1", this.allStreams.p1)
this._listenStream("p2", this.allStreams.p2)
this.allStreams.omniscient.write(
`>start ${JSON.stringify({ formatid: FORMAT })}\n` +
`>player p1 ${JSON.stringify({ name: this.p1.name, team: Teams.pack(t1) })}\n` +
`>player p2 ${JSON.stringify({ name: this.p2.name, team: Teams.pack(t2) })}`
)
}
async _listenOmniscient(stream) {
try {
for await (const chunk of stream) {
for (const line of chunk.split("\n")) {
if (line.startsWith("|request|") || line.startsWith(">")) continue
this.protocolLog.push(line)
}
}
} catch {}
}
async _listenStream(side, stream) {
const player = this[side]
try {
for await (const chunk of stream) {
for (const line of chunk.split("\n")) {
if (line.startsWith("|request|")) {
player.request = JSON.parse(line.slice(9))
} else if (line.startsWith("|win|")) {
this.winner = line.slice(5)
this.battleOver = true
this.log.push({ turn: this.turn, text: `Battle over! Winner: ${this.winner}`, type: "win" })
} else if (line === "|tie" || line.startsWith("|tie|")) {
this.winner = "tie"
this.battleOver = true
this.log.push({ turn: this.turn, text: "Battle ended in a tie!", type: "win" })
} else if (line.startsWith("|turn|")) {
this.turn = parseInt(line.split("|")[2])
this.log.push({ turn: this.turn, text: `--- Turn ${this.turn} ---`, type: "turn" })
} else {
const fmt = this._formatLine(line)
if (fmt) this.log.push({ turn: this.turn, text: fmt.text, type: fmt.type })
}
}
}
} catch {}
}
_formatLine(line) {
const p = line.split("|").filter(Boolean)
const t = p[0]
const sn = (id) => id?.includes(":") ? id.split(": ")[1] : id
switch (t) {
case "move": return { text: `${sn(p[1])} used ${p[2]}!`, type: "move" }
case "-damage": return { text: `${sn(p[1])} → ${p[2]}`, type: "damage" }
case "-supereffective": return { text: "It's super effective!", type: "super" }
case "-resisted": return { text: "It's not very effective...", type: "resist" }
case "switch": return { text: `${sn(p[1])} sent out ${p[2]?.split(",")[0]}!`, type: "switch" }
case "faint": return { text: `${sn(p[1])} fainted!`, type: "faint" }
case "-crit": return { text: "A critical hit!", type: "crit" }
case "-miss": return { text: `${sn(p[1])} missed!`, type: "miss" }
case "-status": return { text: `${sn(p[1])} is ${p[2]}!`, type: "status" }
case "-heal": return { text: `${sn(p[1])} healed → ${p[2]}`, type: "heal" }
case "-boost": return { text: `${sn(p[1])}'s ${p[2]} rose!`, type: "boost" }
case "-unboost": return { text: `${sn(p[1])}'s ${p[2]} fell!`, type: "boost" }
default: return null
}
}
_parseState(side) {
const player = this[side]
const req = player.request
if (!req) return { active: null, team: [], moves: [], mustSwitch: false }
const team = (req.side?.pokemon || []).map((p, i) => {
const [hp, maxHp] = (p.condition || "0 fnt").split("/").map(s => parseInt(s))
return {
slot: i + 1, name: p.details.split(",")[0],
hp: isNaN(hp) ? 0 : hp, maxHp: isNaN(maxHp) ? 0 : maxHp,
active: !!p.active, fainted: p.condition.includes("fnt"),
types: (Dex.species.get(p.details.split(",")[0])?.types) || ["Normal"],
}
})
const activePoke = team.find(p => p.active)
const moves = (req.active?.[0]?.moves || []).map((m, i) => {
const moveData = Dex.moves.get(m.move)
return {
slot: i + 1, name: m.move, type: moveData?.type || "Normal",
basePower: moveData?.basePower || 0, pp: m.pp, maxPp: m.maxpp,
disabled: m.disabled || false, category: moveData?.category || "Physical",
}
})
return {
active: activePoke, team, moves,
mustSwitch: !!req.forceSwitch, canMove: !!req.active && !req.forceSwitch,
}
}
_think(side) {
const state = this._parseState(side)
const oppSide = side === "p1" ? "p2" : "p1"
const oppState = this._parseState(oppSide)
const thoughts = []
let choice = null
if (state.mustSwitch) {
thoughts.push(`My ${state.active?.name || "Pokemon"} fainted. I need to switch.`)
const alive = state.team.filter(p => !p.fainted && !p.active)
if (alive.length === 0) return { choice: null, thoughts }
if (oppState.active) {
const oppTypes = oppState.active.types
thoughts.push(`Opponent has ${oppState.active.name} (${oppTypes.join("/")}). Let me find a good counter.`)
let best = alive[0], bestScore = -999
for (const p of alive) {
let score = p.hp / p.maxHp * 100
const defMult = oppTypes.reduce((m, ot) => m * getEffectiveness(ot, p.types), 1)
if (defMult < 1) { score += 30; thoughts.push(` ${p.name} resists ${oppTypes.join("/")} attacks.`) }
if (defMult > 1) { score -= 20; thoughts.push(` ${p.name} is weak to ${oppTypes.join("/")} — risky.`) }
if (score > bestScore) { bestScore = score; best = p }
}
thoughts.push(`Decision: Switch to ${best.name} (HP: ${best.hp}/${best.maxHp}).`)
choice = `switch ${best.slot}`
} else {
const best = alive.sort((a, b) => b.hp - a.hp)[0]
thoughts.push(`No info on opponent. Sending ${best.name} (highest HP: ${best.hp}/${best.maxHp}).`)
choice = `switch ${best.slot}`
}
return { choice, thoughts }
}
if (!state.canMove || !state.active) return { choice: null, thoughts: ["Waiting..."] }
const myActive = state.active
const myHpPct = myActive.maxHp > 0 ? Math.round(myActive.hp / myActive.maxHp * 100) : 0
thoughts.push(`My ${myActive.name} (${myActive.types.join("/")}): ${myActive.hp}/${myActive.maxHp} HP (${myHpPct}%).`)
if (oppState.active) {
const opp = oppState.active
const oppHpPct = opp.maxHp > 0 ? Math.round(opp.hp / opp.maxHp * 100) : 0
thoughts.push(`Facing ${opp.name} (${opp.types.join("/")}): ~${oppHpPct}% HP.`)
let bestMove = null, bestScore = -999
for (const m of state.moves) {
if (m.disabled || m.pp <= 0) continue
let score = m.basePower
const eff = getEffectiveness(m.type, opp.types)
score *= eff
if (myActive.types.includes(m.type)) score *= 1.5
const effLabel = eff > 1 ? "SUPER EFFECTIVE" : eff < 1 ? "not very effective" : "neutral"
const stab = myActive.types.includes(m.type) ? " + STAB" : ""
thoughts.push(` ${m.name} (${m.type}, ${m.basePower} BP): ${effLabel}${stab} → score ${Math.round(score)}`)
if (score > bestScore) { bestScore = score; bestMove = m }
}
if (bestScore < 40 && myHpPct > 30) {
const alive = state.team.filter(p => !p.fainted && !p.active)
for (const p of alive) {
const defMult = opp.types.reduce((m2, ot) => m2 * getEffectiveness(ot, p.types), 1)
if (defMult < 1) {
thoughts.push(`My moves are weak. ${p.name} would resist — switching.`)
return { choice: `switch ${p.slot}`, thoughts }
}
}
}
if (bestMove) {
thoughts.push(`Decision: Use ${bestMove.name} (score ${Math.round(bestScore)}).`)
choice = `move ${bestMove.slot}`
} else {
thoughts.push("No usable moves. Struggle.")
choice = "move 1"
}
} else {
const bestMove = state.moves.filter(m => !m.disabled && m.pp > 0).sort((a, b) => b.basePower - a.basePower)[0]
if (bestMove) {
thoughts.push(`No info on opponent. Using strongest move: ${bestMove.name}.`)
choice = `move ${bestMove.slot}`
} else { choice = "move 1" }
}
return { choice, thoughts }
}
async playTurn() {
if (this.battleOver) return false
await new Promise(r => setTimeout(r, 200))
for (const side of ["p1", "p2"]) {
const player = this[side]
if (!player.request || player.request.wait) continue
const { choice, thoughts } = this._think(side)
this.thinking[side] = thoughts
this.lastChoice[side] = choice
if (choice) {
this.allStreams[side].write(choice)
player.request = null
}
}
await new Promise(r => setTimeout(r, 300))
return !this.battleOver
}
async autoPlay() {
if (this.autoRunning) return
this.autoRunning = true
await new Promise(r => setTimeout(r, 500))
while (!this.battleOver) {
await this.playTurn()
await new Promise(r => setTimeout(r, TURN_DELAY))
}
this.autoRunning = false
}
getFullState() {
return {
battleId: this.battleId, turn: this.turn, battleOver: this.battleOver, winner: this.winner,
p1: {
name: this.p1.name,
team: this._parseState("p1").team,
active: this._parseState("p1").active,
moves: this._parseState("p1").moves,
choice: this.lastChoice.p1,
thinking: this.thinking.p1,
},
p2: {
name: this.p2.name,
team: this._parseState("p2").team,
active: this._parseState("p2").active,
moves: this._parseState("p2").moves,
choice: this.lastChoice.p2,
thinking: this.thinking.p2,
},
log: this.log.slice(-30),
protocolLog: this.protocolLog,
}
}
}
// ---------------------------------------------------------------------------
// DAP World Server (via agent-world-sdk)
// ---------------------------------------------------------------------------
fs.mkdirSync(DATA_DIR, { recursive: true })
let currentDemo = null
const POKEMON_MANIFEST = {
name: WORLD_NAME,
theme: "pokemon-battle",
description: "Gen random Pokemon battle arena. Watch two AI agents battle it out.",
objective: "Watch the battle unfold. No player actions required — battles are fully automated.",
rules: [
"Each battle is a random-team match between Agent Alpha and Agent Beta.",
"Agents reason about type matchups, STAB, and HP to choose moves.",
"The battle ends when all Pokemon on one side faint.",
],
actions: {},
state_fields: [
"battleId — unique battle identifier",
"turn — current turn number",
"p1/p2 — team states with HP, moves, and AI thinking",
"log — recent battle events",
"battleOver — true when the battle has ended",
"winner — the winner when battleOver is true",
],
}
const server = await createWorldServer(
{
worldId: WORLD_ID,
worldName: WORLD_NAME,
worldTheme: "pokemon-battle",
port: PORT,
publicPort: parseInt(process.env.PUBLIC_PORT ?? String(PORT)),
publicAddr: process.env.PUBLIC_ADDR ?? null,
dataDir: DATA_DIR,
bootstrapUrl: process.env.BOOTSTRAP_URL,
maxAgents: parseInt(process.env.MAX_AGENTS ?? "2"),
isPublic: (process.env.WORLD_PUBLIC ?? "true") === "true",
password: process.env.WORLD_PASSWORD ?? "",
broadcastIntervalMs: parseInt(process.env.BROADCAST_INTERVAL_MS ?? "5000"),
setupRoutes(fastify) {
const webDir = path.join(__dirname, "web")
fastify.get("/", async (_req, reply) => {
const html = fs.readFileSync(path.join(webDir, "demo.html"), "utf8")
return reply.type("text/html").send(html)
})
fastify.get("/demo.js", async (_req, reply) => {
const js = fs.readFileSync(path.join(webDir, "demo.js"), "utf8")
return reply.type("application/javascript").send(js)
})
fastify.get("/demo.css", async (_req, reply) => {
const css = fs.readFileSync(path.join(webDir, "demo.css"), "utf8")
return reply.type("text/css").send(css)
})
fastify.post("/demo/start", async () => {
currentDemo = new DemoBattle()
await new Promise(r => setTimeout(r, 500))
currentDemo.autoPlay()
return { ok: true, battleId: currentDemo.battleId }
})
fastify.get("/demo/state", async () => {
if (!currentDemo) return { ok: false, error: "No demo running. POST /demo/start first." }
return { ok: true, ...currentDemo.getFullState() }
})
fastify.post("/demo/restart", async () => {
currentDemo = new DemoBattle()
await new Promise(r => setTimeout(r, 500))
currentDemo.autoPlay()
return { ok: true, battleId: currentDemo.battleId }
})
},
},
{
async onJoin(_agentId, _data) {
return { manifest: POKEMON_MANIFEST, state: currentDemo?.getFullState() ?? null }
},
async onAction(_agentId, _data) {
// Battle is fully automated — no player actions
return { ok: true, state: currentDemo?.getFullState() ?? null }
},
async onLeave(_agentId) {},
getState() {
return currentDemo?.getFullState() ?? { ok: false, error: "No battle running" }
},
}
)
// Auto-start initial battle
currentDemo = new DemoBattle()
await new Promise(r => setTimeout(r, 500))
currentDemo.autoPlay()
console.log(`[demo] Pokemon Battle Demo on http://localhost:${PORT}/`)
console.log(`[demo] Turn delay: ${TURN_DELAY}ms, team size: ${TEAM_SIZE}`)
console.log(`[demo] Auto-battle started: ${currentDemo.battleId.slice(0, 8)}`)