diff options
| author | nsfisis <nsfisis@gmail.com> | 2024-07-28 19:42:05 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2024-07-28 19:52:31 +0900 |
| commit | 22ddf340f0b0c8d0cd04c34d9fa1481a1fbf422f (patch) | |
| tree | fab36f8dc1a2be23e331752b3e3d35e10d797ecf /backend/game/http.go | |
| parent | 7bd55ee264f7eefda6c1f71865a2c6287d7e20fa (diff) | |
| download | phperkaigi-2025-albatross-22ddf340f0b0c8d0cd04c34d9fa1481a1fbf422f.tar.gz phperkaigi-2025-albatross-22ddf340f0b0c8d0cd04c34d9fa1481a1fbf422f.tar.zst phperkaigi-2025-albatross-22ddf340f0b0c8d0cd04c34d9fa1481a1fbf422f.zip | |
refactor(backend): move game-related code to game module
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()) +} |
