diff options
Diffstat (limited to 'backend/game/http.go')
| -rw-r--r-- | backend/game/http.go | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/backend/game/http.go b/backend/game/http.go new file mode 100644 index 0000000..a5a7ded --- /dev/null +++ b/backend/game/http.go @@ -0,0 +1,56 @@ +package game + +import ( + "net/http" + "strconv" + + "github.com/labstack/echo/v4" +) + +type sockHandler struct { + hubs *GameHubs +} + +func newSockHandler(hubs *GameHubs) *sockHandler { + return &sockHandler{ + hubs: hubs, + } +} + +func (h *sockHandler) HandleSockGolfPlay(c echo.Context) error { + gameId := c.Param("gameId") + gameIdInt, err := strconv.Atoi(gameId) + if err != nil { + return echo.NewHTTPError(http.StatusBadRequest, "Invalid game id") + } + var foundHub *GameHub + for _, hub := range h.hubs.hubs { + if hub.game.gameID == gameIdInt { + foundHub = hub + break + } + } + if foundHub == nil { + return echo.NewHTTPError(http.StatusNotFound, "Game not found") + } + return servePlayerWs(foundHub, c.Response(), c.Request(), "a") +} + +func (h *sockHandler) HandleSockGolfWatch(c echo.Context) error { + gameId := c.Param("gameId") + gameIdInt, err := strconv.Atoi(gameId) + if err != nil { + return echo.NewHTTPError(http.StatusBadRequest, "Invalid game id") + } + var foundHub *GameHub + for _, hub := range h.hubs.hubs { + if hub.game.gameID == gameIdInt { + foundHub = hub + break + } + } + if foundHub == nil { + return echo.NewHTTPError(http.StatusNotFound, "Game not found") + } + return serveWatcherWs(foundHub, c.Response(), c.Request()) +} |
