Compare commits
No commits in common. "master" and "simple-color-fixing" have entirely different histories.
master
...
simple-col
15
.vscode/launch.json
vendored
Normal file
15
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
// Use IntelliSense to learn about possible attributes.
|
||||||
|
// Hover to view descriptions of existing attributes.
|
||||||
|
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "Launch Package",
|
||||||
|
"type": "go",
|
||||||
|
"request": "launch",
|
||||||
|
"mode": "debug",
|
||||||
|
"program": "${workspaceFolder}/main.go"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
@ -30,6 +30,7 @@ func HostGameHandler(c *gin.Context) {
|
|||||||
passphrase := lobby.Passphrase.String()
|
passphrase := lobby.Passphrase.String()
|
||||||
info := api.PlayerInfo{
|
info := api.PlayerInfo{
|
||||||
PlayerID: &player.Uuid,
|
PlayerID: &player.Uuid,
|
||||||
|
LobbyID: &lobby.Uuid,
|
||||||
Passphrase: &passphrase,
|
Passphrase: &passphrase,
|
||||||
}
|
}
|
||||||
c.Header("Access-Control-Allow-Origin", "*")
|
c.Header("Access-Control-Allow-Origin", "*")
|
||||||
@ -53,14 +54,14 @@ func GetLobbyForPassphraseHandler(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
passphrase := api.Passphrase{
|
lobbyInfo := api.LobbyInfo{
|
||||||
Value: (*string)(&lobby.Passphrase),
|
ID: &lobby.Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Header("Access-Control-Allow-Origin", "*")
|
c.IndentedJSON(http.StatusOK, lobbyInfo)
|
||||||
c.IndentedJSON(http.StatusOK, passphrase)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: this will be replaced by the JoinGameHandler()
|
||||||
func JoinPrivateGame(c *gin.Context) {
|
func JoinPrivateGame(c *gin.Context) {
|
||||||
limiter.Take()
|
limiter.Take()
|
||||||
|
|
||||||
@ -68,29 +69,27 @@ func JoinPrivateGame(c *gin.Context) {
|
|||||||
err := c.ShouldBindJSON(&req)
|
err := c.ShouldBindJSON(&req)
|
||||||
if err != nil || req.Passphrase == nil || *req.Passphrase == "" {
|
if err != nil || req.Passphrase == nil || *req.Passphrase == "" {
|
||||||
c.IndentedJSON(http.StatusNotFound, req)
|
c.IndentedJSON(http.StatusNotFound, req)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
u := lobbies.GetUsher()
|
u := lobbies.GetUsher()
|
||||||
|
|
||||||
if req.PlayerID != nil { //is reconnect
|
if req.Passphrase != nil &&
|
||||||
|
*req.Passphrase != "" &&
|
||||||
|
req.PlayerID != nil &&
|
||||||
|
req.LobbyID != nil { //is reconnect
|
||||||
lobby := u.FindExistingPrivateLobby(utils.Passphrase(*req.Passphrase))
|
lobby := u.FindExistingPrivateLobby(utils.Passphrase(*req.Passphrase))
|
||||||
var found bool
|
_, found := lobby.GetPlayerByUUID(*req.PlayerID)
|
||||||
if lobby != nil {
|
|
||||||
_, found = lobby.GetPlayerByUUID(*req.PlayerID)
|
|
||||||
}
|
|
||||||
if found {
|
if found {
|
||||||
c.Header("Access-Control-Allow-Origin", "*")
|
|
||||||
c.IndentedJSON(
|
c.IndentedJSON(
|
||||||
http.StatusOK,
|
http.StatusOK,
|
||||||
api.PlayerInfo{
|
api.PlayerInfo{
|
||||||
PlayerID: req.PlayerID,
|
PlayerID: req.PlayerID,
|
||||||
|
LobbyID: req.LobbyID,
|
||||||
Passphrase: req.Passphrase,
|
Passphrase: req.Passphrase,
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
c.IndentedJSON(http.StatusNotFound, req)
|
c.IndentedJSON(http.StatusNotFound, req)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,6 +107,7 @@ func JoinPrivateGame(c *gin.Context) {
|
|||||||
|
|
||||||
info := api.PlayerInfo{
|
info := api.PlayerInfo{
|
||||||
PlayerID: &player.Uuid,
|
PlayerID: &player.Uuid,
|
||||||
|
LobbyID: &lobby.Uuid,
|
||||||
Passphrase: req.Passphrase,
|
Passphrase: req.Passphrase,
|
||||||
}
|
}
|
||||||
c.Header("Access-Control-Allow-Origin", "*")
|
c.Header("Access-Control-Allow-Origin", "*")
|
||||||
@ -117,22 +117,25 @@ func JoinPrivateGame(c *gin.Context) {
|
|||||||
func JoinGameHandler(c *gin.Context) {
|
func JoinGameHandler(c *gin.Context) {
|
||||||
limiter.Take()
|
limiter.Take()
|
||||||
|
|
||||||
passphrase := api.Passphrase{}
|
id := c.Param("id")
|
||||||
err := c.ShouldBindJSON(&passphrase)
|
idAsUUID, err := uuid.Parse(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.IndentedJSON(http.StatusBadRequest, nil)
|
c.IndentedJSON(http.StatusBadRequest, nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
passphrase := api.Passphrase{}
|
||||||
|
c.ShouldBindJSON(&passphrase)
|
||||||
|
|
||||||
u := lobbies.GetUsher()
|
u := lobbies.GetUsher()
|
||||||
lobby := u.FindExistingPrivateLobby(utils.Passphrase(*passphrase.Value))
|
lobby := u.GetLobbyByID(idAsUUID)
|
||||||
if lobby == nil {
|
if lobby == nil {
|
||||||
c.IndentedJSON(http.StatusNotFound, nil)
|
c.IndentedJSON(http.StatusNotFound, nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
lobbyInfo := api.Passphrase{
|
lobbyInfo := api.LobbyInfo{
|
||||||
Value: (*string)(&lobby.Passphrase),
|
ID: &lobby.Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
c.IndentedJSON(http.StatusOK, lobbyInfo)
|
c.IndentedJSON(http.StatusOK, lobbyInfo)
|
||||||
|
@ -9,12 +9,13 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/google/uuid"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_GetLobbyFromPassphraseHandler(t *testing.T) {
|
func Test_GetLobbyFromPassphraseHandler(t *testing.T) {
|
||||||
var passphraseURLParameter string
|
var passphraseURLParameter string
|
||||||
var receivedPhrase string
|
var hostedLobbyId uuid.UUID
|
||||||
|
|
||||||
t.Run("host a lobby", func(t *testing.T) {
|
t.Run("host a lobby", func(t *testing.T) {
|
||||||
r1 := httptest.NewRecorder()
|
r1 := httptest.NewRecorder()
|
||||||
@ -29,7 +30,8 @@ func Test_GetLobbyFromPassphraseHandler(t *testing.T) {
|
|||||||
err := json.Unmarshal(r1.Body.Bytes(), &playerInfo)
|
err := json.Unmarshal(r1.Body.Bytes(), &playerInfo)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
receivedPhrase = *playerInfo.Passphrase
|
receivedPhrase := *playerInfo.Passphrase
|
||||||
|
hostedLobbyId = *playerInfo.LobbyID
|
||||||
|
|
||||||
passphrase := utils.NewPassphraseFromString(receivedPhrase)
|
passphrase := utils.NewPassphraseFromString(receivedPhrase)
|
||||||
passphraseURLParameter = passphrase.AsURLParam()
|
passphraseURLParameter = passphrase.AsURLParam()
|
||||||
@ -46,10 +48,10 @@ func Test_GetLobbyFromPassphraseHandler(t *testing.T) {
|
|||||||
|
|
||||||
engine.ServeHTTP(r2, getLobbyRequest)
|
engine.ServeHTTP(r2, getLobbyRequest)
|
||||||
|
|
||||||
passhrase := api.Passphrase{}
|
lobbyInfo := api.LobbyInfo{}
|
||||||
err := json.Unmarshal(r2.Body.Bytes(), &passhrase)
|
err := json.Unmarshal(r2.Body.Bytes(), &lobbyInfo)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, http.StatusOK, r2.Code)
|
assert.Equal(t, hostedLobbyId, *lobbyInfo.ID)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"mchess_server/api"
|
"mchess_server/api"
|
||||||
"mchess_server/utils"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"mchess_server/lobbies"
|
"mchess_server/lobbies"
|
||||||
@ -24,6 +23,7 @@ var upgrader = gorillaws.Upgrader{
|
|||||||
func RegisterWebSocketConnection(c *gin.Context) {
|
func RegisterWebSocketConnection(c *gin.Context) {
|
||||||
limiter.Take()
|
limiter.Take()
|
||||||
|
|
||||||
|
log.Println(c.Request)
|
||||||
conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
|
conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
@ -55,7 +55,7 @@ func waitForAndHandlePlayerID(ctx context.Context, conn *gorillaws.Conn) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
lobby := lobbies.GetLobbyRegistry().GetLobbyByPassphrase(utils.NewPassphraseFromString(*info.Passphrase))
|
lobby := lobbies.GetLobbyRegistry().GetLobbyByUUID(*info.LobbyID)
|
||||||
if lobby == nil {
|
if lobby == nil {
|
||||||
conn.WriteMessage(msgType, []byte("lobby not found"))
|
conn.WriteMessage(msgType, []byte("lobby not found"))
|
||||||
conn.Close()
|
conn.Close()
|
||||||
@ -68,13 +68,11 @@ func waitForAndHandlePlayerID(ctx context.Context, conn *gorillaws.Conn) {
|
|||||||
conn.Close()
|
conn.Close()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if player.Conn.HasWebsocketConnection() {
|
||||||
|
player.Conn.Close("closing existing connection")
|
||||||
|
}
|
||||||
lobby.Game.SetWebsocketConnectionFor(ctx, player, conn)
|
lobby.Game.SetWebsocketConnectionFor(ctx, player, conn)
|
||||||
|
log.Println("player after setting connection: ", player)
|
||||||
log.Println("player after setting connection: ")
|
|
||||||
log.Println("id: ", player.Uuid)
|
|
||||||
log.Println("color: ", player.GetColor())
|
|
||||||
log.Println("Connection: ", player.Conn.ID)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ConnectWsForGame(c *gin.Context) {
|
func ConnectWsForGame(c *gin.Context) {
|
||||||
|
7
api/lobby_info.go
Normal file
7
api/lobby_info.go
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import "github.com/google/uuid"
|
||||||
|
|
||||||
|
type LobbyInfo struct {
|
||||||
|
ID *uuid.UUID `json:"id,omitempty"`
|
||||||
|
}
|
@ -4,5 +4,6 @@ import "github.com/google/uuid"
|
|||||||
|
|
||||||
type PlayerInfo struct {
|
type PlayerInfo struct {
|
||||||
PlayerID *uuid.UUID `json:"playerID,omitempty"`
|
PlayerID *uuid.UUID `json:"playerID,omitempty"`
|
||||||
|
LobbyID *uuid.UUID `json:"lobbyID,omitempty"`
|
||||||
Passphrase *string `json:"passphrase,omitempty"`
|
Passphrase *string `json:"passphrase,omitempty"`
|
||||||
}
|
}
|
||||||
|
@ -105,7 +105,7 @@ func (game *Game) Handle() {
|
|||||||
log.Println("Error while reading message:", err)
|
log.Println("Error while reading message:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Println("Player ", game.currentTurnPlayer.color.String(), " moved:\n", receivedMove)
|
log.Println("Player ", game.currentTurnPlayer, " moved:\n", receivedMove)
|
||||||
|
|
||||||
game.gameState = CheckMove
|
game.gameState = CheckMove
|
||||||
case CheckMove:
|
case CheckMove:
|
||||||
@ -117,7 +117,7 @@ func (game *Game) Handle() {
|
|||||||
log.Println("Error marshalling 'colorDetermined' message for player 1", err)
|
log.Println("Error marshalling 'colorDetermined' message for player 1", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
game.currentTurnPlayer.writeMessage(string(invalidMoveMessage))
|
game.currentTurnPlayer.writeMessage(invalidMoveMessage)
|
||||||
game.gameState = PlayerToMove
|
game.gameState = PlayerToMove
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -157,14 +157,6 @@ func (game *Game) AddPlayersToGame(player *Player) {
|
|||||||
game.players = append(game.players, player)
|
game.players = append(game.players, player)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (game *Game) AreBothPlayersConnected() bool {
|
|
||||||
if len(game.GetPlayers()) < 2 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return game.players[0].hasWebsocketConnection() && game.players[1].hasWebsocketConnection()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (game *Game) killGame() {
|
func (game *Game) killGame() {
|
||||||
log.Println("Game should be killed")
|
log.Println("Game should be killed")
|
||||||
}
|
}
|
||||||
@ -181,16 +173,15 @@ func (game Game) notifyPlayersAboutGameStart() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
game.GetPlayer1().writeMessage(string(colorDeterminedPlayer1))
|
game.GetPlayer1().writeMessage(colorDeterminedPlayer1)
|
||||||
game.GetPlayer1().SendBoardState(types.Move{}, game.board.PGN(), types.White)
|
game.GetPlayer1().SendBoardState(types.Move{}, game.board.PGN(), types.White)
|
||||||
|
|
||||||
game.GetPlayer2().writeMessage(string(colorDeterminedPlayer2))
|
game.GetPlayer2().writeMessage(colorDeterminedPlayer2)
|
||||||
game.GetPlayer2().SendBoardState(types.Move{}, game.board.PGN(), types.White)
|
game.GetPlayer2().SendBoardState(types.Move{}, game.board.PGN(), types.White)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (game Game) broadcastMove(move types.Move) error {
|
func (game Game) broadcastMove(move types.Move) error {
|
||||||
log.Println("broadcast move")
|
|
||||||
err := game.GetPlayer1().SendBoardState(move, game.board.PGN(), game.currentTurnPlayer.GetColor())
|
err := game.GetPlayer1().SendBoardState(move, game.board.PGN(), game.currentTurnPlayer.GetColor())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -221,9 +212,5 @@ func (game *Game) playerDisconnected(p *Player) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (game *Game) SetWebsocketConnectionFor(ctx context.Context, p *Player, ws *gorillaws.Conn) {
|
func (game *Game) SetWebsocketConnectionFor(ctx context.Context, p *Player, ws *gorillaws.Conn) {
|
||||||
p.SetWebsocketConnectionAndSendBoardState(ctx, ws, &game.board)
|
p.SetWebsocketConnectionAndSendBoardState(ctx, ws, game.board.PGN(), game.board.colorToMove)
|
||||||
}
|
|
||||||
|
|
||||||
func (game *Game) SendBoardStateTo(p *Player) {
|
|
||||||
p.SendBoardState(game.board.getLastMove(), game.board.PGN(), game.board.colorToMove)
|
|
||||||
}
|
}
|
||||||
|
@ -36,21 +36,20 @@ func (p Player) hasWebsocketConnection() bool {
|
|||||||
|
|
||||||
func (p *Player) SetWebsocketConnection(ctx context.Context, ws *gorillaws.Conn) {
|
func (p *Player) SetWebsocketConnection(ctx context.Context, ws *gorillaws.Conn) {
|
||||||
p.Conn.SetWebsocketConnection(ws)
|
p.Conn.SetWebsocketConnection(ws)
|
||||||
p.Conn.SetForColor(p.color)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Player) SetWebsocketConnectionAndSendBoardState(
|
func (p *Player) SetWebsocketConnectionAndSendBoardState(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
ws *gorillaws.Conn,
|
ws *gorillaws.Conn,
|
||||||
board *Board,
|
boardPosition string,
|
||||||
|
turnColor types.ChessColor,
|
||||||
) {
|
) {
|
||||||
p.SetWebsocketConnection(ctx, ws)
|
p.SetWebsocketConnection(ctx, ws)
|
||||||
p.SendBoardState(board.getLastMove(), board.PGN(), board.colorToMove)
|
p.SendBoardState(types.Move{}, boardPosition, turnColor)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Player) SetColor(color types.ChessColor) {
|
func (p *Player) SetColor(color types.ChessColor) {
|
||||||
p.color = color
|
p.color = color
|
||||||
p.Conn.SetForColor(p.color)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Player) GetColor() types.ChessColor {
|
func (p *Player) GetColor() types.ChessColor {
|
||||||
@ -89,8 +88,11 @@ func (p *Player) SendBoardState(move types.Move, boardPosition string, turnColor
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
p.writeMessage(string(messageToSend))
|
err = p.writeMessage(messageToSend)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error during message writing:", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,8 +107,11 @@ func (p *Player) SendMoveAndPosition(move types.Move, boardPosition string) erro
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
p.writeMessage(string(messageToSend))
|
err = p.writeMessage(messageToSend)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error during message writing:", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,20 +125,26 @@ func (p *Player) SendGameEnded(reason GameEndedReason) error {
|
|||||||
log.Println("Error while marshalling: ", err)
|
log.Println("Error while marshalling: ", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
p.writeMessage(string(messageToSend))
|
err = p.writeMessage(messageToSend)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error during message writing:", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Player) writeMessage(msg string) {
|
func (p *Player) writeMessage(msg []byte) error {
|
||||||
p.Conn.Write(msg)
|
return p.Conn.Write(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Player) ReadMove() (types.Move, error) {
|
func (p *Player) ReadMove() (types.Move, error) {
|
||||||
receivedMessage := p.readMessage()
|
receivedMessage, err := p.readMessage()
|
||||||
|
if err != nil {
|
||||||
|
return types.Move{}, err
|
||||||
|
}
|
||||||
|
|
||||||
var msg api.WebsocketMessage
|
var msg api.WebsocketMessage
|
||||||
err := json.Unmarshal(receivedMessage, &msg)
|
err = json.Unmarshal(receivedMessage, &msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return types.Move{}, err
|
return types.Move{}, err
|
||||||
}
|
}
|
||||||
@ -145,9 +156,9 @@ func (p *Player) ReadMove() (types.Move, error) {
|
|||||||
return *msg.Move, nil
|
return *msg.Move, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Player) readMessage() []byte {
|
func (p *Player) readMessage() ([]byte, error) {
|
||||||
msg := p.Conn.Read()
|
msg, err := p.Conn.Read()
|
||||||
log.Printf("Reading message from %s: %s", p.color.String(), string(msg))
|
log.Printf("Reading message: %s from player %s", string(msg), p.Uuid.String())
|
||||||
|
|
||||||
return msg
|
return msg, err
|
||||||
}
|
}
|
||||||
|
@ -50,7 +50,7 @@ func (b *MessageBuffer) Insert(msg string) {
|
|||||||
b.cond.Broadcast()
|
b.cond.Broadcast()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *MessageBuffer) Get() string {
|
func (b *MessageBuffer) Get() (string, error) {
|
||||||
b.cond.L.Lock()
|
b.cond.L.Lock()
|
||||||
defer b.cond.L.Unlock()
|
defer b.cond.L.Unlock()
|
||||||
|
|
||||||
@ -69,7 +69,7 @@ func (b *MessageBuffer) Get() string {
|
|||||||
}
|
}
|
||||||
b.getIndex = b.incrementAndWrapIndex(b.getIndex)
|
b.getIndex = b.incrementAndWrapIndex(b.getIndex)
|
||||||
|
|
||||||
return msg.content
|
return msg.content, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b MessageBuffer) incrementAndWrapIndex(index int) int {
|
func (b MessageBuffer) incrementAndWrapIndex(index int) int {
|
||||||
|
@ -66,7 +66,8 @@ func Test_MessageBuffer_GetWaitsForFirstData(t *testing.T) {
|
|||||||
buf.Insert("delayed-message")
|
buf.Insert("delayed-message")
|
||||||
}()
|
}()
|
||||||
|
|
||||||
msg := buf.Get()
|
msg, err := buf.Get()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
endTime := time.Now()
|
endTime := time.Now()
|
||||||
|
|
||||||
@ -78,7 +79,8 @@ func Test_MessageBuffer_GetWaitsForNewData(t *testing.T) {
|
|||||||
buf := newMessageBuffer(2)
|
buf := newMessageBuffer(2)
|
||||||
|
|
||||||
buf.Insert("message-1")
|
buf.Insert("message-1")
|
||||||
msg := buf.Get()
|
msg, err := buf.Get()
|
||||||
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, "message-1", msg)
|
assert.Equal(t, "message-1", msg)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
@ -87,7 +89,8 @@ func Test_MessageBuffer_GetWaitsForNewData(t *testing.T) {
|
|||||||
buf.Insert("delayed-message")
|
buf.Insert("delayed-message")
|
||||||
}()
|
}()
|
||||||
|
|
||||||
msg = buf.Get()
|
msg, err = buf.Get()
|
||||||
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, "delayed-message", msg)
|
assert.Equal(t, "delayed-message", msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,7 +117,8 @@ func Test_MessageBuffer_IndexesAreCorrectAfterOverwritingOldData(t *testing.T) {
|
|||||||
},
|
},
|
||||||
buf.messages)
|
buf.messages)
|
||||||
|
|
||||||
msg := buf.Get()
|
msg, err := buf.Get()
|
||||||
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, "message-2", msg)
|
assert.Equal(t, "message-2", msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,11 +126,13 @@ func Test_MessageBuffer_GetWaitsForNewDataIfOldOneWasAlreadyGotten(t *testing.T)
|
|||||||
buf := newMessageBuffer(2)
|
buf := newMessageBuffer(2)
|
||||||
|
|
||||||
buf.Insert(message1)
|
buf.Insert(message1)
|
||||||
msg := buf.Get()
|
msg, err := buf.Get()
|
||||||
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, message1, msg)
|
assert.Equal(t, message1, msg)
|
||||||
|
|
||||||
buf.Insert(message2)
|
buf.Insert(message2)
|
||||||
msg = buf.Get()
|
msg, err = buf.Get()
|
||||||
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, message2, msg)
|
assert.Equal(t, message2, msg)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
@ -134,7 +140,8 @@ func Test_MessageBuffer_GetWaitsForNewDataIfOldOneWasAlreadyGotten(t *testing.T)
|
|||||||
buf.Insert(message3)
|
buf.Insert(message3)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
msg = buf.Get()
|
msg, err = buf.Get()
|
||||||
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, message3, msg)
|
assert.Equal(t, message3, msg)
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -150,8 +157,9 @@ func Test_MessageBuffer_InsertCatchesUpWithRead(t *testing.T) {
|
|||||||
buf.Insert(message6)
|
buf.Insert(message6)
|
||||||
buf.Insert(message7)
|
buf.Insert(message7)
|
||||||
|
|
||||||
msg := buf.Get()
|
msg, err := buf.Get()
|
||||||
|
|
||||||
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, message3, msg)
|
assert.Equal(t, message3, msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -164,7 +172,7 @@ func Test_MessageBuffer_FuckShitUp(t *testing.T) {
|
|||||||
var readMsg = make([]string, 0)
|
var readMsg = make([]string, 0)
|
||||||
go func() {
|
go func() {
|
||||||
for i := 0; i < size*10; i++ {
|
for i := 0; i < size*10; i++ {
|
||||||
msg := buf.Get()
|
msg, _ := buf.Get()
|
||||||
if msg == "99" {
|
if msg == "99" {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -3,38 +3,24 @@ package connection
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"log"
|
"log"
|
||||||
"mchess_server/types"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
gorillaws "github.com/gorilla/websocket"
|
gorillaws "github.com/gorilla/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Connection struct {
|
type Connection struct {
|
||||||
ID uuid.UUID
|
ws *gorillaws.Conn
|
||||||
ws *gorillaws.Conn
|
wsConnectionEstablished chan bool
|
||||||
ctx context.Context
|
wsWriteLock sync.Mutex
|
||||||
rxBuffer *MessageBuffer
|
ctx context.Context
|
||||||
txBuffer *MessageBuffer
|
buffer MessageBuffer
|
||||||
disconnectCallback func()
|
disconnectCallback func()
|
||||||
forColor types.ChessColor
|
|
||||||
|
|
||||||
/* this is a hack since a user using the same
|
|
||||||
browser might get the same object of type Connection in two instances of
|
|
||||||
the same browser. Then if Close() gets called for both of these instances
|
|
||||||
the first Close() will set ws=nil, and the seconds one will dereference this
|
|
||||||
nil pointer. The correct way to fix this, is to make sure, two instances
|
|
||||||
of the same browser do not get the same connection instance.
|
|
||||||
At the moment, the usage of localStorage 'identifies' an already connected
|
|
||||||
player and reuses the old Connection struct instance */
|
|
||||||
closingMutex sync.Mutex
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConnection(options ...func(*Connection)) *Connection {
|
func NewConnection(options ...func(*Connection)) *Connection {
|
||||||
connection := Connection{
|
connection := Connection{
|
||||||
ID: uuid.New(),
|
buffer: *newMessageBuffer(100),
|
||||||
rxBuffer: newMessageBuffer(100),
|
wsConnectionEstablished: make(chan bool),
|
||||||
txBuffer: newMessageBuffer(100),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, option := range options {
|
for _, option := range options {
|
||||||
@ -64,10 +50,6 @@ func WithDisconnectCallback(cb func()) func(*Connection) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (conn *Connection) SetForColor(color types.ChessColor) {
|
|
||||||
conn.forColor = color
|
|
||||||
}
|
|
||||||
|
|
||||||
func (conn *Connection) SetDisconnectCallback(cb func()) {
|
func (conn *Connection) SetDisconnectCallback(cb func()) {
|
||||||
conn.disconnectCallback = cb
|
conn.disconnectCallback = cb
|
||||||
}
|
}
|
||||||
@ -76,81 +58,57 @@ func (conn *Connection) HasWebsocketConnection() bool {
|
|||||||
return conn.ws != nil
|
return conn.ws != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (conn *Connection) readFromRxBuffer() {
|
|
||||||
for {
|
|
||||||
_, msg, err := conn.ws.ReadMessage()
|
|
||||||
if err != nil {
|
|
||||||
conn.logConnection("while reading from websocket: %w", "ERROR:", err.Error())
|
|
||||||
conn.Close("")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
conn.rxBuffer.Insert(string(msg))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (conn *Connection) writeTxBuffer() {
|
|
||||||
for {
|
|
||||||
msg := conn.txBuffer.Get()
|
|
||||||
|
|
||||||
if conn.ws == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err := conn.ws.WriteMessage(gorillaws.TextMessage, []byte(msg))
|
|
||||||
if err != nil {
|
|
||||||
conn.logConnection("while writing to websocket: %w", "ERROR:", err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (conn *Connection) SetWebsocketConnection(ws *gorillaws.Conn) {
|
func (conn *Connection) SetWebsocketConnection(ws *gorillaws.Conn) {
|
||||||
if ws == nil {
|
if ws == nil {
|
||||||
conn.logConnection("ERROR: setting ws = null")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
conn.ws = ws
|
conn.ws = ws
|
||||||
|
|
||||||
go conn.readFromRxBuffer()
|
select {
|
||||||
go conn.writeTxBuffer()
|
case conn.wsConnectionEstablished <- true:
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
defer conn.logConnection("websocket connection set")
|
go func() {
|
||||||
|
for {
|
||||||
|
_, msg, err := conn.ws.ReadMessage()
|
||||||
|
if err != nil {
|
||||||
|
log.Println("while reading from websocket: %w", err)
|
||||||
|
if conn.disconnectCallback != nil {
|
||||||
|
conn.disconnectCallback()
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
conn.buffer.Insert(string(msg))
|
||||||
|
}
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (conn *Connection) Write(msg string) {
|
func (conn *Connection) Write(msg []byte) error {
|
||||||
conn.logConnection("Writing message: ", string(msg))
|
conn.wsWriteLock.Lock()
|
||||||
conn.txBuffer.Insert(msg)
|
defer conn.wsWriteLock.Unlock()
|
||||||
|
|
||||||
|
if conn.ws == nil { //if ws is not yet set, we wait for it
|
||||||
|
<-conn.wsConnectionEstablished
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Writing message: %s", string(msg))
|
||||||
|
return conn.ws.WriteMessage(gorillaws.TextMessage, msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (conn *Connection) Read() []byte {
|
func (conn *Connection) Read() ([]byte, error) {
|
||||||
msg := conn.rxBuffer.Get()
|
msg, err := conn.buffer.Get()
|
||||||
|
if err != nil {
|
||||||
|
conn.ws = nil
|
||||||
|
return nil, err // TODO: Tell game-handler that connection was lost
|
||||||
|
}
|
||||||
|
|
||||||
return []byte(msg)
|
return []byte(msg), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (conn *Connection) Close(msg string) {
|
func (conn *Connection) Close(msg string) {
|
||||||
/* This is a hack, do not try this at home.
|
|
||||||
See more details at the definition of Connection */
|
|
||||||
conn.closingMutex.Lock()
|
|
||||||
defer conn.closingMutex.Unlock()
|
|
||||||
|
|
||||||
if conn == nil || conn.ws == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
conn.logConnection("closing websocket connection")
|
|
||||||
conn.ws.WriteMessage(gorillaws.TextMessage, []byte(msg))
|
conn.ws.WriteMessage(gorillaws.TextMessage, []byte(msg))
|
||||||
conn.ws.Close()
|
conn.ws.Close()
|
||||||
conn.ws = nil
|
conn.ws = nil
|
||||||
conn.txBuffer.Insert("we do this to make txBuffer.Get() return")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (con *Connection) logConnection(v ...string) {
|
|
||||||
a := ""
|
|
||||||
|
|
||||||
for _, s := range v {
|
|
||||||
a += s + ", "
|
|
||||||
}
|
|
||||||
log.Println(a, "on connection ", con.ID, ", for color ", con.forColor.String())
|
|
||||||
}
|
}
|
||||||
|
28
go.mod
28
go.mod
@ -5,27 +5,27 @@ go 1.22
|
|||||||
require (
|
require (
|
||||||
github.com/gin-gonic/gin v1.10.0
|
github.com/gin-gonic/gin v1.10.0
|
||||||
github.com/google/uuid v1.6.0
|
github.com/google/uuid v1.6.0
|
||||||
github.com/samber/lo v1.41.0
|
github.com/samber/lo v1.39.0
|
||||||
github.com/stretchr/testify v1.9.0
|
github.com/stretchr/testify v1.9.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require github.com/benbjohnson/clock v1.3.5 // indirect
|
require github.com/benbjohnson/clock v1.3.0 // indirect
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/bytedance/sonic v1.11.9 // indirect
|
github.com/bytedance/sonic v1.11.6 // indirect
|
||||||
github.com/bytedance/sonic/loader v0.1.1 // indirect
|
github.com/bytedance/sonic/loader v0.1.1 // indirect
|
||||||
github.com/cloudwego/base64x v0.1.4 // indirect
|
github.com/cloudwego/base64x v0.1.4 // indirect
|
||||||
github.com/cloudwego/iasm v0.2.0 // indirect
|
github.com/cloudwego/iasm v0.2.0 // indirect
|
||||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
github.com/gabriel-vasile/mimetype v1.4.4 // indirect
|
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
|
||||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||||
github.com/go-playground/locales v0.14.1 // indirect
|
github.com/go-playground/locales v0.14.1 // indirect
|
||||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||||
github.com/go-playground/validator/v10 v10.22.0 // indirect
|
github.com/go-playground/validator/v10 v10.20.0 // indirect
|
||||||
github.com/goccy/go-json v0.10.3 // indirect
|
github.com/goccy/go-json v0.10.2 // indirect
|
||||||
github.com/gorilla/websocket v1.5.3
|
github.com/gorilla/websocket v1.5.1
|
||||||
github.com/json-iterator/go v1.1.12 // indirect
|
github.com/json-iterator/go v1.1.12 // indirect
|
||||||
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
|
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
|
||||||
github.com/leodido/go-urn v1.4.0 // indirect
|
github.com/leodido/go-urn v1.4.0 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||||
@ -36,11 +36,11 @@ require (
|
|||||||
github.com/ugorji/go/codec v1.2.12 // indirect
|
github.com/ugorji/go/codec v1.2.12 // indirect
|
||||||
go.uber.org/ratelimit v0.3.1
|
go.uber.org/ratelimit v0.3.1
|
||||||
golang.org/x/arch v0.8.0 // indirect
|
golang.org/x/arch v0.8.0 // indirect
|
||||||
golang.org/x/crypto v0.24.0 // indirect
|
golang.org/x/crypto v0.23.0 // indirect
|
||||||
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect
|
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 // indirect
|
||||||
golang.org/x/net v0.26.0 // indirect
|
golang.org/x/net v0.25.0 // indirect
|
||||||
golang.org/x/sys v0.21.0 // indirect
|
golang.org/x/sys v0.20.0 // indirect
|
||||||
golang.org/x/text v0.16.0 // indirect
|
golang.org/x/text v0.15.0 // indirect
|
||||||
google.golang.org/protobuf v1.34.2 // indirect
|
google.golang.org/protobuf v1.34.1 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
34
go.sum
34
go.sum
@ -1,9 +1,7 @@
|
|||||||
github.com/benbjohnson/clock v1.3.5 h1:VvXlSJBzZpA/zum6Sj74hxwYI2DIxRWuNIoXAzHZz5o=
|
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
|
||||||
github.com/benbjohnson/clock v1.3.5/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
|
github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
|
||||||
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
|
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
|
||||||
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
|
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
|
||||||
github.com/bytedance/sonic v1.11.9 h1:LFHENlIY/SLzDWverzdOvgMztTxcfcF+cqNsz9pK5zg=
|
|
||||||
github.com/bytedance/sonic v1.11.9/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
|
|
||||||
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
|
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
|
||||||
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
|
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
|
||||||
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
|
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
|
||||||
@ -15,8 +13,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
|
|||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
|
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
|
||||||
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
|
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
|
||||||
github.com/gabriel-vasile/mimetype v1.4.4 h1:QjV6pZ7/XZ7ryI2KuyeEDE8wnh7fHP9YnQy+R0LnH8I=
|
|
||||||
github.com/gabriel-vasile/mimetype v1.4.4/go.mod h1:JwLei5XPtWdGiMFB5Pjle1oEeoSeEuJfJE+TtfvdB/s=
|
|
||||||
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
|
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
|
||||||
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
|
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
|
||||||
github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
|
github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
|
||||||
@ -29,10 +25,8 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
|
|||||||
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
||||||
github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8=
|
github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8=
|
||||||
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
|
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
|
||||||
github.com/go-playground/validator/v10 v10.22.0 h1:k6HsTZ0sTnROkhS//R0O+55JgM8C4Bx7ia+JlgcnOao=
|
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
|
||||||
github.com/go-playground/validator/v10 v10.22.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
|
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
||||||
github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA=
|
|
||||||
github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
|
||||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||||
@ -40,15 +34,11 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
|||||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
|
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
|
||||||
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
|
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
|
||||||
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
|
||||||
github.com/gorilla/websocket v1.5.3/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 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||||
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||||
github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
|
github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
|
||||||
github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
|
github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
|
||||||
github.com/klauspost/cpuid/v2 v2.2.8 h1:+StwCXwm9PdpiEkPyzBXIy+M9KUb4ODm0Zarf1kS5BM=
|
|
||||||
github.com/klauspost/cpuid/v2 v2.2.8/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
|
|
||||||
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
|
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
|
||||||
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
||||||
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
|
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
|
||||||
@ -65,8 +55,6 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
|
|||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/samber/lo v1.39.0 h1:4gTz1wUhNYLhFSKl6O+8peW0v2F4BCY034GRpU9WnuA=
|
github.com/samber/lo v1.39.0 h1:4gTz1wUhNYLhFSKl6O+8peW0v2F4BCY034GRpU9WnuA=
|
||||||
github.com/samber/lo v1.39.0/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
|
github.com/samber/lo v1.39.0/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
|
||||||
github.com/samber/lo v1.41.0 h1:vZEqVQNmX/ykONfla8tiW6cdoLZxb3+LCZ63B8z/2eE=
|
|
||||||
github.com/samber/lo v1.41.0/go.mod h1:w7R6fO7h2lrnx/s0bWcZ55vXJI89p5UPM6+kyDL373E=
|
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||||
@ -83,8 +71,6 @@ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS
|
|||||||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||||
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
|
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
|
||||||
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
|
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
|
||||||
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
|
|
||||||
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
|
||||||
go.uber.org/ratelimit v0.3.1 h1:K4qVE+byfv/B3tC+4nYWP7v/6SimcO7HzHekoMNBma0=
|
go.uber.org/ratelimit v0.3.1 h1:K4qVE+byfv/B3tC+4nYWP7v/6SimcO7HzHekoMNBma0=
|
||||||
go.uber.org/ratelimit v0.3.1/go.mod h1:6euWsTB6U/Nb3X++xEUXA8ciPJvr19Q/0h1+oDcJhRk=
|
go.uber.org/ratelimit v0.3.1/go.mod h1:6euWsTB6U/Nb3X++xEUXA8ciPJvr19Q/0h1+oDcJhRk=
|
||||||
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
|
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
|
||||||
@ -92,30 +78,18 @@ golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
|
|||||||
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
|
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
|
||||||
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
|
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
|
||||||
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
|
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
|
||||||
golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
|
|
||||||
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
|
|
||||||
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 h1:vr/HnozRka3pE4EsMEg1lgkXJkTFJCVUX+S/ZT6wYzM=
|
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 h1:vr/HnozRka3pE4EsMEg1lgkXJkTFJCVUX+S/ZT6wYzM=
|
||||||
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc=
|
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc=
|
||||||
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 h1:yixxcjnhBmY0nkL253HFVIm0JsFHwrHdT3Yh6szTnfY=
|
|
||||||
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8/go.mod h1:jj3sYF3dwk5D+ghuXyeI3r5MFf+NT2An6/9dOA95KSI=
|
|
||||||
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
|
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
|
||||||
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
|
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
|
||||||
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
|
|
||||||
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
|
|
||||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
|
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
|
||||||
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
|
|
||||||
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
|
||||||
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
|
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
|
||||||
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||||
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
|
|
||||||
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
|
|
||||||
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
|
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
|
||||||
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
|
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
|
||||||
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
|
|
||||||
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
|
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
@ -8,15 +8,17 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Lobby struct {
|
type Lobby struct {
|
||||||
Uuid uuid.UUID
|
Uuid uuid.UUID
|
||||||
Game *chess.Game
|
Game *chess.Game
|
||||||
Passphrase utils.Passphrase
|
PlayerJoined chan bool
|
||||||
|
Passphrase utils.Passphrase
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewEmptyLobbyWithUUID(uuid uuid.UUID) *Lobby {
|
func NewEmptyLobbyWithUUID(uuid uuid.UUID) *Lobby {
|
||||||
return &Lobby{
|
return &Lobby{
|
||||||
Uuid: uuid,
|
Uuid: uuid,
|
||||||
Game: chess.NewGame(),
|
Game: chess.NewGame(),
|
||||||
|
PlayerJoined: make(chan bool),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,16 +31,12 @@ func newEmptyLobbyWithPassphrase() *Lobby {
|
|||||||
|
|
||||||
func (l *Lobby) AddPlayerAndStartGameIfFull(player *chess.Player) {
|
func (l *Lobby) AddPlayerAndStartGameIfFull(player *chess.Player) {
|
||||||
l.Game.AddPlayersToGame(player)
|
l.Game.AddPlayersToGame(player)
|
||||||
if l.ContainsTwoPlayers() {
|
if l.IsFull() {
|
||||||
l.Game.StartHandling()
|
l.Game.StartHandling()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Lobby) AreBothPlayersConnected() bool {
|
func (w *Lobby) IsFull() bool {
|
||||||
return w.Game.AreBothPlayersConnected()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w *Lobby) ContainsTwoPlayers() bool {
|
|
||||||
return len(w.Game.GetPlayers()) == 2
|
return len(w.Game.GetPlayers()) == 2
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type LobbyRegistry struct {
|
type LobbyRegistry struct {
|
||||||
lobbies map[utils.Passphrase]*Lobby
|
lobbies map[uuid.UUID]*Lobby
|
||||||
}
|
}
|
||||||
|
|
||||||
var instance *LobbyRegistry
|
var instance *LobbyRegistry
|
||||||
@ -21,7 +21,7 @@ func GetLobbyRegistry() *LobbyRegistry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func newLobbyRegistry() *LobbyRegistry {
|
func newLobbyRegistry() *LobbyRegistry {
|
||||||
return &LobbyRegistry{lobbies: make(map[utils.Passphrase]*Lobby)}
|
return &LobbyRegistry{lobbies: make(map[uuid.UUID]*Lobby)}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *LobbyRegistry) CreateNewPrivateLobby() *Lobby {
|
func (r *LobbyRegistry) CreateNewPrivateLobby() *Lobby {
|
||||||
@ -32,7 +32,7 @@ func (r *LobbyRegistry) CreateNewPrivateLobby() *Lobby {
|
|||||||
|
|
||||||
func (r *LobbyRegistry) GetLobbyForPlayer() *Lobby {
|
func (r *LobbyRegistry) GetLobbyForPlayer() *Lobby {
|
||||||
for _, lobby := range r.lobbies {
|
for _, lobby := range r.lobbies {
|
||||||
if !lobby.ContainsTwoPlayers() {
|
if !lobby.IsFull() {
|
||||||
return lobby
|
return lobby
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -42,6 +42,10 @@ func (r *LobbyRegistry) GetLobbyForPlayer() *Lobby {
|
|||||||
return newLobby
|
return newLobby
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *LobbyRegistry) GetLobbyByUUID(uuid uuid.UUID) *Lobby {
|
||||||
|
return r.lobbies[uuid]
|
||||||
|
}
|
||||||
|
|
||||||
func (r *LobbyRegistry) GetLobbyByPassphrase(p utils.Passphrase) *Lobby {
|
func (r *LobbyRegistry) GetLobbyByPassphrase(p utils.Passphrase) *Lobby {
|
||||||
for _, lobby := range r.lobbies {
|
for _, lobby := range r.lobbies {
|
||||||
if lobby.Passphrase == p {
|
if lobby.Passphrase == p {
|
||||||
@ -52,6 +56,6 @@ func (r *LobbyRegistry) GetLobbyByPassphrase(p utils.Passphrase) *Lobby {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *LobbyRegistry) addNewLobby(lobby *Lobby) uuid.UUID {
|
func (r *LobbyRegistry) addNewLobby(lobby *Lobby) uuid.UUID {
|
||||||
r.lobbies[lobby.Passphrase] = lobby
|
r.lobbies[lobby.Uuid] = lobby
|
||||||
return lobby.Uuid
|
return lobby.Uuid
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,8 @@ package lobbies
|
|||||||
import (
|
import (
|
||||||
"mchess_server/chess"
|
"mchess_server/chess"
|
||||||
"mchess_server/utils"
|
"mchess_server/utils"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Usher struct {
|
type Usher struct {
|
||||||
@ -22,15 +24,25 @@ func GetUsher() *Usher {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (u *Usher) WelcomeNewPlayer(player *chess.Player) *Lobby {
|
func (u *Usher) WelcomeNewPlayer(player *chess.Player) *Lobby {
|
||||||
return GetLobbyRegistry().GetLobbyForPlayer()
|
lobby := GetLobbyRegistry().GetLobbyForPlayer()
|
||||||
|
return lobby
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *Usher) CreateNewPrivateLobby(player *chess.Player) *Lobby {
|
func (u *Usher) CreateNewPrivateLobby(player *chess.Player) *Lobby {
|
||||||
return GetLobbyRegistry().CreateNewPrivateLobby()
|
lobby := GetLobbyRegistry().CreateNewPrivateLobby()
|
||||||
|
return lobby
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *Usher) FindExistingPrivateLobby(p utils.Passphrase) *Lobby {
|
func (u *Usher) FindExistingPrivateLobby(p utils.Passphrase) *Lobby {
|
||||||
return GetLobbyRegistry().GetLobbyByPassphrase(p)
|
lobby := GetLobbyRegistry().GetLobbyByPassphrase(p)
|
||||||
|
if lobby == nil || lobby.IsFull() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return lobby
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*Usher) GetLobbyByID(id uuid.UUID) *Lobby {
|
||||||
|
return GetLobbyRegistry().GetLobbyByUUID(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *Usher) AddPlayerToLobbyAndStartGameIfFull(player *chess.Player, lobby *Lobby) {
|
func (u *Usher) AddPlayerToLobbyAndStartGameIfFull(player *chess.Player, lobby *Lobby) {
|
||||||
|
1
main.go
1
main.go
@ -13,7 +13,6 @@ var cert_file = cert_path + "fullchain.pem"
|
|||||||
var key_file = cert_path + "privkey.pem"
|
var key_file = cert_path + "privkey.pem"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
log.SetFlags(log.LstdFlags | log.Lmicroseconds)
|
|
||||||
var debugMode bool
|
var debugMode bool
|
||||||
|
|
||||||
debugModeLong := flag.Bool("debug", false, "activates debug mode")
|
debugModeLong := flag.Bool("debug", false, "activates debug mode")
|
||||||
|
Loading…
Reference in New Issue
Block a user