Added id parameter to points.json to return only a team's score

This commit is contained in:
Jack Miner 2019-11-19 19:16:21 -07:00
parent 08f2e95f89
commit 7726444a98
No known key found for this signature in database
GPG Key ID: 8B9B7509AED5001E
3 changed files with 22 additions and 10 deletions

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) {
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) {

View File

@ -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")
}

View File

@ -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: