2015-04-25 12:48:13 -06:00
|
|
|
package lila.playban
|
|
|
|
|
2019-11-29 19:16:11 -07:00
|
|
|
import reactivemongo.api.bson._
|
2019-08-21 04:44:19 -06:00
|
|
|
import scala.concurrent.duration._
|
2015-04-25 12:48:13 -06:00
|
|
|
|
2020-01-04 19:50:42 -07:00
|
|
|
import chess.{ Centis, Color, Status }
|
2019-12-02 11:48:11 -07:00
|
|
|
import lila.common.{ Bus, Iso, Uptime }
|
2016-04-02 04:13:25 -06:00
|
|
|
import lila.db.dsl._
|
2019-12-13 07:30:20 -07:00
|
|
|
import lila.game.{ Game, Player, Pov, Source }
|
2020-01-26 22:07:27 -07:00
|
|
|
import lila.msg.{ MsgApi, MsgPreset }
|
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,
|
2018-03-10 08:56:38 -07:00
|
|
|
feedback: PlaybanFeedback,
|
2019-12-01 11:03:39 -07:00
|
|
|
userRepo: UserRepo,
|
2019-12-23 18:01:45 -07:00
|
|
|
cacheApi: lila.memo.CacheApi,
|
2020-01-26 22:07:27 -07:00
|
|
|
messenger: MsgApi
|
2019-12-13 18:17:43 -07:00
|
|
|
)(implicit ec: scala.concurrent.ExecutionContext) {
|
2015-04-25 12:48:13 -06:00
|
|
|
|
|
|
|
import lila.db.BSON.BSONJodaDateTimeHandler
|
2019-11-29 19:16:11 -07:00
|
|
|
import reactivemongo.api.bson.Macros
|
2019-12-13 07:30:20 -07:00
|
|
|
implicit private val OutcomeBSONHandler = tryHandler[Outcome](
|
2019-12-01 11:03:39 -07:00
|
|
|
{ case BSONInteger(v) => Outcome(v) toTry s"No such playban outcome: $v" },
|
|
|
|
x => BSONInteger(x.id)
|
|
|
|
)
|
2019-12-13 07:30:20 -07:00
|
|
|
implicit private val RageSitBSONHandler = intIsoHandler(Iso.int[RageSit](RageSit.apply, _.counter))
|
|
|
|
implicit private val BanBSONHandler = Macros.handler[TempBan]
|
|
|
|
implicit private val UserRecordBSONHandler = Macros.handler[UserRecord]
|
2015-04-25 12:48:13 -06:00
|
|
|
|
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] =
|
2020-01-04 19:46:20 -07:00
|
|
|
(game.source.exists(blameableSources.contains) && game.hasClock) ?? {
|
2018-03-16 17:20:53 -06:00
|
|
|
if (game.rated) fuTrue
|
2020-01-04 19:46:20 -07:00
|
|
|
else !userRepo.containsEngine(game.userIds)
|
2016-07-19 13:47:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
private def IfBlameable[A: ornicar.scalalib.Zero](game: Game)(f: => Fu[A]): Fu[A] =
|
2021-02-15 12:26:28 -07:00
|
|
|
Uptime.startedSinceMinutes(0) ?? {
|
2018-03-12 06:16:58 -06:00
|
|
|
blameable(game) flatMap { _ ?? f }
|
|
|
|
}
|
2015-04-25 15:06:44 -06:00
|
|
|
|
2020-05-05 22:11:15 -06:00
|
|
|
def abort(pov: Pov, isOnGame: Set[Color]): Funit =
|
|
|
|
IfBlameable(pov.game) {
|
|
|
|
pov.player.userId.ifTrue(isOnGame(pov.opponent.color)) ?? { userId =>
|
|
|
|
save(Outcome.Abort, userId, RageSit.Reset) >>- feedback.abort(pov)
|
|
|
|
}
|
2018-03-10 08:56:38 -07:00
|
|
|
}
|
2017-10-22 16:03:47 -06:00
|
|
|
|
2020-05-05 22:11:15 -06:00
|
|
|
def noStart(pov: Pov): Funit =
|
|
|
|
IfBlameable(pov.game) {
|
|
|
|
pov.player.userId ?? { userId =>
|
|
|
|
save(Outcome.NoPlay, userId, RageSit.Reset) >>- feedback.noStart(pov)
|
|
|
|
}
|
2018-03-10 08:56:38 -07:00
|
|
|
}
|
2015-04-25 15:06:44 -06:00
|
|
|
|
2017-09-11 23:09:46 -06:00
|
|
|
def rageQuit(game: Game, quitterColor: Color): Funit =
|
2021-02-17 08:38:56 -07:00
|
|
|
IfBlameable(game) {
|
2018-03-10 08:56:38 -07:00
|
|
|
game.player(quitterColor).userId ?? { userId =>
|
2020-01-04 19:07:03 -07:00
|
|
|
save(Outcome.RageQuit, userId, RageSit.imbalanceInc(game, quitterColor)) >>-
|
2019-09-26 13:45:53 -06:00
|
|
|
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 = {
|
|
|
|
|
2020-05-05 22:11:15 -06:00
|
|
|
def unreasonableTime =
|
|
|
|
game.clock map { c =>
|
|
|
|
(c.estimateTotalSeconds / 12) atLeast 15 atMost (3 * 60)
|
|
|
|
}
|
2017-10-18 13:02:59 -06:00
|
|
|
|
|
|
|
// flagged after waiting a long time
|
2019-12-13 07:30:20 -07:00
|
|
|
def sitting: Option[Funit] =
|
|
|
|
for {
|
|
|
|
userId <- game.player(flaggerColor).userId
|
|
|
|
seconds = nowSeconds - game.movedAt.getSeconds
|
|
|
|
if unreasonableTime.exists(seconds >= _)
|
2020-01-04 19:07:03 -07:00
|
|
|
} yield save(Outcome.Sitting, userId, RageSit.imbalanceInc(game, flaggerColor)) >>-
|
2020-09-29 01:51:32 -06:00
|
|
|
feedback.sitting(Pov(game, flaggerColor)) >>
|
2019-12-13 07:30:20 -07:00
|
|
|
propagateSitting(game, userId)
|
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
|
2019-12-13 07:30:20 -07:00
|
|
|
def sitMoving: Option[Funit] =
|
|
|
|
game.player(flaggerColor).userId.ifTrue {
|
|
|
|
~(for {
|
|
|
|
movetimes <- game moveTimes flaggerColor
|
|
|
|
lastMovetime <- movetimes.lastOption
|
|
|
|
limit <- unreasonableTime
|
|
|
|
} yield lastMovetime.toSeconds >= limit)
|
|
|
|
} map { userId =>
|
2020-01-04 19:07:03 -07:00
|
|
|
save(Outcome.SitMoving, userId, RageSit.imbalanceInc(game, flaggerColor)) >>-
|
2020-09-29 01:51:32 -06:00
|
|
|
feedback.sitting(Pov(game, flaggerColor)) >>
|
2019-12-13 07:30:20 -07:00
|
|
|
propagateSitting(game, userId)
|
|
|
|
}
|
2017-10-18 13:02:59 -06:00
|
|
|
|
2021-02-17 08:38:56 -07:00
|
|
|
IfBlameable(game) {
|
|
|
|
sitting orElse
|
|
|
|
sitMoving getOrElse
|
|
|
|
good(game, flaggerColor)
|
2017-09-11 23:09:46 -06:00
|
|
|
}
|
2017-10-18 13:02:59 -06:00
|
|
|
}
|
2016-07-18 02:22:13 -06:00
|
|
|
|
2019-09-26 13:45:53 -06:00
|
|
|
private def propagateSitting(game: Game, userId: User.ID): Funit =
|
|
|
|
rageSitCache get userId map { rageSit =>
|
2019-11-29 17:07:51 -07:00
|
|
|
if (rageSit.isBad) Bus.publish(SittingDetected(game, userId), "playban")
|
2019-08-24 16:10:08 -06:00
|
|
|
}
|
|
|
|
|
2017-09-11 23:09:46 -06:00
|
|
|
def other(game: Game, status: Status.type => Status, winner: Option[Color]): Funit =
|
2021-02-17 08:38:56 -07:00
|
|
|
IfBlameable(game) {
|
|
|
|
~(for {
|
|
|
|
w <- winner
|
|
|
|
loser = game.player(!w)
|
|
|
|
loserId <- loser.userId
|
|
|
|
} yield {
|
|
|
|
if (Status.NoStart is status)
|
|
|
|
save(Outcome.NoPlay, loserId, RageSit.Reset) >>- feedback.noStart(Pov(game, !w))
|
|
|
|
else
|
|
|
|
game.clock
|
|
|
|
.filter {
|
|
|
|
_.remainingTime(loser.color) < Centis(1000) &&
|
|
|
|
game.turnOf(loser) &&
|
|
|
|
Status.Resign.is(status)
|
|
|
|
}
|
|
|
|
.map { c =>
|
|
|
|
(c.estimateTotalSeconds / 10) atLeast 15 atMost (3 * 60)
|
|
|
|
}
|
|
|
|
.exists(_ < nowSeconds - game.movedAt.getSeconds)
|
|
|
|
.option {
|
|
|
|
save(Outcome.SitResign, loserId, RageSit.imbalanceInc(game, loser.color)) >>-
|
|
|
|
feedback.sitting(Pov(game, loser.color)) >>
|
|
|
|
propagateSitting(game, loserId)
|
|
|
|
}
|
|
|
|
.getOrElse {
|
|
|
|
good(game, !w)
|
|
|
|
}
|
|
|
|
})
|
2017-09-11 23:09:46 -06:00
|
|
|
}
|
2015-04-25 15:06:44 -06:00
|
|
|
|
2021-02-17 08:38:56 -07:00
|
|
|
private def good(game: Game, loserColor: Color): Funit =
|
|
|
|
game.player(loserColor).userId ?? {
|
|
|
|
save(Outcome.Good, _, RageSit.redeem(game))
|
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)
|
|
|
|
|
2020-05-05 22:11:15 -06:00
|
|
|
def currentBan(userId: User.ID): Fu[Option[TempBan]] =
|
|
|
|
!cleanUserIds.get(userId) ?? {
|
2020-08-21 09:18:23 -06:00
|
|
|
coll
|
2020-05-05 22:11:15 -06:00
|
|
|
.find(
|
|
|
|
$doc("_id" -> userId, "b.0" $exists true),
|
2020-08-21 09:18:23 -06:00
|
|
|
$doc("_id" -> false, "b" -> $doc("$slice" -> -1)).some
|
2020-05-05 22:11:15 -06:00
|
|
|
)
|
|
|
|
.one[Bdoc]
|
|
|
|
.dmap {
|
|
|
|
_.flatMap(_.getAsOpt[List[TempBan]]("b")).??(_.find(_.inEffect))
|
|
|
|
} addEffect { ban =>
|
|
|
|
if (ban.isEmpty) cleanUserIds put userId
|
|
|
|
}
|
2019-12-13 07:30:20 -07:00
|
|
|
}
|
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]] =
|
2019-12-09 20:11:53 -07:00
|
|
|
coll.primitiveOne[Vector[Outcome]]($id(userId), "o").map(~_) map { outcomes =>
|
2016-07-18 04:44:54 -06:00
|
|
|
outcomes.collect {
|
2019-07-27 20:34:05 -06:00
|
|
|
case Outcome.RageQuit | Outcome.Sitting | Outcome.NoPlay | Outcome.Abort => false
|
2019-12-13 07:30:20 -07:00
|
|
|
case Outcome.Good => true
|
2016-07-18 04:44:54 -06:00
|
|
|
} match {
|
2020-08-17 16:10:52 -06:00
|
|
|
case c if c.sizeIs >= 5 => Some(c.count(identity).toDouble / c.size)
|
2020-08-21 09:18:23 -06:00
|
|
|
case _ => none
|
2016-07-18 04:44:54 -06:00
|
|
|
}
|
2015-04-25 15:06:44 -06:00
|
|
|
}
|
|
|
|
|
2019-12-13 07:30:20 -07:00
|
|
|
def bans(userIds: List[User.ID]): Fu[Map[User.ID, Int]] =
|
2020-08-21 09:18:23 -06:00
|
|
|
coll
|
2019-12-13 07:30:20 -07:00
|
|
|
.find(
|
|
|
|
$inIds(userIds),
|
2020-08-21 09:18:23 -06:00
|
|
|
$doc("b" -> true).some
|
2019-12-13 07:30:20 -07:00
|
|
|
)
|
2020-07-19 08:13:08 -06:00
|
|
|
.cursor[Bdoc]()
|
|
|
|
.list()
|
2019-12-13 07:30:20 -07:00
|
|
|
.map {
|
|
|
|
_.flatMap { obj =>
|
|
|
|
obj.getAsOpt[User.ID]("_id") flatMap { id =>
|
|
|
|
obj.getAsOpt[Barr]("b") map { id -> _.size }
|
|
|
|
}
|
|
|
|
}.toMap
|
|
|
|
}
|
2015-04-25 23:44:35 -06:00
|
|
|
|
2019-09-26 13:45:53 -06:00
|
|
|
def getRageSit(userId: User.ID) = rageSitCache get userId
|
|
|
|
|
2019-12-24 07:31:19 -07:00
|
|
|
private val rageSitCache = cacheApi[User.ID, RageSit](32768, "playban.ragesit") {
|
2019-12-23 18:01:45 -07:00
|
|
|
_.expireAfterAccess(20 minutes)
|
|
|
|
.buildAsyncFuture { userId =>
|
|
|
|
coll.primitiveOne[RageSit]($doc("_id" -> userId, "c" $exists true), "c").map(_ | RageSit.empty)
|
|
|
|
}
|
|
|
|
}
|
2019-08-21 11:39:04 -06:00
|
|
|
|
2020-01-04 19:07:03 -07:00
|
|
|
private def save(outcome: Outcome, userId: User.ID, rsUpdate: RageSit.Update): Funit = {
|
2019-12-10 14:01:18 -07:00
|
|
|
lila.mon.playban.outcome(outcome.key).increment()
|
2019-12-13 07:30:20 -07:00
|
|
|
coll.ext
|
2020-01-16 10:52:20 -07:00
|
|
|
.findAndUpdate[UserRecord](
|
2019-12-13 07:30:20 -07:00
|
|
|
selector = $id(userId),
|
|
|
|
update = $doc(
|
2020-01-04 19:45:50 -07:00
|
|
|
$push("o" -> $doc("$each" -> List(outcome), "$slice" -> -30)) ++ {
|
2020-01-04 19:07:03 -07:00
|
|
|
rsUpdate match {
|
|
|
|
case RageSit.Reset => $min("c" -> 0)
|
|
|
|
case RageSit.Inc(v) if v != 0 => $inc("c" -> v)
|
|
|
|
case _ => $empty
|
|
|
|
}
|
|
|
|
}
|
2019-12-13 07:30:20 -07:00
|
|
|
),
|
|
|
|
fetchNewObject = true,
|
|
|
|
upsert = true
|
2020-01-16 10:52:20 -07:00
|
|
|
) orFail s"can't find newly created record for user $userId" flatMap { record =>
|
2019-12-13 07:30:20 -07:00
|
|
|
(outcome != Outcome.Good) ?? {
|
|
|
|
userRepo.createdAtById(userId).flatMap { _ ?? { legiferate(record, _) } }
|
2020-01-04 19:07:03 -07:00
|
|
|
} >>
|
|
|
|
registerRageSit(record, rsUpdate)
|
2019-12-13 07:30:20 -07:00
|
|
|
}
|
2019-06-30 15:00:01 -06:00
|
|
|
}.void logFailure lila.log("playban")
|
2015-04-25 15:06:44 -06:00
|
|
|
|
2020-05-05 22:11:15 -06:00
|
|
|
private def registerRageSit(record: UserRecord, update: RageSit.Update): Funit =
|
|
|
|
update match {
|
|
|
|
case RageSit.Inc(delta) =>
|
|
|
|
rageSitCache.put(record.userId, fuccess(record.rageSit))
|
|
|
|
(delta < 0) ?? {
|
|
|
|
if (record.rageSit.isTerrible) funit
|
2021-02-15 12:26:28 -07:00
|
|
|
else if (record.rageSit.isVeryBad) {
|
|
|
|
Bus.publish(
|
|
|
|
lila.hub.actorApi.mod.AutoWarning(record.userId, MsgPreset.sittingAuto.name),
|
|
|
|
"autoWarning"
|
|
|
|
)
|
|
|
|
messenger.postPreset(record.userId, MsgPreset.sittingAuto).void
|
|
|
|
} else funit
|
2020-05-05 22:11:15 -06:00
|
|
|
}
|
|
|
|
case _ => funit
|
|
|
|
}
|
2019-09-26 13:45:53 -06:00
|
|
|
|
2019-12-13 07:30:20 -07:00
|
|
|
private def legiferate(record: UserRecord, accCreatedAt: DateTime): Funit =
|
|
|
|
record.bannable(accCreatedAt) ?? { ban =>
|
|
|
|
(!record.banInEffect) ?? {
|
|
|
|
lila.mon.playban.ban.count.increment()
|
2019-12-14 18:45:22 -07:00
|
|
|
lila.mon.playban.ban.mins.record(ban.mins)
|
2019-12-13 07:30:20 -07:00
|
|
|
Bus.publish(lila.hub.actorApi.playban.Playban(record.userId, ban.mins), "playban")
|
|
|
|
coll.update
|
|
|
|
.one(
|
|
|
|
$id(record.userId),
|
|
|
|
$unset("o") ++
|
|
|
|
$push(
|
|
|
|
"b" -> $doc(
|
|
|
|
"$each" -> List(ban),
|
|
|
|
"$slice" -> -30
|
|
|
|
)
|
|
|
|
)
|
2018-11-29 20:34:05 -07:00
|
|
|
)
|
2019-12-13 07:30:20 -07:00
|
|
|
.void >>- cleanUserIds.remove(record.userId)
|
|
|
|
}
|
2017-10-19 22:02:55 -06:00
|
|
|
}
|
2015-04-25 12:48:13 -06:00
|
|
|
}
|