lila/modules/game/src/main/CrosstableApi.scala

111 lines
3.2 KiB
Scala
Raw Normal View History

package lila.game
import org.joda.time.DateTime
2020-04-03 07:03:57 -06:00
import scala.concurrent.ExecutionContext
import lila.db.AsyncCollFailingSilently
2016-04-01 06:23:56 -06:00
import lila.db.dsl._
2020-04-03 07:03:57 -06:00
import lila.user.User
2016-04-01 06:23:56 -06:00
final class CrosstableApi(
coll: AsyncCollFailingSilently,
matchupColl: AsyncCollFailingSilently,
enabled: () => Boolean
2020-04-03 07:03:57 -06:00
)(implicit ec: ExecutionContext) {
2017-10-21 14:01:50 -06:00
import Crosstable.{ Matchup, Result }
import Crosstable.{ BSONFields => F }
2020-05-05 22:11:15 -06:00
def apply(game: Game): Fu[Option[Crosstable]] =
2020-09-21 01:28:28 -06:00
game.twoUserIds ?? { case (u1, u2) =>
apply(u1, u2) dmap some
2020-05-05 22:11:15 -06:00
}
2020-05-05 22:11:15 -06:00
def withMatchup(game: Game): Fu[Option[Crosstable.WithMatchup]] =
2020-09-21 01:28:28 -06:00
game.twoUserIds ?? { case (u1, u2) =>
withMatchup(u1, u2) dmap some
2020-05-05 22:11:15 -06:00
}
2020-04-03 07:03:57 -06:00
def apply(u1: User.ID, u2: User.ID): Fu[Crosstable] =
2021-09-18 03:53:02 -06:00
justFetch(u1, u2) dmap { _ | Crosstable.empty(u1, u2) }
def justFetch(u1: User.ID, u2: User.ID): Fu[Option[Crosstable]] = enabled() ??
coll(_.one[Crosstable](select(u1, u2)))
2020-04-03 07:03:57 -06:00
def withMatchup(u1: User.ID, u2: User.ID): Fu[Crosstable.WithMatchup] =
2020-11-26 02:02:50 -07:00
apply(u1, u2) zip getMatchup(u1, u2) dmap { case (crosstable, matchup) =>
2020-09-21 01:28:28 -06:00
Crosstable.WithMatchup(crosstable, matchup)
}
def nbGames(u1: User.ID, u2: User.ID): Fu[Int] = enabled() ??
coll {
_.find(
2019-12-13 07:30:20 -07:00
select(u1, u2),
$doc("s1" -> true, "s2" -> true).some
)
.one[Bdoc] dmap { res =>
~(for {
o <- res
s1 <- o.int("s1")
s2 <- o.int("s2")
} yield (s1 + s2) / 10)
}
2019-12-13 07:30:20 -07:00
}
def add(game: Game): Funit =
2020-05-05 22:11:15 -06:00
game.userIds.distinct.sorted match {
case List(u1, u2) =>
val result = Result(game.id, game.winnerUserId)
val bsonResult = Crosstable.crosstableBSONHandler.writeResult(result, u1)
def incScore(userId: User.ID): Int =
game.winnerUserId match {
case Some(u) if u == userId => 10
case None => 5
case _ => 0
}
val inc1 = incScore(u1)
val inc2 = incScore(u2)
val updateCrosstable = enabled() ?? coll {
_.update
.one(
select(u1, u2),
$inc(
F.score1 -> inc1,
F.score2 -> inc2
) ++ $push(
Crosstable.BSONFields.results -> $doc(
"$each" -> List(bsonResult),
"$slice" -> -Crosstable.maxGames
)
),
upsert = true
)
.void
}
val updateMatchup = matchupColl {
_.update
.one(
select(u1, u2),
$inc(
F.score1 -> inc1,
F.score2 -> inc2
) ++ $set(
F.lastPlayed -> DateTime.now
),
upsert = true
)
.void
}
2020-05-05 22:11:15 -06:00
updateCrosstable zip updateMatchup void
case _ => funit
}
2017-07-10 02:28:14 -06:00
private val matchupProjection = $doc(F.lastPlayed -> false)
def getMatchup(u1: User.ID, u2: User.ID): Fu[Option[Matchup]] =
matchupColl(_.find(select(u1, u2), matchupProjection.some).one[Matchup])
2019-01-22 03:41:39 -07:00
private def select(u1: User.ID, u2: User.ID) =
$id(Crosstable.makeKey(u1, u2))
}