aboutsummaryrefslogtreecommitdiff
path: root/client/src/Messages.elm
blob: 02d2a37a7ae40255995914d8b93b3016d1d3deda (plain)
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
module Messages exposing(..)

import Json.Decode
import Json.Encode exposing (Value)
-- The following module comes from bartavelle/json-helpers
import Json.Helpers exposing (..)
import Dict exposing (Dict)
import Set exposing (Set)


type BlackCard  =
    BlackCard Int

jsonDecBlackCard : Json.Decode.Decoder ( BlackCard )
jsonDecBlackCard =
    Json.Decode.lazy (\_ -> Json.Decode.map BlackCard (Json.Decode.int))


jsonEncBlackCard : BlackCard -> Value
jsonEncBlackCard (BlackCard v1) =
    Json.Encode.int v1



type WhiteCard  =
    WhiteCard Int

jsonDecWhiteCard : Json.Decode.Decoder ( WhiteCard )
jsonDecWhiteCard =
    Json.Decode.lazy (\_ -> Json.Decode.map WhiteCard (Json.Decode.int))


jsonEncWhiteCard : WhiteCard -> Value
jsonEncWhiteCard (WhiteCard v1) =
    Json.Encode.int v1



type alias Cards  =
   { black: (List String)
   , white: (List String)
   }

jsonDecCards : Json.Decode.Decoder ( Cards )
jsonDecCards =
   Json.Decode.succeed (\pblack pwhite -> {black = pblack, white = pwhite})
   |> required "black" (Json.Decode.list (Json.Decode.string))
   |> required "white" (Json.Decode.list (Json.Decode.string))

jsonEncCards : Cards -> Value
jsonEncCards  val =
   Json.Encode.object
   [ ("black", (Json.Encode.list Json.Encode.string) val.black)
   , ("white", (Json.Encode.list Json.Encode.string) val.white)
   ]



type alias PlayerView  =
   { name: String
   , admin: Bool
   , ready: Bool
   , points: Int
   }

jsonDecPlayerView : Json.Decode.Decoder ( PlayerView )
jsonDecPlayerView =
   Json.Decode.succeed (\pname padmin pready ppoints -> {name = pname, admin = padmin, ready = pready, points = ppoints})
   |> required "name" (Json.Decode.string)
   |> required "admin" (Json.Decode.bool)
   |> required "ready" (Json.Decode.bool)
   |> required "points" (Json.Decode.int)

jsonEncPlayerView : PlayerView -> Value
jsonEncPlayerView  val =
   Json.Encode.object
   [ ("name", Json.Encode.string val.name)
   , ("admin", Json.Encode.bool val.admin)
   , ("ready", Json.Encode.bool val.ready)
   , ("points", Json.Encode.int val.points)
   ]



type alias VotedView  =
   { proposal: (List WhiteCard)
   , score: Int
   , winners: (List String)
   }

jsonDecVotedView : Json.Decode.Decoder ( VotedView )
jsonDecVotedView =
   Json.Decode.succeed (\pproposal pscore pwinners -> {proposal = pproposal, score = pscore, winners = pwinners})
   |> required "proposal" (Json.Decode.list (jsonDecWhiteCard))
   |> required "score" (Json.Decode.int)
   |> required "winners" (Json.Decode.list (Json.Decode.string))

jsonEncVotedView : VotedView -> Value
jsonEncVotedView  val =
   Json.Encode.object
   [ ("proposal", (Json.Encode.list jsonEncWhiteCard) val.proposal)
   , ("score", Json.Encode.int val.score)
   , ("winners", (Json.Encode.list Json.Encode.string) val.winners)
   ]



type TableView  =
    Proposing BlackCard (List WhiteCard)
    | Voting BlackCard (List (List WhiteCard)) (Maybe Int) (Maybe Int)
    | Tally BlackCard (List VotedView)

jsonDecTableView : Json.Decode.Decoder ( TableView )
jsonDecTableView =
    let jsonDecDictTableView = Dict.fromList
            [ ("Proposing", Json.Decode.lazy (\_ -> Json.Decode.map2 Proposing (Json.Decode.index 0 (jsonDecBlackCard)) (Json.Decode.index 1 (Json.Decode.list (jsonDecWhiteCard)))))
            , ("Voting", Json.Decode.lazy (\_ -> Json.Decode.map4 Voting (Json.Decode.index 0 (jsonDecBlackCard)) (Json.Decode.index 1 (Json.Decode.list (Json.Decode.list (jsonDecWhiteCard)))) (Json.Decode.index 2 (Json.Decode.maybe (Json.Decode.int))) (Json.Decode.index 3 (Json.Decode.maybe (Json.Decode.int)))))
            , ("Tally", Json.Decode.lazy (\_ -> Json.Decode.map2 Tally (Json.Decode.index 0 (jsonDecBlackCard)) (Json.Decode.index 1 (Json.Decode.list (jsonDecVotedView)))))
            ]
    in  decodeSumObjectWithSingleField  "TableView" jsonDecDictTableView

