refactor: extract shared player.ID type and GenerateID to player package
Both game and snake packages had identical PlayerID types and the snake package imported game.GenerateID. Now both use player.ID and player.GenerateID from the shared player package.
This commit is contained in:
@@ -2,11 +2,10 @@ package game
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"sync"
|
||||
|
||||
"github.com/ryanhamamura/c4/db/repository"
|
||||
"github.com/ryanhamamura/c4/player"
|
||||
)
|
||||
|
||||
type PlayerSession struct {
|
||||
@@ -40,7 +39,7 @@ func (gs *GameStore) makeNotify(gameID string) func() {
|
||||
}
|
||||
|
||||
func (gs *GameStore) Create() *GameInstance {
|
||||
id := GenerateID(4)
|
||||
id := player.GenerateID(4)
|
||||
gi := NewGameInstance(id)
|
||||
gi.queries = gs.queries
|
||||
gi.notify = gs.makeNotify(id)
|
||||
@@ -107,12 +106,6 @@ func (gs *GameStore) Delete(id string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func GenerateID(size int) string {
|
||||
b := make([]byte, size)
|
||||
_, _ = rand.Read(b)
|
||||
return hex.EncodeToString(b)
|
||||
}
|
||||
|
||||
type GameInstance struct {
|
||||
game *Game
|
||||
gameMu sync.RWMutex
|
||||
@@ -166,7 +159,7 @@ func (gi *GameInstance) GetGame() *Game {
|
||||
return gi.game
|
||||
}
|
||||
|
||||
func (gi *GameInstance) GetPlayerColor(pid PlayerID) int {
|
||||
func (gi *GameInstance) GetPlayerColor(pid player.ID) int {
|
||||
gi.gameMu.RLock()
|
||||
defer gi.gameMu.RUnlock()
|
||||
for _, p := range gi.game.Players {
|
||||
|
||||
Reference in New Issue
Block a user