Make reference theme work with participant ID stuff

This commit is contained in:
John Donaldson 2022-01-28 11:26:01 -08:00
parent c4cfee94f4
commit 8363ca589a
7 changed files with 94 additions and 48 deletions

View File

@ -29,6 +29,7 @@ func NewHTTPServer(base string, server *MothServer) *HTTPServer {
h.HandleMothFunc("/", h.ThemeHandler) h.HandleMothFunc("/", h.ThemeHandler)
h.HandleMothFunc("/state", h.StateHandler) h.HandleMothFunc("/state", h.StateHandler)
h.HandleMothFunc("/register", h.RegisterHandler) h.HandleMothFunc("/register", h.RegisterHandler)
h.HandleMothFunc("/assign", h.AssignParticipantHandler)
h.HandleMothFunc("/answer", h.AnswerHandler) h.HandleMothFunc("/answer", h.AnswerHandler)
h.HandleMothFunc("/content/", h.ContentHandler) h.HandleMothFunc("/content/", h.ContentHandler)
@ -132,7 +133,7 @@ func (h *HTTPServer) AssignParticipantHandler(mh MothRequestHandler, w http.Resp
return return
} }
if err := mh.AssignParticipant(); err != ErrAlreadyRegistered { if err := mh.AssignParticipant(); err == ErrAlreadyRegistered {
jsend.Sendf(w, jsend.Success, "already assigned", "participant and team have already been associated") jsend.Sendf(w, jsend.Success, "already assigned", "participant and team have already been associated")
} else if err != nil { } else if err != nil {
jsend.Sendf(w, jsend.Fail, "unable to associate participant and team", err.Error()) jsend.Sendf(w, jsend.Fail, "unable to associate participant and team", err.Error())

View File

@ -139,6 +139,17 @@ func (mh *MothRequestHandler) PuzzlesOpen(cat string, points int, path string) (
// CheckAnswer returns an error if answer is not a correct answer for puzzle points in category cat // CheckAnswer returns an error if answer is not a correct answer for puzzle points in category cat
func (mh *MothRequestHandler) CheckAnswer(cat string, points int, answer string) error { func (mh *MothRequestHandler) CheckAnswer(cat string, points int, answer string) error {
correct := false correct := false
teamID, err := mh.State.ParticipantTeam(mh.participantID)
if err != nil {
return fmt.Errorf("invalid participant ID")
}
if _, err := mh.State.TeamName(teamID); err != nil {
return fmt.Errorf("invalid team ID")
}
for _, provider := range mh.PuzzleProviders { for _, provider := range mh.PuzzleProviders {
if ok, err := provider.CheckAnswer(cat, points, answer); err != nil { if ok, err := provider.CheckAnswer(cat, points, answer); err != nil {
return err return err
@ -146,20 +157,13 @@ func (mh *MothRequestHandler) CheckAnswer(cat string, points int, answer string)
correct = true correct = true
} }
} }
if !correct { if !correct {
mh.State.LogEvent("wrong", mh.participantID, mh.teamID, cat, points) mh.State.LogEvent("wrong", mh.participantID, teamID, cat, points)
return fmt.Errorf("incorrect answer") return fmt.Errorf("incorrect answer")
} }
mh.State.LogEvent("correct", mh.participantID, mh.teamID, cat, points) mh.State.LogEvent("correct", mh.participantID, teamID, cat, points)
if _, err := mh.State.TeamName(mh.teamID); err != nil {
return fmt.Errorf("invalid team ID")
}
if _, err := mh.State.ParticipantTeam(mh.participantID); err != nil {
return fmt.Errorf("invalid participant ID")
}
if err := mh.State.AwardPoints(mh.participantID, cat, points); err != nil { if err := mh.State.AwardPoints(mh.participantID, cat, points); err != nil {
return fmt.Errorf("error awarding points: %s", err) return fmt.Errorf("error awarding points: %s", err)
@ -190,7 +194,7 @@ func (mh *MothRequestHandler) AssignParticipant() error {
} }
if mh.teamID == "" { if mh.teamID == "" {
return fmt.Errorf("empty participant ID") return fmt.Errorf("empty team ID")
} }
mh.State.LogEvent("assign", mh.participantID, mh.teamID, "", 0) mh.State.LogEvent("assign", mh.participantID, mh.teamID, "", 0)

View File

@ -183,6 +183,7 @@ func (s *State) ParticipantTeam(participantID string) (string, error) {
teamID, ok := s.participantTeams[participantID] teamID, ok := s.participantTeams[participantID]
if !ok { if !ok {
fmt.Println("Could not find participant")
return "", fmt.Errorf("participant (%s) has not registered with a team", participantID) return "", fmt.Errorf("participant (%s) has not registered with a team", participantID)
} }

View File

@ -16,11 +16,9 @@
</div> </div>
<form id="login"> <form id="login">
<!--
<span id="pid"> <span id="pid">
Participant ID: <input name="pid"> (optional) <br> Participant ID: <input name="pid"><br>
</span> </span>
-->
Team ID: <input name="id"> <br> Team ID: <input name="id"> <br>
Team name: <input name="name"> <br> Team name: <input name="name"> <br>
<input type="submit" value="Sign In"> <input type="submit" value="Sign In">

View File

@ -155,37 +155,74 @@ function login(e) {
let pide = document.querySelector("[name=pid]") let pide = document.querySelector("[name=pid]")
let participantId = pide?pide.value:Math.floor(Math.random() * Number.MAX_SAFE_INTEGER) let participantId = pide?pide.value:Math.floor(Math.random() * Number.MAX_SAFE_INTEGER)
fetch("register", { if (participantId != "") {
method: "POST", fetch("assign", {
body: new FormData(e.target), method: "POST",
}) body: new FormData(e.target),
.then(resp => { })
if (resp.ok) { .then(resp => {
resp.json() if (resp.ok) {
.then(obj => { resp.json()
if ((obj.status == "success") || (obj.data.short == "Already registered")) { .then(obj => {
toast("Logged in") if ((obj.status == "success") || (obj.data.short == "already assigned")) {
sessionStorage.id = teamId toast("Logged in")
sessionStorage.pid = participantId sessionStorage.id = teamId
showPuzzles() sessionStorage.pid = participantId
heartbeat() showPuzzles()
} else { heartbeat()
toast(obj.data.description) } else {
} toast(obj.data.description)
}) }
.catch(err => { })
toast("Oops, the server has lost its mind. You probably need to tell someone so they can fix it.") .catch(err => {
console.log(err, resp) toast("Oops, the server has lost its mind. You probably need to tell someone so they can fix it.")
}) console.log(err, resp)
} else { })
toast("Oops, something's wrong with the server. Try again in a few seconds.") } else {
console.log(resp) toast("Oops, something's wrong with the server. Try again in a few seconds.")
} console.log(resp)
}) }
.catch(err => { })
toast("Oops, something went wrong. Try again in a few seconds.") .catch(err => {
console.log(err) toast("Oops, something went wrong. Try again in a few seconds.")
}) console.log(err)
})
} else {
toast("Participant ID may not be empty")
}
if (teamId != "") {
fetch("register", {
method: "POST",
body: new FormData(e.target),
})
.then(resp => {
if (resp.ok) {
resp.json()
.then(obj => {
if ((obj.status == "success") || (obj.data.short == "Already registered")) {
toast("Team registered")
} else {
toast(obj.data.description)
}
})
.catch(err => {
toast("Oops, the server has lost its mind. You probably need to tell someone so they can fix it.")
console.log(err, resp)
})
} else {
toast("Oops, something's wrong with the server. Try again in a few seconds.")
console.log(resp)
}
})
.catch(err => {
toast("Oops, something went wrong. Try again in a few seconds.")
console.log(err)
})
} else {
toast("Team ID must be provided")
}
} }
function init() { function init() {

View File

@ -22,7 +22,7 @@
<input type="hidden" name="cat"> <input type="hidden" name="cat">
<input type="hidden" name="points"> <input type="hidden" name="points">
<input type="hidden" name="xAnswer"> <input type="hidden" name="xAnswer">
Team ID: <input type="text" name="id"> <br> Participant ID: <input type="text" name="pid"> <br>
Answer: <input type="text" name="answer" id="answer"> <span id="answer_ok"></span><br> Answer: <input type="text" name="answer" id="answer"> <span id="answer_ok"></span><br>
<input type="submit" value="Submit"> <input type="submit" value="Submit">
</form> </form>

View File

@ -207,9 +207,14 @@ function init() {
loadPuzzle(categoryName, points, puzzleId || points) loadPuzzle(categoryName, points, puzzleId || points)
} }
let teamId = sessionStorage.getItem("id") /*let teamId = sessionStorage.getItem("id")
if (teamId) { if (teamId) {
document.querySelector("input[name=id]").value = teamId document.querySelector("input[name=id]").value = teamId
}*/
let participantId = sessionStorage.getItem("pid")
if (participantId) {
document.querySelector("input[name=pid]").value = participantId
} }
if (document.querySelector("#answer")) { if (document.querySelector("#answer")) {