Display results

This commit is contained in:
Vojtěch Káně
2021-04-01 16:36:38 +02:00
parent de28e7ab8c
commit 688f87922d
6 changed files with 142 additions and 6 deletions

View File

@@ -10,6 +10,7 @@ import (
"time"
"vkane.cz/tinyquiz/pkg/model"
"vkane.cz/tinyquiz/pkg/model/ent"
"vkane.cz/tinyquiz/pkg/rtcomm"
)
func (app *application) home(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
@@ -148,6 +149,10 @@ func (app *application) nextQuestion(w http.ResponseWriter, r *http.Request, par
app.serverError(w, err)
return
}
} else if errors.Is(err, model.NoNextQuestion) {
app.rtClients.SendToAll(sessionId, rtcomm.StateUpdate{Results: true})
w.WriteHeader(http.StatusNoContent)
return
} else {
app.serverError(w, err)
return
@@ -190,3 +195,36 @@ func (app *application) answer(w http.ResponseWriter, r *http.Request, params ht
return
}
}
func (app *application) resultsGeneral(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
type resultsData struct {
Results []model.PlayerResult
Session *ent.Session
Player *ent.Player
templateData
}
td := &resultsData{}
setDefaultTemplateData(&td.templateData)
var playerUid uuid.UUID
if uid, err := uuid.Parse(params.ByName("playerUid")); err == nil {
playerUid = uid
} else {
app.clientError(w, http.StatusBadRequest)
return
}
if results, session, player, err := app.model.GetResults(playerUid, r.Context()); err == nil {
td.Results = results
td.Session = session
td.Player = player
} else if errors.Is(err, model.NoSuchEntity) {
app.clientError(w, http.StatusNotFound)
return
} else {
app.serverError(w, err)
return
}
app.render(w, r, "results.page.tmpl.html", td)
}

View File

@@ -70,6 +70,7 @@ func main() {
mux.GET("/game/:playerUid", app.game)
mux.POST("/game/:playerUid/rpc/next", app.nextQuestion)
mux.POST("/game/:playerUid/answers/:choiceUid", app.answer)
mux.GET("/results/:playerUid", app.resultsGeneral)
mux.GET("/ws/:playerUid", app.processWebSocket)