Parse habits.txt model for habit game

This commit is contained in:
2022-02-03 14:51:53 -07:00
parent e3ce4e20b7
commit c8e043b726
3 changed files with 78 additions and 3 deletions

View File

@@ -2,6 +2,24 @@ package;
import kiss.Prelude;
import kiss.List;
import kiss.Stream;
import sys.io.File;
enum EntryType {
Daily(daysOfWeek:Array<Int>);
Bonus;
Todo;
}
typedef EntryLabel = {
label:String,
points:Int
};
typedef Entry = {
type: EntryType,
labels: Array<EntryLabel>
};
@:build(kiss.Kiss.build())
class HabitModel {}

View File

@@ -1,3 +1,60 @@
(prop :Array<Entry> dailyEntries [])
(prop :Array<Entry> bonusEntries [])
(prop :Array<Entry> todoEntries [])
(defNew [&prop :String textFile]
// TODO parse it out
)
(let [s (Stream.fromFile textFile)
&mut lastHeader ""]
// TODO could be whileLet
(loop
(case (s.takeLine)
((Some "DAILY")
(set lastHeader "DAILY"))
((Some "BONUS")
(set lastHeader "BONUS"))
((Some "TODO")
(set lastHeader "TODO"))
((when (apply = (concat ["-"] (line.split ""))) (Some line))
(continue))
((Some line)
(.push
(case lastHeader
("DAILY" dailyEntries)
("BONUS" bonusEntries)
("TODO" todoEntries)
(otherwise (throw "bad header")))
(object
type
(case lastHeader
("BONUS" Bonus)
("TODO" Todo)
("DAILY"
(Daily
(case (line.split ":")
([noColon] (collect (range 7)))
([::&mut preColon ...afterColon]
(set line (afterColon.join ":"))
(filter
[
// disambiguate Th from T:
(when (contains preColon "Th") {(set preColon (StringTools.replace preColon "Th" "")) 4})
(when (contains preColon "S") 0)
(when (contains preColon "M") 1)
(when (contains preColon "T") 2)
(when (contains preColon "W") 3)
(when (contains preColon "F") 5)
(when (contains preColon "S") 6)
]))
(otherwise (throw "bad line")))))
(otherwise (throw "bad header")))
labels
(for l (line.split "/")
(object
label (StringTools.replace l "|" "")
points (count (l.split "") ->c (= c "|")))))))
(otherwise (break))))))
(method :Void save []
(File.saveContent textFile
// TODO stringify the entries
""))

View File

@@ -2,4 +2,4 @@
(method &override :Void update [:Float elapsed] (super.update elapsed))
(prop &mut :HabitModel model null)
(method setModel [m] (set model m))
(method setModel [m] (set model ~m))