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:
@@ -18,6 +18,7 @@ import (
|
||||
"github.com/ryanhamamura/c4/features/c4game/components"
|
||||
"github.com/ryanhamamura/c4/features/c4game/pages"
|
||||
"github.com/ryanhamamura/c4/game"
|
||||
"github.com/ryanhamamura/c4/player"
|
||||
)
|
||||
|
||||
func HandleGamePage(store *game.GameStore, sessions *scs.SessionManager, queries *repository.Queries) http.HandlerFunc {
|
||||
@@ -30,15 +31,15 @@ func HandleGamePage(store *game.GameStore, sessions *scs.SessionManager, queries
|
||||
return
|
||||
}
|
||||
|
||||
playerID := game.PlayerID(sessions.GetString(r.Context(), "player_id"))
|
||||
playerID := player.ID(sessions.GetString(r.Context(), "player_id"))
|
||||
if playerID == "" {
|
||||
playerID = game.PlayerID(game.GenerateID(8))
|
||||
playerID = player.ID(player.GenerateID(8))
|
||||
sessions.Put(r.Context(), "player_id", string(playerID))
|
||||
}
|
||||
|
||||
userID := sessions.GetString(r.Context(), "user_id")
|
||||
if userID != "" {
|
||||
playerID = game.PlayerID(userID)
|
||||
playerID = player.ID(userID)
|
||||
}
|
||||
|
||||
nickname := sessions.GetString(r.Context(), "nickname")
|
||||
@@ -95,10 +96,10 @@ func HandleGameEvents(store *game.GameStore, nc *nats.Conn, sessions *scs.Sessio
|
||||
return
|
||||
}
|
||||
|
||||
playerID := game.PlayerID(sessions.GetString(r.Context(), "player_id"))
|
||||
playerID := player.ID(sessions.GetString(r.Context(), "player_id"))
|
||||
userID := sessions.GetString(r.Context(), "user_id")
|
||||
if userID != "" {
|
||||
playerID = game.PlayerID(userID)
|
||||
playerID = player.ID(userID)
|
||||
}
|
||||
|
||||
myColor := gi.GetPlayerColor(playerID)
|
||||
@@ -185,10 +186,10 @@ func HandleDropPiece(store *game.GameStore, sessions *scs.SessionManager) http.H
|
||||
return
|
||||
}
|
||||
|
||||
playerID := game.PlayerID(sessions.GetString(r.Context(), "player_id"))
|
||||
playerID := player.ID(sessions.GetString(r.Context(), "player_id"))
|
||||
userID := sessions.GetString(r.Context(), "user_id")
|
||||
if userID != "" {
|
||||
playerID = game.PlayerID(userID)
|
||||
playerID = player.ID(userID)
|
||||
}
|
||||
|
||||
myColor := gi.GetPlayerColor(playerID)
|
||||
@@ -229,10 +230,10 @@ func HandleSendChat(store *game.GameStore, nc *nats.Conn, sessions *scs.SessionM
|
||||
return
|
||||
}
|
||||
|
||||
playerID := game.PlayerID(sessions.GetString(r.Context(), "player_id"))
|
||||
playerID := player.ID(sessions.GetString(r.Context(), "player_id"))
|
||||
userID := sessions.GetString(r.Context(), "user_id")
|
||||
if userID != "" {
|
||||
playerID = game.PlayerID(userID)
|
||||
playerID = player.ID(userID)
|
||||
}
|
||||
|
||||
myColor := gi.GetPlayerColor(playerID)
|
||||
@@ -298,10 +299,10 @@ func HandleSetNickname(store *game.GameStore, sessions *scs.SessionManager) http
|
||||
|
||||
sessions.Put(r.Context(), "nickname", signals.Nickname)
|
||||
|
||||
playerID := game.PlayerID(sessions.GetString(r.Context(), "player_id"))
|
||||
playerID := player.ID(sessions.GetString(r.Context(), "player_id"))
|
||||
userID := sessions.GetString(r.Context(), "user_id")
|
||||
if userID != "" {
|
||||
playerID = game.PlayerID(userID)
|
||||
playerID = player.ID(userID)
|
||||
}
|
||||
|
||||
if gi.GetPlayerColor(playerID) == 0 {
|
||||
|
||||
Reference in New Issue
Block a user