diff --git a/src/handlers.go b/src/handlers.go index 3a513b4..69d0729 100644 --- a/src/handlers.go +++ b/src/handlers.go @@ -287,11 +287,19 @@ func (ctx *Instance) manifestHandler(w http.ResponseWriter, req *http.Request) { // Package up files for currently-unlocked puzzles in categories for category_name, category := range ctx.categories { - for _, file := range category.zf.File { - parts := strings.Split(file.Name, "/") + if _, ok := ctx.MaxPointsUnlocked[category_name]; ok { // Check that the category is actually unlocked. This should never fail, probably + for _, file := range category.zf.File { + parts := strings.Split(file.Name, "/") - if (parts[0] == "content") { - manifest = append(manifest, path.Join("content", category_name, path.Join(parts[1:]...))) + if (parts[0] == "content") { // Only pick up content files, not thing like map.txt + for _, puzzlemap := range category.puzzlemap { // Figure out which puzzles are currently unlocked + if (puzzlemap.Path == parts[1] && puzzlemap.Points <= ctx.MaxPointsUnlocked[category_name]) { + + manifest = append(manifest, path.Join("content", category_name, path.Join(parts[1:]...))) + break + } + } + } } } } diff --git a/src/instance.go b/src/instance.go index fb274e3..fc7112b 100644 --- a/src/instance.go +++ b/src/instance.go @@ -29,6 +29,7 @@ type Instance struct { Runtime RuntimeConfig categories map[string]*Mothball + MaxPointsUnlocked map[string]int update chan bool jPuzzleList []byte jPointsLog []byte diff --git a/src/maintenance.go b/src/maintenance.go index 6144906..c5b64bb 100644 --- a/src/maintenance.go +++ b/src/maintenance.go @@ -12,10 +12,6 @@ import ( "time" ) -type PuzzleMap struct { - Points int - Path string -} func (pm *PuzzleMap) MarshalJSON() ([]byte, error) { if pm == nil { @@ -39,47 +35,32 @@ func (ctx *Instance) generatePuzzleList() { } } + ret := map[string][]PuzzleMap{} for catName, mb := range ctx.categories { - mf, err := mb.Open("map.txt") - if err != nil { - // File isn't in there - continue - } - defer mf.Close() - - pm := make([]PuzzleMap, 0, 30) + filtered_puzzlemap := make([]PuzzleMap, 0, 30) completed := true - scanner := bufio.NewScanner(mf) - for scanner.Scan() { - line := scanner.Text() - var pointval int - var dir string + for _, pm := range mb.puzzlemap { + filtered_puzzlemap = append(filtered_puzzlemap, pm) - n, err := fmt.Sscanf(line, "%d %s", &pointval, &dir) - if err != nil { - log.Printf("Parsing map for %s: %v", catName, err) - continue - } else if n != 2 { - log.Printf("Parsing map for %s: short read", catName) - continue - } - - pm = append(pm, PuzzleMap{pointval, dir}) - - if pointval > maxByCategory[catName] { + if pm.Points > maxByCategory[catName] { completed = false + maxByCategory[catName] = pm.Points break } } + if completed { - pm = append(pm, PuzzleMap{0, ""}) + filtered_puzzlemap = append(filtered_puzzlemap, PuzzleMap{0, ""}) } - ret[catName] = pm + ret[catName] = filtered_puzzlemap } + // Cache the unlocked points for use in other functions + ctx.MaxPointsUnlocked = maxByCategory + jpl, err := json.Marshal(ret) if err != nil { log.Printf("Marshalling puzzles.js: %v", err) diff --git a/src/mothball.go b/src/mothball.go index 149dbf5..42ba7f9 100644 --- a/src/mothball.go +++ b/src/mothball.go @@ -2,17 +2,26 @@ package main import ( "archive/zip" + "bufio" "fmt" "io" "io/ioutil" + "log" "os" "strings" "time" ) +type PuzzleMap struct { + Points int + Path string +} + + type Mothball struct { zf *zip.ReadCloser filename string + puzzlemap []PuzzleMap mtime time.Time } @@ -150,6 +159,36 @@ func (m *Mothball) Refresh() error { m.zf = zf m.mtime = mtime + mf, err := m.Open("map.txt") + if err != nil { + // File isn't in there + } else { + defer mf.Close() + + pm := make([]PuzzleMap, 0, 30) + scanner := bufio.NewScanner(mf) + + for scanner.Scan() { + line := scanner.Text() + + var pointval int + var dir string + + n, err := fmt.Sscanf(line, "%d %s", &pointval, &dir) + if err != nil { + log.Printf("Parsing map for %v", err) + } else if n != 2 { + log.Printf("Parsing map: short read") + } + + pm = append(pm, PuzzleMap{pointval, dir}) + + } + + m.puzzlemap = pm + } + + return nil }