2020-09-08 17:49:02 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/dirtbags/moth/pkg/transpile"
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewTranspilerProvider returns a new TranspilerProvider.
|
|
|
|
func NewTranspilerProvider(fs afero.Fs) TranspilerProvider {
|
|
|
|
return TranspilerProvider{fs}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TranspilerProvider provides puzzles generated from source files on disk
|
|
|
|
type TranspilerProvider struct {
|
|
|
|
fs afero.Fs
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inventory returns a Category list for this provider.
|
|
|
|
func (p TranspilerProvider) Inventory() []Category {
|
|
|
|
ret := make([]Category, 0)
|
|
|
|
inv, err := transpile.FsInventory(p.fs)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
for name, points := range inv {
|
|
|
|
ret = append(ret, Category{name, points})
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
type nopCloser struct {
|
|
|
|
io.ReadSeeker
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c nopCloser) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open returns a file associated with the given category and point value.
|
|
|
|
func (p TranspilerProvider) Open(cat string, points int, filename string) (ReadSeekCloser, time.Time, error) {
|
|
|
|
c := transpile.NewFsCategory(p.fs, cat)
|
|
|
|
switch filename {
|
|
|
|
case "", "puzzle.json":
|
|
|
|
p, err := c.Puzzle(points)
|
|
|
|
if err != nil {
|
|
|
|
return nopCloser{new(bytes.Reader)}, time.Time{}, err
|
|
|
|
}
|
|
|
|
jp, err := json.Marshal(p)
|
|
|
|
if err != nil {
|
|
|
|
return nopCloser{new(bytes.Reader)}, time.Time{}, err
|
|
|
|
}
|
|
|
|
return nopCloser{bytes.NewReader(jp)}, time.Now(), nil
|
|
|
|
default:
|
|
|
|
r, err := c.Open(points, filename)
|
|
|
|
return r, time.Now(), err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckAnswer checks whether an answer si correct.
|
|
|
|
func (p TranspilerProvider) CheckAnswer(cat string, points int, answer string) (bool, error) {
|
|
|
|
c := transpile.NewFsCategory(p.fs, cat)
|
|
|
|
return c.Answer(points, answer), nil
|
|
|
|
}
|
|
|
|
|
2020-09-15 15:58:21 -06:00
|
|
|
// Mothball packages up a category into a mothball.
|
2020-10-12 17:44:44 -06:00
|
|
|
func (p TranspilerProvider) Mothball(cat string, w io.Writer) error {
|
2020-09-15 15:58:21 -06:00
|
|
|
c := transpile.NewFsCategory(p.fs, cat)
|
2020-10-12 17:44:44 -06:00
|
|
|
return transpile.Mothball(c, w)
|
2020-09-15 15:58:21 -06:00
|
|
|
}
|
|
|
|
|
2020-09-08 17:49:02 -06:00
|
|
|
// Maintain performs housekeeping.
|
|
|
|
func (p TranspilerProvider) Maintain(updateInterval time.Duration) {
|
|
|
|
// Nothing to do here.
|
|
|
|
}
|
2021-10-20 11:29:55 -06:00
|
|
|
|
|
|
|
func (p TranspilerProvider) refresh() {
|
|
|
|
// Nothing to do for a theme
|
|
|
|
}
|