From 58fb521585f0973fe637010b07143957e2a68945 Mon Sep 17 00:00:00 2001 From: mpuchstein Date: Wed, 13 Nov 2024 12:00:02 +0100 Subject: [PATCH] added GetLobbyByID and GetPlayerByID TODO: how to identify players? --- cmd/ttbsd/main.go | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/cmd/ttbsd/main.go b/cmd/ttbsd/main.go index 3ca687a..a651986 100644 --- a/cmd/ttbsd/main.go +++ b/cmd/ttbsd/main.go @@ -2,6 +2,7 @@ package main import ( "net/http" + "strconv" "github.com/gin-gonic/gin" ) @@ -15,7 +16,7 @@ func NewPlayer(name string) Player { } type Settings struct { - Set0 string `json:"set0" :"set_0"` + Set0 string `json:"set0"` } func DefSettings() *Settings { @@ -36,10 +37,23 @@ func GetLobbies(c *gin.Context) { 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) { 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 lobbies = make(map[int]*Lobby) //nolint:gochecknoglobals @@ -51,12 +65,16 @@ func main() { "message": "pong", }) }) - router.GET("/GET/lobby", GetLobbies) - router.GET("/GET/player", GetPlayers) + router.GET("/lobby", GetLobbies) + router.GET("/lobby/:id", GetLobbyByID) + router.GET("/player", GetPlayers) + router.GET("/player/:id", GetPlayerByID) var tmpPlayer = [2]Player{NewPlayer("Abc"), NewPlayer("Xyz")} players["cookie0"] = &tmpPlayer[0] players["cookie1"] = &tmpPlayer[1] lobbies[lobbyc] = NewLobby(lobbyc, &tmpPlayer, DefSettings()) - router.Run("localhost:8080") - + err := router.Run("localhost:8080") + if (err) != nil { + panic(err) + } }