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 {
|
type PlayerInfo struct {
|
||||||
PlayerID uuid.UUID `json:"playerID"`
|
PlayerID uuid.UUID `json:"playerID"`
|
||||||
LobbyID uuid.UUID `json:"lobbyID"`
|
LobbyID uuid.UUID `json:"lobbyID"`
|
||||||
|
Passphrase *string `json:"passphrase,omitempty"`
|
||||||
}
|
}
|
||||||
|
@ -4,9 +4,9 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"log"
|
||||||
"mchess_server/api"
|
"mchess_server/api"
|
||||||
"mchess_server/types"
|
"mchess_server/types"
|
||||||
"log"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
@ -11,7 +11,7 @@ type Lobby struct {
|
|||||||
Uuid uuid.UUID
|
Uuid uuid.UUID
|
||||||
Game *chess.Game
|
Game *chess.Game
|
||||||
PlayerJoined chan bool
|
PlayerJoined chan bool
|
||||||
Passphrase string
|
Passphrase utils.Passphrase
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewEmptyLobbyWithUUID(uuid uuid.UUID) *Lobby {
|
func NewEmptyLobbyWithUUID(uuid uuid.UUID) *Lobby {
|
||||||
@ -24,7 +24,7 @@ func NewEmptyLobbyWithUUID(uuid uuid.UUID) *Lobby {
|
|||||||
|
|
||||||
func NewEmptyLobbyWithPassphrase()*Lobby {
|
func NewEmptyLobbyWithPassphrase()*Lobby {
|
||||||
lobby := NewEmptyLobbyWithUUID(uuid.New())
|
lobby := NewEmptyLobbyWithUUID(uuid.New())
|
||||||
lobby.Passphrase = string(utils.NewPassphrase())
|
lobby.Passphrase = utils.NewPassphrase()
|
||||||
|
|
||||||
return lobby
|
return lobby
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package lobby_registry
|
package lobby_registry
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"mchess_server/utils"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -38,6 +40,15 @@ func (r *LobbyRegistry) GetLobbyByUUID(uuid uuid.UUID) *Lobby {
|
|||||||
return r.lobbies[uuid]
|
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 {
|
func (r *LobbyRegistry) addNewLobby(lobby *Lobby) uuid.UUID {
|
||||||
r.lobbies[lobby.Uuid] = lobby
|
r.lobbies[lobby.Uuid] = lobby
|
||||||
return lobby.Uuid
|
return lobby.Uuid
|
||||||
|
42
main.go
42
main.go
@ -4,11 +4,12 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"mchess_server/api"
|
"mchess_server/api"
|
||||||
"mchess_server/chess"
|
"mchess_server/chess"
|
||||||
lobbies "mchess_server/lobby_registry"
|
lobbies "mchess_server/lobby_registry"
|
||||||
"mchess_server/usher"
|
"mchess_server/usher"
|
||||||
"log"
|
"mchess_server/utils"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
@ -69,17 +70,50 @@ func registerForRandomGame(c *gin.Context) {
|
|||||||
|
|
||||||
func hostPrivateGame(c *gin.Context) {
|
func hostPrivateGame(c *gin.Context) {
|
||||||
player := chess.NewPlayer(uuid.New())
|
player := chess.NewPlayer(uuid.New())
|
||||||
usher.GetUsher()
|
u := usher.GetUsher()
|
||||||
|
|
||||||
mut.Lock()
|
mut.Lock()
|
||||||
lobby := usher.NewUsher().FindNewPrivateLobby(player)
|
lobby := u.FindNewPrivateLobby(player)
|
||||||
|
u.AddPlayerToLobbyAndStartGameIfFull(player, lobby)
|
||||||
mut.Unlock()
|
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) {
|
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) {
|
func registerWebSocketConnection(c *gin.Context) {
|
||||||
|
@ -3,6 +3,7 @@ package usher
|
|||||||
import (
|
import (
|
||||||
"mchess_server/chess"
|
"mchess_server/chess"
|
||||||
lobbies "mchess_server/lobby_registry"
|
lobbies "mchess_server/lobby_registry"
|
||||||
|
"mchess_server/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Usher struct {
|
type Usher struct {
|
||||||
@ -10,13 +11,13 @@ type Usher struct {
|
|||||||
|
|
||||||
var instance *Usher
|
var instance *Usher
|
||||||
|
|
||||||
func NewUsher() *Usher {
|
func newUsher() *Usher {
|
||||||
return &Usher{}
|
return &Usher{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetUsher() *Usher {
|
func GetUsher() *Usher {
|
||||||
if instance == nil {
|
if instance == nil {
|
||||||
instance = NewUsher()
|
instance = newUsher()
|
||||||
}
|
}
|
||||||
return instance
|
return instance
|
||||||
}
|
}
|
||||||
@ -31,6 +32,10 @@ func (u*Usher) FindNewPrivateLobby(player *chess.Player) *lobbies.Lobby {
|
|||||||
return 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) {
|
func (u *Usher) AddPlayerToLobbyAndStartGameIfFull(player *chess.Player, lobby *lobbies.Lobby) {
|
||||||
lobby.AddPlayerAndStartGameIfFull(player)
|
lobby.AddPlayerAndStartGameIfFull(player)
|
||||||
}
|
}
|
||||||
|
@ -12,8 +12,8 @@ func NewPassphrase() Passphrase {
|
|||||||
var phrase string
|
var phrase string
|
||||||
var retries int
|
var retries int
|
||||||
var word string
|
var word string
|
||||||
var words int = 3
|
var words int = 2
|
||||||
|
//TODO make sure passphrases are unique
|
||||||
for words > 0 {
|
for words > 0 {
|
||||||
retries = 20
|
retries = 20
|
||||||
for {
|
for {
|
||||||
@ -53,7 +53,7 @@ func isAccecpable(s string) bool {
|
|||||||
func isProfanity(s string) bool {
|
func isProfanity(s string) bool {
|
||||||
contains := []string{
|
contains := []string{
|
||||||
"nigg",
|
"nigg",
|
||||||
"fagg",
|
"fag",
|
||||||
}
|
}
|
||||||
startsWith := []string{
|
startsWith := []string{
|
||||||
"spic",
|
"spic",
|
||||||
|
Loading…
Reference in New Issue
Block a user