mirror of https://github.com/dirtbags/moth.git
sort puzzlecmd inventory
This commit is contained in:
parent
8aea668af7
commit
97301fb222
|
@ -9,6 +9,7 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -27,7 +28,7 @@ func (pc PuzzleCommand) Inventory() (inv []Category) {
|
||||||
|
|
||||||
cmd := exec.CommandContext(ctx, pc.Path, pc.Args...)
|
cmd := exec.CommandContext(ctx, pc.Path, pc.Args...)
|
||||||
cmd.Env = os.Environ()
|
cmd.Env = os.Environ()
|
||||||
cmd.Env = append(cmd.Env, "action=inventory")
|
cmd.Env = append(cmd.Env, "ACTION=inventory")
|
||||||
|
|
||||||
stdout, err := cmd.Output()
|
stdout, err := cmd.Output()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -55,6 +56,7 @@ func (pc PuzzleCommand) Inventory() (inv []Category) {
|
||||||
}
|
}
|
||||||
puzzles = append(puzzles, points)
|
puzzles = append(puzzles, points)
|
||||||
}
|
}
|
||||||
|
sort.Ints(puzzles)
|
||||||
inv = append(inv, Category{name, puzzles})
|
inv = append(inv, Category{name, puzzles})
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
@ -75,10 +77,10 @@ func (pc PuzzleCommand) Open(cat string, points int, path string) (ReadSeekClose
|
||||||
|
|
||||||
cmd := exec.CommandContext(ctx, pc.Path, pc.Args...)
|
cmd := exec.CommandContext(ctx, pc.Path, pc.Args...)
|
||||||
cmd.Env = os.Environ()
|
cmd.Env = os.Environ()
|
||||||
cmd.Env = append(cmd.Env, "action=open")
|
cmd.Env = append(cmd.Env, "ACTION=open")
|
||||||
cmd.Env = append(cmd.Env, "cat="+cat)
|
cmd.Env = append(cmd.Env, "CAT="+cat)
|
||||||
cmd.Env = append(cmd.Env, "points="+strconv.Itoa(points))
|
cmd.Env = append(cmd.Env, "POINTS="+strconv.Itoa(points))
|
||||||
cmd.Env = append(cmd.Env, "path="+path)
|
cmd.Env = append(cmd.Env, "FILENAME="+path)
|
||||||
|
|
||||||
stdoutBytes, err := cmd.Output()
|
stdoutBytes, err := cmd.Output()
|
||||||
stdout := NullReadSeekCloser{bytes.NewReader(stdoutBytes)}
|
stdout := NullReadSeekCloser{bytes.NewReader(stdoutBytes)}
|
||||||
|
@ -95,10 +97,10 @@ func (pc PuzzleCommand) CheckAnswer(cat string, points int, answer string) error
|
||||||
|
|
||||||
cmd := exec.CommandContext(ctx, pc.Path, pc.Args...)
|
cmd := exec.CommandContext(ctx, pc.Path, pc.Args...)
|
||||||
cmd.Env = os.Environ()
|
cmd.Env = os.Environ()
|
||||||
cmd.Env = append(cmd.Env, "action=answer")
|
cmd.Env = append(cmd.Env, "ACTION=answer")
|
||||||
cmd.Env = append(cmd.Env, "cat="+cat)
|
cmd.Env = append(cmd.Env, "CAT="+cat)
|
||||||
cmd.Env = append(cmd.Env, "points="+strconv.Itoa(points))
|
cmd.Env = append(cmd.Env, "POINTS="+strconv.Itoa(points))
|
||||||
cmd.Env = append(cmd.Env, "answer="+answer)
|
cmd.Env = append(cmd.Env, "ANSWER="+answer)
|
||||||
|
|
||||||
stdout, err := cmd.Output()
|
stdout, err := cmd.Output()
|
||||||
if ee, ok := err.(*exec.ExitError); ok {
|
if ee, ok := err.(*exec.ExitError); ok {
|
||||||
|
|
|
@ -28,6 +28,9 @@ func TestPuzzleCommand(t *testing.T) {
|
||||||
if len(cat.Puzzles) != 3 {
|
if len(cat.Puzzles) != 3 {
|
||||||
t.Errorf("nealegoy wrong number of puzzles: %d", len(cat.Puzzles))
|
t.Errorf("nealegoy wrong number of puzzles: %d", len(cat.Puzzles))
|
||||||
}
|
}
|
||||||
|
if cat.Puzzles[2] != 3 {
|
||||||
|
t.Errorf("out of order point values were not sorted")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,4 +62,9 @@ func TestPuzzleCommand(t *testing.T) {
|
||||||
} else {
|
} else {
|
||||||
f.Close()
|
f.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if f, _, err := pc.Open("pategory", 1, "not.there"); err == nil {
|
||||||
|
f.Close()
|
||||||
|
t.Errorf("Non-existent file didn't return error: %#v", f)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,30 +1,36 @@
|
||||||
#! /bin/sh -e
|
#! /bin/sh -e
|
||||||
|
|
||||||
case "$action:$cat:$points" in
|
fail () {
|
||||||
inventory::)
|
echo "$@" 1>&2
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
case "$ACTION:$CAT:$POINTS" in
|
||||||
|
inventory::)
|
||||||
echo "pategory 1 2 3 4 5 10 20 300"
|
echo "pategory 1 2 3 4 5 10 20 300"
|
||||||
echo "nealegory 1 2 3"
|
echo "nealegory 1 3 2"
|
||||||
;;
|
;;
|
||||||
open:*:*)
|
open:*:*)
|
||||||
if [ "$path" = "moo.txt" ]; then
|
case "$CAT:$POINTS:$FILENAME" in
|
||||||
|
*:*:moo.txt)
|
||||||
echo "Moo."
|
echo "Moo."
|
||||||
else
|
|
||||||
cat $cat_$points_$path
|
|
||||||
fi
|
|
||||||
;;
|
;;
|
||||||
answer:pategory:1)
|
*)
|
||||||
if [ "$answer" = "answer" ]; then
|
fail "Cannot open: $FILENAME"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
;;
|
||||||
|
answer:pategory:1)
|
||||||
|
if [ "$ANSWER" = "answer" ]; then
|
||||||
echo "correct"
|
echo "correct"
|
||||||
else
|
else
|
||||||
echo "Sorry, wrong answer."
|
echo "Sorry, wrong answer."
|
||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
answer:pategory:2)
|
answer:pategory:2)
|
||||||
echo "Internal error" 1>&2
|
fail "Internal error"
|
||||||
exit 1
|
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
echo "ERROR: Unknown action: $action" 1>&2
|
fail "ERROR: Unknown action: $action"
|
||||||
exit 1
|
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
Loading…
Reference in New Issue