2024-05-07 21:31:25 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"mchess_server/api"
|
2024-05-09 20:29:48 +00:00
|
|
|
"mchess_server/chess"
|
2024-05-12 13:36:30 +00:00
|
|
|
"mchess_server/lobbies"
|
2024-05-07 21:31:25 +00:00
|
|
|
"mchess_server/utils"
|
|
|
|
"net/http"
|
2024-05-09 20:29:48 +00:00
|
|
|
"sync"
|
2024-05-07 21:31:25 +00:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2024-05-09 20:29:48 +00:00
|
|
|
"github.com/google/uuid"
|
2024-05-12 13:36:30 +00:00
|
|
|
"go.uber.org/ratelimit"
|
2024-05-07 21:31:25 +00:00
|
|
|
)
|
|
|
|
|
2024-05-09 20:29:48 +00:00
|
|
|
var mut sync.Mutex
|
2024-05-12 13:36:30 +00:00
|
|
|
var limiter = ratelimit.New(10)
|
|
|
|
|
|
|
|
func HostGameHandler(c *gin.Context) {
|
|
|
|
limiter.Take()
|
2024-05-07 21:31:25 +00:00
|
|
|
|
2024-05-09 20:29:48 +00:00
|
|
|
player := chess.NewPlayer(uuid.New())
|
2024-05-12 13:36:30 +00:00
|
|
|
u := lobbies.GetUsher()
|
2024-05-09 20:29:48 +00:00
|
|
|
|
|
|
|
mut.Lock()
|
|
|
|
defer mut.Unlock()
|
|
|
|
lobby := u.CreateNewPrivateLobby(player)
|
|
|
|
u.AddPlayerToLobbyAndStartGameIfFull(player, lobby)
|
|
|
|
|
|
|
|
passphrase := lobby.Passphrase.String()
|
|
|
|
info := api.PlayerInfo{
|
|
|
|
PlayerID: &player.Uuid,
|
|
|
|
Passphrase: &passphrase,
|
|
|
|
}
|
|
|
|
c.Header("Access-Control-Allow-Origin", "*")
|
|
|
|
c.IndentedJSON(http.StatusOK, info)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetLobbyForPassphraseHandler(c *gin.Context) {
|
2024-05-12 13:36:30 +00:00
|
|
|
limiter.Take()
|
|
|
|
|
2024-05-09 20:29:48 +00:00
|
|
|
reqPassphrase := c.Param("phrase")
|
|
|
|
if reqPassphrase == "" {
|
|
|
|
c.IndentedJSON(http.StatusBadRequest, reqPassphrase)
|
2024-05-07 21:31:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-09 20:29:48 +00:00
|
|
|
passPhraseWithSpaces := utils.ConvertToPassphraseWithSpaces(reqPassphrase)
|
|
|
|
lobby := lobbies.GetLobbyRegistry().GetLobbyByPassphrase(passPhraseWithSpaces)
|
2024-05-07 21:31:25 +00:00
|
|
|
|
|
|
|
if lobby == nil {
|
|
|
|
c.IndentedJSON(http.StatusNotFound, reqPassphrase)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-20 13:36:13 +00:00
|
|
|
passphrase := api.Passphrase{
|
|
|
|
Value: (*string)(&lobby.Passphrase),
|
2024-05-07 21:31:25 +00:00
|
|
|
}
|
|
|
|
|
2024-05-15 17:45:35 +00:00
|
|
|
c.Header("Access-Control-Allow-Origin", "*")
|
2024-05-20 13:36:13 +00:00
|
|
|
c.IndentedJSON(http.StatusOK, passphrase)
|
2024-05-07 21:31:25 +00:00
|
|
|
}
|
2024-05-09 20:29:48 +00:00
|
|
|
|
2024-05-12 13:36:30 +00:00
|
|
|
// TODO: this will be replaced by the JoinGameHandler()
|
2024-05-09 20:29:48 +00:00
|
|
|
func JoinPrivateGame(c *gin.Context) {
|
2024-05-12 13:36:30 +00:00
|
|
|
limiter.Take()
|
|
|
|
|
2024-05-09 20:29:48 +00:00
|
|
|
req := api.PlayerInfo{}
|
|
|
|
err := c.ShouldBindJSON(&req)
|
|
|
|
if err != nil || req.Passphrase == nil || *req.Passphrase == "" {
|
|
|
|
c.IndentedJSON(http.StatusNotFound, req)
|
2024-05-15 12:28:14 +00:00
|
|
|
return
|
2024-05-09 20:29:48 +00:00
|
|
|
}
|
|
|
|
|
2024-05-12 13:36:30 +00:00
|
|
|
u := lobbies.GetUsher()
|
2024-05-09 20:29:48 +00:00
|
|
|
|
|
|
|
if req.Passphrase != nil &&
|
|
|
|
*req.Passphrase != "" &&
|
2024-05-20 13:36:13 +00:00
|
|
|
req.PlayerID != nil { //is reconnect
|
2024-05-09 20:29:48 +00:00
|
|
|
lobby := u.FindExistingPrivateLobby(utils.Passphrase(*req.Passphrase))
|
2024-05-15 12:28:14 +00:00
|
|
|
var found bool
|
|
|
|
if lobby != nil {
|
|
|
|
_, found = lobby.GetPlayerByUUID(*req.PlayerID)
|
|
|
|
}
|
2024-05-09 20:29:48 +00:00
|
|
|
if found {
|
2024-05-15 17:45:35 +00:00
|
|
|
c.Header("Access-Control-Allow-Origin", "*")
|
2024-05-09 20:29:48 +00:00
|
|
|
c.IndentedJSON(
|
|
|
|
http.StatusOK,
|
|
|
|
api.PlayerInfo{
|
|
|
|
PlayerID: req.PlayerID,
|
|
|
|
Passphrase: req.Passphrase,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
c.IndentedJSON(http.StatusNotFound, req)
|
2024-05-15 12:28:14 +00:00
|
|
|
return
|
2024-05-09 20:29:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
player := chess.NewPlayer(uuid.New())
|
|
|
|
|
|
|
|
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,
|
|
|
|
Passphrase: req.Passphrase,
|
|
|
|
}
|
|
|
|
c.Header("Access-Control-Allow-Origin", "*")
|
|
|
|
c.IndentedJSON(http.StatusOK, info)
|
|
|
|
}
|
2024-05-11 20:57:47 +00:00
|
|
|
|
2024-05-12 13:36:30 +00:00
|
|
|
func JoinGameHandler(c *gin.Context) {
|
|
|
|
limiter.Take()
|
|
|
|
|
2024-05-20 13:36:13 +00:00
|
|
|
passphrase := api.Passphrase{}
|
|
|
|
err := c.ShouldBindJSON(&passphrase)
|
2024-05-12 13:36:30 +00:00
|
|
|
if err != nil {
|
|
|
|
c.IndentedJSON(http.StatusBadRequest, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
u := lobbies.GetUsher()
|
2024-05-20 13:36:13 +00:00
|
|
|
lobby := u.FindExistingPrivateLobby(utils.Passphrase(*passphrase.Value))
|
2024-05-12 13:36:30 +00:00
|
|
|
if lobby == nil {
|
|
|
|
c.IndentedJSON(http.StatusNotFound, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-20 13:36:13 +00:00
|
|
|
lobbyInfo := api.Passphrase{
|
|
|
|
Value: (*string)(&lobby.Passphrase),
|
2024-05-12 13:36:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.IndentedJSON(http.StatusOK, lobbyInfo)
|
2024-05-11 20:57:47 +00:00
|
|
|
}
|