Many changes. Handle rudimentary lobbies. Handle game with simple state machine. Flip one player's board.

This commit is contained in:
Marco 2022-12-14 22:19:47 +01:00
parent 609d5f3833
commit 625e5b11fa
6 changed files with 167 additions and 53 deletions

View File

@ -7,6 +7,7 @@ import (
) )
func main() { func main() {
http.HandleFunc("/", server.SocketHandler) http.HandleFunc("/", server.SocketHandler)
err := http.ListenAndServe("localhost:8080", nil) err := http.ListenAndServe("localhost:8080", nil)
@ -16,3 +17,7 @@ func main() {
log.Println("We ended") log.Println("We ended")
} }
func HandleFuncWrapper(a func()) {
a()
}

View File

@ -1,49 +0,0 @@
package server
import (
"log"
"math/rand"
"github.com/gorilla/websocket"
)
type chessGame struct {
id int
}
func NewChessGame() *chessGame {
var game chessGame = chessGame{
id: rand.Int(),
}
return &game
}
func (game *chessGame) handle(conn *websocket.Conn) {
for {
messageType, receivedMessage, err := conn.ReadMessage()
if err != nil {
log.Println("Error during message reading:", err)
break
}
move, err := parseMove(string(receivedMessage))
if err != nil {
log.Println("Game: ", game.id, err)
return
}
log.Println("Game ", game.id, " move: ", move)
messageToBeSent := moveToString(*move)
log.Println("About to send: ", messageToBeSent)
err = conn.WriteMessage(messageType, []byte(messageToBeSent))
if err != nil {
log.Println("Error during message writing:", err)
break
}
}
}

105
server/game.go Normal file
View File

@ -0,0 +1,105 @@
package server
import (
"log"
"math/rand"
)
type chessGame struct {
id int
players [2]Player
currentTurnPlayer Player
}
const (
Player0_Move = 0
Player1_Move = 1
Check_Player_Change = 2
)
func NewChessGame(gameId int, players [2]Player) *chessGame {
var game chessGame = chessGame{
players: players,
id: gameId,
}
return &game
}
func (game *chessGame) handle() {
gameState := Player0_Move
game.currentTurnPlayer = game.players[0]
var move *chessMove
var messageType int
var receivedMessage []byte
game.players[0].conn.WriteMessage(1, []byte("fb"))
for {
switch gameState {
case Player0_Move:
var err error
messageType, receivedMessage, err = game.players[0].conn.ReadMessage()
if err != nil {
log.Println("Error during message reading:", err)
return
}
move, err = parseMove(string(receivedMessage))
if err != nil {
log.Println("Game: ", game.id, err)
return
}
gameState = Check_Player_Change
case Player1_Move:
var err error
messageType, receivedMessage, err = game.players[1].conn.ReadMessage()
if err != nil {
log.Println("Error during message reading:", err)
return
}
move, err = parseMove(string(receivedMessage))
if err != nil {
log.Println("Game: ", game.id, err)
return
}
gameState = Check_Player_Change
case Check_Player_Change:
if move.realMove {
if game.currentTurnPlayer == game.players[0] {
game.currentTurnPlayer = game.players[1]
gameState = Player1_Move
} else {
game.currentTurnPlayer = game.players[0]
gameState = Player0_Move
}
err := game.currentTurnPlayer.conn.WriteMessage(messageType, []byte(receivedMessage))
if err != nil {
log.Println("Error during message writing:", err)
break
}
} else {
if game.currentTurnPlayer == game.players[0] {
gameState = Player0_Move
} else {
gameState = Player1_Move
}
}
}
log.Println("GameState = ", gameState)
}
}
func addPlayersToGame(players [2]Player) {
log.Printf("Adding players \n%s\n%s\nto new game", players[0].name, players[1].name)
game := NewChessGame(rand.Int(), players)
go game.handle()
}

33
server/lobby.go Normal file
View File

@ -0,0 +1,33 @@
package server
import "log"
type Lobby struct {
players []Player
}
var lobbyInstance *Lobby = nil
func GetLobby() *Lobby {
if lobbyInstance == nil {
lobbyInstance = newLobby()
}
return lobbyInstance
}
func newLobby() *Lobby {
return &Lobby{}
}
func (lobby *Lobby) registerPlayer(player *Player) {
lobby.players = append(lobby.players, *player)
log.Println(lobby)
if len(lobby.players) == 2 {
player1 := lobby.players[0]
player2 := lobby.players[1]
addPlayersToGame([2]Player{player1, player2})
}
}

19
server/player.go Normal file
View File

@ -0,0 +1,19 @@
package server
import "github.com/gorilla/websocket"
type Player struct {
name string
conn *websocket.Conn
}
func NewPlayer(name string, conn *websocket.Conn) *Player {
return &Player{
name: name,
conn: conn,
}
}
func (player *Player) movePlayerToGame(game chessGame) {
}

View File

@ -1,12 +1,15 @@
package server package server
import ( import (
"fmt"
"log" "log"
"net/http" "net/http"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
) )
var player_number = 0
var upgrader = websocket.Upgrader{} // use default options var upgrader = websocket.Upgrader{} // use default options
func SocketHandler(w http.ResponseWriter, r *http.Request) { func SocketHandler(w http.ResponseWriter, r *http.Request) {
@ -17,8 +20,6 @@ func SocketHandler(w http.ResponseWriter, r *http.Request) {
log.Print("Error during connection upgrading:", err) log.Print("Error during connection upgrading:", err)
return return
} }
defer conn.Close()
defer log.Println("SocketHandler exited")
// Check if first message is the passphrase // Check if first message is the passphrase
msg_type, msg, err := conn.ReadMessage() msg_type, msg, err := conn.ReadMessage()
@ -33,6 +34,6 @@ func SocketHandler(w http.ResponseWriter, r *http.Request) {
conn.WriteMessage(msg_type, []byte("Password correct. Let's play")) conn.WriteMessage(msg_type, []byte("Password correct. Let's play"))
} }
var game = NewChessGame() GetLobby().registerPlayer(NewPlayer("Player"+fmt.Sprint(player_number), conn))
game.handle(conn) player_number = player_number + 1
} }