added GetLobbyByID and GetPlayerByID TODO: how to identify players?

This commit is contained in:
2024-11-13 12:00:02 +01:00
parent 9a17f33f90
commit 58fb521585

View File

@@ -2,6 +2,7 @@ package main
import ( import (
"net/http" "net/http"
"strconv"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@@ -15,7 +16,7 @@ func NewPlayer(name string) Player {
} }
type Settings struct { type Settings struct {
Set0 string `json:"set0" :"set_0"` Set0 string `json:"set0"`
} }
func DefSettings() *Settings { func DefSettings() *Settings {
@@ -36,10 +37,23 @@ func GetLobbies(c *gin.Context) {
c.IndentedJSON(http.StatusOK, lobbies) c.IndentedJSON(http.StatusOK, lobbies)
} }
func GetLobbyByID(c *gin.Context) {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
panic(err)
}
c.IndentedJSON(http.StatusOK, lobbies[id])
}
func GetPlayers(c *gin.Context) { func GetPlayers(c *gin.Context) {
c.IndentedJSON(http.StatusOK, players) c.IndentedJSON(http.StatusOK, players)
} }
func GetPlayerByID(c *gin.Context) {
id := c.Param("id")
c.IndentedJSON(http.StatusOK, players[id])
}
var players = make(map[string]*Player) //nolint:gochecknoglobals var players = make(map[string]*Player) //nolint:gochecknoglobals
var lobbies = make(map[int]*Lobby) //nolint:gochecknoglobals var lobbies = make(map[int]*Lobby) //nolint:gochecknoglobals
@@ -51,12 +65,16 @@ func main() {
"message": "pong", "message": "pong",
}) })
}) })
router.GET("/GET/lobby", GetLobbies) router.GET("/lobby", GetLobbies)
router.GET("/GET/player", GetPlayers) router.GET("/lobby/:id", GetLobbyByID)
router.GET("/player", GetPlayers)
router.GET("/player/:id", GetPlayerByID)
var tmpPlayer = [2]Player{NewPlayer("Abc"), NewPlayer("Xyz")} var tmpPlayer = [2]Player{NewPlayer("Abc"), NewPlayer("Xyz")}
players["cookie0"] = &tmpPlayer[0] players["cookie0"] = &tmpPlayer[0]
players["cookie1"] = &tmpPlayer[1] players["cookie1"] = &tmpPlayer[1]
lobbies[lobbyc] = NewLobby(lobbyc, &tmpPlayer, DefSettings()) lobbies[lobbyc] = NewLobby(lobbyc, &tmpPlayer, DefSettings())
router.Run("localhost:8080") err := router.Run("localhost:8080")
if (err) != nil {
panic(err)
}
} }