-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathClientOut.scala
459 lines (413 loc) · 16.7 KB
/
ClientOut.scala
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
package lila.ws
package ipc
import scala.util.chaining._
import strategygames.format.{ FEN, Uci }
import strategygames.variant.Variant
import strategygames.{
Centis,
Player => PlayerIndex,
GameFamily,
GameLogic,
MoveMetrics,
Pos,
PromotableRole,
Role
}
import lila.ws.util.LilaJsObject.augment
import play.api.libs.json._
import scala.util.{ Success, Try }
sealed trait ClientOut extends ClientMsg
sealed trait ClientOutSite extends ClientOut
sealed trait ClientOutLobby extends ClientOut
sealed trait ClientOutStudy extends ClientOut
sealed trait ClientOutRound extends ClientOut
sealed trait ClientOutRacer extends ClientOut
object ClientOut {
case class Ping(lag: Option[Int]) extends ClientOutSite
case class Watch(ids: Set[Game.Id]) extends ClientOutSite
case object MoveLat extends ClientOutSite
case object Notified extends ClientOutSite
case object FollowingOnline extends ClientOutSite
case class Opening(variant: Variant, path: Path, fen: FEN) extends ClientOutSite
case class Move(orig: Pos, dest: Pos)
case class AnaMove(
orig: Pos,
dest: Pos,
fen: FEN,
path: Path,
variant: Variant,
chapterId: Option[ChapterId],
promotion: Option[PromotableRole],
uci: Option[String],
fullCapture: Option[Boolean],
payload: JsObject
) extends ClientOutSite
case class AnaDrop(
role: Role,
pos: Pos,
fen: FEN,
path: Path,
variant: Variant,
chapterId: Option[ChapterId],
payload: JsObject,
halfMove: Option[Move] = None
) extends ClientOutSite
case class AnaPass(
fen: FEN,
path: Path,
variant: Variant,
chapterId: Option[ChapterId],
payload: JsObject
) extends ClientOutSite
case class AnaDests(
fen: FEN,
path: Path,
variant: Variant,
chapterId: Option[ChapterId],
uci: Option[String],
lastUci: Option[String],
fullCapture: Option[Boolean]
) extends ClientOutSite
case class MsgType(dest: User.ID) extends ClientOutSite
case class SiteForward(payload: JsObject) extends ClientOutSite
case class UserForward(payload: JsObject) extends ClientOutSite
case class Unexpected(msg: JsValue) extends ClientOutSite
case object WrongHole extends ClientOutSite
case object Ignore extends ClientOutSite
// lobby
case class Idle(value: Boolean, payload: JsValue) extends ClientOutLobby
case class LobbyForward(payload: JsValue) extends ClientOutLobby
// study
case class StudyForward(payload: JsValue) extends ClientOutStudy
// round
case class RoundPlayerForward(payload: JsValue) extends ClientOutRound
case class RoundMove(
gf: GameFamily,
uci: Uci,
blur: Boolean,
lag: MoveMetrics,
ackId: Option[Int]
) extends ClientOutRound
case class RoundSelectSquaresOffer(
gf: GameFamily,
squares: String
) extends ClientOutRound
case class RoundHold(mean: Int, sd: Int) extends ClientOutRound
case class RoundBerserk(ackId: Option[Int]) extends ClientOutRound
case class RoundSelfReport(name: String) extends ClientOutRound
case class RoundFlag(playerIndex: PlayerIndex) extends ClientOutRound
case object RoundBye extends ClientOutRound
// chat
case class ChatSay(msg: String) extends ClientOut
case class ChatTimeout(suspect: String, reason: String, text: String) extends ClientOut
// challenge
case object ChallengePing extends ClientOut
// palantir
case object PalantirPing extends ClientOut
// storm
case class StormKey(key: String, pad: String) extends ClientOutSite
// racer
case class RacerScore(score: Int) extends ClientOutRacer
case object RacerJoin extends ClientOutRacer
// Strategy games has few issues right now, so we have to implement our own
// lookup that turns some role into a promotable role.
// Step 1, turn a ground name into a "Role". Note that we can't currently match
// between Role and Promotable role, because the wrapper layer
// currently wraps them all as a role
def groundToRole(lib: GameLogic, variant: Variant)(groundName: Option[String]): Option[Role] =
groundName.flatMap(
Role
.allByGroundName(lib, variant.gameFamily)
.get(_)
)
// Step 2, Turn a role into a Promotable role by looping through all of the promotable
// roles of a given given gamelogic and seeing if one exists by that name.
// TODO: I would try and make this more efficient, but it would result in
// some leaking of abstractions here, which I don't want.
def roleToPromotable(lib: GameLogic)(role: Option[Role]): Option[PromotableRole] =
role.flatMap(r => Role.allPromotable(lib).filter(_.name == r.name).headOption)
// impl
def parse(str: String): Try[ClientOut] =
if (str == "null" || str == """{"t":"p"}""") emptyPing
else
Try(Json parse str) map {
case o: JsObject =>
o str "t" flatMap {
case "p" => Some(Ping(o int "l"))
case "startWatching" =>
o str "d" map { d =>
Watch(d.split(" ").take(16).map(Game.Id.apply).toSet)
} orElse Some(Ignore) // old apps send empty watch lists
case "moveLat" => Some(MoveLat)
case "notified" => Some(Notified)
case "following_onlines" => Some(FollowingOnline)
case "opening" =>
for {
d <- o obj "d"
path <- d str "path"
fen <- d str "fen"
lib = dataGameLogic(d)
variant = dataVariant(d, lib)
} yield Opening(variant, Path(path), FEN(lib, fen))
case "anaMove" =>
for {
d <- o obj "d"
lib = dataGameLogic(d)
orig <- d str "orig" flatMap (p => Pos.fromKey(lib, p))
dest <- d str "dest" flatMap (p => Pos.fromKey(lib, p))
path <- d str "path"
fen <- d str "fen"
variant = dataVariant(d, lib)
chapterId = d.str("ch").map(ChapterId.apply)
promotion = d
.str("promotion")
.pipe(groundToRole(lib, variant))
.pipe(roleToPromotable(lib))
uci = d str "uci"
fullCapture = d boolean "fullCapture"
} yield AnaMove(
orig,
dest,
FEN(lib, fen),
Path(path),
variant,
chapterId,
promotion,
uci,
fullCapture,
o
)
case "anaDrop" =>
for {
d <- o obj "d"
lib = dataGameLogic(d)
variant = dataVariant(d, lib)
role <- d str "role" flatMap Role.allByGroundName(lib, variant.gameFamily).get
pos <- d str "pos" flatMap (p => Pos.fromKey(lib, p))
path <- d str "path"
fen <- d str "fen"
chapterId = d str "ch" map ChapterId.apply
} yield AnaDrop(
role,
pos,
FEN(lib, fen),
Path(path),
variant,
chapterId,
o,
(d obj "halfMove").flatMap(halfMove => {
for {
orig <- halfMove str "orig" flatMap (p => Pos.fromKey(lib, p))
dest <- halfMove str "dest" flatMap (p => Pos.fromKey(lib, p))
} yield Move(orig, dest)
})
)
case "anaPass" =>
for {
d <- o obj "d"
lib = dataGameLogic(d)
variant = dataVariant(d, lib)
path <- d str "path"
fen <- d str "fen"
chapterId = d str "ch" map ChapterId.apply
} yield AnaPass(
FEN(lib, fen),
Path(path),
variant,
chapterId,
o
)
case "anaDests" =>
for {
d <- o obj "d"
path <- d str "path"
fen <- d str "fen"
lib = dataGameLogic(d)
variant = dataVariant(d, lib)
chapterId = d str "ch" map ChapterId.apply
uci = d str "uci"
lastUci = d str "lastUci"
fullCapture = d boolean "fullCapture"
} yield AnaDests(
FEN(lib, fen),
Path(path),
variant,
chapterId,
uci,
lastUci,
fullCapture
)
case "evalGet" | "evalPut" => Some(SiteForward(o))
case "msgType" => o str "d" map MsgType.apply
case "msgSend" | "msgRead" => Some(UserForward(o))
// lobby
case "idle" => o boolean "d" map { Idle(_, o) }
case "join" | "cancel" | "joinSeek" | "cancelSeek" | "poolIn" | "poolOut" | "hookIn" |
"hookOut" =>
Some(LobbyForward(o))
// study
case "like" | "setPath" | "deleteNode" | "promote" | "forceVariation" | "setRole" | "kick" |
"leave" | "shapes" | "addChapter" | "setChapter" | "editChapter" | "descStudy" |
"descChapter" | "deleteChapter" | "clearAnnotations" | "sortChapters" | "editStudy" |
"setTag" | "setComment" | "deleteComment" | "setGamebook" | "toggleGlyph" | "explorerGame" |
"requestAnalysis" | "invite" | "relaySync" | "setTopics" =>
Some(StudyForward(o))
// round
case "move" =>
for {
d <- o obj "d"
lib = dataGameLogic(d)
variant = dataVariant(d, lib)
move <- d str "u" flatMap (m =>
Uci.Move.apply(lib, variant.gameFamily, m)
) orElse parseOldMove(d, lib, variant)
blur = d int "b" contains 1
ackId = d int "a"
} yield RoundMove(variant.gameFamily, move, blur, parseMetrics(d), ackId)
case "drop" =>
for {
d <- o obj "d"
lib = dataGameLogic(d)
variant = dataVariant(d, lib)
role <- d str "role" flatMap { g =>
Role.allByGroundName(lib, variant.gameFamily).get(g)
}
pos <- d str "pos"
drop <- Uci.Drop.fromStrings(lib, variant.gameFamily, role.name, pos)
blur = d int "b" contains 1
ackId = d int "a"
} yield RoundMove(variant.gameFamily, drop, blur, parseMetrics(d), ackId)
case "lift" =>
for {
d <- o obj "d"
lib = dataGameLogic(d)
variant = dataVariant(d, lib)
pos <- d str "pos"
lift <- Uci.Lift.fromStrings(lib, variant.gameFamily, pos)
blur = d int "b" contains 1
ackId = d int "a"
} yield RoundMove(variant.gameFamily, lift, blur, parseMetrics(d), ackId)
case "undo" =>
for {
d <- o obj "d"
lib = dataGameLogic(d)
variant = dataVariant(d, lib)
undo <- Uci.Undo.apply(lib)
blur = d int "b" contains 1
ackId = d int "a"
} yield RoundMove(variant.gameFamily, undo, blur, parseMetrics(d), ackId)
case "endturn" =>
for {
d <- o obj "d"
lib = dataGameLogic(d)
variant = dataVariant(d, lib)
endTurn <- Uci.EndTurn.apply(lib, variant.gameFamily)
blur = d int "b" contains 1
ackId = d int "a"
} yield RoundMove(variant.gameFamily, endTurn, blur, parseMetrics(d), ackId)
case "pass" =>
for {
d <- o obj "d"
lib = dataGameLogic(d)
variant = dataVariant(d, lib)
pass <- Uci.Pass.apply(lib, variant.gameFamily)
blur = d int "b" contains 1
ackId = d int "a"
} yield RoundMove(variant.gameFamily, pass, blur, parseMetrics(d), ackId)
case "diceroll" =>
for {
d <- o obj "d"
lib = dataGameLogic(d)
variant = dataVariant(d, lib)
diceroll <- Uci.DoRoll.apply(lib)
blur = d int "b" contains 1
ackId = d int "a"
} yield RoundMove(variant.gameFamily, diceroll, blur, parseMetrics(d), ackId)
case "cubeaction" =>
for {
d <- o obj "d"
lib = dataGameLogic(d)
variant = dataVariant(d, lib)
interaction <- d str "interaction"
cubeaction <- Uci.CubeAction.fromStrings(lib, interaction)
blur = d int "b" contains 1
ackId = d int "a"
} yield RoundMove(variant.gameFamily, cubeaction, blur, parseMetrics(d), ackId)
case "selectSquares" =>
for {
d <- o obj "d"
lib = dataGameLogic(d)
variant = dataVariant(d, lib)
squares <- (d str "s").map(_.split(",").flatMap(p => Pos.fromKey(lib, p)))
ss <- Uci.SelectSquares.fromSquares(lib, variant.gameFamily, squares.toList)
blur = d int "b" contains 1
ackId = d int "a"
} yield RoundMove(variant.gameFamily, ss, blur, parseMetrics(d), ackId)
case "hold" =>
for {
d <- o obj "d"
mean <- d int "mean"
sd <- d int "sd"
} yield RoundHold(mean, sd)
case "berserk" => Some(RoundBerserk(o obj "d" flatMap (_ int "a")))
case "rep" => o obj "d" flatMap (_ str "n") map RoundSelfReport.apply
case "flag" => o str "d" flatMap PlayerIndex.fromName map RoundFlag.apply
case "bye2" => Some(RoundBye)
case "palantirPing" => Some(PalantirPing)
case "moretime" | "rematch-yes" | "rematch-no" | "takeback-yes" | "takeback-no" | "draw-yes" |
"draw-no" | "draw-claim" | "resign" | "resign-force" | "draw-force" | "abort" | "outoftime" |
"select-squares-accept" | "select-squares-decline" =>
Some(RoundPlayerForward(o))
case "select-squares-offer" =>
for {
d <- o obj "d"
lib = dataGameLogic(d)
variant = dataVariant(d, lib)
squares <- d str "s"
} yield RoundSelectSquaresOffer(variant.gameFamily, squares)
// chat
case "talk" => o str "d" map { ChatSay.apply }
case "timeout" =>
for {
data <- o obj "d"
userId <- data str "userId"
reason <- data str "reason"
text <- data str "text"
} yield ChatTimeout(userId, reason, text)
case "ping" => Some(ChallengePing)
// storm
case "sk1" =>
o str "d" flatMap { s =>
s split '!' match {
case Array(key, pad) => Some(StormKey(key, pad))
case _ => None
}
}
// racer
case "racerScore" => o int "d" map RacerScore.apply
case "racerJoin" => Some(RacerJoin)
case "wrongHole" => Some(WrongHole)
case _ => None
} getOrElse Unexpected(o)
case js => Unexpected(js)
}
private val emptyPing: Try[ClientOut] = Success(Ping(None))
private def dataGameLogic(d: JsObject): GameLogic =
GameLogic(d int "lib" getOrElse 0)
private def dataVariant(d: JsObject, lib: GameLogic): Variant =
Variant.orDefault(lib, d str "variant" getOrElse "")
private def parseOldMove(d: JsObject, lib: GameLogic, variant: Variant) =
for {
orig <- d str "from"
dest <- d str "to"
prom = d str "promotion"
move <- Uci.Move.fromStrings(lib, variant.gameFamily, orig, dest, prom)
} yield move
private def parseMetrics(d: JsObject) =
MoveMetrics(
d.int("l") map Centis.ofMillis,
d.str("s") flatMap { v =>
Try(Centis(Integer.parseInt(v, 36))).toOption
}
)
}