diff --git a/TODO.md b/TODO.md index 81a2aa1..a05e17a 100644 --- a/TODO.md +++ b/TODO.md @@ -1 +1,3 @@ * Figure out how to log JSend short text in addition to HTTP code +* We've got logic in state.go and httpd.go that is neither httpd nor state specific. + Pull this into some other file that means "here are the brains of the server". diff --git a/cmd/mothd/httpd.go b/cmd/mothd/httpd.go index 831905c..51bd254 100644 --- a/cmd/mothd/httpd.go +++ b/cmd/mothd/httpd.go @@ -95,9 +95,28 @@ func (h *HTTPServer) StateHandler(w http.ResponseWriter, req *http.Request) { state.PointsLog = export.PointsLog state.Puzzles = make(map[string][]int) - log.Println("Puzzles") - for category := range h.Puzzles.Inventory() { - log.Println(category) + + //XXX: move to brains.go + for _, category := range h.Puzzles.Inventory() { + maxSolved := 0 + + // XXX: We don't have to iterate the log for every category + for _, a := range export.PointsLog { + if (a.Category == category.Name) && (a.Points > maxSolved) { + maxSolved = a.Points + } + } + + // Append sentry (end of puzzles) + allPuzzles := append(category.Puzzles, 0) + puzzles := make([]int, 0, len(allPuzzles)) + for i, val := range allPuzzles { + puzzles = allPuzzles[:i+1] + if val > maxSolved { + break + } + } + state.Puzzles[category.Name] = puzzles } JSONWrite(w, state) diff --git a/cmd/mothd/mothballs.go b/cmd/mothd/mothballs.go index eb9c3f3..543475c 100644 --- a/cmd/mothd/mothballs.go +++ b/cmd/mothd/mothballs.go @@ -5,6 +5,8 @@ import ( "io" "log" "strings" + "bufio" + "strconv" ) type Mothballs struct { @@ -30,11 +32,26 @@ func (m *Mothballs) Open(cat string, points int, filename string) (io.ReadCloser } func (m *Mothballs) Inventory() []Category { + categories := make([]Category, 0, 20) for cat, zfs := range m.categories { - map, err := zfs.Open("map.txt") - log.Println("mothballs", cat, zfs) + pointsList := make([]int, 0, 20) + pf, err := zfs.Open("puzzles.txt") + if err != nil { + // No puzzles = no category + continue + } + scanner := bufio.NewScanner(pf) + for scanner.Scan() { + line := scanner.Text() + if pointval, err := strconv.Atoi(line); err != nil { + log.Printf("Reading points for %s: %s", cat, err.Error()) + } else { + pointsList = append(pointsList, pointval) + } + } + categories = append(categories, Category{cat, pointsList}) } - return []Category{} + return categories } func (m *Mothballs) Update() { diff --git a/devel/mothballer.py b/devel/mothballer.py index 5ec9df6..ca6b3b1 100755 --- a/devel/mothballer.py +++ b/devel/mothballer.py @@ -71,9 +71,11 @@ def package(categoryname, categorydir, seed): cat = moth.Category(categorydir, seed) answers = {} summary = {} + puzzles = [] for puzzle in cat: logging.info("Processing point value {}".format(puzzle.points)) + puzzles.append(puzzle.points) answers[puzzle.points] = puzzle.answers summary[puzzle.points] = puzzle.summary @@ -85,6 +87,7 @@ def package(categoryname, categorydir, seed): obj = puzzle.package() zf.writestr(os.path.join(puzzledir, 'puzzle.json'), json.dumps(obj)) + zf.writestr("puzzles.txt", "\n".join(str(p) for p in puzzles) + "\n") write_kv_pairs(zf, 'answers.txt', answers) write_kv_pairs(zf, 'summaries.txt', summary)