aboutsummaryrefslogtreecommitdiffhomepage
path: root/backend/admin
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2025-03-04 22:55:01 +0900
committernsfisis <nsfisis@gmail.com>2025-03-08 10:12:44 +0900
commit1e6df136d8202c8adf65948527f4c3e7583b338c (patch)
tree7c82476f6bbbc71d72ab7e71e39559eca197fd95 /backend/admin
parent54316868c3bec1ff9b04643dfe6c13cf56bf3246 (diff)
downloadphperkaigi-2025-albatross-1e6df136d8202c8adf65948527f4c3e7583b338c.tar.gz
phperkaigi-2025-albatross-1e6df136d8202c8adf65948527f4c3e7583b338c.tar.zst
phperkaigi-2025-albatross-1e6df136d8202c8adf65948527f4c3e7583b338c.zip
websocket to polling
Diffstat (limited to 'backend/admin')
-rw-r--r--backend/admin/handler.go46
-rw-r--r--backend/admin/renderer.go2
-rw-r--r--backend/admin/templates/game_edit.html11
-rw-r--r--backend/admin/templates/games.html11
4 files changed, 25 insertions, 45 deletions
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 @@
</select>
</div>
<div>
- <label>State</label>
- <select name="state" required>
- <option value="closed"{{ if eq .Game.State "closed" }} selected{{ end }}>Closed</option>
- <option value="waiting"{{ if eq .Game.State "waiting" }} selected{{ end }}>Waiting</option>
- <option value="starting"{{ if eq .Game.State "starting" }} selected{{ end }}>Starting</option>
- <option value="gaming"{{ if eq .Game.State "gaming" }} selected{{ end }}>Gaming</option>
- <option value="finished"{{ if eq .Game.State "finished" }} selected{{ end }}>Finished</option>
+ <label>Is Public</label>
+ <select name="is_public" required>
+ <option value="public"{{ if .Game.IsPublic }} selected{{ end }}>Public</option>
+ <option value="private"{{ if not .Game.IsPublic }} selected{{ end }}>Private</option>
</select>
</div>
<div>
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 }}
<li>
<a href="{{ $.BasePath }}/admin/games/{{ .GameID }}">
- {{ .DisplayName }} (id={{ .GameID }} type={{ .GameType }} state={{ .State }})
+ {{ .DisplayName }} (id={{ .GameID }} type={{ .GameType }} {{ if not .IsPublic }}private{{ end }})
</a>
<ul>
- {{ if and (ne .State "closed") (ne .State "finished") }}
- <li>
- <a href="{{ $.BasePath }}/golf/{{ .GameID }}/watch">Watch</a>
- </li>
+ {{ if .IsPublic }}
+ <li><a href="{{ $.BasePath }}/golf/{{ .GameID }}/play">Play</a></li>
+ {{ end }}
+ {{ if .IsPublic }}
+ <li><a href="{{ $.BasePath }}/golf/{{ .GameID }}/watch">Watch</a></li>
{{ end }}
</ul>
</li>