Skip to content

Resolve issue with duo/trio/quad team forming logic - #4758

Open
SGunner2014 wants to merge 3 commits into
openfrontio:mainfrom
SGunner2014:bugfix/4253-fix-group-pairings
Open

Resolve issue with duo/trio/quad team forming logic#4758
SGunner2014 wants to merge 3 commits into
openfrontio:mainfrom
SGunner2014:bugfix/4253-fix-group-pairings

Conversation

@SGunner2014

Copy link
Copy Markdown

Add approved & assigned issue number here:

Resolves #4253

Description:

This resolves an issue with duos/trios/quads where humans would be automatically assigned to the emptiest teams, thereby resulting in combinations of humans + nations fighting each other, whereas the preferred behaviour is for humans to be put together where possible.

Please complete the following:

  • I have added screenshots for all UI updates
  • I process any text displayed to the user through translateText() and I've added it to the en.json file
  • I have added relevant tests to the test directory

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

sjg.

@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

Team assignment now receives explicit Duos, Trios, and Quads context. Human placement prefers fuller teams in these modes. Game assignment and lobby previews pass the mode context. Tests cover existing behavior and nation placement.

Changes

Team packing behavior

Layer / File(s) Summary
Mode-aware team assignment
src/core/game/TeamAssignment.ts
assignTeams and lobby preview assignment use the mode context to prefer fuller teams for equal friend counts in Duos, Trios, and Quads.
Game and lobby wiring
src/core/game/GameImpl.ts, src/client/components/LobbyPlayerView.ts
Game assignment and lobby previews pass the configured team mode into team assignment.
Assignment validation
tests/TeamAssignment.test.ts
Existing tests pass the explicit non-special-mode flag. New tests verify human packing and nation placement across Duos, Trios, and Quads.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

Suggested reviewers: evanpelle

Poem

Humans gather, nations align,
Duos and Trios form a line.
Quads pack teams with measured care,
The lobby preview matches there.
Assignment follows the mode.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the fix for duo, trio, and quad team formation logic.
Description check ✅ Passed The description directly explains the team assignment issue, the intended behavior, and the added tests.
Linked Issues check ✅ Passed The changes implement human grouping for Duos, Trios, and Quads, update both assignment call sites, preserve numeric spreading, and add coverage.
Out of Scope Changes check ✅ Passed The changes are limited to team assignment logic, its game and lobby-preview call sites, and related tests.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/core/game/TeamAssignment.ts (1)

107-120: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Put friend groups before friendless humans.

The new tie-breaker cannot preserve a friend pair when a friendless human is processed first. In Duos, [friendless, A↔B] fills the first team with friendless + A, forcing B onto another team. Partition humans with present friends before friendless humans before placement, and add this regression case.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/core/game/TeamAssignment.ts` around lines 107 - 120, The team-placement
flow in TeamAssignment must prioritize humans with existing friends before
friendless humans. Partition or order the input before placement so
friend-connected players are processed first, then add a regression case
covering a friendless player preceding an A↔B pair and verify the pair remains
together.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@src/core/game/TeamAssignment.ts`:
- Around line 107-120: The team-placement flow in TeamAssignment must prioritize
humans with existing friends before friendless humans. Partition or order the
input before placement so friend-connected players are processed first, then add
a regression case covering a friendless player preceding an A↔B pair and verify
the pair remains together.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: be14334f-cc5f-4e20-9b93-807172f79f7a

📥 Commits

Reviewing files that changed from the base of the PR and between ad3fcbe and 4be62af.

📒 Files selected for processing (4)
  • src/client/components/LobbyPlayerView.ts
  • src/core/game/GameImpl.ts
  • src/core/game/TeamAssignment.ts
  • tests/TeamAssignment.test.ts

coderabbitai[bot]
coderabbitai Bot previously approved these changes Jul 28, 2026
@SGunner2014
SGunner2014 marked this pull request as ready for review July 28, 2026 20:51
@SGunner2014
SGunner2014 requested a review from a team as a code owner July 28, 2026 20:51
@SGunner2014

Copy link
Copy Markdown
Author

Looking at the review from coderabbit, I can see this is outside the scope of the original ticket because it's behaviour that was already present in the code before I made changes. I can update the PR to include a fix to this flagged issue also, if this is preferred?

Comment thread src/core/game/TeamAssignment.ts Outdated
export function assignTeamsLobbyPreview(
players: PlayerInfo[],
teams: Team[],
isDuosTriosQuads: boolean,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead let's pass in playerTeams directly here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@evanpelle Done :)

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/core/game/TeamAssignment.ts (1)

107-107: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Place players with present friends before friendless players.

The new tie-breaker only selects a fuller team for the current player. It does not reserve a slot for a later friend. With two teams and maxTeamSize = 2, input [friendless, A, B] assigns friendless and A together, then sends friend B to the other team. The placement loop preserves otherPlayers order before adding nations. (raw.githubusercontent.com)

When isDuosTriosQuads is true, partition otherPlayers into players with present friends and friendless players. Keep the friend-connected group first. Preserve the existing order for explicitly numbered team counts. Add a regression test for the friendless-first case.

Suggested ordering change
+  const hasPresentFriend = (p: PlayerInfo): boolean =>
+    p.clientID !== null && (friendGraph.get(p.clientID)?.size ?? 0) > 0;
+  const friendConnectedPlayers = otherPlayers.filter(hasPresentFriend);
+  const friendlessPlayers = otherPlayers.filter((p) => !hasPresentFriend(p));
+  const orderedOtherPlayers = isDuosTriosQuads
+    ? friendConnectedPlayers.concat(friendlessPlayers)
+    : otherPlayers;
-  for (const p of otherPlayers.concat(nationPlayers)) {
+  for (const p of orderedOtherPlayers.concat(nationPlayers)) {

Based on the PR objective, friend pairs must precede friendless humans. As per coding guidelines, add tests for this src/core/ change.

Also applies to: 119-120

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/core/game/TeamAssignment.ts` at line 107, Update the player ordering used
by the placement loop in TeamAssignment so that, when isDuosTriosQuads is true,
players with present friends are processed before friendless players while
preserving each group’s existing order; leave explicitly numbered team counts
unchanged. Add a regression test covering the friendless-first input case to
verify friends are placed together.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@src/core/game/TeamAssignment.ts`:
- Line 107: Update the player ordering used by the placement loop in
TeamAssignment so that, when isDuosTriosQuads is true, players with present
friends are processed before friendless players while preserving each group’s
existing order; leave explicitly numbered team counts unchanged. Add a
regression test covering the friendless-first input case to verify friends are
placed together.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 09007994-adad-4383-b27c-33f9aafff7c1

📥 Commits

Reviewing files that changed from the base of the PR and between 4be62af and 0382894.

📒 Files selected for processing (2)
  • src/client/components/LobbyPlayerView.ts
  • src/core/game/TeamAssignment.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/client/components/LobbyPlayerView.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Triage

Development

Successfully merging this pull request may close these issues.

Duos/Trios/Quads: humans are spread across teams and paired with nations instead of each other

2 participants