1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
package game
import (
"net/http"
"strconv"
"github.com/labstack/echo/v4"
"github.com/nsfisis/iosdc-2024-albatross/backend/auth"
)
type sockHandler struct {
hubs *GameHubs
}
func newSockHandler(hubs *GameHubs) *sockHandler {
return &sockHandler{
hubs: hubs,
}
}
func (h *sockHandler) HandleSockGolfPlay(c echo.Context) error {
jwt := c.QueryParam("token")
claims, err := auth.ParseJWT(jwt)
if err != nil {
return echo.NewHTTPError(http.StatusUnauthorized, err.Error())
}
// TODO: check user permission
gameID, err := strconv.Atoi(c.Param("gameId"))
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid game id")
}
hub := h.hubs.getHub(gameID)
if hub == nil {
return echo.NewHTTPError(http.StatusNotFound, "Game not found")
}
return servePlayerWs(hub, c.Response(), c.Request(), claims.UserID)
}
func (h *sockHandler) HandleSockGolfWatch(c echo.Context) error {
jwt := c.QueryParam("token")
claims, err := auth.ParseJWT(jwt)
if err != nil {
return echo.NewHTTPError(http.StatusUnauthorized, err.Error())
}
if !claims.IsAdmin {
return echo.NewHTTPError(http.StatusForbidden, "Permission denied")
}
gameID, err := strconv.Atoi(c.Param("gameId"))
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid game id")
}
hub := h.hubs.getHub(gameID)
if hub == nil {
return echo.NewHTTPError(http.StatusNotFound, "Game not found")
}
return serveWatcherWs(hub, c.Response(), c.Request())
}
|