From 1e6df136d8202c8adf65948527f4c3e7583b338c Mon Sep 17 00:00:00 2001 From: nsfisis Date: Tue, 4 Mar 2025 22:55:01 +0900 Subject: websocket to polling --- backend/admin/handler.go | 46 +++++++++++----------------------- backend/admin/renderer.go | 2 +- backend/admin/templates/game_edit.html | 11 +++----- backend/admin/templates/games.html | 11 ++++---- 4 files changed, 25 insertions(+), 45 deletions(-) (limited to 'backend/admin') diff --git a/backend/admin/handler.go b/backend/admin/handler.go index ca70639..9123ba0 100644 --- a/backend/admin/handler.go +++ b/backend/admin/handler.go @@ -24,18 +24,17 @@ const ( var jst = time.FixedZone("Asia/Tokyo", 9*60*60) type Handler struct { - q *db.Queries - hubs GameHubsInterface + q *db.Queries + hub GameHubInterface } -type GameHubsInterface interface { - StartGame(gameID int) error -} +// TODO +type GameHubInterface any -func NewHandler(q *db.Queries, hubs GameHubsInterface) *Handler { +func NewHandler(q *db.Queries, hub GameHubInterface) *Handler { return &Handler{ - q: q, - hubs: hubs, + q: q, + hub: hub, } } @@ -150,20 +149,20 @@ func (h *Handler) postUserFetchIcon(c echo.Context) error { } func (h *Handler) getGames(c echo.Context) error { - rows, err := h.q.ListGames(c.Request().Context()) + rows, err := h.q.ListAllGames(c.Request().Context()) if err != nil { return echo.NewHTTPError(http.StatusInternalServerError, err.Error()) } games := make([]echo.Map, len(rows)) for i, g := range rows { var startedAt string - if !g.StartedAt.Valid { + if g.StartedAt.Valid { startedAt = g.StartedAt.Time.In(jst).Format("2006-01-02T15:04") } games[i] = echo.Map{ "GameID": g.GameID, "GameType": g.GameType, - "State": g.State, + "IsPublic": g.IsPublic, "DisplayName": g.DisplayName, "DurationSeconds": g.DurationSeconds, "StartedAt": startedAt, @@ -192,7 +191,7 @@ func (h *Handler) getGameEdit(c echo.Context) error { } var startedAt string - if !row.StartedAt.Valid { + if row.StartedAt.Valid { startedAt = row.StartedAt.Time.In(jst).Format("2006-01-02T15:04") } @@ -202,7 +201,7 @@ func (h *Handler) getGameEdit(c echo.Context) error { "Game": echo.Map{ "GameID": row.GameID, "GameType": row.GameType, - "State": row.State, + "IsPublic": row.IsPublic, "DisplayName": row.DisplayName, "DurationSeconds": row.DurationSeconds, "StartedAt": startedAt, @@ -216,16 +215,9 @@ func (h *Handler) postGameEdit(c echo.Context) error { if err != nil { return echo.NewHTTPError(http.StatusBadRequest, "Invalid game id") } - row, err := h.q.GetGameByID(c.Request().Context(), int32(gameID)) - if err != nil { - if errors.Is(err, pgx.ErrNoRows) { - return echo.NewHTTPError(http.StatusNotFound) - } - return echo.NewHTTPError(http.StatusInternalServerError, err.Error()) - } gameType := c.FormValue("game_type") - state := c.FormValue("state") + isPublic := c.FormValue("is_public") == "public" displayName := c.FormValue("display_name") durationSeconds, err := strconv.Atoi(c.FormValue("duration_seconds")) if err != nil { @@ -267,7 +259,7 @@ func (h *Handler) postGameEdit(c echo.Context) error { err = h.q.UpdateGame(c.Request().Context(), db.UpdateGameParams{ GameID: int32(gameID), GameType: gameType, - State: state, + IsPublic: isPublic, DisplayName: displayName, DurationSeconds: int32(durationSeconds), StartedAt: changedStartedAt, @@ -277,15 +269,5 @@ func (h *Handler) postGameEdit(c echo.Context) error { return echo.NewHTTPError(http.StatusInternalServerError, err.Error()) } - { - // TODO: - if state != row.State && state == "starting" { - err := h.hubs.StartGame(int(gameID)) - if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, err.Error()) - } - } - } - return c.Redirect(http.StatusSeeOther, basePath+"/admin/games") } diff --git a/backend/admin/renderer.go b/backend/admin/renderer.go index d38c701..ba9dba9 100644 --- a/backend/admin/renderer.go +++ b/backend/admin/renderer.go @@ -27,7 +27,7 @@ func NewRenderer() *Renderer { } } -func (r *Renderer) Render(w io.Writer, name string, data interface{}, _ echo.Context) error { +func (r *Renderer) Render(w io.Writer, name string, data any, _ echo.Context) error { tmpl, ok := r.templates[name] if !ok { t, err := template.ParseFS(templatesFS, "templates/base.html", "templates/"+name+".html") diff --git a/backend/admin/templates/game_edit.html b/backend/admin/templates/game_edit.html index 48a0625..2c80558 100644 --- a/backend/admin/templates/game_edit.html +++ b/backend/admin/templates/game_edit.html @@ -22,13 +22,10 @@
- - + +
diff --git a/backend/admin/templates/games.html b/backend/admin/templates/games.html index 9dd9cae..3be6726 100644 --- a/backend/admin/templates/games.html +++ b/backend/admin/templates/games.html @@ -9,13 +9,14 @@ {{ range .Games }}
  • - {{ .DisplayName }} (id={{ .GameID }} type={{ .GameType }} state={{ .State }}) + {{ .DisplayName }} (id={{ .GameID }} type={{ .GameType }} {{ if not .IsPublic }}private{{ end }})
      - {{ if and (ne .State "closed") (ne .State "finished") }} -
    • - Watch -
    • + {{ if .IsPublic }} +
    • Play
    • + {{ end }} + {{ if .IsPublic }} +
    • Watch
    • {{ end }}
  • -- cgit v1.2.3-70-g09d2