-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathEvents.elm
129 lines (100 loc) · 3.72 KB
/
Events.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
module Events exposing (Event, decodeEvents)
import Json.Encode
import Json.Decode exposing ((:=), at)
import Json.Decode.Extra exposing ((|:))
type Event
= EPlayerHasRegistered PlayerHasRegistered
| EQuizWasCreated QuizWasCreated
| EQuizWasPublished QuizWasPublished
| EQuestionAddedToQuiz QuestionAddedToQuiz
| EGameWasFinished GameWasFinished
type alias PlayerHasRegistered =
{ player_id : Int
, last_name : String
, first_name : String
, id: String
, timestamp : String
}
type alias QuizWasCreated =
{ quiz_title : String
, quiz_id : Int
, owner_id : Int
, id : String
, timestamp : String
}
type alias QuizWasPublished =
{ quiz_id : Int
, id : String
, timestamp : String
}
type alias QuestionAddedToQuiz =
{ quiz_id : Int
, question_id : Int
, question : String
, id : String
, timestamp : String
, answer : String
}
type alias GameWasFinished =
{ game_id : String
, id : String
, timestamp : String
}
decodeEvents : Json.Decode.Decoder (List Event)
decodeEvents =
Json.Decode.list decodeEvent
decodeEvent : Json.Decode.Decoder Event
decodeEvent =
("type" := Json.Decode.string) `Json.Decode.andThen` eventInfo
eventInfo : String -> Json.Decode.Decoder Event
eventInfo eventName =
case eventName of
"PlayerHasRegistered" ->
Json.Decode.map (\e -> EPlayerHasRegistered e) decodePlayerHasRegistered
"QuizWasCreated" ->
Json.Decode.map (\e -> EQuizWasCreated e) decodeQuizWasCreated
"QuizWasPublished" ->
Json.Decode.map (\e -> EQuizWasPublished e) decodeQuizWasPublished
"QuestionAddedToQuiz" ->
Json.Decode.map (\e -> EQuestionAddedToQuiz e) decodeQuestionAddedToQuiz
"GameWasFinished" ->
Json.Decode.map (\e -> EGameWasFinished e) decodeGameWasFinished
_ ->
Json.Decode.fail (eventName ++ " is not a recognized event")
decodePlayerHasRegistered : Json.Decode.Decoder PlayerHasRegistered
decodePlayerHasRegistered =
Json.Decode.succeed PlayerHasRegistered
|: (at ["payload", "player_id"] Json.Decode.int)
|: (at ["payload", "last_name"] Json.Decode.string)
|: (at ["payload", "first_name"] Json.Decode.string)
|: (at ["id"] Json.Decode.string)
|: (at ["timestamp"] Json.Decode.string)
decodeQuizWasCreated : Json.Decode.Decoder QuizWasCreated
decodeQuizWasCreated =
Json.Decode.succeed QuizWasCreated
|: (at ["payload", "quiz_title"] Json.Decode.string)
|: (at ["payload", "quiz_id"] Json.Decode.int)
|: (at ["payload", "owner_id"] Json.Decode.int)
|: (at ["id"] Json.Decode.string)
|: (at ["timestamp"] Json.Decode.string)
decodeQuizWasPublished : Json.Decode.Decoder QuizWasPublished
decodeQuizWasPublished =
Json.Decode.succeed QuizWasPublished
|: (at ["payload", "quiz_id"] Json.Decode.int)
|: (at ["id"] Json.Decode.string)
|: (at ["timestamp"] Json.Decode.string)
decodeQuestionAddedToQuiz : Json.Decode.Decoder QuestionAddedToQuiz
decodeQuestionAddedToQuiz =
Json.Decode.succeed QuestionAddedToQuiz
|: (at ["payload", "quiz_id"] Json.Decode.int)
|: (at ["payload", "question_id"] Json.Decode.int)
|: (at ["payload", "question"] Json.Decode.string)
|: (at ["id"] Json.Decode.string)
|: (at ["timestamp"] Json.Decode.string)
|: (at ["payload", "answer"] Json.Decode.string)
decodeGameWasFinished : Json.Decode.Decoder GameWasFinished
decodeGameWasFinished =
Json.Decode.succeed GameWasFinished
|: (at ["payload", "game_id"] Json.Decode.string)
|: (at ["id"] Json.Decode.string)
|: (at ["timestamp"] Json.Decode.string)