-
Notifications
You must be signed in to change notification settings - Fork 3
/
Update.elm
121 lines (109 loc) · 3.24 KB
/
Update.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
module Update (Action(..), update) where
import Model exposing (Model)
import Ethos exposing (Ethos)
import Government exposing (Government, GovernmentForm)
import Trait exposing (Trait, Family(None))
ethosesAreValid : List Ethos -> Bool
ethosesAreValid ethoses =
Ethos.remainingPoints ethoses >= 0
enforceGovernmentIsValid : Model -> Model
enforceGovernmentIsValid model =
case model.selectedGovernment of
Nothing ->
model
Just g ->
if Government.isAvailable model.selectedEthoses g then
model
else
{ model | selectedGovernment = Nothing }
traitsAreValid : List Trait -> Bool
traitsAreValid traits =
(Trait.remainingPoints traits >= 0) && (Trait.remainingTraits traits >= 0)
type Action
= NoOp
| Init Model
| SetName String
| SelectEthos Ethos
| DeselectEthos Ethos
| HoverEthos Ethos
| DehoverEthos
| SelectGovernment Government
| DeselectGovernment
| HoverGovernment Government
| DehoverGovernment
| HoverGovernmentForm GovernmentForm
| DehoverGovernmentForm
| SelectTrait Trait
| DeselectTrait Trait
| HoverTrait Trait
| DehoverTrait
update : Action -> Model -> Model
update action model =
case action of
NoOp ->
model
Init newModel ->
newModel
SetName name ->
{ model | name = name }
SelectEthos ethos ->
let
newEthoses =
List.filter (\x -> x.dichotomy /= ethos.dichotomy) model.selectedEthoses
|> List.append [ ethos ]
newModel =
if ethosesAreValid newEthoses then
{ model | selectedEthoses = newEthoses }
else
model
in
enforceGovernmentIsValid newModel
DeselectEthos ethos ->
let
newEthoses = List.filter (\x -> x /= ethos) model.selectedEthoses
newModel = { model | selectedEthoses = newEthoses }
in
enforceGovernmentIsValid newModel
HoverEthos ethos ->
{ model | hoveredEthos = Just ethos }
DehoverEthos ->
{ model | hoveredEthos = Nothing }
SelectGovernment government ->
let
newModel =
{ model | selectedGovernment = Just government }
in
enforceGovernmentIsValid newModel
DeselectGovernment ->
{ model | selectedGovernment = Nothing }
HoverGovernment government ->
{ model | hoveredGovernment = Just government }
DehoverGovernment ->
{ model | hoveredGovernment = Nothing }
HoverGovernmentForm form' ->
{ model | hoveredGovernmentForm = Just form' }
DehoverGovernmentForm ->
{ model | hoveredGovernmentForm = Nothing }
SelectTrait trait ->
let
newTraits =
if trait.family == None then
[trait] ++ model.selectedTraits
else
List.filter (\x -> x.family /= trait.family) model.selectedTraits
|> flip (++) [trait]
in
if traitsAreValid newTraits then
{ model | selectedTraits = newTraits }
else
model
DeselectTrait trait ->
let
newTraits =
List.filter (\x -> x /= trait) model.selectedTraits
in
{ model | selectedTraits = newTraits }
HoverTrait trait ->
{ model | hoveredTrait = Just trait }
DehoverTrait ->
{ model | hoveredTrait = Nothing }