2015-04-25 12:48:13 -06:00
|
|
|
package lila.playban
|
|
|
|
|
|
|
|
import reactivemongo.bson._
|
2019-08-21 04:44:19 -06:00
|
|
|
import scala.concurrent.duration._
|
2015-04-25 12:48:13 -06:00
|
|
|
|
2019-07-11 14:18:24 -06:00
|
|
|
import chess.variant._
|
2019-08-21 04:44:19 -06:00
|
|
|
import chess.{ Status, Color }
|
2018-11-29 20:34:05 -07:00
|
|
|
import lila.common.PlayApp.{ startedSinceMinutes, isDev }
|
2015-04-25 12:48:13 -06:00
|
|
|
import lila.db.BSON._
|
2016-04-02 04:13:25 -06:00
|
|
|
import lila.db.dsl._
|
2015-04-25 15:06:44 -06:00
|
|
|
import lila.game.{ Pov, Game, Player, Source }
|
2019-08-24 07:21:29 -06:00
|
|
|
import lila.message.{ MessageApi, ModPreset }
|
2017-12-10 10:12:30 -07:00
|
|
|
import lila.user.{ User, UserRepo }
|
2015-04-25 12:48:13 -06:00
|
|
|
|
2019-06-30 07:28:33 -06:00
|
|
|
import org.joda.time.DateTime
|
|
|
|
|
2015-04-27 06:07:35 -06:00
|
|
|
final class PlaybanApi(
|
|
|
|
coll: Coll,
|
2017-09-11 23:09:46 -06:00
|
|
|
sandbag: SandbagWatch,
|
2018-03-10 08:56:38 -07:00
|
|
|
feedback: PlaybanFeedback,
|
2019-08-23 10:12:18 -06:00
|
|
|
bus: lila.common.Bus,
|
2019-08-24 04:39:42 -06:00
|
|
|
asyncCache: lila.memo.AsyncCache.Builder,
|
|
|
|
messenger: MessageApi
|
2017-02-14 08:34:07 -07:00
|
|
|
) {
|
2015-04-25 12:48:13 -06:00
|
|
|
|
|
|
|
import lila.db.BSON.BSONJodaDateTimeHandler
|
|
|
|
import reactivemongo.bson.Macros
|
|
|
|
private implicit val OutcomeBSONHandler = new BSONHandler[BSONInteger, Outcome] {
|
|
|
|
def read(bsonInt: BSONInteger): Outcome = Outcome(bsonInt.value) err s"No such playban outcome: ${bsonInt.value}"
|
|
|
|
def write(x: Outcome) = BSONInteger(x.id)
|
|
|
|
}
|
2015-04-25 15:06:44 -06:00
|
|
|
private implicit val banBSONHandler = Macros.handler[TempBan]
|
2015-04-25 12:48:13 -06:00
|
|
|
private implicit val UserRecordBSONHandler = Macros.handler[UserRecord]
|
|
|
|
|
2015-04-25 15:06:44 -06:00
|
|
|
private case class Blame(player: Player, outcome: Outcome)
|
|
|
|
|
2017-09-11 23:09:46 -06:00
|
|
|
private val blameableSources: Set[Source] = Set(Source.Lobby, Source.Pool, Source.Tournament)
|
2016-11-29 18:07:23 -07:00
|
|
|
|
2016-07-19 13:47:22 -06:00
|
|
|
private def blameable(game: Game): Fu[Boolean] =
|
2017-10-22 16:03:47 -06:00
|
|
|
(game.source.exists(s => blameableSources(s)) && game.hasClock) ?? {
|
2018-03-16 17:20:53 -06:00
|
|
|
if (game.rated) fuTrue
|
2016-07-19 13:47:22 -06:00
|
|
|
else UserRepo.containsEngine(game.userIds) map (!_)
|
|
|
|
}
|
|
|
|
|
|
|
|
private def IfBlameable[A: ornicar.scalalib.Zero](game: Game)(f: => Fu[A]): Fu[A] =
|
2018-04-13 00:51:02 -06:00
|
|
|
(isDev || startedSinceMinutes(10)) ?? {
|
2018-03-12 06:16:58 -06:00
|
|
|
blameable(game) flatMap { _ ?? f }
|
|
|
|
}
|
2015-04-25 15:06:44 -06:00
|
|
|
|
2019-07-03 16:07:14 -06:00
|
|
|
private def roughWinEstimate(game: Game, color: Color) = {
|
2019-07-11 07:52:13 -06:00
|
|
|
(game.chess.board.materialImbalance, game.variant) match {
|
2019-07-11 14:18:24 -06:00
|
|
|
case (_, Antichess | Crazyhouse | Horde) => 0
|
2019-07-11 07:52:13 -06:00
|
|
|
case (a, _) if a >= 5 => 1
|
|
|
|
case (a, _) if a <= -5 => -1
|
2019-07-03 16:07:14 -06:00
|
|
|
case _ => 0
|
|
|
|
}
|
|
|
|
} * (if (color == Color.White) 1 else -1)
|
|
|
|
|
2016-09-07 11:33:24 -06:00
|
|
|
def abort(pov: Pov, isOnGame: Set[Color]): Funit = IfBlameable(pov.game) {
|
2018-03-10 08:56:38 -07:00
|
|
|
pov.player.userId.ifTrue(isOnGame(pov.opponent.color)) ?? { userId =>
|
2019-07-03 16:07:14 -06:00
|
|
|
save(Outcome.Abort, userId, 0) >>- feedback.abort(pov)
|
2018-03-10 08:56:38 -07:00
|
|
|
}
|
2017-10-22 16:03:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
def noStart(pov: Pov): Funit = IfBlameable(pov.game) {
|
2018-03-10 08:56:38 -07:00
|
|
|
pov.player.userId ?? { userId =>
|
2019-07-03 16:07:14 -06:00
|
|
|
save(Outcome.NoPlay, userId, 0) >>- feedback.noStart(pov)
|
2018-03-10 08:56:38 -07:00
|
|
|
}
|
2015-04-26 05:04:22 -06:00
|
|
|
}
|
2015-04-25 15:06:44 -06:00
|
|
|
|
2017-09-11 23:09:46 -06:00
|
|
|
def rageQuit(game: Game, quitterColor: Color): Funit =
|
|
|
|
sandbag(game, quitterColor) >> IfBlameable(game) {
|
2018-03-10 08:56:38 -07:00
|
|
|
game.player(quitterColor).userId ?? { userId =>
|
2019-07-03 16:07:14 -06:00
|
|
|
save(Outcome.RageQuit, userId, roughWinEstimate(game, quitterColor)) >>- feedback.rageQuit(Pov(game, quitterColor))
|
2018-03-10 08:56:38 -07:00
|
|
|
}
|
2017-09-11 23:09:46 -06:00
|
|
|
}
|
2015-04-25 15:06:44 -06:00
|
|
|
|
2017-10-18 13:02:59 -06:00
|
|
|
def flag(game: Game, flaggerColor: Color): Funit = {
|
|
|
|
|
|
|
|
def unreasonableTime = game.clock map { c =>
|
2017-10-24 19:00:08 -06:00
|
|
|
(c.estimateTotalSeconds / 12) atLeast 15 atMost (3 * 60)
|
2017-10-18 13:02:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// flagged after waiting a long time
|
|
|
|
def sitting = for {
|
|
|
|
userId <- game.player(flaggerColor).userId
|
|
|
|
seconds = nowSeconds - game.movedAt.getSeconds
|
|
|
|
limit <- unreasonableTime
|
|
|
|
if seconds >= limit
|
2019-08-24 15:34:40 -06:00
|
|
|
} yield save(Outcome.Sitting, userId, roughWinEstimate(game, flaggerColor)) >>-
|
|
|
|
feedback.sitting(Pov(game, flaggerColor)) >>-
|
|
|
|
bus.publish(SittingDetected(game, userId), 'playban)
|
2017-10-18 13:02:59 -06:00
|
|
|
|
|
|
|
// flagged after waiting a short time;
|
|
|
|
// but the previous move used a long time.
|
|
|
|
// assumes game was already checked for sitting
|
|
|
|
def sitMoving = for {
|
|
|
|
userId <- game.player(flaggerColor).userId
|
|
|
|
movetimes <- game moveTimes flaggerColor
|
|
|
|
lastMovetime <- movetimes.lastOption
|
|
|
|
limit <- unreasonableTime
|
|
|
|
if lastMovetime.toSeconds >= limit
|
2019-08-24 15:34:40 -06:00
|
|
|
} yield save(Outcome.SitMoving, userId, roughWinEstimate(game, flaggerColor)) >>-
|
|
|
|
feedback.sitting(Pov(game, flaggerColor)) >>-
|
|
|
|
bus.publish(SittingDetected(game, userId), 'playban)
|
2017-10-18 13:02:59 -06:00
|
|
|
|
2017-10-18 17:16:52 -06:00
|
|
|
sandbag(game, flaggerColor) flatMap { isSandbag =>
|
|
|
|
IfBlameable(game) {
|
2017-10-18 17:26:38 -06:00
|
|
|
sitting orElse
|
|
|
|
sitMoving getOrElse
|
|
|
|
goodOrSandbag(game, flaggerColor, isSandbag)
|
2017-10-18 13:02:59 -06:00
|
|
|
}
|
2017-09-11 23:09:46 -06:00
|
|
|
}
|
2017-10-18 13:02:59 -06:00
|
|
|
}
|
2016-07-18 02:22:13 -06:00
|
|
|
|
2017-09-11 23:09:46 -06:00
|
|
|
def other(game: Game, status: Status.type => Status, winner: Option[Color]): Funit =
|
2017-10-18 17:16:52 -06:00
|
|
|
winner.?? { w => sandbag(game, !w) } flatMap { isSandbag =>
|
|
|
|
IfBlameable(game) {
|
2017-10-18 17:26:38 -06:00
|
|
|
~(for {
|
2017-10-18 17:16:52 -06:00
|
|
|
w <- winner
|
|
|
|
loserId <- game.player(!w).userId
|
2017-10-18 17:26:38 -06:00
|
|
|
} yield {
|
2019-07-03 16:07:14 -06:00
|
|
|
if (Status.NoStart is status) save(Outcome.NoPlay, loserId, 0) >>- feedback.noStart(Pov(game, !w))
|
2017-10-18 17:26:38 -06:00
|
|
|
else goodOrSandbag(game, !w, isSandbag)
|
|
|
|
})
|
2017-10-18 17:16:52 -06:00
|
|
|
}
|
2017-09-11 23:09:46 -06:00
|
|
|
}
|
2015-04-25 15:06:44 -06:00
|
|
|
|
2017-10-18 17:16:52 -06:00
|
|
|
private def goodOrSandbag(game: Game, color: Color, isSandbag: Boolean): Funit =
|
2018-03-10 08:56:38 -07:00
|
|
|
game.player(color).userId ?? { userId =>
|
|
|
|
if (isSandbag) feedback.sandbag(Pov(game, color))
|
2019-07-03 16:07:14 -06:00
|
|
|
save(if (isSandbag) Outcome.Sandbag else Outcome.Good, userId, 0)
|
2017-10-18 17:16:52 -06:00
|
|
|
}
|
2017-06-19 13:39:39 -06:00
|
|
|
|
2019-08-21 04:44:19 -06:00
|
|
|
// memorize users without any ban to save DB reads
|
|
|
|
private val cleanUserIds = new lila.memo.ExpireSetMemo(30 minutes)
|
|
|
|
|
|
|
|
def currentBan(userId: User.ID): Fu[Option[TempBan]] = !cleanUserIds.get(userId) ?? {
|
|
|
|
coll.find(
|
|
|
|
$doc("_id" -> userId, "b.0" $exists true),
|
|
|
|
$doc("_id" -> false, "b" -> $doc("$slice" -> -1))
|
|
|
|
).uno[Bdoc].map {
|
|
|
|
_.flatMap(_.getAs[List[TempBan]]("b")).??(_.find(_.inEffect))
|
|
|
|
} addEffect { ban =>
|
|
|
|
if (ban.isEmpty) cleanUserIds put userId
|
|
|
|
}
|
|
|
|
}
|
2015-04-25 15:06:44 -06:00
|
|
|
|
2017-12-10 10:12:30 -07:00
|
|
|
def hasCurrentBan(userId: User.ID): Fu[Boolean] = currentBan(userId).map(_.isDefined)
|
2017-08-04 08:12:21 -06:00
|
|
|
|
2017-12-10 10:12:30 -07:00
|
|
|
def completionRate(userId: User.ID): Fu[Option[Double]] =
|
2016-07-18 04:44:54 -06:00
|
|
|
coll.primitiveOne[List[Outcome]]($id(userId), "o").map(~_) map { outcomes =>
|
|
|
|
outcomes.collect {
|
2019-07-27 20:34:05 -06:00
|
|
|
case Outcome.RageQuit | Outcome.Sitting | Outcome.NoPlay | Outcome.Abort => false
|
2017-02-14 08:34:07 -07:00
|
|
|
case Outcome.Good => true
|
2016-07-18 04:44:54 -06:00
|
|
|
} match {
|
|
|
|
case c if c.size >= 5 => Some(c.count(identity).toDouble / c.size)
|
2017-02-14 08:34:07 -07:00
|
|
|
case _ => none
|
2016-07-18 04:44:54 -06:00
|
|
|
}
|
2015-04-25 15:06:44 -06:00
|
|
|
}
|
|
|
|
|
2017-12-10 10:12:30 -07:00
|
|
|
def bans(userIds: List[User.ID]): Fu[Map[User.ID, Int]] = coll.find(
|
2016-04-02 04:13:25 -06:00
|
|
|
$inIds(userIds),
|
|
|
|
$doc("b" -> true)
|
2019-08-21 04:44:19 -06:00
|
|
|
).list[Bdoc]().map {
|
2015-04-25 23:44:35 -06:00
|
|
|
_.flatMap { obj =>
|
2017-12-10 10:12:30 -07:00
|
|
|
obj.getAs[User.ID]("_id") flatMap { id =>
|
2016-07-18 02:22:13 -06:00
|
|
|
obj.getAs[Barr]("b") map { id -> _.stream.size }
|
2015-04-25 23:44:35 -06:00
|
|
|
}
|
2017-03-26 05:50:31 -06:00
|
|
|
}(scala.collection.breakOut)
|
2015-04-25 23:44:35 -06:00
|
|
|
}
|
|
|
|
|
2019-08-23 10:12:18 -06:00
|
|
|
private val sitAndDcCounterCache = asyncCache.multi[User.ID, Int](
|
|
|
|
name = "playban.sit_dc_counter",
|
|
|
|
f = userId => coll.primitiveOne[Int]($doc("_id" -> userId, "c" $exists true), "c").map(~_),
|
|
|
|
expireAfter = _.ExpireAfterWrite(30 minutes)
|
|
|
|
)
|
2019-08-21 11:39:04 -06:00
|
|
|
|
2019-08-23 10:12:18 -06:00
|
|
|
def sitAndDcCounter(userId: User.ID): Fu[Int] = sitAndDcCounterCache get userId
|
2019-07-03 16:07:14 -06:00
|
|
|
|
|
|
|
private def save(outcome: Outcome, userId: User.ID, sitAndDcCounterChange: Int): Funit = {
|
2017-10-22 17:14:35 -06:00
|
|
|
lila.mon.playban.outcome(outcome.key)()
|
2019-06-30 16:26:31 -06:00
|
|
|
coll.findAndUpdate(
|
|
|
|
selector = $id(userId),
|
|
|
|
update = $doc("$push" -> $doc(
|
|
|
|
"o" -> $doc(
|
|
|
|
"$each" -> List(outcome),
|
|
|
|
"$slice" -> -30
|
|
|
|
)
|
2019-07-03 16:07:14 -06:00
|
|
|
), "$inc" -> $doc("c" -> sitAndDcCounterChange)),
|
2019-06-30 16:26:31 -06:00
|
|
|
fetchNewObject = true,
|
|
|
|
upsert = true
|
|
|
|
).map(_.value) map2 UserRecordBSONHandler.read flatMap {
|
|
|
|
case None => fufail(s"can't find record for user $userId")
|
|
|
|
case _ if outcome == Outcome.Good => funit
|
|
|
|
case Some(record) => UserRepo.createdAtById(userId) flatMap {
|
|
|
|
o => o map { d => legiferate(record, d) } getOrElse funit
|
2019-06-30 15:00:01 -06:00
|
|
|
}
|
2019-08-23 10:12:18 -06:00
|
|
|
} addEffect { _ =>
|
2019-08-24 04:39:42 -06:00
|
|
|
if (sitAndDcCounterChange != 0) {
|
|
|
|
sitAndDcCounterCache refresh userId
|
2019-08-24 07:21:29 -06:00
|
|
|
if (sitAndDcCounterChange < 0) {
|
|
|
|
sitAndDcCounter(userId) map { counter =>
|
|
|
|
if (counter == -10) {
|
|
|
|
for {
|
|
|
|
mod <- UserRepo.lichess
|
|
|
|
user <- UserRepo byId userId
|
|
|
|
} yield (mod zip user).headOption.?? {
|
|
|
|
case (m, u) =>
|
|
|
|
lila.log("stall").info(s"https://lichess.org/@/${u.username}")
|
|
|
|
messenger.sendPreset(m, u, ModPreset.sittingAuto).void
|
|
|
|
}
|
|
|
|
} else if (counter <= -20) {
|
|
|
|
lila.log("stall").warn(s"Close https://lichess.org/@/${userId} ragesit=$counter")
|
|
|
|
// bus.publish(lila.hub.actorApi.playban.SitcounterClose(userId), 'playban)
|
2019-08-24 04:39:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-30 16:26:31 -06:00
|
|
|
}
|
|
|
|
|
2019-06-30 15:00:01 -06:00
|
|
|
}.void logFailure lila.log("playban")
|
2015-04-25 15:06:44 -06:00
|
|
|
|
2019-06-30 16:10:33 -06:00
|
|
|
private def legiferate(record: UserRecord, accCreatedAt: DateTime): Funit = record.bannable(accCreatedAt) ?? { ban =>
|
2018-11-29 20:34:05 -07:00
|
|
|
(!record.banInEffect) ?? {
|
|
|
|
lila.mon.playban.ban.count()
|
|
|
|
lila.mon.playban.ban.mins(ban.mins)
|
|
|
|
bus.publish(lila.hub.actorApi.playban.Playban(record.userId, ban.mins), 'playban)
|
|
|
|
coll.update(
|
|
|
|
$id(record.userId),
|
|
|
|
$unset("o") ++
|
|
|
|
$push(
|
|
|
|
"b" -> $doc(
|
|
|
|
"$each" -> List(ban),
|
|
|
|
"$slice" -> -30
|
2017-10-19 22:02:55 -06:00
|
|
|
)
|
2018-11-29 20:34:05 -07:00
|
|
|
)
|
2019-08-21 04:44:19 -06:00
|
|
|
).void >>- cleanUserIds.remove(record.userId)
|
2017-10-19 22:02:55 -06:00
|
|
|
}
|
2015-04-25 15:06:44 -06:00
|
|
|
}
|
2015-04-25 12:48:13 -06:00
|
|
|
}
|