From ee4841f5437c3da7cb3a546ae754ff8937c4d59d Mon Sep 17 00:00:00 2001 From: Marco Date: Sat, 22 Apr 2023 19:23:46 +0200 Subject: [PATCH] Rename chessGame to Game. Remove gorilla websocket. --- go.mod | 3 +-- go.sum | 2 -- main.go | 3 --- server/game.go | 29 +++++++++++------------------ server/lobby.go | 5 +++-- server/player.go | 3 +-- server/server.go | 25 ++----------------------- 7 files changed, 18 insertions(+), 52 deletions(-) diff --git a/go.mod b/go.mod index 49247a5..17052a6 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.20 require ( github.com/gin-gonic/autotls v0.0.5 github.com/gin-gonic/gin v1.9.0 - github.com/gorilla/websocket v1.5.0 + github.com/google/uuid v1.3.0 ) require ( @@ -16,7 +16,6 @@ require ( github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/validator/v10 v10.11.2 // indirect github.com/goccy/go-json v0.10.0 // indirect - github.com/google/uuid v1.3.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/cpuid/v2 v2.0.9 // indirect github.com/leodido/go-urn v1.2.1 // indirect diff --git a/go.sum b/go.sum index 5da78e0..a8e9861 100644 --- a/go.sum +++ b/go.sum @@ -28,8 +28,6 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= -github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4= diff --git a/main.go b/main.go index 8270ac5..c62d43a 100644 --- a/main.go +++ b/main.go @@ -9,15 +9,12 @@ import ( "github.com/gin-gonic/autotls" "github.com/gin-gonic/gin" "github.com/google/uuid" - "github.com/gorilla/websocket" ) var cert_path = "/etc/letsencrypt/live/chess.sw-gross.de/" var cert_file = cert_path + "fullchain.pem" var key_file = cert_path + "privkey.pem" -var upgrader = websocket.Upgrader{} - // album represents data about a record album. type album struct { ID string `json:"id"` diff --git a/server/game.go b/server/game.go index c9da184..2f57c10 100644 --- a/server/game.go +++ b/server/game.go @@ -2,11 +2,12 @@ package server import ( "log" - "math/rand" + + "github.com/google/uuid" ) -type chessGame struct { - id int +type Game struct { + id uuid.UUID players [2]Player currentTurnPlayer Player } @@ -16,16 +17,15 @@ const ( CheckPlayerChange = 1 ) -func NewChessGame(gameId int, players [2]Player) *chessGame { - var game chessGame = chessGame{ - players: players, - id: gameId, +func NewGame() *Game { + var game Game = Game{ + id: uuid.New(), } return &game } -func (game *chessGame) handle() { +func (game *Game) handle() { defer log.Println("Game ", game.id, ": handle() ended") @@ -35,12 +35,6 @@ func (game *chessGame) handle() { var messageType int var receivedMessage []byte - 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 { @@ -96,11 +90,10 @@ func (game *chessGame) handle() { } -func addPlayersToGame(players [2]Player) { - log.Printf("Adding players %d and %d to new game", players[0].uuid, players[1].uuid) - game := NewChessGame(rand.Int(), players) +func (game *Game) addPlayersToGame(players [2]Player) { + log.Printf("Adding players %s and %s to new game", players[0].uuid.String(), players[1].uuid.String()) - go game.handle() + game.players = players } func removePlayersFromLobby(players [2]Player) { diff --git a/server/lobby.go b/server/lobby.go index ba59387..200395c 100644 --- a/server/lobby.go +++ b/server/lobby.go @@ -33,7 +33,8 @@ func (lobby Lobby) RegisterPlayer(player *Player) { index += 1 } - addPlayersToGame(players) - removePlayersFromLobby(players) + game := NewGame() + + game.addPlayersToGame(players) } } diff --git a/server/player.go b/server/player.go index c29580b..e576c97 100644 --- a/server/player.go +++ b/server/player.go @@ -2,12 +2,11 @@ package server import ( "github.com/google/uuid" - "github.com/gorilla/websocket" ) type Player struct { uuid uuid.UUID - conn *websocket.Conn + //Websocket connection here } func NewPlayer(uuid uuid.UUID) *Player { diff --git a/server/server.go b/server/server.go index 8b8404b..7af7cdb 100644 --- a/server/server.go +++ b/server/server.go @@ -1,40 +1,19 @@ package server import ( - "log" "net/http" - - "github.com/gorilla/websocket" ) var AppPath = "/home/m/projects/programming/flutter_projects/mchess/build/web" -var player_number = 0 - -var upgrader = websocket.Upgrader{} - func PlayHandler(w http.ResponseWriter, r *http.Request) { - log.Println("PlayHandler invoked") - w.Write([]byte("Hello, this is your websocket connection speaking.")) - // Allow connections from any origin - upgrader.CheckOrigin = func(r *http.Request) bool { return true } - _, err := upgrader.Upgrade(w, r, nil) - if err != nil { - log.Print("Error during connection upgrading:", err) - return - } } func WriteMessageToPlayer(player *Player, msg []byte, msgType int) error { - log.Printf("Writing message: %s (with messagetype %d) to player %d", string(msg), msgType, player.uuid) - - return player.conn.WriteMessage(msgType, msg) + panic("Not implemented") } 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.uuid) - - return msgType, msg, err + panic("Not implemented") }