Make some minor changes for private games.
This commit is contained in:
parent
1ea2c597be
commit
4b04b3532c
@ -5,4 +5,5 @@ import "github.com/google/uuid"
|
||||
type PlayerInfo struct {
|
||||
PlayerID uuid.UUID `json:"playerID"`
|
||||
LobbyID uuid.UUID `json:"lobbyID"`
|
||||
Passphrase *string `json:"passphrase,omitempty"`
|
||||
}
|
||||
|
@ -4,9 +4,9 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log"
|
||||
"mchess_server/api"
|
||||
"mchess_server/types"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
@ -11,7 +11,7 @@ type Lobby struct {
|
||||
Uuid uuid.UUID
|
||||
Game *chess.Game
|
||||
PlayerJoined chan bool
|
||||
Passphrase string
|
||||
Passphrase utils.Passphrase
|
||||
}
|
||||
|
||||
func NewEmptyLobbyWithUUID(uuid uuid.UUID) *Lobby {
|
||||
@ -24,7 +24,7 @@ func NewEmptyLobbyWithUUID(uuid uuid.UUID) *Lobby {
|
||||
|
||||
func NewEmptyLobbyWithPassphrase()*Lobby {
|
||||
lobby := NewEmptyLobbyWithUUID(uuid.New())
|
||||
lobby.Passphrase = string(utils.NewPassphrase())
|
||||
lobby.Passphrase = utils.NewPassphrase()
|
||||
|
||||
return lobby
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
package lobby_registry
|
||||
|
||||
import (
|
||||
"mchess_server/utils"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
@ -38,6 +40,15 @@ func (r *LobbyRegistry) GetLobbyByUUID(uuid uuid.UUID) *Lobby {
|
||||
return r.lobbies[uuid]
|
||||
}
|
||||
|
||||
func (r *LobbyRegistry) GetLobbyByPassphrase(p utils.Passphrase) *Lobby {
|
||||
for _,lobby := range r.lobbies {
|
||||
if lobby.Passphrase == p {
|
||||
return lobby
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *LobbyRegistry) addNewLobby(lobby *Lobby) uuid.UUID {
|
||||
r.lobbies[lobby.Uuid] = lobby
|
||||
return lobby.Uuid
|
||||
|
42
main.go
42
main.go
@ -4,11 +4,12 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"mchess_server/api"
|
||||
"mchess_server/chess"
|
||||
lobbies "mchess_server/lobby_registry"
|
||||
"mchess_server/usher"
|
||||
"log"
|
||||
"mchess_server/utils"
|
||||
"net/http"
|
||||
"os"
|
||||
"sync"
|
||||
@ -69,17 +70,50 @@ func registerForRandomGame(c *gin.Context) {
|
||||
|
||||
func hostPrivateGame(c *gin.Context) {
|
||||
player := chess.NewPlayer(uuid.New())
|
||||
usher.GetUsher()
|
||||
u := usher.GetUsher()
|
||||
|
||||
mut.Lock()
|
||||
lobby := usher.NewUsher().FindNewPrivateLobby(player)
|
||||
lobby := u.FindNewPrivateLobby(player)
|
||||
u.AddPlayerToLobbyAndStartGameIfFull(player, lobby)
|
||||
mut.Unlock()
|
||||
|
||||
c.IndentedJSON(http.StatusOK, lobby.Passphrase)
|
||||
passphrase := lobby.Passphrase.String()
|
||||
info := api.PlayerInfo{
|
||||
PlayerID: player.Uuid,
|
||||
LobbyID: lobby.Uuid,
|
||||
Passphrase: &passphrase,
|
||||
}
|
||||
|
||||
c.Header("Access-Control-Allow-Origin", "*")
|
||||
c.IndentedJSON(http.StatusOK, info)
|
||||
}
|
||||
|
||||
func joinPrivateGame(c *gin.Context) {
|
||||
req := api.PlayerInfo{}
|
||||
err := c.ShouldBindJSON(req)
|
||||
if err!= nil || req.Passphrase == nil || *req.Passphrase == "" {
|
||||
c.IndentedJSON(http.StatusNotFound, req)
|
||||
}
|
||||
|
||||
player := chess.NewPlayer(uuid.New())
|
||||
u := usher.GetUsher()
|
||||
|
||||
mut.Lock()
|
||||
defer mut.Unlock()
|
||||
lobby := u.FindExistingPrivateLobby(utils.Passphrase(*req.Passphrase))
|
||||
if lobby != nil {
|
||||
u.AddPlayerToLobbyAndStartGameIfFull(player, lobby)
|
||||
} else {
|
||||
c.IndentedJSON(http.StatusNotFound, req)
|
||||
return
|
||||
}
|
||||
|
||||
info := api.PlayerInfo{
|
||||
PlayerID: player.Uuid,
|
||||
LobbyID: lobby.Uuid,
|
||||
Passphrase: req.Passphrase,
|
||||
}
|
||||
c.IndentedJSON(http.StatusOK, info)
|
||||
}
|
||||
|
||||
func registerWebSocketConnection(c *gin.Context) {
|
||||
|
@ -3,6 +3,7 @@ package usher
|
||||
import (
|
||||
"mchess_server/chess"
|
||||
lobbies "mchess_server/lobby_registry"
|
||||
"mchess_server/utils"
|
||||
)
|
||||
|
||||
type Usher struct {
|
||||
@ -10,13 +11,13 @@ type Usher struct {
|
||||
|
||||
var instance *Usher
|
||||
|
||||
func NewUsher() *Usher {
|
||||
func newUsher() *Usher {
|
||||
return &Usher{}
|
||||
}
|
||||
|
||||
func GetUsher() *Usher {
|
||||
if instance == nil {
|
||||
instance = NewUsher()
|
||||
instance = newUsher()
|
||||
}
|
||||
return instance
|
||||
}
|
||||
@ -31,6 +32,10 @@ func (u*Usher) FindNewPrivateLobby(player *chess.Player) *lobbies.Lobby {
|
||||
return lobby
|
||||
}
|
||||
|
||||
func (u *Usher) FindExistingPrivateLobby(p utils.Passphrase) *lobbies.Lobby {
|
||||
return lobbies.GetLobbyRegistry().GetLobbyByPassphrase(p)
|
||||
}
|
||||
|
||||
func (u *Usher) AddPlayerToLobbyAndStartGameIfFull(player *chess.Player, lobby *lobbies.Lobby) {
|
||||
lobby.AddPlayerAndStartGameIfFull(player)
|
||||
}
|
||||
|
@ -12,8 +12,8 @@ func NewPassphrase() Passphrase {
|
||||
var phrase string
|
||||
var retries int
|
||||
var word string
|
||||
var words int = 3
|
||||
|
||||
var words int = 2
|
||||
//TODO make sure passphrases are unique
|
||||
for words > 0 {
|
||||
retries = 20
|
||||
for {
|
||||
@ -53,7 +53,7 @@ func isAccecpable(s string) bool {
|
||||
func isProfanity(s string) bool {
|
||||
contains := []string{
|
||||
"nigg",
|
||||
"fagg",
|
||||
"fag",
|
||||
}
|
||||
startsWith := []string{
|
||||
"spic",
|
||||
|
Loading…
Reference in New Issue
Block a user