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

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