Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/client/components/LobbyPlayerView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ export class LobbyTeamView extends LitElement {
const assignment = assignTeamsLobbyPreview(
players,
teams,
this.teamCount,
this.effectiveNationCount,
);
const buckets = new Map<Team, ClientInfo[]>();
Expand Down
8 changes: 7 additions & 1 deletion src/core/game/GameImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,13 @@ export class GameImpl implements Game {
...this._humans,
...this._nations.map((n) => n.playerInfo),
];
const playerToTeam = assignTeams(allPlayers, this.playerTeams);
const pt = this._config.playerTeams();
const isDuosTriosQuads = pt === Duos || pt === Trios || pt === Quads;
const playerToTeam = assignTeams(
allPlayers,
this.playerTeams,
isDuosTriosQuads,
);
for (const [playerInfo, team] of playerToTeam.entries()) {
if (team === "kicked") {
console.warn(`Player ${playerInfo.name} was kicked from team`);
Expand Down
19 changes: 14 additions & 5 deletions src/core/game/TeamAssignment.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { PseudoRandom } from "../PseudoRandom";
import { ClientID } from "../Schemas";
import { ClientID, TeamCountConfig } from "../Schemas";
import { simpleHash } from "../Util";
import { PlayerInfo, PlayerType, Team } from "./Game";
import { Duos, PlayerInfo, PlayerType, Quads, Team, Trios } from "./Game";

export function assignTeams(
players: PlayerInfo[],
teams: Team[],
isDuosTriosQuads: boolean,
maxTeamSize: number = getMaxTeamSize(players.length, teams.length),
): Map<PlayerInfo, Team | "kicked"> {
const result = new Map<PlayerInfo, Team | "kicked">();
Expand Down Expand Up @@ -103,7 +104,7 @@ export function assignTeams(
p.clientID !== null ? friendGraph.get(p.clientID) : undefined;
let bestTeam: Team | null = null;
let bestFriendCount = -1;
let bestSize = Infinity;
let bestSize = isDuosTriosQuads ? -1 : Infinity;
for (const t of teams) {
const size = teamPlayerCount.get(t) ?? 0;
if (size >= maxTeamSize) continue;
Expand All @@ -115,7 +116,8 @@ export function assignTeams(
}
if (
friendsOnTeam > bestFriendCount ||
(friendsOnTeam === bestFriendCount && size < bestSize)
(friendsOnTeam === bestFriendCount &&
(isDuosTriosQuads ? size > bestSize : size < bestSize))
) {
bestFriendCount = friendsOnTeam;
bestSize = size;
Expand All @@ -141,6 +143,7 @@ export function assignTeams(
const otherPlayers = nonClanPlayers.filter(
(p) => p.playerType !== PlayerType.Nation,
);

for (const p of otherPlayers.concat(nationPlayers)) {
placePlayer(p);
}
Expand All @@ -151,13 +154,19 @@ export function assignTeams(
export function assignTeamsLobbyPreview(
players: PlayerInfo[],
teams: Team[],
teamCount: TeamCountConfig,
nationCount: number,
): Map<PlayerInfo, Team | "kicked"> {
const maxTeamSize = getMaxTeamSize(
players.length + nationCount,
teams.length,
);
return assignTeams(players, teams, maxTeamSize);
return assignTeams(
players,
teams,
teamCount === Duos || teamCount === Trios || teamCount === Quads,
maxTeamSize,
);
}

export function getMaxTeamSize(numPlayers: number, numTeams: number): number {
Expand Down
125 changes: 82 additions & 43 deletions tests/TeamAssignment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe("assignTeams", () => {
createPlayer("4"),
];

const result = assignTeams(players, teams);
const result = assignTeams(players, teams, false);

// Check that players are assigned alternately
expect(result.get(players[0])).toEqual(ColoredTeams.Red);
Expand All @@ -60,7 +60,7 @@ describe("assignTeams", () => {
createPlayer("4", "CLANB"),
];

const result = assignTeams(players, teams);
const result = assignTeams(players, teams, false);

// Check that clan members are on the same team
expect(result.get(players[0])).toEqual(ColoredTeams.Red);
Expand All @@ -77,7 +77,7 @@ describe("assignTeams", () => {
createPlayer("4"),
];

const result = assignTeams(players, teams);
const result = assignTeams(players, teams, false);

// Check that clan members are together and non-clan players balance teams
expect(result.get(players[0])).toEqual(ColoredTeams.Red);
Expand All @@ -96,7 +96,7 @@ describe("assignTeams", () => {
createPlayer("6", "CLANB"),
];

const result = assignTeams(players, teams);
const result = assignTeams(players, teams, false);

// Check that players are kicked when teams are full
expect(result.get(players[0])).toEqual(ColoredTeams.Red);
Expand All @@ -110,13 +110,13 @@ describe("assignTeams", () => {
});

it("should handle empty player list", () => {
const result = assignTeams([], teams);
const result = assignTeams([], teams, false);
expect(result.size).toBe(0);
});

it("should handle single player", () => {
const players = [createPlayer("1")];
const result = assignTeams(players, teams);
const result = assignTeams(players, teams, false);
expect(result.get(players[0])).toEqual(ColoredTeams.Red);
});

Expand All @@ -130,7 +130,7 @@ describe("assignTeams", () => {
createPlayer("6", "CLANC"),
];

const result = assignTeams(players, teams);
const result = assignTeams(players, teams, false);

// Check that larger clans are assigned first
expect(result.get(players[0])).toEqual(ColoredTeams.Red);
Expand Down Expand Up @@ -159,15 +159,19 @@ describe("assignTeams", () => {
createPlayer("14"),
];

const result = assignTeams(players, [
ColoredTeams.Red,
ColoredTeams.Blue,
ColoredTeams.Yellow,
ColoredTeams.Green,
ColoredTeams.Purple,
ColoredTeams.Orange,
ColoredTeams.Teal,
]);
const result = assignTeams(
players,
[
ColoredTeams.Red,
ColoredTeams.Blue,
ColoredTeams.Yellow,
ColoredTeams.Green,
ColoredTeams.Purple,
ColoredTeams.Orange,
ColoredTeams.Teal,
],
false,
);

expect(result.get(players[0])).toEqual(ColoredTeams.Red);
expect(result.get(players[1])).toEqual(ColoredTeams.Red);
Expand All @@ -193,7 +197,7 @@ describe("assignTeams", () => {
createPlayerWithFriends("4", []),
];

const result = assignTeams(players, teams);
const result = assignTeams(players, teams, false);

expect(result.get(players[0])).toEqual(result.get(players[1]));
expect(result.get(players[2])).not.toEqual(result.get(players[0]));
Expand All @@ -211,7 +215,7 @@ describe("assignTeams", () => {
createPlayerWithFriends("6", []),
];

const result = assignTeams(players, teams);
const result = assignTeams(players, teams, false);

const teamOf1 = result.get(players[0]);
expect(result.get(players[1])).toEqual(teamOf1);
Expand All @@ -226,7 +230,7 @@ describe("assignTeams", () => {
createPlayerWithFriends("4", []),
];

const result = assignTeams(players, teams);
const result = assignTeams(players, teams, false);

expect(result.get(players[0])).toEqual(result.get(players[1]));
});
Expand All @@ -243,7 +247,7 @@ describe("assignTeams", () => {
createPlayerWithFriends("6", [], undefined),
];

const result = assignTeams(players, teams);
const result = assignTeams(players, teams, false);

const teamOf1 = result.get(players[0]);
expect(result.get(players[1])).toEqual(teamOf1);
Expand All @@ -262,7 +266,7 @@ describe("assignTeams", () => {
createPlayerWithFriends("6", []),
];

const result = assignTeams(players, teams);
const result = assignTeams(players, teams, false);

expect(result.get(players[0])).toEqual(ColoredTeams.Red);
expect(result.get(players[1])).toEqual(ColoredTeams.Red);
Expand All @@ -284,7 +288,7 @@ describe("assignTeams", () => {
createPlayerWithFriends("player-4", [], undefined, "client-4"),
];

const result = assignTeams(players, teams);
const result = assignTeams(players, teams, false);

expect(result.get(players[0])).toEqual(result.get(players[1]));
expect(result.get(players[2])).not.toEqual(result.get(players[0]));
Expand Down Expand Up @@ -319,7 +323,7 @@ describe("assignTeams", () => {
createPinnedPlayer("4", 0),
];

const result = assignTeams(players, teams);
const result = assignTeams(players, teams, false);

expect(result.get(players[0])).toEqual(ColoredTeams.Red);
expect(result.get(players[1])).toEqual(ColoredTeams.Blue);
Expand All @@ -337,7 +341,7 @@ describe("assignTeams", () => {
createPinnedPlayer("4", 0),
];

const result = assignTeams(players, teams);
const result = assignTeams(players, teams, false);

expect(result.get(players[0])).toEqual(ColoredTeams.Red);
expect(result.get(players[1])).toEqual(ColoredTeams.Blue);
Expand All @@ -353,7 +357,7 @@ describe("assignTeams", () => {
createPinnedPlayer("4", null),
];

const result = assignTeams(players, teams);
const result = assignTeams(players, teams, false);

expect(result.get(players[3])).toEqual(ColoredTeams.Blue);
});
Expand All @@ -366,7 +370,7 @@ describe("assignTeams", () => {
createPinnedPlayer("4", null),
];

const result = assignTeams(players, teams);
const result = assignTeams(players, teams, false);

for (const p of players) {
expect([ColoredTeams.Red, ColoredTeams.Blue]).toContain(result.get(p));
Expand All @@ -381,7 +385,7 @@ describe("assignTeams", () => {
createPinnedPlayer("4", null),
];

const result = assignTeams(players, teams);
const result = assignTeams(players, teams, false);

expect(result.get(players[1])).toEqual(ColoredTeams.Red);
});
Expand All @@ -393,29 +397,64 @@ describe("assignTeams", () => {
createPinnedPlayer("3", 0),
];

const result = assignTeams(players, teams, 1);
const result = assignTeams(players, teams, false);

expect(result.get(players[0])).toEqual(ColoredTeams.Red);
expect(result.get(players[1])).toEqual(ColoredTeams.Red);
expect(result.get(players[2])).toEqual(ColoredTeams.Red);
});

it("should still kick when every team is at capacity", () => {
// 5 friends in a clique, 2 teams, maxTeamSize = ceil(5/2) = 3.
// Total capacity is 6, so we have slack — nobody should get kicked.
// But if we force capacity below player count, kicks resume.
const players = [
createPlayerWithFriends("1", ["2", "3", "4", "5"]),
createPlayerWithFriends("2", []),
createPlayerWithFriends("3", []),
createPlayerWithFriends("4", []),
createPlayerWithFriends("5", []),
];
// In duos/trios/quads mode humans pack onto a team and nations fill the rest.
const createNation = (id: string): PlayerInfo => {
return new PlayerInfo(`Nation ${id}`, PlayerType.Nation, null, id, false);
};

const result = assignTeams(players, teams, 2);
for (const mode of [
{ name: "duos", size: 2 },
{ name: "trios", size: 3 },
{ name: "quads", size: 4 },
]) {
it(`should fill one team with humans before putting nations on the other (${mode.name})`, () => {
const humans = Array.from({ length: mode.size }, (_, i) =>
createPlayer(`${i + 1}`),
);
const nations = Array.from({ length: mode.size }, (_, i) =>
createNation(`${i + 1}`),
);
const players = [...humans, ...nations];

const result = assignTeams(players, teams, true, mode.size);

for (const h of humans) {
expect(result.get(h)).toEqual(ColoredTeams.Red);
}
for (const n of nations) {
expect(result.get(n)).toEqual(ColoredTeams.Blue);
}
});
}

it("should fill team gaps with humans before placing nations (trios)", () => {
// 4 humans + 2 nations, 2 teams, maxTeamSize 3.
// Red packs to 3 humans, Blue holds 1 human + 2 nations.
const humans = Array.from({ length: 4 }, (_, i) =>
createPlayer(`${i + 1}`),
);
const nations = Array.from({ length: 2 }, (_, i) =>
createNation(`${i + 1}`),
);
const players = [...humans, ...nations];

// maxTeamSize=2, 2 teams → capacity 4, 5 players → 1 must be kicked.
const kicked = players.filter((p) => result.get(p) === "kicked");
expect(kicked.length).toBe(1);
const result = assignTeams(players, teams, true, 3);

const redHumans = humans.filter((h) => result.get(h) === ColoredTeams.Red);
const blueHumans = humans.filter(
(h) => result.get(h) === ColoredTeams.Blue,
);
expect(redHumans.length).toBe(3);
expect(blueHumans.length).toBe(1);
for (const n of nations) {
expect(result.get(n)).toEqual(ColoredTeams.Blue);
}
});
});
Loading