Skip to content

Transfer host when lobby creator disconnects 🔧#3374

Open
FloPinguin wants to merge 1 commit intomainfrom
fix/lobby-host-transfer
Open

Transfer host when lobby creator disconnects 🔧#3374
FloPinguin wants to merge 1 commit intomainfrom
fix/lobby-host-transfer

Conversation

@FloPinguin
Copy link
Contributor

Description:

When the host of a private lobby leaves, the remaining players were shown a new host but that player had no actual host controls. DougDoug and many other people had this problem. This fixes the issue by:

  • Server: Reassigning creatorPersistentID to the next active player when the host disconnects (private lobbies, pre-game only)
  • Client: Automatically switching the new host's JoinLobbyModal to a HostLobbyModal with full controls (edit settings, kick players, start game)
  • Preserving all lobby config values (map, gamemode, nation count, unlimited gold, etc.) set by the original host
  • Showing a "You are now the host of this lobby" notification

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
  • I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced

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

FloPinguin

When the host of a private lobby disconnects, transfer host role to the
next active player. The new host's JoinLobbyModal automatically converts
to a HostLobbyModal with full controls (edit settings, kick, start).
Nation count and other config values are preserved from the original host.
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 7, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: c7104065-d78e-4a0a-aff2-cd2d6865a7b5

📥 Commits

Reviewing files that changed from the base of the PR and between 815f1de and 1e47e2c.

📒 Files selected for processing (5)
  • resources/lang/en.json
  • src/client/HostLobbyModal.ts
  • src/client/JoinLobbyModal.ts
  • src/client/Main.ts
  • src/server/GameServer.ts

Walkthrough

These changes implement automatic host transfer functionality. When the original host disconnects from a private lobby during the lobby phase, the server selects a new host, notifies the client, and the client reopens the host modal with the existing configuration intact.

Changes

Cohort / File(s) Summary
Translation & UI Display
resources/lang/en.json, src/client/HostLobbyModal.ts
Added new translation key host_modal.you_are_now_host to display confirmation message and introduced openExisting() method that restores an existing lobby's configuration into the host modal, including nation slider restoration via map-manifest lookup.
Client-Side Host Transfer Detection
src/client/JoinLobbyModal.ts
Added hostTransferDispatched flag to track and emit host-transfer event exactly once when the current client becomes the lobby creator in a private lobby, passing lobbyId, gameConfig, and clients as event detail.
Host Transfer Event Handler
src/client/Main.ts
Extended DocumentEventMap with new host-transfer event type and added handleHostTransfer() to close the join modal, navigate to host page, open the host modal with existing lobby state, and display confirmation message to the new host.
Server-Side Host Automatic Selection
src/server/GameServer.ts
Added maybeTransferHost() private method that automatically selects the first active client as new host when the current host disconnects during lobby phase in private games, updating creatorPersistentID and broadcasting updated lobby info.

Sequence Diagram

sequenceDiagram
    participant Host as Original Host
    participant Server
    participant NewClient as New Client (JoinLobbyModal)
    participant Main
    participant HostModal

    Host->>Server: Disconnect WebSocket
    Server->>Server: maybeTransferHost()<br/>Select NewClient as host
    Server->>Server: Update creatorPersistentID
    Server->>NewClient: Broadcast updated lobby info<br/>(NewClient is now creator)
    
    NewClient->>NewClient: handleLobbyInfo()<br/>Detect: currentClient == creator
    NewClient->>NewClient: Set hostTransferDispatched=true
    NewClient->>Main: Dispatch host-transfer event<br/>(lobbyId, gameConfig, clients)
    
    Main->>Main: handleHostTransfer()<br/>Validate data
    Main->>NewClient: Close JoinLobbyModal
    Main->>Main: Navigate to host page
    Main->>HostModal: Call openExisting()<br/>(lobbyId, gameConfig, clients)
    HostModal->>HostModal: loadNationCountThenRestore()<br/>Hydrate form state
    HostModal->>Main: Ready with restored configuration
    Main->>NewClient: Display confirmation:<br/>"You are now the host"
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Poem

🎭 When the host leaves the lobby's embrace,
A new leader steps into their place—
The server finds someone to guide the crew,
Forms whisper "host duties now belong to you!" 🎮

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: implementing host transfer when a lobby creator disconnects, which is the central feature of this PR.
Description check ✅ Passed The description is clearly related to the changeset, explaining the problem, solution across server and client, and addressing all checklist items.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


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 and usage tips.

@ryanbarlow97
Copy link
Contributor

In discord we said we would rather close the lobby instead of host transfer, there’s two tickets open saying the same

Copy link
Collaborator

@evanpelle evanpelle left a comment

Choose a reason for hiding this comment

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

regarding the ux, do we want to transfer the host or just tell players that the host has left and the game is cancelled?

this.lobbyId = lobbyId;

// Hydrate form state from the existing game config
this.selectedMap = gameConfig.gameMap;
Copy link
Collaborator

Choose a reason for hiding this comment

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

do you think it would be possible to just store the gameConfig as a field instead of storing each element seperately?

Each time we add a new config option, we need to remember to add it here

@FloPinguin
Copy link
Contributor Author

regarding the ux, do we want to transfer the host or just tell players that the host has left and the game is cancelled?

I'm not totally sure. Wonder said that a discord discussion apparently concluded that we should close the lobby

But letting the lobby open reduces frustration, everybody doesn't need to join again

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

Labels

Projects

Status: Triage

Development

Successfully merging this pull request may close these issues.

3 participants