Files
OpenFrontIO/src/core/execution/Executor.ts
T
2024-08-04 19:51:23 -07:00

55 lines
1.7 KiB
TypeScript

import PriorityQueue from "priority-queue-typescript";
import {Cell, Execution, MutableGame, Game, MutablePlayer, PlayerInfo, TerraNullius, Tile} from "../Game";
import {AttackIntent, BoatAttackIntentSchema, Intent, Turn} from "../Schemas";
import {AttackExecution} from "./AttackExecution";
import {SpawnExecution} from "./SpawnExecution";
import {BotSpawner} from "./BotSpawner";
import {BoatAttackExecution} from "./BoatAttackExecution";
export class Executor {
constructor(private gs: Game) {
}
addTurn(turn: Turn) {
turn.intents.forEach(i => this.addIntent(i))
}
addIntent(intent: Intent) {
if (intent.type == "attack") {
this.gs.addExecution(
new AttackExecution(
intent.troops,
intent.attackerID,
intent.targetID,
new Cell(intent.targetX, intent.targetY)
)
)
} else if (intent.type == "spawn") {
this.gs.addExecution(
new SpawnExecution(
new PlayerInfo(intent.name, intent.isBot),
new Cell(intent.x, intent.y),
)
)
} else if (intent.type == "boat") {
this.gs.addExecution(
new BoatAttackExecution(
intent.attackerID,
intent.targetID,
new Cell(intent.x, intent.y),
intent.troops,
)
)
} else {
throw new Error(`intent type ${intent} not found`)
}
}
spawnBots(numBots: number): void {
new BotSpawner(this.gs).spawnBots(numBots).forEach(i => this.addIntent(i))
}
}