Skip to content

Commit

Permalink
Remove duplicate code for joining a player
Browse files Browse the repository at this point in the history
  • Loading branch information
Bios-Marcel committed Nov 9, 2024
1 parent 7866c0c commit f008303
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 17 deletions.
9 changes: 0 additions & 9 deletions internal/game/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,6 @@ func (lobby *Lobby) AppendFill(fill *FillEvent) {
lobby.currentDrawing = append(lobby.currentDrawing, fill)
}

func createPlayer(name string) *Player {
return &Player{
Name: SanitizeName(name),
ID: uuid.Must(uuid.NewV4()),
userSession: uuid.Must(uuid.NewV4()),
votedForKick: make(map[uuid.UUID]bool),
}
}

// SanitizeName removes invalid characters from the players name, resolves
// emoji codes, limits the name length and generates a new name if necessary.
func SanitizeName(name string) string {
Expand Down
16 changes: 8 additions & 8 deletions internal/game/lobby.go
Original file line number Diff line number Diff line change
Expand Up @@ -1012,12 +1012,7 @@ func CreateLobby(
}
}

player := createPlayer(playerName)
// Since the lobby is new and we aren't ready yet, the state needs to be
// standby.
player.State = Standby

lobby.players = append(lobby.players, player)
player := lobby.JoinPlayer(playerName)
lobby.Owner = player
lobby.creator = player

Expand Down Expand Up @@ -1135,8 +1130,13 @@ func (lobby *Lobby) GetAvailableWordHints(player *Player) []*WordHint {

// JoinPlayer creates a new player object using the given name and adds it
// to the lobbies playerlist. The new players is returned.
func (lobby *Lobby) JoinPlayer(playerName string) *Player {
player := createPlayer(playerName)
func (lobby *Lobby) JoinPlayer(name string) *Player {
player := &Player{
Name: SanitizeName(name),
ID: uuid.Must(uuid.NewV4()),
userSession: uuid.Must(uuid.NewV4()),
votedForKick: make(map[uuid.UUID]bool),
}

if lobby.State == Ongoing {
// Joining an existing game will mark you as a guesser, as someone is
Expand Down

0 comments on commit f008303

Please sign in to comment.