diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b37b48..8eb2c9d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Added +- URL parameter to points.json to allow returning only the JSON for a single + team by its team id (e.g., points.json?id=abc123). ## [3.4.2] - 2019-11-18 ### Fixed diff --git a/src/handlers.go b/src/handlers.go index 00eca05..a0ceded 100644 --- a/src/handlers.go +++ b/src/handlers.go @@ -183,9 +183,14 @@ func (ctx *Instance) puzzlesHandler(w http.ResponseWriter, req *http.Request) { } func (ctx *Instance) pointsHandler(w http.ResponseWriter, req *http.Request) { + teamId, ok := req.URL.Query()["id"] + pointsLog := ctx.jPointsLog + if ok && len(teamId[0]) > 0 { + pointsLog = ctx.generatePointsLog(teamId[0]) + } w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) - w.Write(ctx.jPointsLog) + w.Write(pointsLog) } func (ctx *Instance) contentHandler(w http.ResponseWriter, req *http.Request) { diff --git a/src/instance.go b/src/instance.go index a415b40..0ec5d8e 100644 --- a/src/instance.go +++ b/src/instance.go @@ -151,7 +151,7 @@ func (ctx *Instance) TooFast(teamId string) bool { return now.Before(next) } -func (ctx *Instance) PointsLog() []*Award { +func (ctx *Instance) PointsLog(teamId string) []*Award { var ret []*Award fn := ctx.StatePath("points.log") @@ -170,6 +170,9 @@ func (ctx *Instance) PointsLog() []*Award { log.Printf("Skipping malformed award line %s: %s", line, err) continue } + if len(teamId) > 0 && cur.TeamId != teamId { + continue + } ret = append(ret, cur) } @@ -194,7 +197,7 @@ func (ctx *Instance) AwardPoints(teamId, category string, points int) error { return fmt.Errorf("No registered team with this hash") } - for _, e := range ctx.PointsLog() { + for _, e := range ctx.PointsLog("") { if a.Same(e) { return fmt.Errorf("Points already awarded to this team in this category") } diff --git a/src/maintenance.go b/src/maintenance.go index 87f35dc..0174902 100644 --- a/src/maintenance.go +++ b/src/maintenance.go @@ -28,7 +28,7 @@ func (pm *PuzzleMap) MarshalJSON() ([]byte, error) { func (ctx *Instance) generatePuzzleList() { maxByCategory := map[string]int{} - for _, a := range ctx.PointsLog() { + for _, a := range ctx.PointsLog("") { if a.Points > maxByCategory[a.Category] { maxByCategory[a.Category] = a.Points } @@ -67,13 +67,13 @@ func (ctx *Instance) generatePuzzleList() { ctx.jPuzzleList = jpl } -func (ctx *Instance) generatePointsLog() { +func (ctx *Instance) generatePointsLog(teamId string) []byte { var ret struct { Teams map[string]string `json:"teams"` Points []*Award `json:"points"` } ret.Teams = map[string]string{} - ret.Points = ctx.PointsLog() + ret.Points = ctx.PointsLog(teamId) teamNumbersById := map[string]int{} for nr, a := range ret.Points { @@ -93,9 +93,13 @@ func (ctx *Instance) generatePointsLog() { jpl, err := json.Marshal(ret) if err != nil { log.Printf("Marshalling points.js: %v", err) - return + return nil } - ctx.jPointsLog = jpl + + if len(teamId) == 0 { + ctx.jPointsLog = jpl + } + return jpl } // maintenance runs @@ -224,7 +228,7 @@ func (ctx *Instance) collectPoints() { } duplicate := false - for _, e := range ctx.PointsLog() { + for _, e := range ctx.PointsLog("") { if award.Same(e) { duplicate = true break @@ -290,7 +294,7 @@ func (ctx *Instance) Maintenance(maintenanceInterval time.Duration) { ctx.readTeams() ctx.collectPoints() ctx.generatePuzzleList() - ctx.generatePointsLog() + ctx.generatePointsLog("") } select { case <-ctx.update: