2018-09-14 18:24:48 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
2018-09-17 17:40:05 -06:00
|
|
|
"io"
|
2018-09-14 18:24:48 -06:00
|
|
|
"io/ioutil"
|
2018-09-17 17:00:08 -06:00
|
|
|
"log"
|
2019-02-25 09:07:53 -07:00
|
|
|
"math/rand"
|
2019-02-21 16:00:06 -07:00
|
|
|
"net/http"
|
2018-09-17 17:00:08 -06:00
|
|
|
"os"
|
2018-09-14 18:24:48 -06:00
|
|
|
"path"
|
|
|
|
"strings"
|
2019-04-29 21:31:16 -06:00
|
|
|
"sync"
|
2018-09-17 17:00:08 -06:00
|
|
|
"time"
|
2018-09-14 18:24:48 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type Instance struct {
|
2019-03-07 20:03:48 -07:00
|
|
|
Base string
|
|
|
|
MothballDir string
|
|
|
|
StateDir string
|
|
|
|
ThemeDir string
|
|
|
|
AttemptInterval time.Duration
|
|
|
|
|
2019-08-15 19:46:49 -06:00
|
|
|
categories map[string]*Zipfs
|
2019-04-29 21:31:16 -06:00
|
|
|
update chan bool
|
|
|
|
jPuzzleList []byte
|
|
|
|
jPointsLog []byte
|
|
|
|
nextAttempt map[string]time.Time
|
|
|
|
nextAttemptMutex *sync.RWMutex
|
|
|
|
mux *http.ServeMux
|
2018-09-14 18:24:48 -06:00
|
|
|
}
|
|
|
|
|
2019-03-07 20:03:48 -07:00
|
|
|
func (ctx *Instance) Initialize() error {
|
2018-09-14 18:24:48 -06:00
|
|
|
// Roll over and die if directories aren't even set up
|
2019-03-07 20:03:48 -07:00
|
|
|
if _, err := os.Stat(ctx.MothballDir); err != nil {
|
|
|
|
return err
|
2018-09-14 18:24:48 -06:00
|
|
|
}
|
2019-03-07 20:03:48 -07:00
|
|
|
if _, err := os.Stat(ctx.StateDir); err != nil {
|
|
|
|
return err
|
2018-09-14 18:24:48 -06:00
|
|
|
}
|
2019-02-25 09:07:53 -07:00
|
|
|
|
2019-03-07 20:03:48 -07:00
|
|
|
ctx.Base = strings.TrimRight(ctx.Base, "/")
|
2019-08-15 19:46:49 -06:00
|
|
|
ctx.categories = map[string]*Zipfs{}
|
2019-03-07 20:03:48 -07:00
|
|
|
ctx.update = make(chan bool, 10)
|
|
|
|
ctx.nextAttempt = map[string]time.Time{}
|
2019-04-29 21:31:16 -06:00
|
|
|
ctx.nextAttemptMutex = new(sync.RWMutex)
|
2019-03-07 20:03:48 -07:00
|
|
|
ctx.mux = http.NewServeMux()
|
|
|
|
|
2019-02-21 16:00:06 -07:00
|
|
|
ctx.BindHandlers()
|
2018-09-17 17:00:08 -06:00
|
|
|
ctx.MaybeInitialize()
|
|
|
|
|
2019-03-07 20:03:48 -07:00
|
|
|
return nil
|
2018-09-14 18:24:48 -06:00
|
|
|
}
|
|
|
|
|
2019-02-21 19:38:53 -07:00
|
|
|
// Stuff people with mediocre handwriting could write down unambiguously, and can be entered without holding down shift
|
2019-02-21 19:40:48 -07:00
|
|
|
const distinguishableChars = "234678abcdefhijkmnpqrtwxyz="
|
2019-02-21 19:38:53 -07:00
|
|
|
|
|
|
|
func mktoken() string {
|
|
|
|
a := make([]byte, 8)
|
2019-02-25 09:07:53 -07:00
|
|
|
for i := range a {
|
2019-02-21 19:38:53 -07:00
|
|
|
char := rand.Intn(len(distinguishableChars))
|
|
|
|
a[i] = distinguishableChars[char]
|
|
|
|
}
|
|
|
|
return string(a)
|
|
|
|
}
|
|
|
|
|
2018-09-17 17:00:08 -06:00
|
|
|
func (ctx *Instance) MaybeInitialize() {
|
|
|
|
// Only do this if it hasn't already been done
|
|
|
|
if _, err := os.Stat(ctx.StatePath("initialized")); err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Print("initialized file missing, re-initializing")
|
|
|
|
|
|
|
|
// Remove any extant control and state files
|
|
|
|
os.Remove(ctx.StatePath("until"))
|
|
|
|
os.Remove(ctx.StatePath("disabled"))
|
|
|
|
os.Remove(ctx.StatePath("points.log"))
|
|
|
|
os.RemoveAll(ctx.StatePath("points.tmp"))
|
|
|
|
os.RemoveAll(ctx.StatePath("points.new"))
|
|
|
|
os.RemoveAll(ctx.StatePath("teams"))
|
|
|
|
|
|
|
|
// Make sure various subdirectories exist
|
2018-09-14 18:24:48 -06:00
|
|
|
os.Mkdir(ctx.StatePath("points.tmp"), 0755)
|
|
|
|
os.Mkdir(ctx.StatePath("points.new"), 0755)
|
2018-09-17 17:00:08 -06:00
|
|
|
os.Mkdir(ctx.StatePath("teams"), 0755)
|
2018-09-14 18:24:48 -06:00
|
|
|
|
|
|
|
// Preseed available team ids if file doesn't exist
|
2018-09-17 17:00:08 -06:00
|
|
|
if f, err := os.OpenFile(ctx.StatePath("teamids.txt"), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644); err == nil {
|
2018-09-14 18:24:48 -06:00
|
|
|
defer f.Close()
|
2019-02-21 19:38:53 -07:00
|
|
|
for i := 0; i <= 100; i += 1 {
|
|
|
|
fmt.Fprintln(f, mktoken())
|
2018-09-14 18:24:48 -06:00
|
|
|
}
|
|
|
|
}
|
2018-09-17 17:00:08 -06:00
|
|
|
|
|
|
|
// Create initialized file that signals whether we're set up
|
|
|
|
f, err := os.OpenFile(ctx.StatePath("initialized"), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
2018-09-14 18:24:48 -06:00
|
|
|
}
|
2018-09-17 17:00:08 -06:00
|
|
|
defer f.Close()
|
|
|
|
fmt.Fprintln(f, "Remove this file to reinitialize the contest")
|
2018-09-14 18:24:48 -06:00
|
|
|
}
|
|
|
|
|
2019-02-21 22:08:21 -07:00
|
|
|
func pathCleanse(parts []string) string {
|
|
|
|
clean := make([]string, len(parts))
|
|
|
|
for i := range parts {
|
|
|
|
part := parts[i]
|
|
|
|
part = strings.TrimLeft(part, ".")
|
|
|
|
if p := strings.LastIndex(part, "/"); p >= 0 {
|
|
|
|
part = part[p+1:]
|
|
|
|
}
|
|
|
|
clean[i] = part
|
|
|
|
}
|
|
|
|
return path.Join(clean...)
|
|
|
|
}
|
|
|
|
|
2018-09-14 18:24:48 -06:00
|
|
|
func (ctx Instance) MothballPath(parts ...string) string {
|
2019-02-21 22:08:21 -07:00
|
|
|
tail := pathCleanse(parts)
|
2018-09-14 18:24:48 -06:00
|
|
|
return path.Join(ctx.MothballDir, tail)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *Instance) StatePath(parts ...string) string {
|
2019-02-21 22:08:21 -07:00
|
|
|
tail := pathCleanse(parts)
|
2018-09-14 18:24:48 -06:00
|
|
|
return path.Join(ctx.StateDir, tail)
|
|
|
|
}
|
|
|
|
|
2019-03-07 20:03:48 -07:00
|
|
|
func (ctx *Instance) ThemePath(parts ...string) string {
|
2019-02-21 22:08:21 -07:00
|
|
|
tail := pathCleanse(parts)
|
2019-03-07 20:03:48 -07:00
|
|
|
return path.Join(ctx.ThemeDir, tail)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *Instance) TooFast(teamId string) bool {
|
|
|
|
now := time.Now()
|
2019-04-29 21:31:16 -06:00
|
|
|
|
|
|
|
ctx.nextAttemptMutex.RLock()
|
2019-03-07 20:03:48 -07:00
|
|
|
next, _ := ctx.nextAttempt[teamId]
|
2019-04-29 21:31:16 -06:00
|
|
|
ctx.nextAttemptMutex.RUnlock()
|
|
|
|
|
|
|
|
ctx.nextAttemptMutex.Lock()
|
2019-03-07 20:03:48 -07:00
|
|
|
ctx.nextAttempt[teamId] = now.Add(ctx.AttemptInterval)
|
2019-04-29 21:31:16 -06:00
|
|
|
ctx.nextAttemptMutex.Unlock()
|
|
|
|
|
2019-03-07 20:03:48 -07:00
|
|
|
return now.Before(next)
|
2018-09-27 17:12:29 -06:00
|
|
|
}
|
|
|
|
|
2018-09-17 18:02:44 -06:00
|
|
|
func (ctx *Instance) PointsLog() []*Award {
|
|
|
|
var ret []*Award
|
2018-09-14 18:24:48 -06:00
|
|
|
|
|
|
|
fn := ctx.StatePath("points.log")
|
|
|
|
f, err := os.Open(fn)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Unable to open %s: %s", fn, err)
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2018-09-17 17:00:08 -06:00
|
|
|
|
2018-09-14 18:24:48 -06:00
|
|
|
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
|
|
|
|
}
|
2018-09-17 18:02:44 -06:00
|
|
|
ret = append(ret, cur)
|
2018-09-14 18:24:48 -06:00
|
|
|
}
|
2018-09-17 17:00:08 -06:00
|
|
|
|
2018-09-14 18:24:48 -06:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2019-03-07 20:03:48 -07:00
|
|
|
// AwardPoints gives points to teamId in category.
|
2018-09-17 18:06:29 -06:00
|
|
|
// 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.
|
2019-03-07 20:03:48 -07:00
|
|
|
func (ctx *Instance) AwardPoints(teamId, category string, points int) error {
|
2018-09-17 18:02:44 -06:00
|
|
|
a := Award{
|
2018-09-17 21:32:24 -06:00
|
|
|
When: time.Now(),
|
2019-03-07 20:03:48 -07:00
|
|
|
TeamId: teamId,
|
2018-09-17 18:02:44 -06:00
|
|
|
Category: category,
|
2018-09-17 21:32:24 -06:00
|
|
|
Points: points,
|
2018-09-17 18:02:44 -06:00
|
|
|
}
|
|
|
|
|
2019-03-07 20:03:48 -07:00
|
|
|
_, err := ctx.TeamName(teamId)
|
2018-09-20 14:42:24 -06:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("No registered team with this hash")
|
|
|
|
}
|
|
|
|
|
2018-09-17 18:02:44 -06:00
|
|
|
for _, e := range ctx.PointsLog() {
|
|
|
|
if a.Same(e) {
|
|
|
|
return fmt.Errorf("Points already awarded to this team in this category")
|
|
|
|
}
|
|
|
|
}
|
2018-09-17 21:32:24 -06:00
|
|
|
|
2019-03-07 20:03:48 -07:00
|
|
|
fn := fmt.Sprintf("%s-%s-%d", teamId, category, points)
|
2018-09-17 18:06:29 -06:00
|
|
|
tmpfn := ctx.StatePath("points.tmp", fn)
|
|
|
|
newfn := ctx.StatePath("points.new", fn)
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile(tmpfn, []byte(a.String()), 0644); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-09-17 18:02:44 -06:00
|
|
|
|
2018-09-17 18:06:29 -06:00
|
|
|
if err := os.Rename(tmpfn, newfn); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-19 17:56:26 -06:00
|
|
|
ctx.update <- true
|
2019-03-07 20:03:48 -07:00
|
|
|
log.Printf("Award %s %s %d", teamId, category, points)
|
2018-09-17 18:06:29 -06:00
|
|
|
return nil
|
2018-09-17 18:02:44 -06:00
|
|
|
}
|
|
|
|
|
2018-09-17 17:40:05 -06:00
|
|
|
func (ctx *Instance) OpenCategoryFile(category string, parts ...string) (io.ReadCloser, error) {
|
2019-03-07 20:03:48 -07:00
|
|
|
mb, ok := ctx.categories[category]
|
2018-09-17 17:40:05 -06:00
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("No such category: %s", category)
|
|
|
|
}
|
|
|
|
|
|
|
|
filename := path.Join(parts...)
|
|
|
|
f, err := mb.Open(filename)
|
|
|
|
return f, err
|
|
|
|
}
|
2018-09-18 21:29:05 -06:00
|
|
|
|
2019-03-07 20:03:48 -07:00
|
|
|
func (ctx *Instance) ValidTeamId(teamId string) bool {
|
2019-04-29 21:31:16 -06:00
|
|
|
ctx.nextAttemptMutex.RLock()
|
2019-03-07 20:03:48 -07:00
|
|
|
_, ok := ctx.nextAttempt[teamId]
|
2019-04-29 21:31:16 -06:00
|
|
|
ctx.nextAttemptMutex.RUnlock()
|
|
|
|
|
2019-03-07 20:03:48 -07:00
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2018-09-18 21:29:05 -06:00
|
|
|
func (ctx *Instance) TeamName(teamId string) (string, error) {
|
|
|
|
teamNameBytes, err := ioutil.ReadFile(ctx.StatePath("teams", teamId))
|
|
|
|
teamName := strings.TrimSpace(string(teamNameBytes))
|
|
|
|
return teamName, err
|
|
|
|
}
|