-
Notifications
You must be signed in to change notification settings - Fork 0
/
Model.elm
130 lines (97 loc) · 3.03 KB
/
Model.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
module Model exposing (..)
import Json.Decode as D exposing (..)
import Json.Decode.Pipeline as DP exposing (..)
import Json.Encode as E exposing (..)
import Players exposing (..)
import Teams exposing (..)
import Url exposing (..)
import Url.Parser exposing (..)
-- MODEL
type alias Model =
{ undraftedPlayers : List Player
, draftedPlayers : List ( Player, String )
, waitingTeams : List Team
, draftedTeams : List Team
, round : Int
, localState : LocalState
}
type TabView
= DraftView
| TeamView
| HistoryView
type HostType
= Local
| Host String
| View String
type alias LocalState =
{ currentView : TabView
, hostingType : HostType
, playerSearch : String
, playerSorts : List PlayerSortEntry
, showMenu : Bool
}
type alias PlayerSort =
Player -> Player -> Order
type alias PlayerSortEntry =
{ tag : String
, sort : PlayerSort
}
initModel : LocalState -> Model
initModel localState =
{ undraftedPlayers = Players.defaultPlayerList (List.length Teams.fullTeamList)
, draftedPlayers = []
, waitingTeams = Teams.fullTeamList
, draftedTeams = []
, round = 1
, localState = localState
}
matchers : Parser (HostType -> a) a
matchers =
Url.Parser.oneOf
[ Url.Parser.map Local top
, Url.Parser.map Host (s "host" </> Url.Parser.string)
, Url.Parser.map View (s "view" </> Url.Parser.string)
]
parseLocation : Url.Url -> HostType
parseLocation location =
case Url.Parser.parse matchers location of
Just route ->
route
Nothing ->
Local
modelDecoder : LocalState -> D.Decoder Model
modelDecoder localState =
D.succeed Model
|> required "undraftedPlayers" (D.list Players.decodePlayer)
|> required "draftedPlayers" (D.list decodeDraftedPlayer)
|> required "waitingTeams" (D.list Teams.decodeTeam)
|> required "draftedTeams" (D.list Teams.decodeTeam)
|> required "round" D.int
|> hardcoded localState
decodeModel : D.Value -> LocalState -> Maybe Model
decodeModel value localState =
Result.toMaybe (D.decodeValue (modelDecoder localState) value)
decodeDraftedPlayer : Decoder ( Player, String )
decodeDraftedPlayer =
D.map2 (\a b -> ( a, b ))
(field "player" Players.decodePlayer)
(field "team" D.string)
encodeModel : Model -> E.Value
encodeModel model =
E.object
[ ( "undraftedPlayers", E.list Players.encodePlayer model.undraftedPlayers )
, ( "draftedPlayers", E.list encodeDraftedPlayer model.draftedPlayers )
, ( "waitingTeams", E.list Teams.encodeTeam model.waitingTeams )
, ( "draftedTeams", E.list Teams.encodeTeam model.draftedTeams )
, ( "round", E.int model.round )
]
encodeDraftedPlayer : ( Player, String ) -> E.Value
encodeDraftedPlayer draftedPlayer =
let
( player, team ) =
draftedPlayer
in
E.object
[ ( "player", Players.encodePlayer player )
, ( "team", E.string team )
]