mirror of https://github.com/dirtbags/moth.git
Make reference theme work with participant ID stuff
This commit is contained in:
parent
c4cfee94f4
commit
8363ca589a
|
@ -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())
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
@ -16,11 +16,9 @@
|
|||
</div>
|
||||
|
||||
<form id="login">
|
||||
<!--
|
||||
<span id="pid">
|
||||
Participant ID: <input name="pid"> (optional) <br>
|
||||
Participant ID: <input name="pid"><br>
|
||||
</span>
|
||||
-->
|
||||
Team ID: <input name="id"> <br>
|
||||
Team name: <input name="name"> <br>
|
||||
<input type="submit" value="Sign In">
|
||||
|
|
|
@ -155,7 +155,8 @@ function login(e) {
|
|||
let pide = document.querySelector("[name=pid]")
|
||||
let participantId = pide?pide.value:Math.floor(Math.random() * Number.MAX_SAFE_INTEGER)
|
||||
|
||||
fetch("register", {
|
||||
if (participantId != "") {
|
||||
fetch("assign", {
|
||||
method: "POST",
|
||||
body: new FormData(e.target),
|
||||
})
|
||||
|
@ -163,7 +164,7 @@ function login(e) {
|
|||
if (resp.ok) {
|
||||
resp.json()
|
||||
.then(obj => {
|
||||
if ((obj.status == "success") || (obj.data.short == "Already registered")) {
|
||||
if ((obj.status == "success") || (obj.data.short == "already assigned")) {
|
||||
toast("Logged in")
|
||||
sessionStorage.id = teamId
|
||||
sessionStorage.pid = participantId
|
||||
|
@ -186,6 +187,42 @@ function login(e) {
|
|||
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() {
|
||||
|
|
|
@ -22,7 +22,7 @@
|
|||
<input type="hidden" name="cat">
|
||||
<input type="hidden" name="points">
|
||||
<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>
|
||||
<input type="submit" value="Submit">
|
||||
</form>
|
||||
|
|
|
@ -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")) {
|
||||
|
|
Loading…
Reference in New Issue