From d62b2005e319ae49380edc717dca2483d4b391c4 Mon Sep 17 00:00:00 2001 From: cpojer Date: Fri, 14 Jun 2024 15:52:26 +0900 Subject: [PATCH] Various invasion related changes and fixes. GitOrigin-RevId: 0092047b1745f08014405e21b10d042c4b73c461 --- athena/lib/__tests__/isPvP.test.tsx | 104 ++++++++++++++++++++++++++++ athena/lib/isPvP.tsx | 5 +- 2 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 athena/lib/__tests__/isPvP.test.tsx diff --git a/athena/lib/__tests__/isPvP.test.tsx b/athena/lib/__tests__/isPvP.test.tsx new file mode 100644 index 00000000..10817d73 --- /dev/null +++ b/athena/lib/__tests__/isPvP.test.tsx @@ -0,0 +1,104 @@ +import { expect, test } from 'vitest'; +import { PlainPlayer } from '../../map/Player.tsx'; +import MapData from '../../MapData.tsx'; +import isPvP from '../isPvP.tsx'; + +const teamWithPlayers = (players: ReadonlyArray) => + players.map( + (player) => + ({ + id: player.id, + name: '', + players: [player], + }) as const, + ); + +test('`isPvP` only considers games with active human players as pvp', () => { + expect( + isPvP( + MapData.createMap({ + active: [1, 2], + teams: teamWithPlayers([ + { funds: 0, id: 1, name: 'AI' }, + { funds: 0, id: 2, name: 'AI' }, + ]), + }), + ), + ).toBe(false); + + expect( + isPvP( + MapData.createMap({ + active: [1, 2], + teams: teamWithPlayers([ + { funds: 0, id: 1, userId: '1' }, + { funds: 0, id: 2, name: 'AI' }, + ]), + }), + ), + ).toBe(false); + + expect( + isPvP( + MapData.createMap({ + active: [1, 2], + teams: teamWithPlayers([ + { funds: 0, id: 1, userId: '1' }, + { funds: 0, id: 2, userId: '2' }, + ]), + }), + ), + ).toBe(true); + + expect( + isPvP( + MapData.createMap({ + active: [1, 2, 3], + teams: teamWithPlayers([ + { funds: 0, id: 1, userId: '1' }, + { funds: 0, id: 2, userId: '2' }, + { funds: 0, id: 3, name: 'AI' }, + ]), + }), + ), + ).toBe(true); + + expect( + isPvP( + MapData.createMap({ + active: [1, 2, 3], + teams: teamWithPlayers([ + { funds: 0, id: 1, userId: '1' }, + { funds: 0, id: 2, userId: '2' }, + { funds: 0, id: 3, userId: '3' }, + ]), + }), + ), + ).toBe(true); + + expect( + isPvP( + MapData.createMap({ + active: [1, 2], + teams: teamWithPlayers([ + { funds: 0, id: 1, userId: '1' }, + { funds: 0, id: 2, userId: '2' }, + { funds: 0, id: 3, name: 'AI' }, + ]), + }), + ), + ).toBe(true); + + expect( + isPvP( + MapData.createMap({ + active: [1, 3], + teams: teamWithPlayers([ + { funds: 0, id: 1, userId: '1' }, + { funds: 0, id: 2, userId: '2' }, + { funds: 0, id: 3, name: 'AI' }, + ]), + }), + ), + ).toBe(false); +}); diff --git a/athena/lib/isPvP.tsx b/athena/lib/isPvP.tsx index a930e3b3..2041965e 100644 --- a/athena/lib/isPvP.tsx +++ b/athena/lib/isPvP.tsx @@ -1,6 +1,7 @@ -import { isHumanPlayer } from '../map/Player.tsx'; import MapData from '../MapData.tsx'; export default function isPvP(map: MapData) { - return map.getPlayers().filter(isHumanPlayer).length > 1; + return ( + map.active.filter((id) => map.getPlayer(id).isHumanPlayer()).length > 1 + ); }