2019-09-02 18:13:37 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-09-02 19:47:24 -06:00
|
|
|
"bufio"
|
2019-09-02 18:13:37 -06:00
|
|
|
"fmt"
|
2019-12-01 18:58:09 -07:00
|
|
|
"github.com/spf13/afero"
|
2019-09-02 18:13:37 -06:00
|
|
|
"log"
|
2019-09-02 19:47:24 -06:00
|
|
|
"math/rand"
|
2019-09-02 18:13:37 -06:00
|
|
|
"os"
|
2019-12-01 18:58:09 -07:00
|
|
|
"path/filepath"
|
2019-09-02 19:47:24 -06:00
|
|
|
"strings"
|
2019-09-02 18:13:37 -06:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Stuff people with mediocre handwriting could write down unambiguously, and can be entered without holding down shift
|
2020-02-29 22:37:22 -07:00
|
|
|
const DistinguishableChars = "234678abcdefhikmnpqrtwxyz="
|
2019-09-02 18:13:37 -06:00
|
|
|
|
|
|
|
// We use the filesystem for synchronization between threads.
|
|
|
|
// The only thing State methods need to know is the path to the state directory.
|
|
|
|
type State struct {
|
2020-02-29 16:51:32 -07:00
|
|
|
afero.Fs
|
2020-02-29 22:37:22 -07:00
|
|
|
Enabled bool
|
2019-09-02 18:13:37 -06:00
|
|
|
}
|
|
|
|
|
2019-12-01 18:58:09 -07:00
|
|
|
func NewState(fs afero.Fs) *State {
|
2019-09-02 18:13:37 -06:00
|
|
|
return &State{
|
2020-02-29 16:51:32 -07:00
|
|
|
Fs: fs,
|
2020-02-29 22:37:22 -07:00
|
|
|
Enabled: true,
|
2019-09-02 18:13:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check a few things to see if this state directory is "enabled".
|
2019-11-24 15:58:43 -07:00
|
|
|
func (s *State) UpdateEnabled() {
|
2020-02-29 16:51:32 -07:00
|
|
|
if _, err := s.Stat("enabled"); os.IsNotExist(err) {
|
2019-11-24 15:58:43 -07:00
|
|
|
s.Enabled = false
|
2020-02-29 22:37:22 -07:00
|
|
|
log.Println("Suspended: enabled file missing")
|
2019-11-24 15:58:43 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
nextEnabled := true
|
2020-02-29 16:51:32 -07:00
|
|
|
untilFile, err := s.Open("hours")
|
2019-11-24 15:58:43 -07:00
|
|
|
if err != nil {
|
|
|
|
return
|
2019-09-02 18:13:37 -06:00
|
|
|
}
|
2019-11-24 15:58:43 -07:00
|
|
|
defer untilFile.Close()
|
2019-09-02 18:13:37 -06:00
|
|
|
|
2019-11-24 15:58:43 -07:00
|
|
|
scanner := bufio.NewScanner(untilFile)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if len(line) < 1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
thisEnabled := true
|
|
|
|
switch line[0] {
|
|
|
|
case '+':
|
|
|
|
thisEnabled = true
|
|
|
|
line = line[1:]
|
|
|
|
case '-':
|
|
|
|
thisEnabled = false
|
|
|
|
line = line[1:]
|
|
|
|
case '#':
|
|
|
|
continue
|
|
|
|
default:
|
2020-02-29 22:37:22 -07:00
|
|
|
log.Println("Misformatted line in hours file")
|
2019-11-24 15:58:43 -07:00
|
|
|
}
|
|
|
|
line = strings.TrimSpace(line)
|
|
|
|
until, err := time.Parse(time.RFC3339, line)
|
2019-09-02 18:13:37 -06:00
|
|
|
if err != nil {
|
2020-02-29 22:37:22 -07:00
|
|
|
log.Println("Suspended: Unparseable until date:", line)
|
2019-11-24 15:58:43 -07:00
|
|
|
continue
|
2019-09-02 18:13:37 -06:00
|
|
|
}
|
|
|
|
if until.Before(time.Now()) {
|
2019-11-24 15:58:43 -07:00
|
|
|
nextEnabled = thisEnabled
|
2019-09-02 18:13:37 -06:00
|
|
|
}
|
|
|
|
}
|
2019-11-24 15:58:43 -07:00
|
|
|
if nextEnabled != s.Enabled {
|
|
|
|
s.Enabled = nextEnabled
|
|
|
|
log.Println("Setting enabled to", s.Enabled, "based on hours file")
|
|
|
|
}
|
2019-09-02 18:13:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns team name given a team ID.
|
|
|
|
func (s *State) TeamName(teamId string) (string, error) {
|
2020-03-01 17:01:01 -07:00
|
|
|
// XXX: directory traversal
|
2019-12-01 18:58:09 -07:00
|
|
|
teamFile := filepath.Join("teams", teamId)
|
2020-02-29 16:51:32 -07:00
|
|
|
teamNameBytes, err := afero.ReadFile(s, teamFile)
|
2019-09-02 18:13:37 -06:00
|
|
|
teamName := strings.TrimSpace(string(teamNameBytes))
|
|
|
|
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return "", fmt.Errorf("Unregistered team ID: %s", teamId)
|
|
|
|
} else if err != nil {
|
|
|
|
return "", fmt.Errorf("Unregistered team ID: %s (%s)", teamId, err)
|
|
|
|
}
|
2019-09-02 19:47:24 -06:00
|
|
|
|
2019-09-02 18:13:37 -06:00
|
|
|
return teamName, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write out team name. This can only be done once.
|
2020-02-29 22:37:22 -07:00
|
|
|
func (s *State) SetTeamName(teamId, teamName string) error {
|
2020-02-29 16:51:32 -07:00
|
|
|
if f, err := s.Open("teamids.txt"); err != nil {
|
2019-12-01 20:53:13 -07:00
|
|
|
return fmt.Errorf("Team IDs file does not exist")
|
2019-12-01 20:47:46 -07:00
|
|
|
} else {
|
2019-12-01 20:53:13 -07:00
|
|
|
found := false
|
|
|
|
scanner := bufio.NewScanner(f)
|
|
|
|
for scanner.Scan() {
|
|
|
|
if scanner.Text() == teamId {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
if !found {
|
|
|
|
return fmt.Errorf("Team ID not found in list of valid Team IDs")
|
|
|
|
}
|
2019-12-01 20:47:46 -07:00
|
|
|
}
|
|
|
|
|
2019-12-01 18:58:09 -07:00
|
|
|
teamFile := filepath.Join("teams", teamId)
|
2020-02-29 16:51:32 -07:00
|
|
|
err := afero.WriteFile(s, teamFile, []byte(teamName), os.ModeExclusive|0644)
|
2019-12-12 20:02:57 -07:00
|
|
|
if os.IsExist(err) {
|
|
|
|
return fmt.Errorf("Team ID is already registered")
|
|
|
|
}
|
2019-09-02 18:13:37 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve the current points log
|
2019-09-02 19:47:24 -06:00
|
|
|
func (s *State) PointsLog() []*Award {
|
2020-02-29 16:51:32 -07:00
|
|
|
f, err := s.Open("points.log")
|
2019-09-02 18:13:37 -06:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2019-09-02 19:47:24 -06:00
|
|
|
|
2019-09-02 18:13:37 -06:00
|
|
|
pointsLog := make([]*Award, 0, 200)
|
|
|
|
scanner := bufio.NewScanner(f)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
cur, err := ParseAward(line)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Skipping malformed award line %s: %s", line, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
pointsLog = append(pointsLog, cur)
|
|
|
|
}
|
|
|
|
return pointsLog
|
|
|
|
}
|
|
|
|
|
2020-03-01 16:10:55 -07:00
|
|
|
// Retrieve current messages
|
|
|
|
func (s *State) Messages() string {
|
2020-02-29 22:37:22 -07:00
|
|
|
bMessages, _ := afero.ReadFile(s, "messages.html")
|
2020-03-01 16:10:55 -07:00
|
|
|
return string(bMessages)
|
2019-09-02 18:13:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 update task makes sure we never have duplicate points in the log.
|
|
|
|
func (s *State) AwardPoints(teamId, category string, points int) error {
|
|
|
|
a := Award{
|
|
|
|
When: time.Now().Unix(),
|
|
|
|
TeamId: teamId,
|
|
|
|
Category: category,
|
|
|
|
Points: points,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := s.TeamName(teamId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, e := range s.PointsLog() {
|
|
|
|
if a.Same(e) {
|
|
|
|
return fmt.Errorf("Points already awarded to this team in this category")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn := fmt.Sprintf("%s-%s-%d", teamId, category, points)
|
2019-12-01 18:58:09 -07:00
|
|
|
tmpfn := filepath.Join("points.tmp", fn)
|
|
|
|
newfn := filepath.Join("points.new", fn)
|
2019-09-02 18:13:37 -06:00
|
|
|
|
2020-02-29 16:51:32 -07:00
|
|
|
if err := afero.WriteFile(s, tmpfn, []byte(a.String()), 0644); err != nil {
|
2019-09-02 18:13:37 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-29 16:51:32 -07:00
|
|
|
if err := s.Rename(tmpfn, newfn); err != nil {
|
2019-09-02 18:13:37 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-29 16:51:32 -07:00
|
|
|
// XXX: update everything
|
2019-09-02 18:13:37 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// collectPoints gathers up files in points.new/ and appends their contents to points.log,
|
|
|
|
// removing each points.new/ file as it goes.
|
|
|
|
func (s *State) collectPoints() {
|
2020-02-29 16:51:32 -07:00
|
|
|
files, err := afero.ReadDir(s, "points.new")
|
2019-09-02 18:13:37 -06:00
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, f := range files {
|
2019-12-01 18:58:09 -07:00
|
|
|
filename := filepath.Join("points.new", f.Name())
|
2020-02-29 16:51:32 -07:00
|
|
|
awardstr, err := afero.ReadFile(s, filename)
|
2019-09-02 18:13:37 -06:00
|
|
|
if err != nil {
|
|
|
|
log.Print("Opening new points: ", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
award, err := ParseAward(string(awardstr))
|
|
|
|
if err != nil {
|
|
|
|
log.Print("Can't parse award file ", filename, ": ", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
duplicate := false
|
|
|
|
for _, e := range s.PointsLog() {
|
|
|
|
if award.Same(e) {
|
|
|
|
duplicate = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if duplicate {
|
|
|
|
log.Print("Skipping duplicate points: ", award.String())
|
|
|
|
} else {
|
|
|
|
log.Print("Award: ", award.String())
|
|
|
|
|
2020-02-29 16:51:32 -07:00
|
|
|
logf, err := s.OpenFile("points.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
2019-09-02 18:13:37 -06:00
|
|
|
if err != nil {
|
|
|
|
log.Print("Can't append to points log: ", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Fprintln(logf, award.String())
|
|
|
|
logf.Close()
|
|
|
|
}
|
|
|
|
|
2020-02-29 16:51:32 -07:00
|
|
|
if err := s.Remove(filename); err != nil {
|
2019-09-02 18:13:37 -06:00
|
|
|
log.Print("Unable to remove new points file: ", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) maybeInitialize() {
|
|
|
|
// Are we supposed to re-initialize?
|
2020-02-29 16:51:32 -07:00
|
|
|
if _, err := s.Stat("initialized"); !os.IsNotExist(err) {
|
2019-09-02 18:13:37 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-29 22:37:22 -07:00
|
|
|
now := time.Now().UTC().Format(time.RFC3339)
|
2019-09-02 18:13:37 -06:00
|
|
|
log.Print("initialized file missing, re-initializing")
|
|
|
|
|
|
|
|
// Remove any extant control and state files
|
2020-02-29 16:51:32 -07:00
|
|
|
s.Remove("enabled")
|
|
|
|
s.Remove("hours")
|
|
|
|
s.Remove("points.log")
|
2020-02-29 22:37:22 -07:00
|
|
|
s.Remove("messages.html")
|
2020-02-29 16:51:32 -07:00
|
|
|
s.RemoveAll("points.tmp")
|
|
|
|
s.RemoveAll("points.new")
|
|
|
|
s.RemoveAll("teams")
|
2019-09-02 18:13:37 -06:00
|
|
|
|
|
|
|
// Make sure various subdirectories exist
|
2020-02-29 16:51:32 -07:00
|
|
|
s.Mkdir("points.tmp", 0755)
|
|
|
|
s.Mkdir("points.new", 0755)
|
|
|
|
s.Mkdir("teams", 0755)
|
2019-09-02 18:13:37 -06:00
|
|
|
|
|
|
|
// Preseed available team ids if file doesn't exist
|
2020-02-29 16:51:32 -07:00
|
|
|
if f, err := s.OpenFile("teamids.txt", os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644); err == nil {
|
2020-02-29 22:37:22 -07:00
|
|
|
id := make([]byte, 8)
|
2019-12-01 20:47:46 -07:00
|
|
|
for i := 0; i < 100; i += 1 {
|
2020-02-29 22:37:22 -07:00
|
|
|
for i := range id {
|
|
|
|
char := rand.Intn(len(DistinguishableChars))
|
|
|
|
id[i] = DistinguishableChars[char]
|
|
|
|
}
|
|
|
|
fmt.Fprintln(f, string(id))
|
2019-09-02 18:13:37 -06:00
|
|
|
}
|
2020-02-29 22:37:22 -07:00
|
|
|
f.Close()
|
2019-09-02 18:13:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create some files
|
2020-02-29 22:37:22 -07:00
|
|
|
if f, err := s.Create("initialized"); err == nil {
|
|
|
|
fmt.Fprintln(f, "initialized: remove to re-initialize the contest.")
|
|
|
|
fmt.Fprintln(f)
|
|
|
|
fmt.Fprintln(f, "This instance was initaliazed at", now)
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
if f, err := s.Create("enabled"); err == nil {
|
|
|
|
fmt.Fprintln(f, "enabled: remove or rename to suspend the contest.")
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
if f, err := s.Create("hours"); err == nil {
|
|
|
|
fmt.Fprintln(f, "# hours: when the contest is enabled")
|
|
|
|
fmt.Fprintln(f, "#")
|
|
|
|
fmt.Fprintln(f, "# Enable: + timestamp")
|
|
|
|
fmt.Fprintln(f, "# Disable: - timestamp")
|
|
|
|
fmt.Fprintln(f, "#")
|
|
|
|
fmt.Fprintln(f, "# You can have multiple start/stop times.")
|
|
|
|
fmt.Fprintln(f, "# Whatever time is the most recent, wins.")
|
|
|
|
fmt.Fprintln(f, "# Times in the future are ignored.")
|
|
|
|
fmt.Fprintln(f)
|
|
|
|
fmt.Fprintln(f, "+", now)
|
|
|
|
fmt.Fprintln(f, "- 3019-10-31T00:00:00Z")
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
if f, err := s.Create("messages.html"); err == nil {
|
|
|
|
fmt.Fprintln(f, "<!-- messages.html: put client broadcast messages here. -->")
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
if f, err := s.Create("points.log"); err == nil {
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
|
2019-09-02 18:13:37 -06:00
|
|
|
}
|
|
|
|
|
2020-02-29 16:51:32 -07:00
|
|
|
func (s *State) Update() {
|
2019-12-01 18:58:09 -07:00
|
|
|
s.maybeInitialize()
|
|
|
|
s.UpdateEnabled()
|
|
|
|
if s.Enabled {
|
|
|
|
s.collectPoints()
|
|
|
|
}
|
|
|
|
}
|