-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
181 lines (139 loc) · 3.88 KB
/
handlers.go
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"fmt"
"net/http"
"strconv"
"strings"
"time"
"example.com/minesweeper/logic"
"example.com/minesweeper/repo"
"github.com/labstack/echo/v4"
)
// Registration of handlers: 👇
func prepareHandlers(e *echo.Echo) {
e.GET("/", getIndex)
e.GET("/favicon.ico", getFavicon)
e.GET("/gameplay", getGameplay)
e.GET("/privacy", getPrivacy)
e.GET("/player_name", getPlayerName)
e.POST("/player_name", postPlayerName)
e.HEAD("/game/:game_id", getGameHeaders)
e.GET("/game/:game_id", getGame)
e.POST("/game", postGame)
e.POST("/game/:game_id/player_action", postPlayerAction)
e.GET("/game/:game_id/stream", getGameStream)
}
// Handlers: 👇
func getIndex(c echo.Context) error {
return c.Render(200, "index.html", newBucketTitled(c, "Home"))
}
func getFavicon(c echo.Context) error {
return c.NoContent(204)
}
func getPrivacy(c echo.Context) error {
return c.Render(200, "privacy.html", newBucketTitled(c, "Privacy"))
}
func getGameplay(c echo.Context) error {
return c.Render(200, "gameplay.html", newBucketTitled(c, "Gameplay"))
}
func getPlayerName(c echo.Context) error {
return c.Render(200, "form_player_name.html", newBucket(c))
}
func postPlayerName(c echo.Context) error {
id := extractPlayerId(c)
p := logic.Player{
PlayerId: id,
Name: c.FormValue("player_name"),
}
r := repo.ExtractRepository(c)
r.SetPlayer(&p)
return c.Redirect(303, "/player_name")
}
func getGame(c echo.Context) error {
b := newBucket(c)
r := repo.ExtractRepository(c)
g := r.Game(c.Param("game_id"))
b["game"] = g
// Auto-join game
if g != nil {
g.AddPlayer(extractPlayer(c))
}
setGameStateHeader(g, c)
return c.Render(200, "show_game.html", b)
}
func getGameHeaders(c echo.Context) error {
r := repo.ExtractRepository(c)
g := r.Game(c.Param("game_id"))
setGameStateHeader(g, c)
return c.NoContent(200)
}
func setGameStateHeader(g *logic.Game, c echo.Context) {
hash := hashGame(g)
c.Response().Header().Set("X-Minesweeper-Game-State", hash)
}
func postGame(c echo.Context) error {
g := logic.NewGame()
g.AddPlayer(extractPlayer(c))
r := repo.ExtractRepository(c)
r.SetGame(g)
return c.Redirect(303, "/game/"+g.GameId)
}
func postPlayerAction(c echo.Context) error {
r := repo.ExtractRepository(c)
p := extractPlayer(c)
g := r.Game(c.Param("game_id"))
if g == nil {
return echo.NewHTTPError(404, "Game not found.")
}
x, y := c.FormValue("x"), c.FormValue("y")
xInt, _ := strconv.Atoi(x)
yInt, _ := strconv.Atoi(y)
action := c.FormValue("player_action")
switch action {
case "open":
g.OpenSquare(*p, xInt, yInt)
case "toggle_flag":
g.ToggleFlaggedSquare(*p, xInt, yInt)
default:
return fmt.Errorf("unsupported player action: \"%s\"", action)
}
return getGame(c)
}
func getGameStream(c echo.Context) error {
c.Response().Header().Set(echo.HeaderContentType, "text/event-stream")
c.Response().WriteHeader(http.StatusOK)
b := newBucket(c)
r := repo.ExtractRepository(c)
g := r.Game(c.Param("game_id"))
b["game"] = g
buffer := strings.Builder{}
for i := 1; i < 6; i++ {
c.Echo().Renderer.Render(&buffer, "show_game_inner.html", b, c)
renderedGame := buffer.String()
prefixedGame := strings.ReplaceAll(renderedGame, "\n", "\ndata: ")
fmt.Fprintf(c.Response(), `data: <turbo-stream action="update" target="show_game"><template>%s</template></turbo-stream>%s`, prefixedGame, "\n\n")
c.Response().Flush()
buffer.Reset()
time.Sleep(2 * time.Second)
}
c.Response().WriteHeader(http.StatusOK)
return nil
}
// Helpers and misc. declarations 👇
// A bucket holds data for a template.
type bucket map[string]interface{}
func newBucket(c echo.Context) bucket {
const defaultTitle = "Welcome"
r := repo.ExtractRepository(c)
p := extractPlayer(c)
return bucket{
"title": defaultTitle,
"player": p,
"games": r.Games(),
}
}
func newBucketTitled(c echo.Context, title string) bucket {
b := newBucket(c)
b["title"] = title
return b
}