Fixing issue where still-locked puzzle content could be downloaded

This commit is contained in:
John Donaldson 2019-11-07 19:22:15 +00:00
parent 0d4b1e9e19
commit 3e079616c1
4 changed files with 64 additions and 35 deletions

View File

@ -287,11 +287,19 @@ func (ctx *Instance) manifestHandler(w http.ResponseWriter, req *http.Request) {
// Package up files for currently-unlocked puzzles in categories // Package up files for currently-unlocked puzzles in categories
for category_name, category := range ctx.categories { for category_name, category := range ctx.categories {
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 { for _, file := range category.zf.File {
parts := strings.Split(file.Name, "/") parts := strings.Split(file.Name, "/")
if (parts[0] == "content") { 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:]...))) manifest = append(manifest, path.Join("content", category_name, path.Join(parts[1:]...)))
break
}
}
}
} }
} }
} }

View File

@ -29,6 +29,7 @@ type Instance struct {
Runtime RuntimeConfig Runtime RuntimeConfig
categories map[string]*Mothball categories map[string]*Mothball
MaxPointsUnlocked map[string]int
update chan bool update chan bool
jPuzzleList []byte jPuzzleList []byte
jPointsLog []byte jPointsLog []byte

View File

@ -12,10 +12,6 @@ import (
"time" "time"
) )
type PuzzleMap struct {
Points int
Path string
}
func (pm *PuzzleMap) MarshalJSON() ([]byte, error) { func (pm *PuzzleMap) MarshalJSON() ([]byte, error) {
if pm == nil { if pm == nil {
@ -39,47 +35,32 @@ func (ctx *Instance) generatePuzzleList() {
} }
} }
ret := map[string][]PuzzleMap{} ret := map[string][]PuzzleMap{}
for catName, mb := range ctx.categories { for catName, mb := range ctx.categories {
mf, err := mb.Open("map.txt") filtered_puzzlemap := make([]PuzzleMap, 0, 30)
if err != nil {
// File isn't in there
continue
}
defer mf.Close()
pm := make([]PuzzleMap, 0, 30)
completed := true completed := true
scanner := bufio.NewScanner(mf)
for scanner.Scan() {
line := scanner.Text()
var pointval int for _, pm := range mb.puzzlemap {
var dir string filtered_puzzlemap = append(filtered_puzzlemap, pm)
n, err := fmt.Sscanf(line, "%d %s", &pointval, &dir) if pm.Points > maxByCategory[catName] {
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] {
completed = false completed = false
maxByCategory[catName] = pm.Points
break break
} }
} }
if completed { 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) jpl, err := json.Marshal(ret)
if err != nil { if err != nil {
log.Printf("Marshalling puzzles.js: %v", err) log.Printf("Marshalling puzzles.js: %v", err)

View File

@ -2,17 +2,26 @@ package main
import ( import (
"archive/zip" "archive/zip"
"bufio"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"log"
"os" "os"
"strings" "strings"
"time" "time"
) )
type PuzzleMap struct {
Points int
Path string
}
type Mothball struct { type Mothball struct {
zf *zip.ReadCloser zf *zip.ReadCloser
filename string filename string
puzzlemap []PuzzleMap
mtime time.Time mtime time.Time
} }
@ -150,6 +159,36 @@ func (m *Mothball) Refresh() error {
m.zf = zf m.zf = zf
m.mtime = mtime 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 return nil
} }