mirror of https://github.com/dirtbags/moth.git
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:
commit
0b8a855284
|
@ -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
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
|
|
@ -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,10 +93,14 @@ func (ctx *Instance) generatePointsLog() {
|
|||
jpl, err := json.Marshal(ret)
|
||||
if err != nil {
|
||||
log.Printf("Marshalling points.js: %v", err)
|
||||
return
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(teamId) == 0 {
|
||||
ctx.jPointsLog = jpl
|
||||
}
|
||||
return jpl
|
||||
}
|
||||
|
||||
// maintenance runs
|
||||
func (ctx *Instance) tidy() {
|
||||
|
@ -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:
|
||||
|
|
Loading…
Reference in New Issue