-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSanta.elm
83 lines (67 loc) · 2.43 KB
/
Santa.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
module Santa where
import Result
import Maybe
import Signal (..)
import Time (..)
import Html (..)
import Html.Attributes (..)
import Html.Events (..)
import Html.Lazy as Ref
import Graphics.Element (Element)
import Graphics.Input as Input
import Keyboard
import Window
import Json.Decode
import Santa.Common (..)
import Santa.Model.State as State
import Santa.Model.State (State)
import Santa.View.View as View
import Santa.Controller.Controller as Controller
import Santa.Controller.Controller (..)
import Santa.Controller.Persistence as Persistence
-- UPDATE
setPurchaseMultiplierSignal : Signal Action
setPurchaseMultiplierSignal =
let selectMultiplier ctrlDown shiftDown =
if | ctrlDown && shiftDown -> SetPurchaseMultiplier 100
| ctrlDown -> SetPurchaseMultiplier 25
| shiftDown -> SetPurchaseMultiplier 10
| otherwise -> SetPurchaseMultiplier 1
in
selectMultiplier <~ Keyboard.ctrl ~ Keyboard.shift
updateProductionSignal : Signal Action
updateProductionSignal = UpdateProduction <~ fps 1
updateDeliveriesSignal : Signal Action
updateDeliveriesSignal = UpdateDeliveries <~ (delay (500 * millisecond) <| fps 1)
updateResearchSignal : Signal Action
updateResearchSignal = UpdateResearch <~ fps 1
startingState : State
startingState =
case getStorage of
Just data -> Maybe.withDefault State.startState <| Result.toMaybe <| Json.Decode.decodeString Persistence.decode data
Nothing -> State.startState
state : Signal State
state =
let signals =
[ actionSignal
, updateProductionSignal
, updateDeliveriesSignal
, updateResearchSignal
, setPurchaseMultiplierSignal
, sampleOn
(keepIf (\x -> x == True) False confirmDialog)
(snd <~ subscribe Controller.requestChannel)
]
in
foldp Controller.step startingState <| mergeMany signals
scene : State -> (Int, Int) -> Element
scene state (w, h) = toElement w h (View.display state)
port showNotification : Signal (List String)
port showNotification = dropRepeats <| .notifications <~ state
port getStorage : Maybe String
port setStorage : Signal String
port setStorage = sampleOn (every (1 * second)) (Persistence.encode <~ state)
port requestDialog : Signal String
port requestDialog = fst <~ subscribe Controller.requestChannel
port confirmDialog : Signal Bool
main = scene <~ state ~ Window.dimensions