jsonEncTableView : TableView -> Value
jsonEncTableView  val =
    let keyval v = case v of
                    Proposing v1 v2 -> ("Proposing", encodeValue (Json.Encode.list identity [jsonEncBlackCard v1, (Json.Encode.list jsonEncWhiteCard) v2]))
                    Voting v1 v2 v3 v4 -> ("Voting", encodeValue (Json.Encode.list identity [jsonEncBlackCard v1, (Json.Encode.list (Json.Encode.list jsonEncWhiteCard)) v2, (maybeEncode (Json.Encode.int)) v3, (maybeEncode (Json.Encode.int)) v4]))
                    Tally v1 v2 -> ("Tally", encodeValue (Json.Encode.list identity [jsonEncBlackCard v1, (Json.Encode.list jsonEncVotedView) v2]))
    in encodeSumObjectWithSingleField keyval val



type alias GameView  =
   { players: (List PlayerView)
   , me: PlayerView
   , table: TableView
   , hand: (List WhiteCard)
   }

jsonDecGameView : Json.Decode.Decoder ( GameView )
jsonDecGameView =
   Json.Decode.succeed (\pplayers pme ptable phand -> {players = pplayers, me = pme, table = ptable, hand = phand})
   |> required "players" (Json.Decode.list (jsonDecPlayerView))
   |> required "me" (jsonDecPlayerView)
   |> required "table" (jsonDecTableView)
   |> required "hand" (Json.Decode.list (jsonDecWhiteCard))

jsonEncGameView : GameView -> Value
jsonEncGameView  val =
   Json.Encode.object
   [ ("players", (Json.Encode.list jsonEncPlayerView) val.players)
   , ("me", jsonEncPlayerView val.me)
   , ("table", jsonEncTableView val.table)
   , ("hand", (Json.Encode.list jsonEncWhiteCard) val.hand)
   ]



type ServerMessage  =
    Welcome String
    | SyncCards Cards
    | SyncGameView GameView

jsonDecServerMessage : Json.Decode.Decoder ( ServerMessage )
jsonDecServerMessage =
    let jsonDecDictServerMessage = Dict.fromList
            [ ("Welcome", Json.Decode.lazy (\_ -> Json.Decode.map Welcome (Json.Decode.string)))
            , ("SyncCards", Json.Decode.lazy (\_ -> Json.Decode.map SyncCards (jsonDecCards)))
            , ("SyncGameView", Json.Decode.lazy (\_ -> Json.Decode.map SyncGameView (jsonDecGameView)))
            ]
    in  decodeSumObjectWithSingleField  "ServerMessage" jsonDecDictServerMessage

jsonEncServerMessage : ServerMessage -> Value
jsonEncServerMessage  val =
    let keyval v = case v of
                    Welcome v1 -> ("Welcome", encodeValue (Json.Encode.string v1))
                    SyncCards v1 -> ("SyncCards", encodeValue (jsonEncCards v1))
                    SyncGameView v1 -> ("SyncGameView", encodeValue (jsonEncGameView v1))
    in encodeSumObjectWithSingleField keyval val



type ClientMessage  =
    ChangeMyName String
    | ProposeWhiteCards (List WhiteCard)
    | SubmitVote Int
    | AdminSkipProposals 
    | AdminSkipVotes 
    | AdminConfirmTally 

jsonDecClientMessage : Json.Decode.Decoder ( ClientMessage )
jsonDecClientMessage =
    let jsonDecDictClientMessage = Dict.fromList
            [ ("ChangeMyName", Json.Decode.lazy (\_ -> Json.Decode.map ChangeMyName (Json.Decode.string)))
            , ("ProposeWhiteCards", Json.Decode.lazy (\_ -> Json.Decode.map ProposeWhiteCards (Json.Decode.list (jsonDecWhiteCard))))
            , ("SubmitVote", Json.Decode.lazy (\_ -> Json.Decode.map SubmitVote (Json.Decode.int)))
            , ("AdminSkipProposals", Json.Decode.lazy (\_ -> Json.Decode.succeed AdminSkipProposals))
            , ("AdminSkipVotes", Json.Decode.lazy (\_ -> Json.Decode.succeed AdminSkipVotes))
            , ("AdminConfirmTally", Json.Decode.lazy (\_ -> Json.Decode.succeed AdminConfirmTally))
            ]
    in  decodeSumObjectWithSingleField  "ClientMessage" jsonDecDictClientMessage

jsonEncClientMessage : ClientMessage -> Value
jsonEncClientMessage  val =
    let keyval v = case v of
                    ChangeMyName v1 -> ("ChangeMyName", encodeValue (Json.Encode.string v1))
                    ProposeWhiteCards v1 -> ("ProposeWhiteCards", encodeValue ((Json.Encode.list jsonEncWhiteCard) v1))
                    SubmitVote v1 -> ("SubmitVote", encodeValue (Json.Encode.int v1))
                    AdminSkipProposals  -> ("AdminSkipProposals", encodeValue (Json.Encode.list identity []))
                    AdminSkipVotes  -> ("AdminSkipVotes", encodeValue (Json.Encode.list identity []))
                    AdminConfirmTally  -> ("AdminConfirmTally", encodeValue (Json.Encode.list identity []))
    in encodeSumObjectWithSingleField keyval val