Players can now play against each other (if they behave)
This commit is contained in:
parent
3a2de183a1
commit
7da9b2dcb2
@ -12,9 +12,9 @@ type chessGame struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Player0_Move = 0
|
WhiteToMove = 0
|
||||||
Player1_Move = 1
|
BlackToMove = 1
|
||||||
Check_Player_Change = 2
|
CheckPlayerChange = 2
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewChessGame(gameId int, players [2]Player) *chessGame {
|
func NewChessGame(gameId int, players [2]Player) *chessGame {
|
||||||
@ -28,20 +28,24 @@ func NewChessGame(gameId int, players [2]Player) *chessGame {
|
|||||||
|
|
||||||
func (game *chessGame) handle() {
|
func (game *chessGame) handle() {
|
||||||
|
|
||||||
gameState := Player0_Move
|
gameState := WhiteToMove
|
||||||
game.currentTurnPlayer = game.players[0]
|
game.currentTurnPlayer = game.players[0]
|
||||||
var move *chessMove
|
var move *chessMove
|
||||||
var messageType int
|
var messageType int
|
||||||
var receivedMessage []byte
|
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 {
|
for {
|
||||||
|
|
||||||
switch gameState {
|
switch gameState {
|
||||||
case Player0_Move:
|
case WhiteToMove:
|
||||||
var err error
|
var err error
|
||||||
messageType, receivedMessage, err = game.players[0].conn.ReadMessage()
|
messageType, receivedMessage, err = ReadMessageFromPlayer(&game.players[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error during message reading:", err)
|
log.Println("Error during message reading:", err)
|
||||||
return
|
return
|
||||||
@ -53,11 +57,11 @@ func (game *chessGame) handle() {
|
|||||||
log.Println("Game: ", game.id, err)
|
log.Println("Game: ", game.id, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
gameState = Check_Player_Change
|
gameState = CheckPlayerChange
|
||||||
|
|
||||||
case Player1_Move:
|
case BlackToMove:
|
||||||
var err error
|
var err error
|
||||||
messageType, receivedMessage, err = game.players[1].conn.ReadMessage()
|
messageType, receivedMessage, err = ReadMessageFromPlayer(&game.players[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error during message reading:", err)
|
log.Println("Error during message reading:", err)
|
||||||
return
|
return
|
||||||
@ -69,29 +73,29 @@ func (game *chessGame) handle() {
|
|||||||
log.Println("Game: ", game.id, err)
|
log.Println("Game: ", game.id, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
gameState = Check_Player_Change
|
gameState = CheckPlayerChange
|
||||||
|
|
||||||
case Check_Player_Change:
|
case CheckPlayerChange:
|
||||||
|
|
||||||
if move.realMove {
|
if move.realMove {
|
||||||
if game.currentTurnPlayer.id == game.players[0].id {
|
if game.currentTurnPlayer.id == game.players[0].id {
|
||||||
game.currentTurnPlayer = game.players[1]
|
game.currentTurnPlayer = game.players[1]
|
||||||
gameState = Player1_Move
|
gameState = BlackToMove
|
||||||
} else {
|
} else {
|
||||||
game.currentTurnPlayer = game.players[0]
|
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 {
|
if err != nil {
|
||||||
log.Println("Error during message writing:", err)
|
log.Println("Error during message writing:", err)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if game.currentTurnPlayer.id == game.players[0].id {
|
if game.currentTurnPlayer.id == game.players[0].id {
|
||||||
gameState = Player0_Move
|
gameState = WhiteToMove
|
||||||
} else {
|
} else {
|
||||||
gameState = Player1_Move
|
gameState = BlackToMove
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,9 +25,9 @@ func (lobby *Lobby) registerPlayer(player *Player) {
|
|||||||
|
|
||||||
log.Println(lobby)
|
log.Println(lobby)
|
||||||
|
|
||||||
if len(lobby.players) == 2 {
|
if len(lobby.players)%2 == 0 {
|
||||||
player1 := lobby.players[0]
|
player1 := lobby.players[len(lobby.players)-2]
|
||||||
player2 := lobby.players[1]
|
player2 := lobby.players[len(lobby.players)-1]
|
||||||
addPlayersToGame([2]Player{player1, player2})
|
addPlayersToGame([2]Player{player1, player2})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,19 +20,19 @@ func SocketHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
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))
|
GetLobby().registerPlayer(NewPlayer(player_number, conn))
|
||||||
player_number = player_number + 1
|
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
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user