mirror of https://github.com/dirtbags/moth.git
Puzzles list appears to be working
This commit is contained in:
parent
81fae86b49
commit
7a5b2aa3c1
2
TODO.md
2
TODO.md
|
@ -1 +1,3 @@
|
||||||
* Figure out how to log JSend short text in addition to HTTP code
|
* 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".
|
||||||
|
|
|
@ -95,9 +95,28 @@ func (h *HTTPServer) StateHandler(w http.ResponseWriter, req *http.Request) {
|
||||||
state.PointsLog = export.PointsLog
|
state.PointsLog = export.PointsLog
|
||||||
|
|
||||||
state.Puzzles = make(map[string][]int)
|
state.Puzzles = make(map[string][]int)
|
||||||
log.Println("Puzzles")
|
|
||||||
for category := range h.Puzzles.Inventory() {
|
//XXX: move to brains.go
|
||||||
log.Println(category)
|
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)
|
JSONWrite(w, state)
|
||||||
|
|
|
@ -5,6 +5,8 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
|
"bufio"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Mothballs struct {
|
type Mothballs struct {
|
||||||
|
@ -30,11 +32,26 @@ func (m *Mothballs) Open(cat string, points int, filename string) (io.ReadCloser
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Mothballs) Inventory() []Category {
|
func (m *Mothballs) Inventory() []Category {
|
||||||
|
categories := make([]Category, 0, 20)
|
||||||
for cat, zfs := range m.categories {
|
for cat, zfs := range m.categories {
|
||||||
map, err := zfs.Open("map.txt")
|
pointsList := make([]int, 0, 20)
|
||||||
log.Println("mothballs", cat, zfs)
|
pf, err := zfs.Open("puzzles.txt")
|
||||||
|
if err != nil {
|
||||||
|
// No puzzles = no category
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
return []Category{}
|
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 categories
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Mothballs) Update() {
|
func (m *Mothballs) Update() {
|
||||||
|
|
|
@ -71,9 +71,11 @@ def package(categoryname, categorydir, seed):
|
||||||
cat = moth.Category(categorydir, seed)
|
cat = moth.Category(categorydir, seed)
|
||||||
answers = {}
|
answers = {}
|
||||||
summary = {}
|
summary = {}
|
||||||
|
puzzles = []
|
||||||
for puzzle in cat:
|
for puzzle in cat:
|
||||||
logging.info("Processing point value {}".format(puzzle.points))
|
logging.info("Processing point value {}".format(puzzle.points))
|
||||||
|
|
||||||
|
puzzles.append(puzzle.points)
|
||||||
answers[puzzle.points] = puzzle.answers
|
answers[puzzle.points] = puzzle.answers
|
||||||
summary[puzzle.points] = puzzle.summary
|
summary[puzzle.points] = puzzle.summary
|
||||||
|
|
||||||
|
@ -85,6 +87,7 @@ def package(categoryname, categorydir, seed):
|
||||||
obj = puzzle.package()
|
obj = puzzle.package()
|
||||||
zf.writestr(os.path.join(puzzledir, 'puzzle.json'), json.dumps(obj))
|
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, 'answers.txt', answers)
|
||||||
write_kv_pairs(zf, 'summaries.txt', summary)
|
write_kv_pairs(zf, 'summaries.txt', summary)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue