-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfront.go
170 lines (146 loc) · 4.07 KB
/
front.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
package main
import (
"log"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/ejv2/gahoot/game"
"github.com/ejv2/gahoot/game/quiz"
)
// handleRoot is the handler for "/".
//
// Shows a page which allows the selection of starting a game or joining a
// game.
func handleRoot(c *gin.Context) {
c.HTML(200, "index.gohtml", nil)
}
// handleJoin is the handler for "/join"
//
// Shows a page in which the game pin can be entered. This redirects either
// back to this page with an error or to the game page with the pin filled out.
func handleJoin(c *gin.Context) {
dat := struct {
Pin game.Pin
PinValid bool
PinPresent bool
JoinError bool
}{}
// Aliases for landing pages.
joinPin := func() {
c.HTML(200, "join.gohtml", dat)
}
joinNick := func() {
c.HTML(200, "join_nick.gohtml", dat)
}
// If PIN provided, second stage is needed - unless there is an error,
// which puts us back on stage one with an error
if p := c.Query("pin"); p != "" {
dat.PinPresent = true
i, err := strconv.ParseUint(p, 10, 32)
if err != nil {
dat.PinValid = false
joinPin()
return
}
dat.Pin = game.Pin(i)
if dat.Pin < 1 {
dat.PinValid = false
joinPin()
return
}
g, ok := Coordinator.GetGame(dat.Pin)
if !ok {
dat.PinValid = false
joinPin()
return
}
// If nick provided, second stage completed.
// Validate nickname with game rules and then request the game
// runner add a new player.
if n := c.Query("nick"); n != "" {
// Notify running game instance
act := game.AddPlayer{Nick: n, ID: make(chan int, 1)}
g.Action <- act
id := int64(<-act.ID)
// Error signalled. Fail the join request.
if id < 0 {
c.Redirect(http.StatusSeeOther, "/join?pin="+strconv.FormatUint(uint64(dat.Pin), 10)+"&error=duplicate")
return
}
c.Redirect(http.StatusSeeOther,
"/play/game/"+p+"?plr="+strconv.FormatInt(id, 10))
return
}
if e := c.Query("error"); e != "" {
dat.JoinError = true
}
joinNick()
return
}
joinPin()
}
// handleCreate is the handler for "/create/"
//
// Shows a page of links to different methods of creating a game.
func handleCreate(c *gin.Context) {
c.HTML(200, "create.gohtml", nil)
}
// handleFind is the handler for "/create/find"
//
// Shows a page which allows the user to find already uploaded or shared games
// on this game server, sorted by category and upload source.
func handleFind(c *gin.Context) {
dat := struct {
Quizzes []quiz.Quiz
Categories []string
}{
Quizzes: QuizManager.GetAll(),
Categories: QuizManager.GetCategories(),
}
c.HTML(200, "create_find.gohtml", dat)
}
// handleUpload is the GET handler for "/create/upload"
//
// Shows an upload form which allows the user to submit a quiz archive to this
// server for use in memory.
func handleUpload(c *gin.Context) {
dat := struct {
// Maximum file size in MB
FileSize int64
}{
FileSize: quiz.MaxQuizSize / (1024 * 1024),
}
c.HTML(200, "create_upload.gohtml", dat)
}
// handleEditor is the handler for "/create/new"
//
// Shows the client-side quiz editor that allows a quiz to be both downloaded
// and saved and played in-memory on this game server.
func handleEditor(c *gin.Context) {
c.Redirect(http.StatusTemporaryRedirect, "/")
}
// handleCreateGame is the handler for "/create/game/{HASH}"
//
// Creates and stores a new game based on the stored hash from the game manager.
// If the hash is not found, redirects back to "/new/find".
func handleCreateGame(c *gin.Context) {
hash := c.Param("hash")
if hash == "" {
log.Panic("handleCreateGame: no hash parameter in required handler")
}
q, ok := QuizManager.GetString(hash)
if !ok {
c.Redirect(http.StatusSeeOther, "/new/find")
c.Abort()
return
}
g := Coordinator.CreateGame(q)
log.Println("Creating new game", g.PIN, "from quiz", q.String()[:12])
c.Redirect(http.StatusSeeOther, "/play/host/"+g.PIN.String())
}
// handleBlankCreateGame is the handler for "/create/game")
//
// Redirects confused visitors back to where they probably want to be.
func handleBlankCreateGame(c *gin.Context) {
c.Redirect(http.StatusMovedPermanently, "/create/")
}