diff --git a/src/core/game/GameImpl.ts b/src/core/game/GameImpl.ts index 96da4b611..75342fbfc 100644 --- a/src/core/game/GameImpl.ts +++ b/src/core/game/GameImpl.ts @@ -124,7 +124,6 @@ export class GameImpl implements Game { this._nations.forEach((n) => this.addPlayer(n.playerInfo)); return; } - const isDuos = this.config().gameConfig().playerTeams === Duos; const allPlayers = [ ...this._humans, ...this._nations.map((n) => n.playerInfo), diff --git a/src/core/game/PlayerImpl.ts b/src/core/game/PlayerImpl.ts index bf5b329d7..b2046cd40 100644 --- a/src/core/game/PlayerImpl.ts +++ b/src/core/game/PlayerImpl.ts @@ -20,6 +20,7 @@ import { Attack, BuildableUnit, Cell, + ColoredTeams, EmojiMessage, Gold, MessageType, @@ -606,6 +607,9 @@ export class PlayerImpl implements Player { if (this.team() == null || other.team() == null) { return false; } + if (this.team() == ColoredTeams.Bot || other.team() == ColoredTeams.Bot) { + return false; + } return this._team == other.team(); } diff --git a/tests/Team.test.ts b/tests/Team.test.ts new file mode 100644 index 000000000..451897df4 --- /dev/null +++ b/tests/Team.test.ts @@ -0,0 +1,42 @@ +import { + ColoredTeams, + Game, + GameMode, + PlayerType, +} from "../src/core/game/Game"; +import { playerInfo, setup } from "./util/Setup"; + +let game: Game; + +describe("Teams", () => { + test("bots are on the same team, but can attack each other", async () => { + game = await setup("Plains", { gameMode: GameMode.Team, playerTeams: 2 }); + + const bot1 = game.addPlayer(playerInfo("bot1", PlayerType.Bot)); + const bot2 = game.addPlayer(playerInfo("bot2", PlayerType.Bot)); + + // Both bots should be on the same team + expect(bot1.team()).toBe(ColoredTeams.Bot); + expect(bot2.team()).toBe(ColoredTeams.Bot); + + // But they should be allowed to attack each other. + expect(bot1.isOnSameTeam(bot2)).toBe(false); + }); + + test("humans spawn on different teams", async () => { + game = await setup( + "Plains", + { + gameMode: GameMode.Team, + playerTeams: 2, + }, + [ + playerInfo("human1", PlayerType.Human), + playerInfo("human2", PlayerType.Human), + ], + ); + expect(game.player("human1").isOnSameTeam(game.player("human2"))).toBe( + false, + ); + }); +}); diff --git a/tests/util/Setup.ts b/tests/util/Setup.ts index 7f8b98a83..b77f74112 100644 --- a/tests/util/Setup.ts +++ b/tests/util/Setup.ts @@ -1,6 +1,12 @@ import fs from "fs/promises"; import path from "path"; -import { Difficulty, GameType } from "../../src/core/game/Game"; +import { + Difficulty, + Game, + GameType, + PlayerInfo, + PlayerType, +} from "../../src/core/game/Game"; import { createGame } from "../../src/core/game/GameImpl"; import { genTerrainFromBin } from "../../src/core/game/TerrainMapLoader"; import { UserSettings } from "../../src/core/game/UserSettings"; @@ -9,7 +15,11 @@ import { generateMap } from "../../src/scripts/TerrainMapGenerator"; import { TestConfig } from "./TestConfig"; import { TestServerConfig } from "./TestServerConfig"; -export async function setup(mapName: string, _gameConfig: GameConfig = {}) { +export async function setup( + mapName: string, + _gameConfig: GameConfig = {}, + humans: PlayerInfo[] = [], +): Promise { // Load the specified map const mapPath = path.join(__dirname, "..", "testdata", `${mapName}.png`); const imageBuffer = await fs.readFile(mapPath); @@ -35,5 +45,9 @@ export async function setup(mapName: string, _gameConfig: GameConfig = {}) { const config = new TestConfig(serverConfig, gameConfig, new UserSettings()); // Create and return the game - return createGame([], [], gameMap, miniGameMap, config); + return createGame(humans, [], gameMap, miniGameMap, config); +} + +export function playerInfo(name: string, type: PlayerType): PlayerInfo { + return new PlayerInfo("fr", name, type, null, name); }