refactor(server): move shared game logic into games/ and introduce GameConfig system
- Moved common/game.go → plugins/games/rules.go
- Contains GameRules interface, MovePayload, Player abstraction
- Centralizes all reusable game rule contract logic
- Added plugins/games/config.go
- Introduces GameConfiguration struct (players, board size, etc.)
- Added global GameConfig and RulesRegistry maps
- Each game (tictactoe, battleship, etc.) now registers its config + rules
- Updated generic_match.go to use:
- GameConfig for board/players initialization
- RulesRegistry for rule lookup during MatchInit
- Removed hardcoded TicTacToe behavior
- Clean error returns when game param missing or invalid
- Updated folder structure:
/plugins/
/games
rules.go (formerly common/game.go)
config.go
tictactoe.go
battleship.go
/structs
/modules
main.go
- Ensures GenericMatch pulls:
m.GameName, m.Mode, m.Config, m.Rules
directly from config/registry at MatchInit
- Removes old duplicated logic and simplifies how games are registered
This commit is contained in:
27
plugins/games/rules.go
Normal file
27
plugins/games/rules.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package games
|
||||
|
||||
import "localrepo/plugins/structs"
|
||||
|
||||
// MovePayload is used for incoming move data from clients.
|
||||
type MovePayload struct {
|
||||
Data map[string]interface{} `json:"data"`
|
||||
}
|
||||
|
||||
type GameRules interface {
|
||||
// Number of players needed to start.
|
||||
MaxPlayers() int
|
||||
|
||||
// Assign symbols/colors/pieces at start.
|
||||
AssignPlayerSymbols(players []*Player)
|
||||
|
||||
// Apply a move.
|
||||
// Returns: (changed, gameOver, winnerIndex)
|
||||
ApplyMove(state *structs.MatchState, playerIdx int, payload MovePayload) (bool, bool, int)
|
||||
|
||||
// If a player leaves, who wins?
|
||||
// Return:
|
||||
// >=0 → winner index
|
||||
// -1 → draw
|
||||
// -2 → invalid
|
||||
ForfeitWinner(state *structs.MatchState, leaverIndex int) int
|
||||
}
|
||||
Reference in New Issue
Block a user