From 67e8dda39df80232d60d11691f68c9ac5d91e9e8 Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Thu, 23 Mar 2023 14:28:11 -0600 Subject: [PATCH] Remove use of participant ID fixes #176 --- cmd/mothd/httpd.go | 3 +-- cmd/mothd/httpd_test.go | 3 --- cmd/mothd/issues_test.go | 2 +- cmd/mothd/server.go | 20 +++++++++----------- cmd/mothd/server_test.go | 7 +++---- cmd/mothd/state.go | 9 ++++----- cmd/mothd/state_test.go | 16 ++++++++-------- docs/logs.md | 8 ++++---- theme/index.html | 5 ----- theme/moth.js | 9 +-------- 10 files changed, 31 insertions(+), 51 deletions(-) diff --git a/cmd/mothd/httpd.go b/cmd/mothd/httpd.go index 54ca156..f9cc27c 100644 --- a/cmd/mothd/httpd.go +++ b/cmd/mothd/httpd.go @@ -44,9 +44,8 @@ func (h *HTTPServer) HandleMothFunc( mothHandler func(MothRequestHandler, http.ResponseWriter, *http.Request), ) { handler := func(w http.ResponseWriter, req *http.Request) { - participantID := req.FormValue("pid") teamID := req.FormValue("id") - mh := h.server.NewHandler(participantID, teamID) + mh := h.server.NewHandler(teamID) mothHandler(mh, w, req) } h.HandleFunc(h.base+pattern, handler) diff --git a/cmd/mothd/httpd_test.go b/cmd/mothd/httpd_test.go index b56f56c..f77e044 100644 --- a/cmd/mothd/httpd_test.go +++ b/cmd/mothd/httpd_test.go @@ -11,11 +11,8 @@ import ( "github.com/spf13/afero" ) -const TestParticipantID = "shipox" - func (hs *HTTPServer) TestRequest(path string, args map[string]string) *httptest.ResponseRecorder { vals := url.Values{} - vals.Set("pid", TestParticipantID) vals.Set("id", TestTeamID) for k, v := range args { vals.Set(k, v) diff --git a/cmd/mothd/issues_test.go b/cmd/mothd/issues_test.go index 26a6f6d..fefd29a 100644 --- a/cmd/mothd/issues_test.go +++ b/cmd/mothd/issues_test.go @@ -15,7 +15,7 @@ func TestIssue156(t *testing.T) { afero.WriteFile(state, "teams/bloop", []byte("bloop: the team"), 0644) state.refresh() - handler := server.NewHandler("", "bloop") + handler := server.NewHandler("bloop") es := handler.ExportState() if _, ok := es.TeamNames["self"]; !ok { t.Fail() diff --git a/cmd/mothd/server.go b/cmd/mothd/server.go index 0c5de3f..c7fe1f0 100644 --- a/cmd/mothd/server.go +++ b/cmd/mothd/server.go @@ -58,7 +58,7 @@ type StateProvider interface { TeamName(teamID string) (string, error) SetTeamName(teamID, teamName string) error AwardPoints(teamID string, cat string, points int) error - LogEvent(event, participantID, teamID, cat string, points int, extra ...string) + LogEvent(event, teamID, cat string, points int, extra ...string) Maintainer } @@ -92,19 +92,17 @@ func NewMothServer(config Configuration, theme ThemeProvider, state StateProvide } // NewHandler returns a new http.RequestHandler for the provided teamID. -func (s *MothServer) NewHandler(participantID, teamID string) MothRequestHandler { +func (s *MothServer) NewHandler(teamID string) MothRequestHandler { return MothRequestHandler{ - MothServer: s, - participantID: participantID, - teamID: teamID, + MothServer: s, + teamID: teamID, } } // MothRequestHandler provides http.RequestHandler for a MothServer. type MothRequestHandler struct { *MothServer - participantID string - teamID string + teamID string } // PuzzlesOpen opens a file associated with a puzzle. @@ -131,7 +129,7 @@ func (mh *MothRequestHandler) PuzzlesOpen(cat string, points int, path string) ( // Log puzzle.json loads if path == "puzzle.json" { - mh.State.LogEvent("load", mh.participantID, mh.teamID, cat, points) + mh.State.LogEvent("load", mh.teamID, cat, points) } return @@ -148,11 +146,11 @@ func (mh *MothRequestHandler) CheckAnswer(cat string, points int, answer string) } } if !correct { - mh.State.LogEvent("wrong", mh.participantID, mh.teamID, cat, points) + mh.State.LogEvent("wrong", mh.teamID, cat, points) return fmt.Errorf("incorrect answer") } - mh.State.LogEvent("correct", mh.participantID, mh.teamID, cat, points) + mh.State.LogEvent("correct", mh.teamID, cat, points) if _, err := mh.State.TeamName(mh.teamID); err != nil { return fmt.Errorf("invalid team ID") @@ -175,7 +173,7 @@ func (mh *MothRequestHandler) Register(teamName string) error { if teamName == "" { return fmt.Errorf("empty team name") } - mh.State.LogEvent("register", mh.participantID, mh.teamID, "", 0) + mh.State.LogEvent("register", mh.teamID, "", 0) return mh.State.SetTeamName(mh.teamID, teamName) } diff --git a/cmd/mothd/server_test.go b/cmd/mothd/server_test.go index caaf61a..828d08b 100644 --- a/cmd/mothd/server_test.go +++ b/cmd/mothd/server_test.go @@ -42,7 +42,7 @@ func (ts TestServer) refresh() { func TestDevelServer(t *testing.T) { server := NewTestServer() server.Config.Devel = true - anonHandler := server.NewHandler("badParticipantId", "badTeamId") + anonHandler := server.NewHandler("badTeamId") { es := anonHandler.ExportState() @@ -57,12 +57,11 @@ func TestDevelServer(t *testing.T) { func TestProdServer(t *testing.T) { teamName := "OurTeam" - participantID := "participantID" teamID := TestTeamID server := NewTestServer() - handler := server.NewHandler(participantID, teamID) - anonHandler := server.NewHandler("badParticipantId", "badTeamId") + handler := server.NewHandler(teamID) + anonHandler := server.NewHandler("badTeamId") { es := handler.ExportState() diff --git a/cmd/mothd/state.go b/cmd/mothd/state.go index 76fe13e..c611d88 100644 --- a/cmd/mothd/state.go +++ b/cmd/mothd/state.go @@ -122,9 +122,9 @@ func (s *State) updateEnabled() { s.enabledWhy = why log.Printf("Setting enabled=%v: %s", s.Enabled, s.enabledWhy) if s.Enabled { - s.LogEvent("enabled", "", "", "", 0, s.enabledWhy) + s.LogEvent("enabled", "", "", 0, s.enabledWhy) } else { - s.LogEvent("disabled", "", "", "", 0, s.enabledWhy) + s.LogEvent("disabled", "", "", 0, s.enabledWhy) } } } @@ -323,7 +323,7 @@ func (s *State) maybeInitialize() { if err := s.reopenEventLog(); err != nil { log.Fatal(err) } - s.LogEvent("init", "", "", "", 0) + s.LogEvent("init", "", "", 0) // Make sure various subdirectories exist s.Mkdir("points.tmp", 0755) @@ -380,12 +380,11 @@ func (s *State) maybeInitialize() { } // LogEvent writes to the event log -func (s *State) LogEvent(event, participantID, teamID, cat string, points int, extra ...string) { +func (s *State) LogEvent(event, teamID, cat string, points int, extra ...string) { s.eventStream <- append( []string{ strconv.FormatInt(time.Now().Unix(), 10), event, - participantID, teamID, cat, strconv.Itoa(points), diff --git a/cmd/mothd/state_test.go b/cmd/mothd/state_test.go index 0101dec..ed7c9e2 100644 --- a/cmd/mothd/state_test.go +++ b/cmd/mothd/state_test.go @@ -164,19 +164,19 @@ func TestStateOutOfOrderAward(t *testing.T) { func TestStateEvents(t *testing.T) { s := NewTestState() - s.LogEvent("moo", "", "", "", 0) - s.LogEvent("moo 2", "", "", "", 0) + s.LogEvent("moo", "", "", 0) + s.LogEvent("moo 2", "", "", 0) - if msg := <-s.eventStream; strings.Join(msg[1:], ":") != "init::::0" { + if msg := <-s.eventStream; strings.Join(msg[1:], ":") != "init:::0" { t.Error("Wrong message from event stream:", msg) } - if msg := <-s.eventStream; !strings.HasPrefix(msg[6], "state/hours.txt") { - t.Error("Wrong message from event stream:", msg[6]) + if msg := <-s.eventStream; !strings.HasPrefix(msg[5], "state/hours.txt") { + t.Error("Wrong message from event stream:", msg[5]) } - if msg := <-s.eventStream; strings.Join(msg[1:], ":") != "moo::::0" { + if msg := <-s.eventStream; strings.Join(msg[1:], ":") != "moo:::0" { t.Error("Wrong message from event stream:", msg) } - if msg := <-s.eventStream; strings.Join(msg[1:], ":") != "moo 2::::0" { + if msg := <-s.eventStream; strings.Join(msg[1:], ":") != "moo 2:::0" { t.Error("Wrong message from event stream:", msg) } } @@ -286,7 +286,7 @@ func TestStateMaintainer(t *testing.T) { t.Error("Team ID too short:", teamID) } - s.LogEvent("Hello!", "", "", "", 0) + s.LogEvent("Hello!", "", "", 0) if len(s.PointsLog()) != 0 { t.Error("Points log is not empty") diff --git a/docs/logs.md b/docs/logs.md index a5b5422..53362fd 100644 --- a/docs/logs.md +++ b/docs/logs.md @@ -49,10 +49,10 @@ It ought to import into any spreadsheet program painlessly. Each line has six fields minimum: -| `timestamp` | `event` | `participantID` | `teamID` | `category` | `points` | `extra`... | -| --- | --- | --- | --- | --- | --- | --- | -| int | string | string | string | string | int | string... | -| Unix epoch | Event type | Participant's (hopefully) unique ID | Team's unique ID | Name of category, if any | Points awarded, if any | Additional fields, if any | +| `timestamp` | `event` | `teamID` | `category` | `points` | `extra`... | +| --- | --- | --- | --- | --- | --- | +| int | string | string | string | int | string... | +| Unix epoch | Event type | Team's unique ID | Name of category, if any | Points awarded, if any | Additional fields, if any | Fields after `points` contain extra fields associated with the event. diff --git a/theme/index.html b/theme/index.html index f780301..32e2115 100644 --- a/theme/index.html +++ b/theme/index.html @@ -16,11 +16,6 @@
- Team ID:
Team name:
diff --git a/theme/moth.js b/theme/moth.js index eb1742c..d88e610 100644 --- a/theme/moth.js +++ b/theme/moth.js @@ -105,7 +105,6 @@ function renderState(obj) { if (devel) { let params = new URLSearchParams(window.location.search) sessionStorage.id = "1" - sessionStorage.pid = "rodney" renderPuzzles(obj.Puzzles) } else if (Object.keys(obj.Puzzles).length > 0) { renderPuzzles(obj.Puzzles) @@ -115,12 +114,9 @@ function renderState(obj) { function heartbeat() { let teamId = sessionStorage.id || "" - let participantId = sessionStorage.pid let url = new URL("state", window.location) url.searchParams.set("id", teamId) - if (participantId) { - url.searchParams.set("pid", participantId) - } + let fd = new FormData() fd.append("id", teamId) fetch(url) @@ -152,8 +148,6 @@ function login(e) { e.preventDefault() let name = document.querySelector("[name=name]").value let teamId = document.querySelector("[name=id]").value - let pide = document.querySelector("[name=pid]") - let participantId = pide?pide.value:Math.floor(Math.random() * Number.MAX_SAFE_INTEGER) fetch("register", { method: "POST", @@ -166,7 +160,6 @@ function login(e) { if ((obj.status == "success") || (obj.data.short == "Already registered")) { toast("Logged in") sessionStorage.id = teamId - sessionStorage.pid = participantId showPuzzles() heartbeat() } else {