From a9f8cba6f91373ce62c8ce59304ec3328034265f Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Wed, 19 Sep 2018 03:29:05 +0000 Subject: [PATCH] Everything but puzzle content serving --- res/basic.css | 4 ++++ res/scoreboard.html | 3 ++- src/award.go | 2 +- src/handlers.go | 32 ++++++++++++++++++++++++-------- src/instance.go | 6 ++++++ src/maintenance.go | 13 ++++++++++--- src/static.go | 8 +++----- 7 files changed, 50 insertions(+), 18 deletions(-) diff --git a/res/basic.css b/res/basic.css index e133165..a8a856e 100644 --- a/res/basic.css +++ b/res/basic.css @@ -32,6 +32,10 @@ input { padding: 0.6em; margin: 0.2em; } +#scoreboard .category { + border: solid white 1px; + display: inline-block; +} nav { border: solid black 2px; } diff --git a/res/scoreboard.html b/res/scoreboard.html index 9d2d513..e16abf6 100644 --- a/res/scoreboard.html +++ b/res/scoreboard.html @@ -1,7 +1,7 @@ - Open Puzzles + Scoreboard @@ -97,6 +97,7 @@ function scoreboard(element, continuous) { var width = maxWidth * catPct; var bar = document.createElement("span"); + bar.classList.add("category"); bar.classList.add("cat" + ncat); bar.style.width = width + "%"; bar.textContent = category + ": " + catTeam; diff --git a/src/award.go b/src/award.go index 759393b..4a8ba75 100644 --- a/src/award.go +++ b/src/award.go @@ -17,7 +17,7 @@ type Award struct { func ParseAward(s string) (*Award, error) { ret := Award{} - s = strings.Trim(s, " \t\n") + s = strings.TrimSpace(s) var whenEpoch int64 diff --git a/src/handlers.go b/src/handlers.go index 527d4d7..cdd6254 100644 --- a/src/handlers.go +++ b/src/handlers.go @@ -5,11 +5,9 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "log" "net/http" "os" - "rand" "strconv" "strings" ) @@ -287,19 +285,37 @@ func (ctx Instance) puzzlesHandler(w http.ResponseWriter, req *http.Request) { } func (ctx Instance) pointsHandler(w http.ResponseWriter, req *http.Request) { - plog := ctx.PointsLog() - jlog, err := json.Marshal(plog) + var ret struct { + Teams map[string]string `json:"teams"` + Points []*Award `json:"points"` + } + ret.Teams = map[string]string{} + ret.Points = ctx.PointsLog() + + teamNumbersById := map[string]int{} + for nr, a := range ret.Points { + teamNumber, ok := teamNumbersById[a.TeamId] + if !ok { + teamName, err := ctx.TeamName(a.TeamId) + if err != nil { + teamName = "[unregistered]" + } + teamNumber = nr + teamNumbersById[a.TeamId] = teamNumber + ret.Teams[strconv.FormatInt(int64(teamNumber), 16)] = teamName + } + a.TeamId = strconv.FormatInt(int64(teamNumber), 16) + } + + jret, err := json.Marshal(ret) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } - // XXX: go through plog, building an array of teams, so we can anonymize team IDs - w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) - - w.Write(jlog) + w.Write(jret) } func (ctx Instance) staticHandler(w http.ResponseWriter, req *http.Request) { diff --git a/src/instance.go b/src/instance.go index 0ecdbcf..bad4112 100644 --- a/src/instance.go +++ b/src/instance.go @@ -159,3 +159,9 @@ func (ctx *Instance) OpenCategoryFile(category string, parts ...string) (io.Read f, err := mb.Open(filename) return f, err } + +func (ctx *Instance) TeamName(teamId string) (string, error) { + teamNameBytes, err := ioutil.ReadFile(ctx.StatePath("teams", teamId)) + teamName := strings.TrimSpace(string(teamNameBytes)) + return teamName, err +} diff --git a/src/maintenance.go b/src/maintenance.go index 518c92d..501e140 100644 --- a/src/maintenance.go +++ b/src/maintenance.go @@ -34,6 +34,16 @@ func (ctx *Instance) Tidy() { } } + // Refresh all current categories + for categoryName, mb := range ctx.Categories { + if err := mb.Refresh(); err != nil { + // Backing file vanished: remove this category + log.Printf("Removing category: %s: %s", categoryName, err) + mb.Close() + delete(ctx.Categories, categoryName) + } + } + // Any new categories? files, err := ioutil.ReadDir(ctx.MothballPath()) if err != nil { @@ -58,9 +68,6 @@ func (ctx *Instance) Tidy() { } } - // Any old categories? - log.Print("XXX: Check for and reap old categories") - ctx.CollectPoints() } diff --git a/src/static.go b/src/static.go index 4176ef4..64d4031 100644 --- a/src/static.go +++ b/src/static.go @@ -60,10 +60,10 @@ func ShowHtml(w http.ResponseWriter, status Status, title string, body string) { fmt.Fprintf(w, "") fmt.Fprintf(w, "%s", title) fmt.Fprintf(w, "") - fmt.Fprintf(w, "") + fmt.Fprintf(w, "") fmt.Fprintf(w, "") fmt.Fprintf(w, "") - fmt.Fprintf(w, "

%s

", statusStr, title) + fmt.Fprintf(w, "

%s

", statusStr, title) fmt.Fprintf(w, "
%s
", body) fmt.Fprintf(w, "