create nuke execution & schema

This commit is contained in:
evanpelle
2024-10-19 08:46:41 -07:00
parent 43b931c4e7
commit 33a85d521a
3 changed files with 68 additions and 2 deletions
+11
View File
@@ -14,6 +14,7 @@ export type Intent = SpawnIntent
| TargetPlayerIntent
| EmojiIntent
| DonateIntent
| NukeIntent
export type AttackIntent = z.infer<typeof AttackIntentSchema>
export type SpawnIntent = z.infer<typeof SpawnIntentSchema>
@@ -25,6 +26,7 @@ export type BreakAllianceIntent = z.infer<typeof BreakAllianceIntentSchema>
export type TargetPlayerIntent = z.infer<typeof TargetPlayerIntentSchema>
export type EmojiIntent = z.infer<typeof EmojiIntentSchema>
export type DonateIntent = z.infer<typeof DonateIntentSchema>
export type NukeIntent = z.infer<typeof NukeIntentSchema>
export type Turn = z.infer<typeof TurnSchema>
export type GameConfig = z.infer<typeof GameConfigSchema>
@@ -142,6 +144,14 @@ export const DonateIntentSchema = BaseIntentSchema.extend({
troops: z.number().nullable(),
})
export const NukeIntentSchema = BaseIntentSchema.extend({
type: z.literal('nuke'),
sender: z.string(),
x: z.number(),
y: z.number(),
magnitude: z.number().nullable(),
})
const IntentSchema = z.union([
AttackIntentSchema,
SpawnIntentSchema,
@@ -153,6 +163,7 @@ const IntentSchema = z.union([
TargetPlayerIntentSchema,
EmojiIntentSchema,
DonateIntentSchema,
NukeIntentSchema,
]);
const TurnSchema = z.object({
+4 -2
View File
@@ -15,6 +15,7 @@ import {BreakAllianceExecution} from "./alliance/BreakAllianceExecution";
import {TargetPlayerExecution} from "./TargetPlayerExecution";
import {EmojiExecution} from "./EmojiExecution";
import {DonateExecution} from "./DonateExecution";
import {NukeExecution} from "./NukeExecution";
@@ -74,8 +75,9 @@ export class Executor {
return new EmojiExecution(intent.sender, intent.recipient, intent.emoji)
} else if (intent.type == "donate") {
return new DonateExecution(intent.sender, intent.recipient, intent.troops)
}
else {
} else if (intent.type == "nuke") {
return new NukeExecution(intent.sender, new Cell(intent.x, intent.y), intent.magnitude)
} else {
throw new Error(`intent type ${intent} not found`)
}
}
+53
View File
@@ -0,0 +1,53 @@
import {Cell, Execution, MutableGame, MutablePlayer, PlayerID, Tile} from "../game/Game";
import {bfs, dist} from "../Util";
export class NukeExecution implements Execution {
private sender: MutablePlayer
private active = true
private toDestroy: Set<Tile> = new Set()
private mg: MutableGame
constructor(
private senderID: PlayerID,
private cell: Cell,
private magnitude: number | null
) { }
init(mg: MutableGame, ticks: number): void {
this.mg = mg
this.sender = mg.player(this.senderID)
if (this.magnitude == null) {
this.magnitude = 50
}
const tile = mg.tile(this.cell)
this.toDestroy = bfs(tile, dist(tile, this.magnitude))
}
tick(ticks: number): void {
for (const tile of this.toDestroy) {
const owner = tile.owner()
if (owner.isPlayer()) {
this.mg.player(owner.id()).relinquish(tile)
}
}
this.active = false
}
owner(): MutablePlayer {
return null
}
isActive(): boolean {
return this.active
}
activeDuringSpawnPhase(): boolean {
return false
}
}