From 5931d15caf9156605587906efb78517e4b94d8d9 Mon Sep 17 00:00:00 2001 From: evanpelle Date: Sat, 7 Sep 2024 20:40:52 -0700 Subject: [PATCH] added local build, improved spawnexec --- package.json | 3 +- src/core/configuration/Config.ts | 9 +++ src/core/configuration/DefaultConfig.ts | 8 +-- src/core/configuration/DevConfig.ts | 4 +- src/core/execution/ExecutionManager.ts | 6 ++ src/core/execution/FakeHumanExecution.ts | 86 ++++++++++++++++++++++++ src/core/execution/SpawnExecution.ts | 9 +-- 7 files changed, 112 insertions(+), 13 deletions(-) create mode 100644 src/core/execution/FakeHumanExecution.ts diff --git a/package.json b/package.json index e2b77becb..6c031069d 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "start:server": "GAME_ENV=prod node --loader ts-node/esm --experimental-specifier-resolution=node src/server/Server.ts", "start:server-dev": "GAME_ENV=dev node --loader ts-node/esm --experimental-specifier-resolution=node src/server/Server.ts", "dev": "GAME_ENV=dev concurrently \"npm run start:client\" \"npm run start:server-dev\"", + "tunnel": "npm run build-prod && npm run start:server", "test": "jest" }, "devDependencies": { @@ -72,4 +73,4 @@ "zod": "^3.23.8" }, "type": "module" -} +} \ No newline at end of file diff --git a/src/core/configuration/Config.ts b/src/core/configuration/Config.ts index fad0d8a0b..90b608cb6 100644 --- a/src/core/configuration/Config.ts +++ b/src/core/configuration/Config.ts @@ -3,6 +3,11 @@ import {Colord, colord} from "colord"; import {devConfig} from "./DevConfig"; import {defaultConfig} from "./DefaultConfig"; +export enum GameEnv { + Dev, + Prod +} + export function getConfig(): Config { // TODO: 'prod' not found in prod env if (process.env.GAME_ENV == 'dev') { @@ -14,6 +19,10 @@ export function getConfig(): Config { } } +export function getGameEnv(): GameEnv { + return GameEnv.Prod +} + export interface Config { theme(): Theme; turnIntervalMs(): number diff --git a/src/core/configuration/DefaultConfig.ts b/src/core/configuration/DefaultConfig.ts index 5d1096775..1d6f43adf 100644 --- a/src/core/configuration/DefaultConfig.ts +++ b/src/core/configuration/DefaultConfig.ts @@ -35,15 +35,15 @@ export class DefaultConfig implements Config { switch (tileToConquer.terrain()) { case TerrainType.Plains: mag = 5 - speed = 5 + speed = 3 break case TerrainType.Highland: mag = 15 - speed = 10 + speed = 5 break case TerrainType.Mountain: mag = 45 - speed = 15 + speed = 10 break } if (defender.isPlayer()) { @@ -99,7 +99,7 @@ export class DefaultConfig implements Config { // console.log(`to add ${toAdd}`) if (player.type() == PlayerType.Bot) { - toAdd *= .5 + toAdd *= .7 } return Math.min(player.troops() + toAdd, max) diff --git a/src/core/configuration/DevConfig.ts b/src/core/configuration/DevConfig.ts index d585c5450..c7a6b199e 100644 --- a/src/core/configuration/DevConfig.ts +++ b/src/core/configuration/DevConfig.ts @@ -9,14 +9,14 @@ export const devConfig = new class extends DefaultConfig { return 2 * 1000 } lobbyLifetime(): number { - return 4 * 1000 + return 2 * 1000 } turnIntervalMs(): number { return 100 } numBots(): number { - return 400 + return 0 } // startTroops(playerInfo: PlayerInfo): number { diff --git a/src/core/execution/ExecutionManager.ts b/src/core/execution/ExecutionManager.ts index 5327ff58e..793323e8a 100644 --- a/src/core/execution/ExecutionManager.ts +++ b/src/core/execution/ExecutionManager.ts @@ -6,6 +6,7 @@ import {BotSpawner} from "./BotSpawner"; import {BoatAttackExecution} from "./BoatAttackExecution"; import {PseudoRandom} from "../PseudoRandom"; import {UpdateNameExecution} from "./UpdateNameExecution"; +import {FakeHumanExecution} from "./FakeHumanExecution"; export class Executor { @@ -57,4 +58,9 @@ export class Executor { spawnBots(numBots: number): Execution[] { return new BotSpawner(this.gs).spawnBots(numBots).map(i => this.createExec(i)) } + + fakeHumanExecutions(numFakes: number): Execution[] { + return [new FakeHumanExecution(null)] + } + } \ No newline at end of file diff --git a/src/core/execution/FakeHumanExecution.ts b/src/core/execution/FakeHumanExecution.ts new file mode 100644 index 000000000..8f5fe4409 --- /dev/null +++ b/src/core/execution/FakeHumanExecution.ts @@ -0,0 +1,86 @@ +import {Cell, Execution, MutableGame, MutablePlayer, Player, PlayerID, PlayerInfo, TerraNullius} from "../Game" +import {PseudoRandom} from "../PseudoRandom" +import {simpleHash} from "../Util"; +import {AttackExecution} from "./AttackExecution"; +import {SpawnExecution} from "./SpawnExecution"; + +export class FakeHumanExecution implements Execution { + + private active = true + private random: PseudoRandom; + private attackRate: number + private mg: MutableGame + private neighborsTerraNullius = true + + + constructor(private bot: MutablePlayer) { + this.random = new PseudoRandom(simpleHash(bot.id())) + this.attackRate = this.random.nextInt(10, 50) + } + activeDuringSpawnPhase(): boolean { + return true + } + + init(mg: MutableGame, ticks: number) { + this.mg = mg + } + + tick(ticks: number) { + + if (ticks < this.mg.config().numSpawnPhaseTurns()) { + if(ticks % 10 == 0) { + this.mg.addExecution(new SpawnExecution( + null, null + )) + } + } + + if (!this.bot.isAlive()) { + this.active = false + return + } + + if (ticks % this.attackRate != 0) { + return + } + + if (this.neighborsTerraNullius) { + for (const b of this.bot.borderTiles()) { + for (const n of b.neighbors()) { + if (n.owner() == this.mg.terraNullius() && n.isLand()) { + this.sendAttack(this.mg.terraNullius()) + return + } + } + } + this.neighborsTerraNullius = false + } + + const border = Array.from(this.bot.borderTiles()).flatMap(t => t.neighbors()).filter(t => t.hasOwner() && t.owner() != this.bot) + + if (border.length == 0) { + return + } + + const toAttack = border[this.random.nextInt(0, border.length)] + this.sendAttack(toAttack.owner()) + } + + sendAttack(toAttack: Player | TerraNullius) { + this.mg.addExecution(new AttackExecution( + this.bot.troops() / 20, + this.bot.id(), + toAttack.isPlayer() ? toAttack.id() : null, + null, + null + )) + } + + owner(): MutablePlayer { + return this.bot + } + + isActive(): boolean { + return this.active + } +} \ No newline at end of file diff --git a/src/core/execution/SpawnExecution.ts b/src/core/execution/SpawnExecution.ts index 61df3fea7..1f50bb474 100644 --- a/src/core/execution/SpawnExecution.ts +++ b/src/core/execution/SpawnExecution.ts @@ -18,12 +18,9 @@ export class SpawnExecution implements Execution { } tick(ticks: number) { - if (!this.isActive()) { - return - } + this.active = false - if (ticks >= this.mg.config().numSpawnPhaseTurns()) { - this.active = false + if (!this.mg.inSpawnPhase()) { return } @@ -44,8 +41,8 @@ export class SpawnExecution implements Execution { if (player.type() == PlayerType.Bot) { this.mg.addExecution(new BotExecution(player)) } - this.active = false } + owner(): MutablePlayer { return null }