fix: use fingerprint as player ID to prevent name collision
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -70,8 +70,8 @@ type GameSession struct {
|
||||
}
|
||||
|
||||
type playerActionMsg struct {
|
||||
PlayerName string
|
||||
Action PlayerAction
|
||||
PlayerID string
|
||||
Action PlayerAction
|
||||
}
|
||||
|
||||
func NewGameSession() *GameSession {
|
||||
@@ -209,12 +209,12 @@ func (s *GameSession) GetState() GameState {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *GameSession) SubmitAction(playerName string, action PlayerAction) {
|
||||
s.actionCh <- playerActionMsg{PlayerName: playerName, Action: action}
|
||||
func (s *GameSession) SubmitAction(playerID string, action PlayerAction) {
|
||||
s.actionCh <- playerActionMsg{PlayerID: playerID, Action: action}
|
||||
}
|
||||
|
||||
// BuyItem handles shop purchases
|
||||
func (s *GameSession) BuyItem(playerName string, itemIdx int) bool {
|
||||
func (s *GameSession) BuyItem(playerID string, itemIdx int) bool {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if s.state.Phase != PhaseShop || itemIdx < 0 || itemIdx >= len(s.state.ShopItems) {
|
||||
@@ -222,7 +222,7 @@ func (s *GameSession) BuyItem(playerName string, itemIdx int) bool {
|
||||
}
|
||||
item := s.state.ShopItems[itemIdx]
|
||||
for _, p := range s.state.Players {
|
||||
if p.Name == playerName && p.Gold >= item.Price {
|
||||
if p.Fingerprint == playerID && p.Gold >= item.Price {
|
||||
p.Gold -= item.Price
|
||||
p.Inventory = append(p.Inventory, item)
|
||||
return true
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
func TestGetStateNoRace(t *testing.T) {
|
||||
s := NewGameSession()
|
||||
p := entity.NewPlayer("Racer", entity.ClassWarrior)
|
||||
p.Fingerprint = "test-fp"
|
||||
s.AddPlayer(p)
|
||||
s.StartGame()
|
||||
|
||||
@@ -30,7 +31,7 @@ func TestGetStateNoRace(t *testing.T) {
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
select {
|
||||
case s.actionCh <- playerActionMsg{PlayerName: "Racer", Action: PlayerAction{Type: ActionWait}}:
|
||||
case s.actionCh <- playerActionMsg{PlayerID: "test-fp", Action: PlayerAction{Type: ActionWait}}:
|
||||
default:
|
||||
}
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
@@ -41,6 +42,7 @@ func TestGetStateNoRace(t *testing.T) {
|
||||
func TestSessionTurnTimeout(t *testing.T) {
|
||||
s := NewGameSession()
|
||||
p := entity.NewPlayer("test", entity.ClassWarrior)
|
||||
p.Fingerprint = "test-fp"
|
||||
s.AddPlayer(p)
|
||||
s.StartFloor()
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ func (s *GameSession) RunTurn() {
|
||||
select {
|
||||
case msg := <-s.actionCh:
|
||||
s.mu.Lock()
|
||||
s.actions[msg.PlayerName] = msg.Action
|
||||
s.actions[msg.PlayerID] = msg.Action
|
||||
s.mu.Unlock()
|
||||
collected++
|
||||
case <-timer.C:
|
||||
@@ -53,8 +53,8 @@ resolve:
|
||||
// Default action for players who didn't submit: Wait
|
||||
for _, p := range s.state.Players {
|
||||
if !p.IsOut() {
|
||||
if _, ok := s.actions[p.Name]; !ok {
|
||||
s.actions[p.Name] = PlayerAction{Type: ActionWait}
|
||||
if _, ok := s.actions[p.Fingerprint]; !ok {
|
||||
s.actions[p.Fingerprint] = PlayerAction{Type: ActionWait}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -79,7 +79,7 @@ func (s *GameSession) resolvePlayerActions() {
|
||||
if p.IsOut() {
|
||||
continue
|
||||
}
|
||||
action, ok := s.actions[p.Name]
|
||||
action, ok := s.actions[p.Fingerprint]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user