Skip to content

Commit 61ca5fc

Browse files
committed
lobby owner now stored as ID
1 parent 923a709 commit 61ca5fc

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

internal/api/v1.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ func (handler *V1Handler) patchLobby(writer http.ResponseWriter, request *http.R
354354
clientsPerIPLimit, clientsPerIPLimitInvalid := ParseClientsPerIPLimit(handler.cfg, request.Form.Get("clients_per_ip_limit"))
355355
publicLobby, publicLobbyInvalid := ParseBoolean("public", request.Form.Get("public"))
356356

357-
owner := lobby.Owner
357+
owner := lobby.GetOwner()
358358
if owner == nil || owner.GetUserSession() != userSession {
359359
http.Error(writer, "only the lobby owner can edit the lobby", http.StatusForbidden)
360360
return

internal/game/data.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ type Lobby struct {
3737

3838
// Whether the game has started, is ongoing or already over.
3939
State State
40-
// Owner references the Player that currently owns the lobby.
40+
// OwnerID references the Player that currently owns the lobby.
4141
// Meaning this player has rights to restart or change certain settings.
42-
Owner *Player
42+
OwnerID uuid.UUID
4343
// ScoreCalculation decides how scores for both guessers and drawers are
4444
// determined.
4545
ScoreCalculation ScoreCalculation
@@ -149,6 +149,10 @@ func (lobby *Lobby) GetPlayer(userSession uuid.UUID) *Player {
149149
return nil
150150
}
151151

152+
func (lobby *Lobby) GetOwner() *Player {
153+
return lobby.GetPlayer(lobby.OwnerID)
154+
}
155+
152156
func (lobby *Lobby) ClearDrawing() {
153157
lobby.currentDrawing = make([]any, 0)
154158
lobby.connectedDrawEventsIndexStack = nil

internal/game/lobby.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func (lobby *Lobby) HandleEvent(eventType string, payload []byte, player *Player
178178
} else if eventType == EventTypeToggleReadiness {
179179
lobby.handleToggleReadinessEvent(player)
180180
} else if eventType == EventTypeStart {
181-
if lobby.State != Ongoing && player == lobby.Owner {
181+
if lobby.State != Ongoing && player.ID == lobby.OwnerID {
182182
lobby.startGame()
183183
}
184184
} else if eventType == EventTypeNameChange {
@@ -429,7 +429,7 @@ func handleKickVoteEvent(lobby *Lobby, player *Player, toKickID uuid.UUID) {
429429
}
430430
}
431431

432-
votesRequired := calculateVotesNeededToKick(playerToKick, lobby)
432+
votesRequired := calculateVotesNeededToKick(lobby)
433433

434434
// We send the kick event to all players, since it was a valid vote.
435435
lobby.Broadcast(&Event{
@@ -464,11 +464,11 @@ func kickPlayer(lobby *Lobby, playerToKick *Player, playerToKickIndex int) {
464464
}
465465

466466
// If the owner is kicked, we choose the next best person as the owner.
467-
if lobby.Owner == playerToKick {
467+
if lobby.OwnerID == playerToKick.ID {
468468
for _, otherPlayer := range lobby.players {
469469
potentialOwner := otherPlayer
470470
if potentialOwner.Connected {
471-
lobby.Owner = potentialOwner
471+
lobby.OwnerID = potentialOwner.ID
472472
lobby.Broadcast(&Event{
473473
Type: EventTypeOwnerChange,
474474
Data: &OwnerChangeEvent{
@@ -517,7 +517,7 @@ func (lobby *Lobby) Drawer() *Player {
517517
return nil
518518
}
519519

520-
func calculateVotesNeededToKick(playerToKick *Player, lobby *Lobby) int {
520+
func calculateVotesNeededToKick(lobby *Lobby) int {
521521
connectedPlayerCount := lobby.GetConnectedPlayerCount()
522522

523523
// If there are only two players, e.g. none of them should be able to
@@ -971,7 +971,7 @@ func CreateLobby(
971971
}
972972

973973
player := lobby.JoinPlayer(playerName)
974-
lobby.Owner = player
974+
lobby.OwnerID = player.ID
975975

976976
return player, lobby, nil
977977
}
@@ -990,7 +990,7 @@ func generateReadyData(lobby *Lobby, player *Player) *ReadyEvent {
990990
PlayerName: player.Name,
991991

992992
GameState: lobby.State,
993-
OwnerID: lobby.Owner.ID,
993+
OwnerID: lobby.OwnerID,
994994
Round: lobby.Round,
995995
Rounds: lobby.Rounds,
996996
DrawingTimeSetting: lobby.DrawingTime,

internal/game/lobby_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
func createLobbyWithDemoPlayers(playercount int) *Lobby {
1818
owner := &Player{}
1919
lobby := &Lobby{
20-
Owner: owner,
20+
OwnerID: owner.ID,
2121
}
2222
for range playercount {
2323
lobby.players = append(lobby.players, &Player{
@@ -65,7 +65,7 @@ func Test_CalculateVotesNeededToKick(t *testing.T) {
6565

6666
for playerCount, expctedRequiredVotes := range expectedResults {
6767
lobby := createLobbyWithDemoPlayers(playerCount)
68-
result := calculateVotesNeededToKick(nil, lobby)
68+
result := calculateVotesNeededToKick(lobby)
6969
if result != expctedRequiredVotes {
7070
t.Errorf("Error. Necessary vote amount was %d, but should've been %d", result, expctedRequiredVotes)
7171
}
@@ -277,7 +277,7 @@ func Test_wordSelectionEvent(t *testing.T) {
277277

278278
drawer := lobby.JoinPlayer("Drawer")
279279
drawer.Connected = true
280-
lobby.Owner = drawer
280+
lobby.OwnerID = drawer.ID
281281

282282
if err := lobby.HandleEvent(EventTypeStart, nil, drawer); err != nil {
283283
t.Errorf("Couldn't start lobby: %s", err)
@@ -343,7 +343,7 @@ func Test_kickDrawer(t *testing.T) {
343343

344344
marcel := lobby.JoinPlayer("marcel")
345345
marcel.Connected = true
346-
lobby.Owner = marcel
346+
lobby.OwnerID = marcel.ID
347347

348348
kevin := lobby.JoinPlayer("kevin")
349349
kevin.Connected = true

0 commit comments

Comments
 (0)