Simplify codebase and fix Enter key on home page
- Enter key now triggers createGame action on home page - Remove redundant setNickname action from home page - Remove unused code: join channel, Leave(), Stop() methods - Consolidate ID generation into game.GenerateID() - Remove unused CreatedAt field from Game struct
This commit is contained in:
@@ -28,7 +28,7 @@ func NewGameStore() *GameStore {
|
||||
}
|
||||
|
||||
func (gs *GameStore) Create() *GameInstance {
|
||||
id := generateGameID()
|
||||
id := GenerateID(4)
|
||||
gi := NewGameInstance(id)
|
||||
gs.gamesMu.Lock()
|
||||
gs.games[id] = gi
|
||||
@@ -44,8 +44,8 @@ func (gs *GameStore) Get(id string) (*GameInstance, bool) {
|
||||
return gi, ok
|
||||
}
|
||||
|
||||
func generateGameID() string {
|
||||
b := make([]byte, 4)
|
||||
func GenerateID(size int) string {
|
||||
b := make([]byte, size)
|
||||
rand.Read(b)
|
||||
return hex.EncodeToString(b)
|
||||
}
|
||||
@@ -55,7 +55,6 @@ type GameInstance struct {
|
||||
gameMu sync.RWMutex
|
||||
players map[PlayerID]Syncable
|
||||
playersMu sync.RWMutex
|
||||
join chan *PlayerSession
|
||||
leave chan PlayerID
|
||||
done chan struct{}
|
||||
dirty bool
|
||||
@@ -65,7 +64,6 @@ func NewGameInstance(id string) *GameInstance {
|
||||
return &GameInstance{
|
||||
game: NewGame(id),
|
||||
players: make(map[PlayerID]Syncable),
|
||||
join: make(chan *PlayerSession, 5),
|
||||
leave: make(chan PlayerID, 5),
|
||||
done: make(chan struct{}),
|
||||
}
|
||||
@@ -101,10 +99,6 @@ func (gi *GameInstance) Join(ps *PlayerSession) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (gi *GameInstance) Leave(pid PlayerID) {
|
||||
gi.leave <- pid
|
||||
}
|
||||
|
||||
func (gi *GameInstance) GetGame() *Game {
|
||||
gi.gameMu.RLock()
|
||||
defer gi.gameMu.RUnlock()
|
||||
@@ -186,7 +180,3 @@ func (gi *GameInstance) publish() {
|
||||
sync.Sync()
|
||||
}
|
||||
}
|
||||
|
||||
func (gi *GameInstance) Stop() {
|
||||
close(gi.done)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package game
|
||||
|
||||
import "time"
|
||||
|
||||
type PlayerID string
|
||||
|
||||
type Player struct {
|
||||
@@ -27,7 +25,6 @@ type Game struct {
|
||||
Status GameStatus
|
||||
Winner *Player
|
||||
WinningCells [][2]int // Coordinates of winning 4 cells for highlighting
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
func NewGame(id string) *Game {
|
||||
@@ -36,6 +33,5 @@ func NewGame(id string) *Game {
|
||||
Board: [6][7]int{},
|
||||
CurrentTurn: 1, // Red goes first
|
||||
Status: StatusWaitingForPlayer,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user