81 lines
1.7 KiB
Go
81 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type Player struct {
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
func NewPlayer(name string) Player {
|
|
return Player{name}
|
|
}
|
|
|
|
type Settings struct {
|
|
Set0 string `json:"set0"`
|
|
}
|
|
|
|
func DefSettings() *Settings {
|
|
return &Settings{Set0: "ich bin ein Test Setting"}
|
|
}
|
|
|
|
type Lobby struct {
|
|
ID int `json:"id"`
|
|
Teams *[2]Player `json:"teams"`
|
|
Settings *Settings `json:"settings"`
|
|
}
|
|
|
|
func NewLobby(ID int, teams *[2]Player, settings *Settings) *Lobby {
|
|
return &Lobby{ID: ID, Teams: teams, Settings: settings}
|
|
}
|
|
|
|
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
|
|
|
|
func main() {
|
|
lobbyc := 0
|
|
router := gin.Default()
|
|
router.GET("/ping", func(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"message": "pong",
|
|
})
|
|
})
|
|
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())
|
|
err := router.Run("localhost:8080")
|
|
if (err) != nil {
|
|
panic(err)
|
|
}
|
|
}
|