forked from WandererXII/lishogi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.scala
More file actions
185 lines (168 loc) · 7.17 KB
/
Copy pathPlayer.scala
File metadata and controls
185 lines (168 loc) · 7.17 KB
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
package lila.round
import shogi.format.usi.Usi
import shogi.{ Centis, MoveMetrics, Status }
import actorApi.round.{ DrawNo, ForecastPlay, HumanPlay, TakebackNo, TooManyPlies }
import cats.data.Validated
import java.util.concurrent.TimeUnit
import lila.common.Bus
import lila.game.actorApi.MoveGameEvent
import lila.game.Game.PlayerId
import lila.game.{ Game, Pov, Progress }
final private class Player(
fishnetPlayer: lila.fishnet.Player,
finisher: Finisher,
scheduleExpiration: ScheduleExpiration
)(implicit ec: scala.concurrent.ExecutionContext) {
sealed private trait MoveResult
private case object Flagged extends MoveResult
private case class MoveApplied(progress: Progress, compedLag: Option[Centis])
extends MoveResult
private[round] def human(play: HumanPlay, round: RoundDuct)(
pov: Pov
)(implicit proxy: GameProxy): Fu[Events] =
play match {
case HumanPlay(_, usi, blur, lag, _) =>
pov match {
case Pov(game, _) if game.plies > Game.maxPlies =>
round ! TooManyPlies
fuccess(Nil)
case Pov(game, color) if game playableBy color =>
applyUsi(game, usi, blur, lag)
.leftMap(e => s"$pov $e")
.fold(errs => fufail(ClientError(errs.toString)), fuccess)
.flatMap {
case Flagged => finisher.outOfTime(game)
case MoveApplied(progress, compedLag) =>
compedLag foreach { lag =>
lila.mon.round.move.lag.moveComp.record(lag.millis, TimeUnit.MILLISECONDS)
}
proxy.save(progress) >>
postHumanOrBotPlay(round, pov, progress, usi)
}
case Pov(game, _) if game.finished => fufail(ClientError(s"$pov game is finished"))
case Pov(game, _) if game.aborted => fufail(ClientError(s"$pov game is aborted"))
case Pov(game, color) if !game.turnOf(color) => fufail(ClientError(s"$pov not your turn"))
case _ => fufail(ClientError(s"$pov move refused for some reason"))
}
}
private[round] def bot(usi: Usi, round: RoundDuct)(pov: Pov)(implicit proxy: GameProxy): Fu[Events] =
pov match {
case Pov(game, _) if game.plies > Game.maxPlies =>
round ! TooManyPlies
fuccess(Nil)
case Pov(game, color) if game playableBy color =>
applyUsi(game, usi, false, botLag)
.fold(errs => fufail(ClientError(errs.toString)), fuccess)
.flatMap {
case Flagged => finisher.outOfTime(game)
case MoveApplied(progress, _) =>
proxy.save(progress) >> postHumanOrBotPlay(round, pov, progress, usi)
}
case Pov(game, _) if game.finished => fufail(ClientError(s"$pov game is finished"))
case Pov(game, _) if game.aborted => fufail(ClientError(s"$pov game is aborted"))
case Pov(game, color) if !game.turnOf(color) => fufail(ClientError(s"$pov not your turn"))
case _ => fufail(ClientError(s"$pov move refused for some reason"))
}
private def postHumanOrBotPlay(
round: RoundDuct,
pov: Pov,
progress: Progress,
usi: Usi
)(implicit proxy: GameProxy): Fu[Events] = {
notifyMove(usi, progress.game)
if (progress.game.finished) moveFinish(progress.game) dmap { progress.events ::: _ }
else {
if (progress.game.playableByAi) requestFishnet(progress.game, round)
if (pov.opponent.isOfferingDraw) round ! DrawNo(PlayerId(pov.player.id))
if (pov.player.isProposingTakeback) round ! TakebackNo(PlayerId(pov.player.id))
if (progress.game.forecastable) round ! ForecastPlay(usi)
scheduleExpiration(progress.game)
fuccess(progress.events)
}
}
private[round] def fishnet(game: Game, ply: Int, usi: Usi)(implicit proxy: GameProxy): Fu[Events] = {
if (game.playable && game.player.isAi && game.playedPlies == ply) {
applyUsi(game, usi, blur = false, metrics = fishnetLag)
.fold(errs => fufail(ClientError(errs.toString)), fuccess)
.flatMap {
case Flagged => finisher.outOfTime(game)
case MoveApplied(progress, _) =>
proxy.save(progress) >>-
notifyMove(usi, progress.game) >> {
if (progress.game.finished) moveFinish(progress.game) dmap { progress.events ::: _ }
else
fuccess(progress.events)
}
}
} else
fufail(
FishnetError(
s"Not AI turn move: ${usi} id: ${game.id} playable: ${game.playable} player: ${game.player}"
)
)
}
private[round] def requestFishnet(game: Game, round: RoundDuct): Funit =
game.playableByAi ?? {
if (game.plies <= fishnetPlayer.maxPlies) fishnetPlayer(game)
else fuccess(round ! actorApi.round.ResignAi)
}
private val fishnetLag = MoveMetrics(clientLag = Centis(5).some)
private val botLag = MoveMetrics(clientLag = Centis(0).some)
private def applyUsi(
game: Game,
usi: Usi,
blur: Boolean,
metrics: MoveMetrics
): Validated[String, MoveResult] =
game.shogi(usi, metrics) map { nsg =>
if (nsg.value.clock.exists(_.outOfTime(game.turnColor, withGrace = false))) Flagged
else MoveApplied(
game.update(nsg.value, usi, blur),
nsg.compensated
)
}
private def notifyMove(usi: Usi, game: Game): Unit = {
import lila.hub.actorApi.round.{ CorresMoveEvent, MoveEvent, SimulMoveEvent }
val color = !game.situation.color
val moveEvent = MoveEvent(
gameId = game.id,
sfen = game.situation.toSfen.value,
usi = usi.usi
)
// I checked and the bus doesn't do much if there's no subscriber for a classifier,
// so we should be good here.
// also used for targeted TvBroadcast subscription
Bus.publish(MoveGameEvent(game, moveEvent.sfen, moveEvent.usi), MoveGameEvent makeChan game.id)
// publish correspondence moves
if (game.isCorrespondence && game.nonAi)
Bus.publish(
CorresMoveEvent(
move = moveEvent,
playerUserId = game.player(color).userId,
mobilePushable = game.mobilePushable,
alarmable = game.alarmable,
unlimited = game.isUnlimited
),
"moveEventCorres"
)
// publish simul moves
for {
simulId <- game.simulId
opponentUserId <- game.player(!color).userId
} Bus.publish(
SimulMoveEvent(move = moveEvent, simulId = simulId, opponentUserId = opponentUserId),
"moveEventSimul"
)
}
private def moveFinish(game: Game)(implicit proxy: GameProxy): Fu[Events] = {
game.status match {
case Status.Mate => finisher.other(game, _.Mate, game.situation.winner)
case Status.Stalemate => finisher.other(game, _.Stalemate, game.situation.winner)
case Status.Impasse27 => finisher.other(game, _.Impasse27, game.situation.winner)
case Status.PerpetualCheck => finisher.other(game, _.PerpetualCheck, game.situation.winner)
case Status.VariantEnd => finisher.other(game, _.VariantEnd, game.situation.winner)
case Status.Draw => finisher.other(game, _.Draw, None)
case _ => fuccess(Nil)
}
}
}