feat(rules): add game-specific metadata attachment and unify match metadata initialization
Added AttachGameMetadata to GameRules interface Implemented metadata setup for Battleship (phase + readiness flags) Implemented no-op metadata hook for TicTacToe Moved generic phase/ready metadata out of MatchInit Added game/mode metadata to match state Fixed json:"metadata" tag in MatchState
This commit is contained in:
@@ -89,7 +89,16 @@ func (b *BattleshipRules) InitBoards(players []*structs.Player, cfg GameConfigur
|
||||
// Assign player boards
|
||||
// ------------------------------
|
||||
func (b *BattleshipRules) AssignPlayerSymbols(players []*structs.Player) {
|
||||
// nothing needed for classic mode
|
||||
// nothing needed for battleship
|
||||
}
|
||||
|
||||
// ------------------------------
|
||||
// Attach Game Metadata
|
||||
// ------------------------------
|
||||
func (b *BattleshipRules) AttachGameMetadata(state *structs.MatchState) {
|
||||
state.Metadata["phase"] = "placement"
|
||||
state.Metadata["p0_ready"] = false
|
||||
state.Metadata["p1_ready"] = false
|
||||
}
|
||||
|
||||
// ------------------------------
|
||||
|
||||
@@ -21,6 +21,8 @@ type GameRules interface {
|
||||
// Assign symbols/colors/pieces at start.
|
||||
AssignPlayerSymbols(players []*structs.Player)
|
||||
|
||||
// Attach Game Metadata
|
||||
AttachGameMetadata(state *structs.MatchState)
|
||||
// Apply a move.
|
||||
// Returns: (changed, gameOver, winnerIndex)
|
||||
ApplyMove(
|
||||
|
||||
@@ -31,6 +31,13 @@ func (t *TicTacToeRules) AssignPlayerSymbols(players []*structs.Player) {
|
||||
players[1].Metadata["symbol"] = "O"
|
||||
}
|
||||
|
||||
// ------------------------------
|
||||
// Attach Game Metadata
|
||||
// ------------------------------
|
||||
func (b *TicTacToeRules) AttachGameMetadata(state *structs.MatchState) {
|
||||
// nothing needed for tictactoe
|
||||
}
|
||||
|
||||
// ValidateMove checks bounds and empty cell.
|
||||
func (t *TicTacToeRules) ValidateMove(state *structs.MatchState, playerIdx int, payload MovePayload) bool {
|
||||
|
||||
|
||||
@@ -130,9 +130,9 @@ func (m *GenericMatch) MatchInit(
|
||||
GameOver: false,
|
||||
Metadata: map[string]interface{}{},
|
||||
}
|
||||
state.Metadata["phase"] = "placement"
|
||||
state.Metadata["p0_ready"] = false
|
||||
state.Metadata["p1_ready"] = false
|
||||
state.Metadata["game"] = m.GameName
|
||||
state.Metadata["mode"] = m.Mode
|
||||
m.Rules.AttachGameMetadata(state)
|
||||
|
||||
label := fmt.Sprintf("%s:%s", m.GameName, m.Mode)
|
||||
|
||||
|
||||
@@ -7,5 +7,5 @@ type MatchState struct {
|
||||
Turn int `json:"turn"` // index in Players[]
|
||||
Winner int `json:"winner"` // -1 = none, >=0 = winner index
|
||||
GameOver bool `json:"game_over"` // true when the match ends
|
||||
Metadata map[string]interface{} `json:metadata` // metadata
|
||||
Metadata map[string]interface{} `json:"metadata"` // metadata
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user