Rename chessGame to Game. Remove gorilla websocket.

This commit is contained in:
Marco 2023-04-22 19:23:46 +02:00
parent e55187ee11
commit ee4841f543
7 changed files with 18 additions and 52 deletions

3
go.mod
View File

@ -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

2
go.sum
View File

@ -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=

View File

@ -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"`

View File

@ -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) {

View File

@ -33,7 +33,8 @@ func (lobby Lobby) RegisterPlayer(player *Player) {
index += 1
}
addPlayersToGame(players)
removePlayersFromLobby(players)
game := NewGame()
game.addPlayersToGame(players)
}
}

View File

@ -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 {

View File

@ -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")
}