-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.elm
262 lines (240 loc) · 7.38 KB
/
Main.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import Html exposing (..)
import Html.Events exposing (..)
import Html.Attributes exposing (type_, placeholder, value, hidden)
import List.Extra
import Random.List
import Select
main = Html.beginnerProgram { model = init, view = view, update = update }
-- MODELS
type alias Model =
{ players: List Player
, currentName: String
, currentRole: Role
, phase: Phase
, story: List String
, selectedPlayer: Maybe Player
, killed: Maybe Player
}
type alias Player =
{ name: String
, role: Role
}
type Role
= Villager
| Werewolf
| Doctor
| Seer
type Phase
= Initializing
| Dusk
| WerewolvesWakeUp
| WerewolvesSleep
| DoctorWakesUp
| DoctorSleeps
| SeerWakesUp
| SeerSleeps
| Dawn
| BurnedAtTheStake
init : (Model, Cmd Message)
init =
( Model
[]
""
Villager
Initializing
[]
Nothing
Nothing
, Cmd.none
)
-- UPDATE
type Message
= NameChanged String
| RoleChanged Role
| CreatePlayer
| RemovePlayer Player
| StartPhase Phase
| SelectPlayer (Maybe Player)
update : Message -> (Model, Cmd Message) -> (Model, Cmd Message)
update message (model, command) =
case message of
NameChanged name ->
( { model | currentName = name }
, Cmd.none
)
RoleChanged role ->
( { model | currentRole = role }
, Cmd.none
)
CreatePlayer ->
if not (model.phase == Initializing) then
(model, Cmd.none)
else
( { model
| players = (Player model.currentName model.currentRole) :: model.players
, currentName = ""
, currentRole = Villager
}
, Cmd.none
)
RemovePlayer player ->
if not (model.phase == Initializing) then
(model, Cmd.none)
else
( { model | players = List.Extra.remove player model.players }
, Cmd.none
)
SelectPlayer player ->
( { model | selectedPlayer = player }
, Cmd.none
)
StartPhase phase ->
let
newModel = case phase of
Initializing -> model
Dusk ->
addToStory "The night has come so everyone goes to sleep" model
WerewolvesWakeUp ->
addToStory "And the werewolves sneak into the village to seek out their pray" model
WerewolvesSleep ->
addToStory
"The werewolves picked a unlucky soul so they sneak back out of the village"
{ model
| killed = model.selectedPlayer
, selectedPlayer = Nothing
}
DoctorWakesUp -> addToStory "Then the Doctor awakens to seek out someone to heal" model
DoctorSleeps ->
addToStory
"The lucky soul has been chosen and the Doctor goes back to sleep"
{ model
| killed = if model.killed == model.selectedPlayer then Nothing else model.killed
, selectedPlayer = Nothing
}
SeerWakesUp ->
addToStory "The seer awakens to see the true nature of a single person amongst us" model
SeerSleeps ->
addToStory "The truth has been revealed and the seer goes back to sleep" model
Dawn ->
case model.killed of
Nothing ->
addToStory "Dawn has come and the village wakes up peacecully... without a bloody murder" model
Just player ->
addToStory
("Dawn has come and the village finds " ++ player.name ++ " murdered in the dark night. Pitchforks and fire! Who will they burn at the stake?")
{ model
| players = (List.Extra.remove player model.players)
, selectedPlayer = Nothing
, killed = Nothing
}
BurnedAtTheStake ->
case model.selectedPlayer of
Nothing -> model
Just player ->
addToStory
("The villagers in their wisdomd decided to burn " ++ player.name ++ " at the stake! Their true identity is revealed, a " ++ toString player.role)
{ model
| players = (List.Extra.remove player model.players)
, selectedPlayer = Nothing
}
in
( { newModel | phase = phase }
, Cmd.none
)
addToStory: String -> Model -> Model
addToStory line model =
{ model | story = line :: model.story }
-- VIEW
view : (Model, Cmd Message) -> Html Message
view (model, command) =
let
initializing = not (model.phase == Initializing)
in
div
[]
[ renderPhase model.phase model
, renderPlayers initializing model.players
, renderStory model.story
]
renderPlayers: Bool -> List Player -> Html Message
renderPlayers gameStarted players =
ul [] (List.map (renderPlayer gameStarted) players)
renderPlayer: Bool -> Player -> Html Message
renderPlayer gameStarted player =
li []
[ text player.name
, a [ onClick (RemovePlayer player), hidden gameStarted ] [ text " [remove]" ]
]
renderStory: List String -> Html Message
renderStory story =
ul [] (List.map (\line -> li [] [ text line ]) story)
renderPhase: Phase -> Model -> Html Message
renderPhase phase model =
case phase of
Initializing ->
renderInitialization model.currentName
Dusk ->
renderClickThroughPhase "Zzzzzzz" WerewolvesWakeUp
WerewolvesWakeUp ->
renderPickPhase (List.filter (\p -> not (p.role == Werewolf)) model.players) model.selectedPlayer WerewolvesSleep
WerewolvesSleep ->
renderClickThroughPhase "Zzzzzzz" DoctorWakesUp
DoctorWakesUp ->
renderPickPhase model.players model.selectedPlayer DoctorSleeps
DoctorSleeps ->
renderClickThroughPhase "Zzzzzzz" SeerWakesUp
SeerWakesUp ->
renderClickThroughPhase "Zzzzzzz" SeerSleeps
SeerSleeps ->
renderClickThroughPhase "Zzzzzzz" Dawn
Dawn ->
renderPickPhase model.players model.selectedPlayer BurnedAtTheStake
BurnedAtTheStake ->
renderClickThroughPhase "Back into the night" Dusk
renderInitialization: String -> Html Message
renderInitialization currentName =
let
config =
{ items = [Villager, Werewolf, Doctor, Seer]
, getId = (\r -> toString r)
, getLabel = (\r -> toString r)
, onSelect = (\r -> RoleChanged r)
, selected = Villager
}
in
div
[]
[ input [ type_ "text", onInput NameChanged, value currentName] []
, Select.selectList config
, button [ onClick CreatePlayer ] [ text "Add" ]
, button [ onClick (StartPhase Dusk) ] [ text "Into the night!" ]
]
renderClickThroughPhase: String -> Phase -> Html Message
renderClickThroughPhase buttonText phase =
div
[]
[ button [ onClick (StartPhase phase) ] [ text buttonText ] ]
renderPickPhase: List Player -> Maybe Player -> Phase -> Html Message
renderPickPhase players selectedPlayer nextPhase =
let
playerToString =
(\mp -> case mp of
Nothing -> "-"
Just p -> p.name
)
config =
{ items = Nothing :: (List.map (\p -> Just p) players)
, getId = playerToString
, getLabel = playerToString
, onSelect = (\mp -> SelectPlayer mp)
, selected = Nothing
}
isBlocked = case selectedPlayer of
Nothing -> True
_ -> False
in
div
[]
[ Select.selectList config
, button [ onClick (StartPhase nextPhase), hidden isBlocked ] [ text "pick" ]
]