diff --git a/projects/habit-puzzle-game/source/HabitModel.hx b/projects/habit-puzzle-game/source/HabitModel.hx index dfd8cdb9..b488d84d 100644 --- a/projects/habit-puzzle-game/source/HabitModel.hx +++ b/projects/habit-puzzle-game/source/HabitModel.hx @@ -2,6 +2,24 @@ package; import kiss.Prelude; import kiss.List; +import kiss.Stream; +import sys.io.File; + +enum EntryType { + Daily(daysOfWeek:Array); + Bonus; + Todo; +} + +typedef EntryLabel = { + label:String, + points:Int +}; + +typedef Entry = { + type: EntryType, + labels: Array +}; @:build(kiss.Kiss.build()) class HabitModel {} \ No newline at end of file diff --git a/projects/habit-puzzle-game/source/HabitModel.kiss b/projects/habit-puzzle-game/source/HabitModel.kiss index 5fea1214..5df32717 100644 --- a/projects/habit-puzzle-game/source/HabitModel.kiss +++ b/projects/habit-puzzle-game/source/HabitModel.kiss @@ -1,3 +1,60 @@ +(prop :Array dailyEntries []) +(prop :Array bonusEntries []) +(prop :Array 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 + "")) diff --git a/projects/habit-puzzle-game/source/HabitState.kiss b/projects/habit-puzzle-game/source/HabitState.kiss index c194a047..98353351 100644 --- a/projects/habit-puzzle-game/source/HabitState.kiss +++ b/projects/habit-puzzle-game/source/HabitState.kiss @@ -2,4 +2,4 @@ (method &override :Void update [:Float elapsed] (super.update elapsed)) (prop &mut :HabitModel model null) -(method setModel [m] (set model m)) \ No newline at end of file +(method setModel [m] (set model ~m)) \ No newline at end of file