Puzzles list appears to be working

This commit is contained in:
Neale Pickett 2020-03-01 14:27:49 -06:00
parent 81fae86b49
commit 7a5b2aa3c1
4 changed files with 47 additions and 6 deletions

View File

@ -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".

View File

@ -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)

View File

@ -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() {

View File

@ -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)