New rule: (category, points) must be unique across the contest. No more having 2 tokens in a category with the same point value.

This commit is contained in:
Neale Pickett 2018-09-18 00:06:29 +00:00
parent f914f1d65d
commit 5070c70d25
2 changed files with 20 additions and 22 deletions

View File

@ -199,7 +199,7 @@ func (ctx Instance) answerHandler(w http.ResponseWriter, req *http.Request) {
return return
} }
if err := ctx.AwardPointsUniquely(teamid, category, points); err != nil { if err := ctx.AwardPoints(teamid, category, points); err != nil {
respond( respond(
w, req, Error, w, req, Error,
"Error awarding points", "Error awarding points",

View File

@ -114,27 +114,12 @@ func (ctx *Instance) PointsLog() []*Award {
return ret return ret
} }
// awardPoints gives points points to team teamid in category category // awardPoints gives points to teamid in category.
// It first checks to make sure these are not duplicate points.
// This is not a perfect check, you can trigger a race condition here.
// It's just a courtesy to the user.
// The maintenance task makes sure we never have duplicate points in the log.
func (ctx *Instance) AwardPoints(teamid, category string, points int) error { func (ctx *Instance) AwardPoints(teamid, category string, points int) error {
fn := fmt.Sprintf("%s-%s-%d", teamid, category, points)
tmpfn := ctx.StatePath("points.tmp", fn)
newfn := ctx.StatePath("points.new", fn)
contents := fmt.Sprintf("%d %s %s %d\n", time.Now().Unix(), teamid, category, points)
if err := ioutil.WriteFile(tmpfn, []byte(contents), 0644); err != nil {
return err
}
if err := os.Rename(tmpfn, newfn); err != nil {
return err
}
log.Printf("Award %s %s %d", teamid, category, points)
return nil
}
func (ctx *Instance) AwardPointsUniquely(teamid, category string, points int) error {
a := Award{ a := Award{
When: time.Now(), When: time.Now(),
TeamId: teamid, TeamId: teamid,
@ -147,8 +132,21 @@ func (ctx *Instance) AwardPointsUniquely(teamid, category string, points int) er
return fmt.Errorf("Points already awarded to this team in this category") return fmt.Errorf("Points already awarded to this team in this category")
} }
} }
fn := fmt.Sprintf("%s-%s-%d", teamid, category, points)
tmpfn := ctx.StatePath("points.tmp", fn)
newfn := ctx.StatePath("points.new", fn)
return ctx.AwardPoints(teamid, category, points) if err := ioutil.WriteFile(tmpfn, []byte(a.String()), 0644); err != nil {
return err
}
if err := os.Rename(tmpfn, newfn); err != nil {
return err
}
log.Printf("Award %s %s %d", teamid, category, points)
return nil
} }
func (ctx *Instance) OpenCategoryFile(category string, parts ...string) (io.ReadCloser, error) { func (ctx *Instance) OpenCategoryFile(category string, parts ...string) (io.ReadCloser, error) {