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
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
}
}
}
}
}
}

View File

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

View File

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

View File

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