Skip to content

Commit

Permalink
Various invasion related changes and fixes.
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 0092047b1745f08014405e21b10d042c4b73c461
  • Loading branch information
cpojer committed Jun 16, 2024
1 parent 1ed9a59 commit d62b200
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 2 deletions.
104 changes: 104 additions & 0 deletions athena/lib/__tests__/isPvP.test.tsx
Original file line number Diff line number Diff line change
@@ -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<PlainPlayer>) =>
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);
});
5 changes: 3 additions & 2 deletions athena/lib/isPvP.tsx
Original file line number Diff line number Diff line change
@@ -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
);
}

0 comments on commit d62b200

Please sign in to comment.