Merge pull request #104 from 3ch01c/team_scoreboard

Added id parameter to points.json handler to return only a team's score
This commit is contained in:
int00h5525 2019-11-25 13:15:57 -06:00 committed by GitHub
commit 0b8a855284
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 10 deletions

View File

@ -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). and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased] ## [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 ## [3.4.2] - 2019-11-18
### Fixed ### Fixed

View File

@ -183,9 +183,14 @@ func (ctx *Instance) puzzlesHandler(w http.ResponseWriter, req *http.Request) {
} }
func (ctx *Instance) pointsHandler(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.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
w.Write(ctx.jPointsLog) w.Write(pointsLog)
} }
func (ctx *Instance) contentHandler(w http.ResponseWriter, req *http.Request) { func (ctx *Instance) contentHandler(w http.ResponseWriter, req *http.Request) {

View File

@ -151,7 +151,7 @@ func (ctx *Instance) TooFast(teamId string) bool {
return now.Before(next) return now.Before(next)
} }
func (ctx *Instance) PointsLog() []*Award { func (ctx *Instance) PointsLog(teamId string) []*Award {
var ret []*Award var ret []*Award
fn := ctx.StatePath("points.log") fn := ctx.StatePath("points.log")
@ -170,6 +170,9 @@ func (ctx *Instance) PointsLog() []*Award {
log.Printf("Skipping malformed award line %s: %s", line, err) log.Printf("Skipping malformed award line %s: %s", line, err)
continue continue
} }
if len(teamId) > 0 && cur.TeamId != teamId {
continue
}
ret = append(ret, cur) 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") return fmt.Errorf("No registered team with this hash")
} }
for _, e := range ctx.PointsLog() { for _, e := range ctx.PointsLog("") {
if a.Same(e) { if a.Same(e) {
return fmt.Errorf("Points already awarded to this team in this category") return fmt.Errorf("Points already awarded to this team in this category")
} }

View File

@ -28,7 +28,7 @@ func (pm *PuzzleMap) MarshalJSON() ([]byte, error) {
func (ctx *Instance) generatePuzzleList() { func (ctx *Instance) generatePuzzleList() {
maxByCategory := map[string]int{} maxByCategory := map[string]int{}
for _, a := range ctx.PointsLog() { for _, a := range ctx.PointsLog("") {
if a.Points > maxByCategory[a.Category] { if a.Points > maxByCategory[a.Category] {
maxByCategory[a.Category] = a.Points maxByCategory[a.Category] = a.Points
} }
@ -67,13 +67,13 @@ func (ctx *Instance) generatePuzzleList() {
ctx.jPuzzleList = jpl ctx.jPuzzleList = jpl
} }
func (ctx *Instance) generatePointsLog() { func (ctx *Instance) generatePointsLog(teamId string) []byte {
var ret struct { var ret struct {
Teams map[string]string `json:"teams"` Teams map[string]string `json:"teams"`
Points []*Award `json:"points"` Points []*Award `json:"points"`
} }
ret.Teams = map[string]string{} ret.Teams = map[string]string{}
ret.Points = ctx.PointsLog() ret.Points = ctx.PointsLog(teamId)
teamNumbersById := map[string]int{} teamNumbersById := map[string]int{}
for nr, a := range ret.Points { for nr, a := range ret.Points {
@ -93,9 +93,13 @@ func (ctx *Instance) generatePointsLog() {
jpl, err := json.Marshal(ret) jpl, err := json.Marshal(ret)
if err != nil { if err != nil {
log.Printf("Marshalling points.js: %v", err) log.Printf("Marshalling points.js: %v", err)
return return nil
} }
if len(teamId) == 0 {
ctx.jPointsLog = jpl ctx.jPointsLog = jpl
}
return jpl
} }
// maintenance runs // maintenance runs
@ -224,7 +228,7 @@ func (ctx *Instance) collectPoints() {
} }
duplicate := false duplicate := false
for _, e := range ctx.PointsLog() { for _, e := range ctx.PointsLog("") {
if award.Same(e) { if award.Same(e) {
duplicate = true duplicate = true
break break
@ -290,7 +294,7 @@ func (ctx *Instance) Maintenance(maintenanceInterval time.Duration) {
ctx.readTeams() ctx.readTeams()
ctx.collectPoints() ctx.collectPoints()
ctx.generatePuzzleList() ctx.generatePuzzleList()
ctx.generatePointsLog() ctx.generatePointsLog("")
} }
select { select {
case <-ctx.update: case <-ctx.update: