feat(games): unify ApplyMove signatures + remove unused player conversion

Updated TicTacToeRules and BattleshipRules to implement ApplyMove(state, playerIdx, payload) (bool, bool, int) as required by GameRules.

Added win/draw resolution logic directly inside each game’s ApplyMove return.

Removed obsolete convertToGamePlayers helper.

Updated GenericMatch to call AssignPlayerSymbols with []*structs.Player directly.

Ensured all rule implementations now fully satisfy the GameRules interface.
This commit is contained in:
2025-12-01 17:02:57 +05:30
parent 3c81a8bf29
commit 3eadb49a72
5 changed files with 109 additions and 67 deletions

View File

@@ -2,6 +2,7 @@ package games
import ( import (
"fmt" "fmt"
"encoding/json"
"localrepo/plugins/structs" "localrepo/plugins/structs"
) )
@@ -64,7 +65,7 @@ func (b *BattleshipRules) MaxPlayers() int { return 2 }
// ------------------------------ // ------------------------------
// Assign player boards // Assign player boards
// ------------------------------ // ------------------------------
func (b *BattleshipRules) AssignPlayerSymbols(players []*Player) { func (b *BattleshipRules) AssignPlayerSymbols(players []*structs.Player) {
// Battleship has no symbols like X/O, // Battleship has no symbols like X/O,
// but we use this hook to initialize per-player boards. // but we use this hook to initialize per-player boards.
@@ -110,30 +111,34 @@ func (b *BattleshipRules) ValidateMove(state *structs.MatchState, playerIdx int,
// ------------------------------ // ------------------------------
// ApplyMove // ApplyMove
// ------------------------------ // ------------------------------
func (b *BattleshipRules) ApplyMove(state *structs.MatchState, playerIdx int, payload MovePayload) { func (b *BattleshipRules) ApplyMove(
state *structs.MatchState,
playerIdx int,
payload MovePayload,
) (bool, bool, int) {
attacker := state.Players[playerIdx]
defenderIdx := 1 - playerIdx
defender := state.Players[defenderIdx]
attacker := state.Players[playerIdx] r := int(payload.Data["row"].(float64))
defenderIdx := 1 - playerIdx c := int(payload.Data["col"].(float64))
defender := state.Players[defenderIdx]
r := int(payload.Data["row"].(float64)) shotBoard := decodeBoard(attacker.Metadata["shot_board"])
c := int(payload.Data["col"].(float64)) shipBoard := decodeBoard(defender.Metadata["ship_board"])
shotBoard := decodeBoard(attacker.Metadata["shot_board"]) if shipBoard[r][c] == "S" {
shipBoard := decodeBoard(defender.Metadata["ship_board"]) shotBoard[r][c] = "H"
shipBoard[r][c] = "X"
} else {
shotBoard[r][c] = "M"
}
if shipBoard[r][c] == "S" { attacker.Metadata["shot_board"] = encodeBoard(shotBoard)
// hit defender.Metadata["ship_board"] = encodeBoard(shipBoard)
shotBoard[r][c] = "H"
shipBoard[r][c] = "X" // ship cell destroyed
} else {
// miss
shotBoard[r][c] = "M"
}
// Save back over, winner := b.CheckGameOver(state)
attacker.Metadata["shot_board"] = encodeBoard(shotBoard)
defender.Metadata["ship_board"] = encodeBoard(shipBoard) return true, over, winner
} }
// ------------------------------ // ------------------------------

View File

@@ -12,7 +12,7 @@ type GameRules interface {
MaxPlayers() int MaxPlayers() int
// Assign symbols/colors/pieces at start. // Assign symbols/colors/pieces at start.
AssignPlayerSymbols(players []*Player) AssignPlayerSymbols(players []*structs.Player)
// Apply a move. // Apply a move.
// Returns: (changed, gameOver, winnerIndex) // Returns: (changed, gameOver, winnerIndex)

View File

@@ -1,8 +1,6 @@
package games package games
import ( import (
"errors"
"fmt"
"localrepo/plugins/structs" "localrepo/plugins/structs"
) )
@@ -18,7 +16,7 @@ func (t *TicTacToeRules) MaxPlayers() int {
} }
// Assign player symbols: X and O // Assign player symbols: X and O
func (t *TicTacToeRules) AssignPlayerSymbols(players []*Player) { func (t *TicTacToeRules) AssignPlayerSymbols(players []*structs.Player) {
if len(players) < 2 { if len(players) < 2 {
return return
} }
@@ -57,16 +55,24 @@ func (t *TicTacToeRules) ValidateMove(state *structs.MatchState, playerIdx int,
} }
// ApplyMove writes X or O to the board. // ApplyMove writes X or O to the board.
func (t *TicTacToeRules) ApplyMove(state *structs.MatchState, playerIdx int, payload MovePayload) { func (t *TicTacToeRules) ApplyMove(
state *structs.MatchState,
playerIdx int,
payload MovePayload,
) (bool, bool, int) {
symbol := state.Players[playerIdx].Metadata["symbol"]
symbol := state.Players[playerIdx].Metadata["symbol"] r := int(payload.Data["row"].(float64))
c := int(payload.Data["col"].(float64))
r := int(payload.Data["row"].(float64)) state.Board.Set(r, c, symbol)
c := int(payload.Data["col"].(float64))
state.Board.Set(r, c, symbol) over, winner := t.CheckGameOver(state)
return true, over, winner
} }
// CheckGameOver determines win/draw state. // CheckGameOver determines win/draw state.
func (t *TicTacToeRules) CheckGameOver(state *structs.MatchState) (bool, int) { func (t *TicTacToeRules) CheckGameOver(state *structs.MatchState) (bool, int) {

View File

@@ -22,11 +22,6 @@ func InitModule(
//-------------------------------------------------------- //--------------------------------------------------------
// 1. Register RPCs // 1. Register RPCs
//-------------------------------------------------------- //--------------------------------------------------------
if err := initializer.RegisterRpc("hello_world", HelloWorld); err != nil {
logger.Error("Failed to register RPC hello_world: %v", err)
return err
}
if err := initializer.RegisterRpc("leave_matchmaking", modules.RpcLeaveMatchmaking); err != nil { if err := initializer.RegisterRpc("leave_matchmaking", modules.RpcLeaveMatchmaking); err != nil {
logger.Error("Failed to register RPC leave_matchmaking: %v", err) logger.Error("Failed to register RPC leave_matchmaking: %v", err)
return err return err

View File

@@ -19,6 +19,9 @@ const (
// GenericMatch is a match implementation that delegates game-specific logic // GenericMatch is a match implementation that delegates game-specific logic
// to a game.GameRules implementation chosen by the match params ("game"). // to a game.GameRules implementation chosen by the match params ("game").
type GenericMatch struct { type GenericMatch struct {
// Registry provided when creating the match factory. Keeps available rules.
Registry map[string]games.GameRules
GameName string GameName string
Mode string Mode string
Config games.GameConfiguration Config games.GameConfiguration
@@ -28,7 +31,7 @@ type GenericMatch struct {
// NewGenericMatch returns a factory function suitable for RegisterMatch. // NewGenericMatch returns a factory function suitable for RegisterMatch.
// Provide a registry mapping game names (strings) to implementations. // Provide a registry mapping game names (strings) to implementations.
func NewGenericMatch( func NewGenericMatch(
registry map[string]games.GameRules registry map[string]games.GameRules,
) func( ) func(
ctx context.Context, ctx context.Context,
logger runtime.Logger, logger runtime.Logger,
@@ -36,9 +39,14 @@ func NewGenericMatch(
nk runtime.NakamaModule, nk runtime.NakamaModule,
) (runtime.Match, error) { ) (runtime.Match, error) {
return func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule) (runtime.Match, error) { return func(
// Empty match instance; MatchInit will fill real config. ctx context.Context,
return &GenericMatch{}, nil logger runtime.Logger,
db *sql.DB,
nk runtime.NakamaModule,
) (runtime.Match, error) {
// The factory stores the registry on each match instance so MatchInit can use it.
return &GenericMatch{Registry: registry}, nil
} }
} }
@@ -71,7 +79,7 @@ func newEmptyBoard(rows, cols int) *structs.Board {
// Match interface methods // Match interface methods
// ------------------------- // -------------------------
// MatchInit: create initial state. Expects params to include "game" (string) and optionally "mode" and board size. // MatchInit: create initial state. Expects params to include "game" (string) and optionally "mode".
func (m *GenericMatch) MatchInit( func (m *GenericMatch) MatchInit(
ctx context.Context, ctx context.Context,
logger runtime.Logger, logger runtime.Logger,
@@ -92,31 +100,40 @@ func (m *GenericMatch) MatchInit(
return nil, 0, "" return nil, 0, ""
} }
// ---- 2. config lookup ----
cfg, found := games.GameConfig[gameName] cfg, found := games.GameConfig[gameName]
if !found { if !found {
logger.Error("MatchInit ERROR: game '%s' not in GameConfig", gameName) logger.Error("MatchInit ERROR: game '%s' not in GameConfig", gameName)
return nil, 0, "" return nil, 0, ""
} }
rules, found := games.RulesRegistry[gameName] // ---- 3. rules lookup from registry (factory-provided) ----
if !found { var rules games.GameRules
logger.Warn("GenericMatch MatchInit: unknown game '%s' — match will start but no rules attached", gameName) if m.Registry != nil {
if r, ok := m.Registry[gameName]; ok {
rules = r
}
}
if rules == nil {
// no rules — abort match creation
logger.Error("MatchInit ERROR: no rules registered for game '%s'", gameName)
return nil, 0, "" return nil, 0, ""
} }
// ---- 2. mode (optional) ---- // ---- 4. mode (optional) ----
mode := "default" mode := "default"
if md, ok := params["mode"].(string); ok && md != "" { if md, ok := params["mode"].(string); ok && md != "" {
mode = md mode = md
} }
// ---- 3. build match instance fields ---- // ---- 5. build match instance fields ----
m.GameName = gameName m.GameName = gameName
m.Mode = mode m.Mode = mode
m.Config = cfg m.Config = cfg
m.Rules = rules m.Rules = rules
// ---- 4. create initial state ---- // ---- 6. create initial state (board from config) ----
state := &structs.MatchState{ state := &structs.MatchState{
Players: []*structs.Player{}, Players: []*structs.Player{},
Board: newEmptyBoard(cfg.Board.Rows, cfg.Board.Cols), Board: newEmptyBoard(cfg.Board.Rows, cfg.Board.Cols),
@@ -128,13 +145,12 @@ func (m *GenericMatch) MatchInit(
label := fmt.Sprintf("%s:%s", m.GameName, m.Mode) label := fmt.Sprintf("%s:%s", m.GameName, m.Mode)
logger.Info("MatchInit OK — game=%s mode=%s players=%d board=%dx%d", m.GameName, m.Mode, cfg.Players, cfg.Board.Rows, cfg.Board.Cols) logger.Info("MatchInit OK — game=%s mode=%s players=%d board=%dx%d", m.GameName, m.Mode, cfg.Players, cfg.Board.Rows, cfg.Board.Cols)
// Store the gameName in the match's Registry via a reserved key? Simpler: keep label informative and rely on Registry lookup by gameName later.
// Tick rate 5 (200ms) is a sensible default; can be tuned per game. // Tick rate 5 (200ms) is a sensible default; can be tuned per game.
return state, 5, label return state, 5, label
} }
// MatchJoinAttempt: basic capacity check using rules.MaxPlayers() // MatchJoinAttempt: basic capacity check using config.Players
func (m *GenericMatch) MatchJoinAttempt( func (m *GenericMatch) MatchJoinAttempt(
ctx context.Context, ctx context.Context,
logger runtime.Logger, logger runtime.Logger,
@@ -149,6 +165,12 @@ func (m *GenericMatch) MatchJoinAttempt(
s := state.(*structs.MatchState) s := state.(*structs.MatchState)
if m.Config.Players <= 0 {
// defensive: require init to have populated config
logger.Error("MatchJoinAttempt ERROR: match config not initialized")
return s, false, "server error"
}
if len(s.Players) >= m.Config.Players { if len(s.Players) >= m.Config.Players {
return s, false, "match full" return s, false, "match full"
} }
@@ -189,16 +211,20 @@ func (m *GenericMatch) MatchJoin(
}) })
} }
if len(s.Players) == m.Config.Players { logger.Info("MatchJoin: now %d players (need %d)", len(s.Players), m.Config.Players)
// Assign player symbols/colors/etc.
if m.Rules != nil && len(s.Players) == m.Config.Players {
// Assign player symbols/colors/etc. Pass structs.Player directly.
m.Rules.AssignPlayerSymbols(s.Players) m.Rules.AssignPlayerSymbols(s.Players)
// Broadcast initial state // Broadcast initial state
if data, err := json.Marshal(s); err == nil { if data, err := json.Marshal(s); err == nil {
if err := dispatcher.BroadcastMessage(OpState, data, nil, nil, true); err != nil { if err := dispatcher.BroadcastMessage(OpState, data, nil, nil, true); err != nil {
logger.Error("BroadcastMessage (initial state) failed: %v", err) logger.Error("BroadcastMessage (initial state) failed: %v", err)
} }
} } else {
logger.Error("Failed to marshal initial state: %v", err)
}
} }
return s return s
@@ -228,26 +254,29 @@ func (m *GenericMatch) MatchLeave(
leaverIdx = indexOfPlayerByID(s.Players, presences[0].GetUserId()) leaverIdx = indexOfPlayerByID(s.Players, presences[0].GetUserId())
} }
winner := m.Rules.ForfeitWinner(s, leaverIdx) if m.Rules != nil {
s.Winner = winner winner := m.Rules.ForfeitWinner(s, leaverIdx)
s.GameOver = true s.Winner = winner
s.GameOver = true
} else {
// fallback: end match as forfeit
s.GameOver = true
s.Winner = -1
}
// broadcast final state // broadcast final state
if data, err := json.Marshal(s); err == nil { if data, err := json.Marshal(s); err == nil {
if err := dispatcher.BroadcastMessage(OpState, data, nil, nil, true); err != nil { if err := dispatcher.BroadcastMessage(OpState, data, nil, nil, true); err != nil {
logger.Error("BroadcastMessage (forfeit) failed: %v", err) logger.Error("BroadcastMessage (forfeit) failed: %v", err)
} }
} else {
logger.Error("Failed to marshal forfeit state: %v", err)
} }
return s return s
} }
// // MatchLoop: handle incoming move messages, delegate to the GameRules implementation
// ──────────────────────────────────────────────────────────
// MatchLoop
// ──────────────────────────────────────────────────────────
//
func (m *GenericMatch) MatchLoop( func (m *GenericMatch) MatchLoop(
ctx context.Context, ctx context.Context,
logger runtime.Logger, logger runtime.Logger,
@@ -267,6 +296,11 @@ func (m *GenericMatch) MatchLoop(
changed := false changed := false
if m.Rules == nil {
logger.Warn("MatchLoop: no rules present for game '%s' -- ignoring messages", m.GameName)
return s
}
for _, msg := range messages { for _, msg := range messages {
if msg.GetOpCode() != OpMove { if msg.GetOpCode() != OpMove {
continue continue
@@ -285,13 +319,15 @@ func (m *GenericMatch) MatchLoop(
continue continue
} }
// Turn enforcement — keep this here for turn-based games. If you want per-game control,
// move this check into the game's ApplyMove implementation or toggle via config.
if playerIdx != s.Turn { if playerIdx != s.Turn {
logger.Warn("Move rejected: not player's turn (idx=%d turn=%d)", playerIdx, s.Turn) logger.Warn("Move rejected: not player's turn (idx=%d turn=%d)", playerIdx, s.Turn)
continue continue
} }
// Delegate to rules.ApplyMove which returns (changed, gameOver, winnerIndex) // Delegate to rules.ApplyMove which returns (changed, gameOver, winnerIndex)
stateChanged, gameOver, winnerIdx := rules.ApplyMove(s, playerIdx, payload) stateChanged, gameOver, winnerIdx := m.Rules.ApplyMove(s, playerIdx, payload)
if stateChanged { if stateChanged {
changed = true changed = true
@@ -301,13 +337,13 @@ func (m *GenericMatch) MatchLoop(
s.GameOver = true s.GameOver = true
s.Winner = winnerIdx s.Winner = winnerIdx
} else { } else {
s.Turn = (s.Turn + 1) % len(s.Players) if len(s.Players) > 0 {
s.Turn = (s.Turn + 1) % len(s.Players)
}
} }
} }
// optional: if gameOver and winnerIdx >= 0, write leaderboard here if desired // Optional: handle leaderboard logic here if needed
// This code left intentionally out — you can add leaderboard writes by asking for that feature.
}
if changed { if changed {
if data, err := json.Marshal(s); err == nil { if data, err := json.Marshal(s); err == nil {