mirror of https://github.com/dirtbags/moth.git
Added id parameter to points.json to return only a team's score
This commit is contained in:
parent
08f2e95f89
commit
7726444a98
|
@ -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) {
|
||||||
|
|
|
@ -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")
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
}
|
}
|
||||||
ctx.jPointsLog = jpl
|
|
||||||
|
if len(teamId) == 0 {
|
||||||
|
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:
|
||||||
|
|
Loading…
Reference in New Issue