forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPushApi.scala
85 lines (78 loc) · 2.74 KB
/
PushApi.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
package lila.push
import akka.actor._
import akka.pattern.ask
import chess.format.Forsyth
import lila.common.LightUser
import lila.game.{ Game, GameRepo, Pov, Namer }
import lila.hub.actorApi.map.Ask
import lila.hub.actorApi.round.{ MoveEvent, IsOnGame }
import lila.user.User
import play.api.libs.json._
private final class PushApi(
googlePush: GooglePush,
implicit val lightUser: String => Option[LightUser],
roundSocketHub: ActorSelection) {
def finish(game: Game): Funit =
if (!game.isCorrespondence || game.hasAi) funit
else game.userIds.map { userId =>
Pov.ofUserId(game, userId) ?? { pov =>
IfAway(pov) {
googlePush(userId) {
GooglePush.Data(
title = pov.win match {
case Some(true) => "You won!"
case Some(false) => "You lost."
case _ => "It's a draw."
},
body = s"Your game with ${opponentName(pov)} is over.",
payload = Json.obj(
"userId" -> userId,
"userData" -> Json.obj(
"gameId" -> game.id,
"fullId" -> pov.fullId,
"color" -> pov.color.name,
"fen" -> Forsyth.exportBoard(game.toChess.board),
"lastMove" -> game.castleLastMoveTime.lastMoveString,
"win" -> pov.win)
))
}
}
}
}.sequenceFu.void
def move(move: MoveEvent): Funit = move.mobilePushable ?? {
GameRepo game move.gameId flatMap {
_ ?? { game =>
val pov = Pov(game, !move.color)
game.player(!move.color).userId ?? { userId =>
game.pgnMoves.lastOption ?? { sanMove =>
IfAway(pov) {
googlePush(userId) {
GooglePush.Data(
title = "It's your turn!",
body = s"${opponentName(pov)} played $sanMove",
payload = Json.obj(
"userId" -> userId,
"userData" -> Json.obj(
"gameId" -> game.id,
"fullId" -> pov.fullId,
"color" -> pov.color.name,
"fen" -> Forsyth.exportBoard(game.toChess.board),
"lastMove" -> game.castleLastMoveTime.lastMoveString,
"secondsLeft" -> pov.remainingSeconds)
))
}
}
}
}
}
}
}
private def IfAway(pov: Pov)(f: => Funit): Funit = {
import makeTimeout.short
roundSocketHub ? Ask(pov.gameId, IsOnGame(pov.color)) mapTo manifest[Boolean] flatMap {
case true => funit
case false => f
}
}
private def opponentName(pov: Pov) = Namer playerString pov.opponent
}