2022-12-14 21:19:47 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2023-04-22 17:23:46 +00:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2022-12-14 21:19:47 +00:00
|
|
|
)
|
|
|
|
|
2023-04-22 17:23:46 +00:00
|
|
|
type Game struct {
|
|
|
|
id uuid.UUID
|
2022-12-14 21:19:47 +00:00
|
|
|
players [2]Player
|
|
|
|
currentTurnPlayer Player
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2022-12-21 23:02:07 +00:00
|
|
|
PlayerToMove = 0
|
|
|
|
CheckPlayerChange = 1
|
2022-12-14 21:19:47 +00:00
|
|
|
)
|
|
|
|
|
2023-04-22 17:23:46 +00:00
|
|
|
func NewGame() *Game {
|
|
|
|
var game Game = Game{
|
|
|
|
id: uuid.New(),
|
2022-12-14 21:19:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &game
|
|
|
|
}
|
|
|
|
|
2023-05-30 20:01:20 +00:00
|
|
|
func (game Game) getPlayer1() *Player {
|
|
|
|
return &game.players[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (game Game) getPlayer2() *Player {
|
|
|
|
return &game.players[1]
|
|
|
|
}
|
|
|
|
|
2023-04-22 17:23:46 +00:00
|
|
|
func (game *Game) handle() {
|
2022-12-21 23:02:07 +00:00
|
|
|
defer log.Println("Game ", game.id, ": handle() ended")
|
|
|
|
|
2023-05-30 20:01:20 +00:00
|
|
|
<-game.getPlayer1().wsConnEstablished
|
|
|
|
log.Println("WS connection for player 1 established")
|
|
|
|
<-game.getPlayer2().wsConnEstablished
|
|
|
|
log.Println("WS connection for player 2 established")
|
|
|
|
|
2022-12-21 23:02:07 +00:00
|
|
|
gameState := PlayerToMove
|
2023-05-30 20:01:20 +00:00
|
|
|
game.currentTurnPlayer = *game.getPlayer1()
|
2023-04-22 19:41:24 +00:00
|
|
|
var move *Move
|
2022-12-14 21:19:47 +00:00
|
|
|
var receivedMessage []byte
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
|
|
|
switch gameState {
|
2022-12-21 23:02:07 +00:00
|
|
|
case PlayerToMove:
|
2022-12-14 21:19:47 +00:00
|
|
|
var err error
|
2023-05-30 20:01:20 +00:00
|
|
|
_, receivedMessage, err = game.currentTurnPlayer.ReadMessageFromPlayer()
|
2022-12-14 21:19:47 +00:00
|
|
|
if err != nil {
|
2022-12-21 23:02:07 +00:00
|
|
|
log.Println("Error while reading message:", err)
|
|
|
|
// At the moment, we return when there is an error while reading a message.
|
|
|
|
// This means, the game just ends uncontrolled
|
2022-12-14 21:19:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
move, err = parseMove(string(receivedMessage))
|
2022-12-21 23:02:07 +00:00
|
|
|
log.Println("Player ", game.currentTurnPlayer, " moved: ", move)
|
2022-12-14 22:16:51 +00:00
|
|
|
|
2022-12-14 21:19:47 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("Game: ", game.id, err)
|
2022-12-21 23:02:07 +00:00
|
|
|
continue
|
2022-12-14 21:19:47 +00:00
|
|
|
}
|
2022-12-18 00:05:23 +00:00
|
|
|
gameState = CheckPlayerChange
|
2022-12-14 21:19:47 +00:00
|
|
|
|
2022-12-18 00:05:23 +00:00
|
|
|
case CheckPlayerChange:
|
2022-12-14 21:19:47 +00:00
|
|
|
|
2023-04-22 19:41:24 +00:00
|
|
|
if game.currentTurnPlayer.Uuid == game.players[0].Uuid {
|
|
|
|
game.currentTurnPlayer = game.players[1]
|
2022-12-14 21:19:47 +00:00
|
|
|
} else {
|
2023-04-22 19:41:24 +00:00
|
|
|
game.currentTurnPlayer = game.players[0]
|
2022-12-14 21:19:47 +00:00
|
|
|
}
|
|
|
|
|
2023-05-30 20:01:20 +00:00
|
|
|
err := game.getPlayer1().WriteMessageToPlayer(receivedMessage)
|
2023-04-22 19:41:24 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("Error during message writing:", err)
|
|
|
|
continue
|
|
|
|
}
|
2023-05-30 20:01:20 +00:00
|
|
|
err = game.getPlayer2().WriteMessageToPlayer(receivedMessage)
|
2023-04-22 19:41:24 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("Error during message writing:", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
gameState = PlayerToMove
|
|
|
|
|
2022-12-14 21:19:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("GameState = ", gameState)
|
2022-12-21 23:02:07 +00:00
|
|
|
if gameState == PlayerToMove {
|
|
|
|
log.Println("with player ", game.currentTurnPlayer, " to move")
|
|
|
|
}
|
2022-12-14 21:19:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-22 17:23:46 +00:00
|
|
|
func (game *Game) addPlayersToGame(players [2]Player) {
|
2023-04-22 19:41:24 +00:00
|
|
|
log.Printf("Adding players %s and %s to new game", players[0].Uuid.String(), players[1].Uuid.String())
|
2022-12-14 21:19:47 +00:00
|
|
|
|
2023-05-30 20:01:20 +00:00
|
|
|
for i := range players {
|
|
|
|
players[i].InGame = true
|
2023-05-28 15:43:05 +00:00
|
|
|
}
|
|
|
|
|
2023-04-22 17:23:46 +00:00
|
|
|
game.players = players
|
2023-05-28 15:43:05 +00:00
|
|
|
|
|
|
|
go game.handle()
|
2022-12-14 21:19:47 +00:00
|
|
|
}
|