This commit is contained in:
Thibault Duplessis 2016-11-30 15:06:24 +01:00
parent e94a0e3b90
commit 828a012199
5 changed files with 61 additions and 20 deletions

View file

@ -8,7 +8,8 @@ import lila.common.PimpedConfig._
final class Env(
system: akka.actor.ActorSystem,
config: Config) {
config: Config,
onStart: String => Unit) {
private val PoolList = (config getStringList "list").toList flatMap PoolConfig.parse
@ -19,6 +20,7 @@ object Env {
lazy val current: Env = "pool" boot new Env(
system = lila.common.PlayApp.system,
config = lila.common.PlayApp loadConfig "pool")
config = lila.common.PlayApp loadConfig "pool",
onStart = lila.game.Env.current.onStart)
}

View file

@ -0,0 +1,36 @@
package lila.pool
import lila.game.{ Game, Player, GameRepo }
import lila.user.{ User, UserRepo }
private final class GameStarter(onStart: Game.ID => Unit) {
def apply(pool: PoolConfig, pairings: List[Pairing]): Funit =
pairings.map(one(pool)).sequenceFu.void
private def one(pool: PoolConfig)(pairing: Pairing): Funit =
UserRepo.byIds(pairing.members.map(_.userId)) flatMap {
case List(u1, u2) => for {
u1White <- UserRepo.firstGetsWhite(u1.id, u2.id)
(whiteUser, blackUser) = u1White.fold(u1 -> u2, u2 -> u1)
game = makeGame(pool, whiteUser, blackUser).start
_ <- GameRepo insertDenormalized game
} yield {
onStart(game.id)
// lila.mon.lobby.hook.join()
// lila.mon.lobby.hook.acceptedRatedClock(hook.clock.show)()
}
case _ => funit
}
private def makeGame(pool: PoolConfig, whiteUser: User, blackUser: User) = Game.make(
game = chess.Game(
board = chess.Board init chess.variant.Standard,
clock = pool.clock.some),
whitePlayer = Player.white.withUser(whiteUser.id, whiteUser.perfs(pool.perfType)),
blackPlayer = Player.black.withUser(blackUser.id, blackUser.perfs(pool.perfType)),
mode = chess.Mode.Rated,
variant = chess.variant.Standard,
source = lila.game.Source.Pool,
pgnImport = None)
}

View file

@ -1,10 +1,10 @@
package lila.pool
object MatchMaking {
case class Pairing(p1: PoolMember, p2: PoolMember) {
def members = Vector(p1, p2)
}
case class Pairing(p1: PoolMember, p2: PoolMember) {
def members = Vector(p1, p2)
}
object MatchMaking {
def apply(members: Vector[PoolMember]): Vector[Pairing] =
members.sortBy(-_.rating) grouped 2 collect {

View file

@ -22,6 +22,7 @@ private final class PoolActor(config: PoolConfig) extends Actor {
case MakePairings =>
val pairings = MatchMaking(members)
members = members diff pairings.flatMap(_.members)
sender ! pairings
}
}

View file

@ -101,19 +101,21 @@ object UserRepo {
}
}
def firstGetsWhite(u1O: Option[String], u2O: Option[String]): Fu[Boolean] =
def firstGetsWhite(u1: User.ID, u2: User.ID): Fu[Boolean] = coll.find(
$inIds(List(u1, u2)),
$id(true)
).sort($doc(F.colorIt -> 1)).uno[Bdoc].map {
_.fold(scala.util.Random.nextBoolean) { doc =>
doc.getAs[User.ID]("_id") contains u1
}
}.addEffect { v =>
incColor(u1, v.fold(1, -1))
incColor(u2, v.fold(-1, 1))
}
def firstGetsWhite(u1O: Option[User.ID], u2O: Option[User.ID]): Fu[Boolean] =
(u1O |@| u2O).tupled.fold(fuccess(scala.util.Random.nextBoolean)) {
case (u1, u2) => coll.find(
$inIds(List(u1, u2)),
$id(true)
).sort($doc(F.colorIt -> 1)).uno[Bdoc].map {
_.fold(scala.util.Random.nextBoolean) { doc =>
doc.getAs[String]("_id") contains u1
}
}.addEffect { v =>
incColor(u1, v.fold(1, -1))
incColor(u2, v.fold(-1, 1))
}
case (u1, u2) => firstGetsWhite(u1, u2)
}
def incColor(userId: User.ID, value: Int): Unit =
@ -189,8 +191,8 @@ object UserRepo {
case _ => none
}) ifFalse ai
).flatten.map(k => BSONElement(k, BSONInteger(1))) ::: List(
totalTime map BSONInteger.apply map(v => BSONElement(s"${F.playTime}.total", v)),
tvTime map BSONInteger.apply map(v => BSONElement(s"${F.playTime}.tv", v))
totalTime map BSONInteger.apply map (v => BSONElement(s"${F.playTime}.total", v)),
tvTime map BSONInteger.apply map (v => BSONElement(s"${F.playTime}.tv", v))
).flatten
coll.update($id(id), $inc(incs))