Files
OpenFrontIO/src/core/game/TeamAssignment.ts
T
VariableVince 5a617b5481 Duo partner SP always same: randomize players before team assignment (#1051)
## Description:

See report here:
https://discord.com/channels/1284581928254701718/1378762423175221319

In Singleplayer Teams > Duos, your duo partner will be the same every
time. In World map for example, it will always Norway.

Fix: randomizing Nation players before their team assignment. This is
done for all Team modes in Singleplayer and Multiplayer.

Other behaviour is unchanged. Clan-players keep their own assignment
logic, and Human non-clan player assignment stays the same. The human in
singleplayer Duos mode lands in Team 1 everytime but with a different
Nation as other team member.

BEFORE
![Before SP Duos
modal](https://github.com/user-attachments/assets/78628b06-d621-4203-86ae-046e081a46fb)

![Before SP Duos always Team
1](https://github.com/user-attachments/assets/6e7ec19c-b1e5-4642-beb9-93b723a27618)

![Before SP Duos always Team 1 always Norway team
member](https://github.com/user-attachments/assets/34794d16-ce90-43ca-a5c2-9827e7c944f5)


AFTER

![After SP Duos Team 1 random duo
partner](https://github.com/user-attachments/assets/5a1d7d6a-74f2-472f-a0ff-ad94e9ae80bd)


## Please complete the following:

- [x] I have added screenshots for all UI updates
- [x] I process any text displayed to the user through translateText()
and I've added it to the en.json file
- [x] I have added relevant tests to the test directory
- [x] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced
- [x] I understand that submitting code with bugs that could have been
caught through manual testing blocks releases and new features for all
contributors

## Please put your Discord username so you can be contacted if a bug or
regression is found:

tryout33
2025-06-06 16:50:15 -07:00

89 lines
2.5 KiB
TypeScript

import { PseudoRandom } from "../PseudoRandom";
import { simpleHash } from "../Util";
import { PlayerInfo, PlayerType, Team } from "./Game";
export function assignTeams(
players: PlayerInfo[],
teams: Team[],
): Map<PlayerInfo, Team | "kicked"> {
const result = new Map<PlayerInfo, Team | "kicked">();
const teamPlayerCount = new Map<Team, number>();
// Group players by clan
const clanGroups = new Map<string, PlayerInfo[]>();
const noClanPlayers: PlayerInfo[] = [];
// Sort players into clan groups or no-clan list
for (const player of players) {
if (player.clan) {
if (!clanGroups.has(player.clan)) {
clanGroups.set(player.clan, []);
}
clanGroups.get(player.clan)!.push(player);
} else {
noClanPlayers.push(player);
}
}
const maxTeamSize = Math.ceil(players.length / teams.length);
// Sort clans by size (largest first)
const sortedClans = Array.from(clanGroups.entries()).sort(
(a, b) => b[1].length - a[1].length,
);
// First, assign clan players
for (const [_, clanPlayers] of sortedClans) {
// Try to keep the clan together on the team with fewer players
let team: Team | null = null;
let teamSize = 0;
for (const t of teams) {
const p = teamPlayerCount.get(t) ?? 0;
if (team !== null && teamSize <= p) continue;
teamSize = p;
team = t;
}
if (team === null) continue;
for (const player of clanPlayers) {
if (teamSize < maxTeamSize) {
teamSize++;
result.set(player, team);
} else {
result.set(player, "kicked");
}
}
teamPlayerCount.set(team, teamSize);
}
// Then, assign non-clan players to balance teams
let nationPlayers = noClanPlayers.filter(
(player) => player.playerType === PlayerType.FakeHuman,
);
if (nationPlayers.length > 0) {
// Shuffle only nations to randomize their team assignment
const random = new PseudoRandom(simpleHash(nationPlayers[0].id));
nationPlayers = random.shuffleArray(nationPlayers);
}
const otherPlayers = noClanPlayers.filter(
(player) => player.playerType !== PlayerType.FakeHuman,
);
for (const player of otherPlayers.concat(nationPlayers)) {
let team: Team | null = null;
let teamSize = 0;
for (const t of teams) {
const p = teamPlayerCount.get(t) ?? 0;
if (team !== null && teamSize <= p) continue;
teamSize = p;
team = t;
}
if (team === null) continue;
teamPlayerCount.set(team, teamSize + 1);
result.set(player, team);
}
return result;
}