forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
195 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package lila.simul | ||
|
||
import akka.actor._ | ||
import akka.pattern.ask | ||
import com.typesafe.config.Config | ||
|
||
import lila.common.PimpedConfig._ | ||
import lila.hub.actorApi.map.Ask | ||
import lila.hub.{ ActorMap, Sequencer } | ||
import lila.socket.actorApi.GetVersion | ||
import lila.socket.History | ||
import makeTimeout.short | ||
|
||
final class Env( | ||
config: Config, | ||
system: ActorSystem, | ||
db: lila.db.Env, | ||
mongoCache: lila.memo.MongoCache.Builder, | ||
flood: lila.security.Flood, | ||
hub: lila.hub.Env, | ||
roundMap: ActorRef, | ||
lightUser: String => Option[lila.common.LightUser], | ||
isOnline: String => Boolean) { | ||
|
||
private val settings = new { | ||
val CollectionSimul = config getString "collection.simul" | ||
} | ||
import settings._ | ||
|
||
lazy val api = new SimulApi( | ||
simulColl = simulColl) | ||
|
||
private[simul] val simulColl = db(CollectionSimul) | ||
} | ||
|
||
object Env { | ||
|
||
private def hub = lila.hub.Env.current | ||
|
||
lazy val current = "[boot] simul" describes new Env( | ||
config = lila.common.PlayApp loadConfig "simul", | ||
system = lila.common.PlayApp.system, | ||
db = lila.db.Env.current, | ||
mongoCache = lila.memo.Env.current.mongoCache, | ||
flood = lila.security.Env.current.flood, | ||
hub = lila.hub.Env.current, | ||
roundMap = lila.round.Env.current.roundMap, | ||
lightUser = lila.user.Env.current.lightUser, | ||
isOnline = lila.user.Env.current.isOnline) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package lila.simul | ||
|
||
import chess.variant.Variant | ||
import org.joda.time.{ DateTime, Duration } | ||
import ornicar.scalalib.Random | ||
|
||
case class Simul( | ||
_id: Simul.ID, | ||
name: String, | ||
clock: SimulClock, | ||
nbPlayers: Int, | ||
players: List[SimulPlayer], | ||
pairings: List[SimulPairing], | ||
variant: Variant, | ||
createdAt: DateTime, | ||
createdBy: String) { | ||
|
||
def id = _id | ||
} | ||
|
||
object Simul { | ||
|
||
type ID = String | ||
|
||
def make( | ||
name: String, | ||
createdBy: String, | ||
clock: SimulClock, | ||
nbPlayers: Int, | ||
variant: chess.variant.Variant): Simul = Simul( | ||
_id = Random nextStringUppercase 8, | ||
name = name, | ||
clock = clock, | ||
createdBy = createdBy, | ||
createdAt = DateTime.now, | ||
variant = variant, | ||
nbPlayers = nbPlayers, | ||
players = Nil, | ||
pairings = Nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package lila.simul | ||
|
||
import org.joda.time.DateTime | ||
import reactivemongo.bson._ | ||
import reactivemongo.core.commands._ | ||
import scala.concurrent.duration._ | ||
|
||
import chess.variant.Variant | ||
import chess.Status | ||
import lila.db.Types.Coll | ||
|
||
private[simul] final class SimulApi( | ||
simulColl: Coll) { | ||
|
||
import lila.db.BSON.BSONJodaDateTimeHandler | ||
import reactivemongo.bson.Macros | ||
private implicit val StatusBSONHandler = new BSONHandler[BSONInteger, Status] { | ||
def read(bsonInt: BSONInteger): Status = Status(bsonInt.value) err s"No such status: ${bsonInt.value}" | ||
def write(x: Status) = BSONInteger(x.id) | ||
} | ||
private implicit val VariantBSONHandler = new BSONHandler[BSONInteger, Variant] { | ||
def read(bsonInt: BSONInteger): Variant = Variant(bsonInt.value) err s"No such variant: ${bsonInt.value}" | ||
def write(x: Variant) = BSONInteger(x.id) | ||
} | ||
private implicit val ClockBSONHandler = Macros.handler[SimulClock] | ||
private implicit val PlayerBSONHandler = Macros.handler[SimulPlayer] | ||
private implicit val PairingBSONHandler = Macros.handler[SimulPairing] | ||
private implicit val SimulBSONHandler = Macros.handler[Simul] | ||
|
||
def find(id: Simul.ID): Fu[Option[Simul]] = | ||
simulColl.find(BSONDocument("_id" -> id)).one[Simul] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package lila.simul | ||
|
||
// All durations are expressed in seconds | ||
case class SimulClock(limit: Int, increment: Int) { | ||
|
||
def limitInMinutes = limit / 60 | ||
|
||
def show = s"${limitInMinutes}+${increment}" | ||
|
||
def chessClock = chess.Clock(limit, increment) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package lila.simul | ||
|
||
import lila.game.{ PovRef, IdGenerator } | ||
|
||
case class SimulPairing( | ||
gameId: String, | ||
status: chess.Status, | ||
user: String, | ||
wins: Option[Boolean], | ||
turns: Option[Int]) { | ||
|
||
def finished = status >= chess.Status.Mate | ||
def playing = !finished | ||
|
||
def finish(s: chess.Status, w: Option[String], t: Int) = copy( | ||
status = s, | ||
wins = w map (user==), | ||
turns = t.some) | ||
} | ||
|
||
private[simul] object SimulPairing { | ||
|
||
def apply(user: String): SimulPairing = new SimulPairing( | ||
gameId = IdGenerator.game, | ||
status = chess.Status.Created, | ||
user = user, | ||
wins = none, | ||
turns = none) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package lila.simul | ||
|
||
import lila.rating.Perf | ||
import lila.user.{ User, Perfs} | ||
import lila.game.PerfPicker | ||
|
||
private[simul] case class SimulPlayer( | ||
id: String, | ||
rating: Int) { | ||
|
||
def is(userId: String): Boolean = id == userId | ||
def is(user: User): Boolean = is(user.id) | ||
def is(other: SimulPlayer): Boolean = is(other.id) | ||
} | ||
|
||
private[simul] object SimulPlayer { | ||
|
||
private[simul] def make(user: User, perfLens: Perfs => Perf): SimulPlayer = new SimulPlayer( | ||
id = user.id, | ||
rating = perfLens(user.perfs).intRating) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package lila | ||
|
||
import lila.socket.WithSocket | ||
|
||
package object simul extends PackageObject with WithPlay with WithSocket |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters