feat: lobby shows player names and classes in room listing
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -13,10 +13,17 @@ const (
|
||||
RoomPlaying
|
||||
)
|
||||
|
||||
type LobbyPlayer struct {
|
||||
Name string
|
||||
Class string // empty until class selected
|
||||
Fingerprint string
|
||||
Ready bool
|
||||
}
|
||||
|
||||
type LobbyRoom struct {
|
||||
Code string
|
||||
Name string
|
||||
Players []string
|
||||
Players []LobbyPlayer
|
||||
Status RoomStatus
|
||||
Session *GameSession
|
||||
}
|
||||
@@ -45,7 +52,7 @@ func (l *Lobby) CreateRoom(name string) string {
|
||||
return code
|
||||
}
|
||||
|
||||
func (l *Lobby) JoinRoom(code, playerName string) error {
|
||||
func (l *Lobby) JoinRoom(code, playerName, fingerprint string) error {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
room, ok := l.rooms[code]
|
||||
@@ -58,10 +65,49 @@ func (l *Lobby) JoinRoom(code, playerName string) error {
|
||||
if room.Status != RoomWaiting {
|
||||
return fmt.Errorf("room %s already in progress", code)
|
||||
}
|
||||
room.Players = append(room.Players, playerName)
|
||||
room.Players = append(room.Players, LobbyPlayer{Name: playerName, Fingerprint: fingerprint})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *Lobby) SetPlayerClass(code, fingerprint, class string) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
if room, ok := l.rooms[code]; ok {
|
||||
for i := range room.Players {
|
||||
if room.Players[i].Fingerprint == fingerprint {
|
||||
room.Players[i].Class = class
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (l *Lobby) SetPlayerReady(code, fingerprint string, ready bool) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
if room, ok := l.rooms[code]; ok {
|
||||
for i := range room.Players {
|
||||
if room.Players[i].Fingerprint == fingerprint {
|
||||
room.Players[i].Ready = ready
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (l *Lobby) AllReady(code string) bool {
|
||||
l.mu.RLock()
|
||||
defer l.mu.RUnlock()
|
||||
room, ok := l.rooms[code]
|
||||
if !ok || len(room.Players) == 0 {
|
||||
return false
|
||||
}
|
||||
for _, p := range room.Players {
|
||||
if !p.Ready {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (l *Lobby) GetRoom(code string) *LobbyRoom {
|
||||
l.mu.RLock()
|
||||
defer l.mu.RUnlock()
|
||||
|
||||
@@ -17,7 +17,7 @@ func TestCreateRoom(t *testing.T) {
|
||||
func TestJoinRoom(t *testing.T) {
|
||||
lobby := NewLobby()
|
||||
code := lobby.CreateRoom("Test Room")
|
||||
err := lobby.JoinRoom(code, "player1")
|
||||
err := lobby.JoinRoom(code, "player1", "fp-player1")
|
||||
if err != nil {
|
||||
t.Errorf("Join failed: %v", err)
|
||||
}
|
||||
@@ -30,7 +30,7 @@ func TestJoinRoom(t *testing.T) {
|
||||
func TestRoomStatusTransition(t *testing.T) {
|
||||
l := NewLobby()
|
||||
code := l.CreateRoom("Test")
|
||||
l.JoinRoom(code, "Alice")
|
||||
l.JoinRoom(code, "Alice", "fp-alice")
|
||||
r := l.GetRoom(code)
|
||||
if r.Status != RoomWaiting {
|
||||
t.Errorf("new room should be Waiting, got %d", r.Status)
|
||||
@@ -40,7 +40,7 @@ func TestRoomStatusTransition(t *testing.T) {
|
||||
if r.Status != RoomPlaying {
|
||||
t.Errorf("started room should be Playing, got %d", r.Status)
|
||||
}
|
||||
err := l.JoinRoom(code, "Bob")
|
||||
err := l.JoinRoom(code, "Bob", "fp-bob")
|
||||
if err == nil {
|
||||
t.Error("should not be able to join a Playing room")
|
||||
}
|
||||
@@ -50,9 +50,9 @@ func TestJoinRoomFull(t *testing.T) {
|
||||
lobby := NewLobby()
|
||||
code := lobby.CreateRoom("Test Room")
|
||||
for i := 0; i < 4; i++ {
|
||||
lobby.JoinRoom(code, "player")
|
||||
lobby.JoinRoom(code, "player", "fp-player")
|
||||
}
|
||||
err := lobby.JoinRoom(code, "player5")
|
||||
err := lobby.JoinRoom(code, "player5", "fp-player5")
|
||||
if err == nil {
|
||||
t.Error("Should reject 5th player")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user