From 8363ca589a9c025454f119b4bedcd67d7f93d6fa Mon Sep 17 00:00:00 2001 From: John Donaldson Date: Fri, 28 Jan 2022 11:26:01 -0800 Subject: [PATCH] Make reference theme work with participant ID stuff --- cmd/mothd/httpd.go | 3 +- cmd/mothd/server.go | 26 +++++++----- cmd/mothd/state.go | 1 + theme/index.html | 4 +- theme/moth.js | 99 +++++++++++++++++++++++++++++++-------------- theme/puzzle.html | 2 +- theme/puzzle.js | 7 +++- 7 files changed, 94 insertions(+), 48 deletions(-) diff --git a/cmd/mothd/httpd.go b/cmd/mothd/httpd.go index 1963f63..541acb0 100644 --- a/cmd/mothd/httpd.go +++ b/cmd/mothd/httpd.go @@ -29,6 +29,7 @@ func NewHTTPServer(base string, server *MothServer) *HTTPServer { h.HandleMothFunc("/", h.ThemeHandler) h.HandleMothFunc("/state", h.StateHandler) h.HandleMothFunc("/register", h.RegisterHandler) + h.HandleMothFunc("/assign", h.AssignParticipantHandler) h.HandleMothFunc("/answer", h.AnswerHandler) h.HandleMothFunc("/content/", h.ContentHandler) @@ -132,7 +133,7 @@ func (h *HTTPServer) AssignParticipantHandler(mh MothRequestHandler, w http.Resp 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") } else if err != nil { jsend.Sendf(w, jsend.Fail, "unable to associate participant and team", err.Error()) diff --git a/cmd/mothd/server.go b/cmd/mothd/server.go index 900b6a9..87f4d73 100644 --- a/cmd/mothd/server.go +++ b/cmd/mothd/server.go @@ -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 func (mh *MothRequestHandler) CheckAnswer(cat string, points int, answer string) error { 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 { if ok, err := provider.CheckAnswer(cat, points, answer); err != nil { return err @@ -146,20 +157,13 @@ func (mh *MothRequestHandler) CheckAnswer(cat string, points int, answer string) correct = true } } + 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") } - mh.State.LogEvent("correct", mh.participantID, mh.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") - } + mh.State.LogEvent("correct", mh.participantID, teamID, cat, points) if err := mh.State.AwardPoints(mh.participantID, cat, points); err != nil { return fmt.Errorf("error awarding points: %s", err) @@ -190,7 +194,7 @@ func (mh *MothRequestHandler) AssignParticipant() error { } if mh.teamID == "" { - return fmt.Errorf("empty participant ID") + return fmt.Errorf("empty team ID") } mh.State.LogEvent("assign", mh.participantID, mh.teamID, "", 0) diff --git a/cmd/mothd/state.go b/cmd/mothd/state.go index 40fa3f9..4183db5 100644 --- a/cmd/mothd/state.go +++ b/cmd/mothd/state.go @@ -183,6 +183,7 @@ func (s *State) ParticipantTeam(participantID string) (string, error) { teamID, ok := s.participantTeams[participantID] if !ok { + fmt.Println("Could not find participant") return "", fmt.Errorf("participant (%s) has not registered with a team", participantID) } diff --git a/theme/index.html b/theme/index.html index f780301..c9d9ed0 100644 --- a/theme/index.html +++ b/theme/index.html @@ -16,11 +16,9 @@
- Team ID:
Team name:
diff --git a/theme/moth.js b/theme/moth.js index eb1742c..6f32255 100644 --- a/theme/moth.js +++ b/theme/moth.js @@ -155,37 +155,74 @@ function login(e) { let pide = document.querySelector("[name=pid]") let participantId = pide?pide.value:Math.floor(Math.random() * Number.MAX_SAFE_INTEGER) - 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("Logged in") - sessionStorage.id = teamId - sessionStorage.pid = participantId - showPuzzles() - heartbeat() - } 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) - }) + if (participantId != "") { + fetch("assign", { + method: "POST", + body: new FormData(e.target), + }) + .then(resp => { + if (resp.ok) { + resp.json() + .then(obj => { + if ((obj.status == "success") || (obj.data.short == "already assigned")) { + toast("Logged in") + sessionStorage.id = teamId + sessionStorage.pid = participantId + showPuzzles() + heartbeat() + } 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("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() { diff --git a/theme/puzzle.html b/theme/puzzle.html index 37206f6..f9d926b 100644 --- a/theme/puzzle.html +++ b/theme/puzzle.html @@ -22,7 +22,7 @@ - Team ID:
+ Participant ID:
Answer:
diff --git a/theme/puzzle.js b/theme/puzzle.js index 9913878..cce7b6e 100644 --- a/theme/puzzle.js +++ b/theme/puzzle.js @@ -207,9 +207,14 @@ function init() { loadPuzzle(categoryName, points, puzzleId || points) } - let teamId = sessionStorage.getItem("id") + /*let teamId = sessionStorage.getItem("id") if (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")) {