-
Notifications
You must be signed in to change notification settings - Fork 0
/
Api.res
149 lines (137 loc) · 3.92 KB
/
Api.res
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
module Schema = {
let nickname = S.string->S.transform(s => {
parser: string =>
switch string->Nickname.fromString {
| Some(nickname) => nickname
| None => s.fail(`Invalid nickname. (${string})`)
},
serializer: Nickname.toString,
})
module Game = {
let code = S.int->S.transform(s => {
parser: int =>
switch int->Int.toString->Game.Code.fromString {
| Some(gameCode) => gameCode
| None => s.fail(`Invalid game code. (${int->Obj.magic})`)
},
serializer: value =>
switch value->Game.Code.toString->Int.fromString {
| Some(int) => int
| None => s.fail(`Invalid game code.`)
},
})
let move = S.union([
S.literal("rock")->S.variant(_ => Game.Move.Rock),
S.literal("paper")->S.variant(_ => Game.Move.Paper),
S.literal("scissors")->S.variant(_ => Game.Move.Scissors),
])
let outcome = S.union([
S.literal("win")->S.variant(_ => Game.Win),
S.literal("draw")->S.variant(_ => Game.Draw),
S.literal("loss")->S.variant(_ => Game.Loss),
])
}
}
let client = Rest.client(~baseUrl=Env.apiUrl)
module CreateGame = {
let make = (): Port.CreateGame.t => {
let route = Rest.route(() => {
path: "/game",
method: "POST",
variables: (s): Port.CreateGame.input => {
nickname: s.field("userName", Schema.nickname),
},
responses: [
(s): Port.CreateGame.data => {
s.status(#200)
{
gameCode: s.field("gameCode", Schema.Game.code),
}
},
],
})
variables => client.call(route, variables)
}
}
module JoinGame = {
let make = (): Port.JoinGame.t => {
let route = Rest.route(() => {
path: "/game/connection",
method: "POST",
variables: (s): Port.JoinGame.input => {
nickname: s.field("userName", Schema.nickname),
gameCode: s.field("gameCode", Schema.Game.code),
},
responses: [
s => {
s.status(#204)
()
},
],
})
variables => client.call(route, variables)
}
}
module RequestGameStatus = {
let make = (): Port.RequestGameStatus.t => {
let route = Rest.route(() => {
path: "/game/status",
method: "POST",
variables: (s): Port.RequestGameStatus.input => {
nickname: s.field("userName", Schema.nickname),
gameCode: s.field("gameCode", Schema.Game.code),
},
responses: [
s => {
s.status(#200)
s.data(
S.union([
S.object(s => {
s.tag("status", "waiting")
Port.RequestGameStatus.WaitingForOpponentJoin
}),
S.object(s => {
s.tag("status", "inProcess")
Port.RequestGameStatus.InProgress
}),
S.object(s => {
s.tag("status", "finished")
s.field(
"gameResult",
S.object(
s => Port.RequestGameStatus.Finished({
outcome: s.field("outcome", Schema.Game.outcome),
yourMove: s.field("yourMove", Schema.Game.move),
opponentsMove: s.field("opponentsMove", Schema.Game.move),
}),
),
)
}),
]),
)
},
],
})
variables => client.call(route, variables)
}
}
module SendMove = {
let make = (): Port.SendMove.t => {
let route = Rest.route(() => {
path: "/game/move",
method: "POST",
variables: (s): Port.SendMove.input => {
nickname: s.field("userName", Schema.nickname),
gameCode: s.field("gameCode", Schema.Game.code),
yourMove: s.field("move", Schema.Game.move),
},
responses: [
s => {
s.status(#204)
()
},
],
})
variables => client.call(route, variables)
}
}