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 @@
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")) {