Skip to content

Commit

Permalink
make tournament pairing more efficient
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Dec 21, 2015
1 parent e78509a commit f0a7d0d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
2 changes: 1 addition & 1 deletion modules/security/src/main/Store.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ object Store {
).one[BSONDocument] map { _ flatMap (_.getAs[String]("user")) }

case class UserIdAndFingerprint(user: String, fp: Option[String])
private implicit val UserIdAndFingerprintBSONReader = Macros.handler[UserIdAndFingerprint]
private implicit val UserIdAndFingerprintBSONReader = Macros.reader[UserIdAndFingerprint]

def userIdAndFingerprint(sessionId: String): Fu[Option[UserIdAndFingerprint]] =
storeColl.find(
Expand Down
9 changes: 8 additions & 1 deletion modules/tournament/src/main/BSONHandlers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ object BSONHandlers {

private implicit val tournamentClockBSONHandler = Macros.handler[TournamentClock]

private implicit val leaderboardRatio = new BSONHandler[BSONInteger, LeaderboardApi.Ratio] {
private implicit val leaderboardRatio = new BSONHandler[BSONInteger, LeaderboardApi.Ratio] {
def read(b: BSONInteger) = LeaderboardApi.Ratio(b.value.toDouble / 100000)
def write(x: LeaderboardApi.Ratio) = BSONInteger((x.value * 100000).toInt)
}
Expand Down Expand Up @@ -127,6 +127,13 @@ object BSONHandlers {
"b2" -> w.intO(o.berserk2))
}

implicit val pairingUidsReader = new BSONDocumentReader[Pairing.Uids] {
def read(doc: BSONDocument) = ~doc.getAs[List[String]]("u") match {
case List(u1, u2) => Pairing.Uids(u1, u2)
case x => sys error s"Invalid pairing uids $x"
}
}

implicit val leaderboardEntryHandler = new BSON[LeaderboardApi.Entry] {
def reads(r: BSON.Reader) = LeaderboardApi.Entry(
id = r str "_id",
Expand Down
9 changes: 6 additions & 3 deletions modules/tournament/src/main/Pairing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ case class Pairing(
def quickDraw = draw && turns.??(20 >)
def notSoQuickFinish = finished && turns.??(14 <=)

def opponentOf(user: String): Option[String] =
if (user == user1) user2.some else if (user == user2) user1.some else none

def wonBy(user: String): Boolean = winner.??(user ==)
def draw: Boolean = finished && winner.isEmpty

Expand All @@ -59,6 +56,12 @@ case class Pairing(

private[tournament] object Pairing {

case class Uids(u1: String, u2: String) {
def contains(user: String): Boolean = u1 == user || u2 == user
def opponentOf(user: String): Option[String] =
if (user == u1) Some(u2) else if (user == u2) Some(u1) else None
}

def apply(tourId: String, u1: String, u2: String): Pairing = new Pairing(
id = IdGenerator.game,
tourId = tourId,
Expand Down
7 changes: 4 additions & 3 deletions modules/tournament/src/main/PairingRepo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ object PairingRepo {
def recentByTour(tourId: String, nb: Int): Fu[List[Pairing]] =
coll.find(selectTour(tourId)).sort(recentSort).cursor[Pairing]().collect[List](nb)

def recentByTourAndUserIds(tourId: String, userIds: Iterable[String], nb: Int): Fu[List[Pairing]] =
def recentUidsByTourAndUserIds(tourId: String, userIds: Iterable[String], nb: Int): Fu[List[Pairing.Uids]] =
coll.find(
selectTour(tourId) ++ BSONDocument("u" -> BSONDocument("$in" -> userIds))
).sort(recentSort).cursor[Pairing]().collect[List](nb)
selectTour(tourId) ++ BSONDocument("u" -> BSONDocument("$in" -> userIds)),
BSONDocument("_id" -> false, "u" -> true)
).sort(recentSort).cursor[Pairing.Uids]().collect[List](nb)

def recentIdsByTourAndUserId(tourId: String, userId: String, nb: Int): Fu[List[String]] =
coll.find(
Expand Down
6 changes: 3 additions & 3 deletions modules/tournament/src/main/arena/PairingSystem.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ object PairingSystem extends AbstractPairingSystem {

case class Data(
tour: Tournament,
recentPairings: List[Pairing],
recentPairings: List[Pairing.Uids],
ranking: Map[String, Int],
nbActiveUsers: Int)

Expand All @@ -23,7 +23,7 @@ object PairingSystem extends AbstractPairingSystem {
users: WaitingUsers,
ranking: Ranking): Fu[Pairings] = Chronometer.result {
for {
recentPairings <- PairingRepo.recentByTourAndUserIds(tour.id, users.all, Math.min(120, users.size * 5))
recentPairings <- PairingRepo.recentUidsByTourAndUserIds(tour.id, users.all, Math.min(100, users.size * 4))
nbActiveUsers <- PlayerRepo.countActive(tour.id)
data = Data(tour, recentPairings, ranking, nbActiveUsers)
preps <- if (recentPairings.isEmpty) evenOrAll(data, users)
Expand All @@ -37,7 +37,7 @@ object PairingSystem extends AbstractPairingSystem {
} yield pairings
} map {
_.resultAndLogIfSlow(500, "tourpairing") { lap =>
s"Arena createPairind http:https://lichess.org/tournament/${tour.id} ${lap.result.size} pairings in ${lap.millis}ms"
s"Arena createPairing http:https://lichess.org/tournament/${tour.id} ${lap.result.size} pairings in ${lap.millis}ms"
}
}

Expand Down

0 comments on commit f0a7d0d

Please sign in to comment.