diff options
| author | nsfisis <nsfisis@gmail.com> | 2024-07-29 22:37:39 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2024-07-29 22:37:39 +0900 |
| commit | 00623621557ed6722c045145b6a28748ec9a5c4a (patch) | |
| tree | 69e369cfe9eac2743c8332f56d9cc2b6b301f2a2 /backend | |
| parent | 3a1e09991708bff1d0c0cfd5b1b091924cca9e8f (diff) | |
| parent | c74c28d8f6dc2ccf44546be05ee520ee4f47a609 (diff) | |
| download | iosdc-japan-2024-albatross-00623621557ed6722c045145b6a28748ec9a5c4a.tar.gz iosdc-japan-2024-albatross-00623621557ed6722c045145b6a28748ec9a5c4a.tar.zst iosdc-japan-2024-albatross-00623621557ed6722c045145b6a28748ec9a5c4a.zip | |
Merge branch 'fix/closed-channel-error'
Diffstat (limited to 'backend')
| -rw-r--r-- | backend/game/http.go | 30 | ||||
| -rw-r--r-- | backend/game/hub.go | 6 |
2 files changed, 13 insertions, 23 deletions
diff --git a/backend/game/http.go b/backend/game/http.go index 8cf7322..d955fa3 100644 --- a/backend/game/http.go +++ b/backend/game/http.go @@ -27,22 +27,15 @@ func (h *sockHandler) HandleSockGolfPlay(c echo.Context) error { } // TODO: check user permission - gameId := c.Param("gameId") - gameIdInt, err := strconv.Atoi(gameId) + gameID, err := strconv.Atoi(c.Param("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 { + hub := h.hubs.getHub(gameID) + if hub == nil { return echo.NewHTTPError(http.StatusNotFound, "Game not found") } - return servePlayerWs(foundHub, c.Response(), c.Request(), claims.UserID) + return servePlayerWs(hub, c.Response(), c.Request(), claims.UserID) } func (h *sockHandler) HandleSockGolfWatch(c echo.Context) error { @@ -55,20 +48,13 @@ func (h *sockHandler) HandleSockGolfWatch(c echo.Context) error { return echo.NewHTTPError(http.StatusForbidden, "Permission denied") } - gameId := c.Param("gameId") - gameIdInt, err := strconv.Atoi(gameId) + gameID, err := strconv.Atoi(c.Param("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 { + hub := h.hubs.getHub(gameID) + if hub == nil { return echo.NewHTTPError(http.StatusNotFound, "Game not found") } - return serveWatcherWs(foundHub, c.Response(), c.Request()) + return serveWatcherWs(hub, c.Response(), c.Request()) } diff --git a/backend/game/hub.go b/backend/game/hub.go index 770d257..f857ec5 100644 --- a/backend/game/hub.go +++ b/backend/game/hub.go @@ -205,8 +205,8 @@ func (hub *gameHub) run() { log.Fatalf("failed to set game state: %v", err) } hub.game.state = gameStateFinished + hub.close() } - hub.close() return } } @@ -255,6 +255,10 @@ func (hubs *GameHubs) Close() { } } +func (hubs *GameHubs) getHub(gameID int) *gameHub { + return hubs.hubs[gameID] +} + func (hubs *GameHubs) RestoreFromDB(ctx context.Context) error { games, err := hubs.q.ListGames(ctx) if err != nil { |
