-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.elm
274 lines (226 loc) · 6.06 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
263
264
265
266
267
268
269
270
271
272
273
274
port module Main exposing (..)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
type alias State a = { a |
name: String,
playerId: Maybe Int,
persisting: Int
}
type alias Player = {
id: Int,
name: String
}
type alias Play = {
id: Int,
playerId: Int,
points: Int
}
type alias Data = {
players: List Player,
plays: List Play
}
type alias Model = State Data
initModel: (Model, Cmd msg)
initModel = ({
players = [],
name = "",
playerId = Nothing,
plays = [],
persisting = 0
}, Cmd.none)
type Msg =
Edit Player |
Score Player Int |
Input String |
Save |
Cancel |
DeletePlay Play |
ReceiveData Model |
DonePersisting Bool
update: Msg -> Model -> (Model, Cmd msg)
update msg model =
case msg of
ReceiveData newModel ->
( newModel, Cmd.none )
Input name ->
({ model | name = name }, Cmd.none)
Cancel ->
({ model | name = "", playerId = Nothing }, Cmd.none)
Save ->
persist (
if model.name == "" then
model
else
save model
)
Edit player ->
({ model | name = player.name, playerId = Just player.id }, Cmd.none)
Score player points ->
persist (score model player points)
DeletePlay play ->
persist { model | plays = (List.filter (\p -> p /= play) model.plays) }
DonePersisting _ ->
({ model | persisting = model.persisting - 1 }, Cmd.none)
persist: Model -> (Model, Cmd msg)
persist model =
( { model | persisting = model.persisting + 1 }, persistData (Data model.players model.plays) )
score: Model -> Player -> Int -> Model
score model player points =
let
play = Play (List.length model.plays) player.id points
in
{ model | plays = play :: model.plays }
save: Model -> Model
save model =
case model.playerId of
Just id ->
modifyPlayer model id
Nothing ->
add model
modifyPlayer: Model -> Int -> Model
modifyPlayer model id =
let
newPlayers = model.players
|> List.map (\player ->
if player.id == id then
{ player | name = model.name }
else
player
)
in
{ model | players = newPlayers, name = "", playerId = Nothing }
add: Model -> Model
add model =
let
player = Player (List.length model.players) model.name
newPlayers = player :: model.players
in
{ model | players = newPlayers, name = "" }
view: Model -> Html Msg
view model =
div [] [
h1 [] [ text "Score Keeper!" ],
loadingMsg model,
playerSection model,
playerForm model,
playsSection model
]
loadingMsg: Model -> Html Msg
loadingMsg {persisting} =
div [] [
if persisting > 0 then
text "saving..."
else
text "saved."
]
playsSection: Model -> Html Msg
playsSection model =
div [] [
playListHeader,
playList model
]
playListHeader : Html msg
playListHeader =
header [] [
div [] [ text "Plays" ],
div [] [ text "Points" ]
]
playList: Model -> Html Msg
playList model =
model.plays
|> List.map (play model)
|> ul []
play: Model -> Play -> Html Msg
play model play =
li [] [
i [ class "remove", onClick (DeletePlay play) ] [],
span [] [ text ((findPlayer model.players play.playerId) |> .name) ],
div [] [ text (toString play.points) ]
]
findPlayer: List Player -> Int -> Player
findPlayer players id =
players
|> List.filter (\player -> player.id == id)
|> List.head
|> Maybe.withDefault (Player -1 ("PLAYER NOT FOUND WITH ID=" ++ (toString id)))
playerSection: Model -> Html Msg
playerSection model =
div [] [
playerListHeader,
playerList model,
pointTotal model
]
playerListHeader: Html Msg
playerListHeader =
header [] [
div [] [ text "Name" ],
div [] [ text "Points" ]
]
playerList: Model -> Html Msg
playerList model =
model.players
|> List.sortBy .name
|> List.map (player model)
|> ul []
player: Model -> Player -> Html Msg
player model player =
li [] [
i [ class "edit", onClick (Edit player) ] [],
span [] [
text player.name
],
br [] [],
button [ type_ "button", onClick (Score player 2) ] [ text "2pt" ],
button [ type_ "button", onClick (Score player 3) ] [ text "3pt" ],
div [] [
playerPointTotal model player
]
]
playerPointTotal: Model -> Player -> Html Msg
playerPointTotal model player =
model.plays
|> List.filter (\play -> play.playerId == player.id)
|> List.map .points
|> List.sum
|> toString
|> text
pointTotal: Model -> Html Msg
pointTotal model =
let
total = List.map .points model.plays
|> List.sum
in
footer [] [
div [] [ text "Total:" ],
div [] [ text (toString total) ]
]
playerForm: Model -> Html Msg
playerForm model =
Html.form [ onSubmit Save ] [
input [
type_ "text",
placeholder "Add/Edit Player...",
onInput Input,
value model.name
] [],
button [ type_ "submit" ] [ text "+" ],
button [ type_ "button", onClick Cancel ] [ text "←" ]
]
port persistData: Data -> Cmd msg
port donePersisting: (Bool -> msg) -> Sub msg
port receiveData: (Data -> msg) -> Sub msg
subscriptions: Model -> Sub Msg
subscriptions model =
Sub.batch [
receiveData (\data -> ReceiveData {model | players = data.players, plays = data.plays}),
donePersisting DonePersisting
]
main: Program Never Model Msg
main =
Html.program {
init = initModel,
view = view,
update = update,
subscriptions = subscriptions
}