Players can now play against each other (if they behave)

This commit is contained in:
Marco 2022-12-18 01:05:23 +01:00
parent 3a2de183a1
commit 7da9b2dcb2
3 changed files with 37 additions and 33 deletions

View File

@ -12,9 +12,9 @@ type chessGame struct {
}
const (
Player0_Move = 0
Player1_Move = 1
Check_Player_Change = 2
WhiteToMove = 0
BlackToMove = 1
CheckPlayerChange = 2
)
func NewChessGame(gameId int, players [2]Player) *chessGame {
@ -28,20 +28,24 @@ func NewChessGame(gameId int, players [2]Player) *chessGame {
func (game *chessGame) handle() {
gameState := Player0_Move
gameState := WhiteToMove
game.currentTurnPlayer = game.players[0]
var move *chessMove
var messageType int
var receivedMessage []byte
game.players[1].conn.WriteMessage(1, []byte("fb"))
WriteMessageToPlayer(&game.players[0], []byte("bd init"), 1)
WriteMessageToPlayer(&game.players[1], []byte("bd init"), 1)
WriteMessageToPlayer(&game.players[0], []byte("cl white"), 1)
WriteMessageToPlayer(&game.players[1], []byte("cl black"), 1)
for {
switch gameState {
case Player0_Move:
case WhiteToMove:
var err error
messageType, receivedMessage, err = game.players[0].conn.ReadMessage()
messageType, receivedMessage, err = ReadMessageFromPlayer(&game.players[0])
if err != nil {
log.Println("Error during message reading:", err)
return
@ -53,11 +57,11 @@ func (game *chessGame) handle() {
log.Println("Game: ", game.id, err)
return
}
gameState = Check_Player_Change
gameState = CheckPlayerChange
case Player1_Move:
case BlackToMove:
var err error
messageType, receivedMessage, err = game.players[1].conn.ReadMessage()
messageType, receivedMessage, err = ReadMessageFromPlayer(&game.players[1])
if err != nil {
log.Println("Error during message reading:", err)
return
@ -69,29 +73,29 @@ func (game *chessGame) handle() {
log.Println("Game: ", game.id, err)
return
}
gameState = Check_Player_Change
gameState = CheckPlayerChange
case Check_Player_Change:
case CheckPlayerChange:
if move.realMove {
if game.currentTurnPlayer.id == game.players[0].id {
game.currentTurnPlayer = game.players[1]
gameState = Player1_Move
gameState = BlackToMove
} else {
game.currentTurnPlayer = game.players[0]
gameState = Player0_Move
gameState = WhiteToMove
}
err := game.currentTurnPlayer.conn.WriteMessage(messageType, []byte(receivedMessage))
err := WriteMessageToPlayer(&game.currentTurnPlayer, receivedMessage, messageType)
if err != nil {
log.Println("Error during message writing:", err)
break
}
} else {
if game.currentTurnPlayer.id == game.players[0].id {
gameState = Player0_Move
gameState = WhiteToMove
} else {
gameState = Player1_Move
gameState = BlackToMove
}
}

View File

@ -25,9 +25,9 @@ func (lobby *Lobby) registerPlayer(player *Player) {
log.Println(lobby)
if len(lobby.players) == 2 {
player1 := lobby.players[0]
player2 := lobby.players[1]
if len(lobby.players)%2 == 0 {
player1 := lobby.players[len(lobby.players)-2]
player2 := lobby.players[len(lobby.players)-1]
addPlayersToGame([2]Player{player1, player2})
}
}

View File

@ -20,19 +20,19 @@ func SocketHandler(w http.ResponseWriter, r *http.Request) {
return
}
// Check if first message is the passphrase
msg_type, msg, err := conn.ReadMessage()
if err != nil {
return
}
if string(msg) != "pw NC4EjHvRcsltY3ibWuYDH9OXbKgDXppfnHNCi1K4jktMspoeZjBnOPExxCpDms7zmxijoKCSaSlZ1fWclfWr7Q32DJnv2k87Z5uM" {
conn.WriteMessage(msg_type, []byte("Wrong password. Connection refused"))
return
} else {
conn.WriteMessage(msg_type, []byte("Password correct. Let's play"))
}
GetLobby().registerPlayer(NewPlayer(player_number, conn))
player_number = player_number + 1
}
func WriteMessageToPlayer(player *Player, msg []byte, msgType int) error {
log.Printf("Writing message: %s (with messagetype %d) to player %d", string(msg), msgType, player.id)
log.Println("")
return player.conn.WriteMessage(msgType, msg)
}
func ReadMessageFromPlayer(player *Player) (messageType int, p []byte, err error) {
msgType, msg, err := player.conn.ReadMessage()
log.Printf("Reading message: %s (with messagetype %d) from player %d", string(msg), msgType, player.id)
log.Println("")
return msgType, msg, err
}