complete Ai stresser
parent
ab2b13cf02
commit
789a08b9c3
|
@ -1,4 +1,5 @@
|
|||
conf/application.conf
|
||||
conf/local.*
|
||||
bin/lilarc.local
|
||||
logs
|
||||
project/project
|
||||
|
|
|
@ -0,0 +1,53 @@
|
|||
package lila.app
|
||||
|
||||
import scala.concurrent.duration._
|
||||
|
||||
import akka.actor._
|
||||
import akka.pattern.{ ask, pipe }
|
||||
import ornicar.scalalib.Random.approximatly
|
||||
|
||||
import makeTimeout.short
|
||||
|
||||
private[app] final class AiStresser(env: lila.ai.Env, system: ActorSystem) {
|
||||
|
||||
def apply {
|
||||
|
||||
(1 to 1024) foreach { i ⇒
|
||||
system.scheduler.scheduleOnce((i*97) millis) {
|
||||
play(i % 8 + 1)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private def play(level: Int) = system.actorOf(Props(new Actor {
|
||||
|
||||
def newGame = lila.game.PgnRepo getOneRandom 30000 map { pgn ⇒
|
||||
Game((~pgn).split(' ').toList, 1)
|
||||
}
|
||||
|
||||
override def preStart {
|
||||
newGame pipeTo self
|
||||
}
|
||||
|
||||
def receive = {
|
||||
case Game(moves, it) if it >= moves.size ⇒ newGame pipeTo self
|
||||
case Game(moves, it) ⇒
|
||||
ai.play(moves take it mkString " ", none, level).effectFold(e ⇒ {
|
||||
logwarn("[ai] server play: " + e)
|
||||
newGame pipeTo self
|
||||
}, { _ ⇒
|
||||
system.scheduler.scheduleOnce(randomize(1 second)) {
|
||||
self ! Game(moves, it + 1)
|
||||
}
|
||||
})
|
||||
}
|
||||
}))
|
||||
|
||||
private def randomize(d: FiniteDuration, ratio: Float = 0.1f): FiniteDuration =
|
||||
approximatly(ratio)(d.toMillis) millis
|
||||
|
||||
private val ai = env.stockfishServer
|
||||
|
||||
private case class Game(moves: List[String], it: Int)
|
||||
}
|
|
@ -32,6 +32,10 @@ final class Env(
|
|||
postApi = Env.forum.postApi,
|
||||
getRank = Env.user.ranking.get) _
|
||||
|
||||
if (config getBoolean "ai.stress") {
|
||||
new AiStresser(Env.ai, system).apply
|
||||
}
|
||||
|
||||
system.actorOf(Props(new actor.Renderer), name = RendererName)
|
||||
|
||||
system.actorOf(Props(new actor.Router(
|
||||
|
|
|
@ -34,8 +34,6 @@ final class Env(
|
|||
|
||||
def isServer = IsServer
|
||||
|
||||
lazy val stresser = new Stresser(this, system)
|
||||
|
||||
// api actor
|
||||
system.actorOf(Props(new Actor {
|
||||
def receive = {
|
||||
|
|
|
@ -1,43 +0,0 @@
|
|||
package lila.ai
|
||||
|
||||
import akka.actor._
|
||||
import akka.pattern.{ ask, pipe }
|
||||
|
||||
import lila.db.api._
|
||||
import lila.game.tube.pgnTube
|
||||
import makeTimeout.short
|
||||
|
||||
private[ai] final class Stresser(env: Env, system: ActorSystem) {
|
||||
|
||||
def apply {
|
||||
|
||||
play(1)
|
||||
|
||||
}
|
||||
|
||||
private def play(level: Int) = new Actor {
|
||||
|
||||
private case class Game(moves: List[String], it: Int)
|
||||
|
||||
def newMoves =
|
||||
$find($query() skip util.Random.nextInt(30000)) map {
|
||||
Moves(_.split(' ').toList, 0)
|
||||
}
|
||||
|
||||
override def preStart {
|
||||
newMoves pipeTo self
|
||||
}
|
||||
|
||||
def receive = {
|
||||
case Game( => newMoves pipeTo self
|
||||
case Moves(moves) ⇒ moves splitAt 1 match {
|
||||
case (List(move), rest) => ai.play(rest take it mkString " ", none, level) addFailureEffect {
|
||||
case e ⇒ logwarn("[ai] server play: " + e)
|
||||
} >> {
|
||||
if (i
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private val ai = env.stockfishServer
|
||||
}
|
|
@ -42,7 +42,10 @@ final class ActorFSM(
|
|||
case Event(Out("readyok"), doing: Doing) ⇒ {
|
||||
val lines = config go doing.current
|
||||
lines.lastOption foreach { line =>
|
||||
println(doing.current.fold(_ ⇒ "P", _ ⇒ "A") + " " + line)
|
||||
println("[%d] %s - %s".format(
|
||||
doing.size,
|
||||
doing.current.fold(_ ⇒ "P", _ ⇒ "A"),
|
||||
line))
|
||||
}
|
||||
lines foreach process.write
|
||||
goto(Running)
|
||||
|
|
|
@ -4,6 +4,7 @@ import scala.concurrent.duration._
|
|||
|
||||
import akka.actor._
|
||||
import akka.pattern.{ ask, pipe }
|
||||
import ornicar.scalalib.Random.approximatly
|
||||
|
||||
final class Scheduler(system: ActorSystem, enabled: Boolean) {
|
||||
|
||||
|
@ -31,11 +32,6 @@ final class Scheduler(system: ActorSystem, enabled: Boolean) {
|
|||
enabled ! system.scheduler.scheduleOnce(delay)(op)
|
||||
}
|
||||
|
||||
private def randomize(d: FiniteDuration, ratio: Float = 0.1f): FiniteDuration = {
|
||||
import scala.util.Random
|
||||
import scala.math.round
|
||||
import ornicar.scalalib.Random.approximatly
|
||||
|
||||
approximatly(0.1f)(d.toMillis) millis
|
||||
}
|
||||
private def randomize(d: FiniteDuration, ratio: Float = 0.1f): FiniteDuration =
|
||||
approximatly(ratio)(d.toMillis) millis
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import play.api.libs.json._
|
|||
|
||||
import lila.common.PimpedJson._
|
||||
import lila.db.api._
|
||||
import lila.db.Implicits._
|
||||
import tube.pgnTube
|
||||
|
||||
object PgnRepo {
|
||||
|
@ -12,7 +13,7 @@ object PgnRepo {
|
|||
|
||||
def get(id: ID): Fu[String] = getOption(id) map (~_)
|
||||
|
||||
def getNonEmpty(id: ID): Fu[Option[String]] =
|
||||
def getNonEmpty(id: ID): Fu[Option[String]] =
|
||||
getOption(id) map (_ filter (_.nonEmpty))
|
||||
|
||||
def getOption(id: ID): Fu[Option[String]] =
|
||||
|
@ -25,6 +26,11 @@ object PgnRepo {
|
|||
}
|
||||
} map (_.flatten.toMap)
|
||||
|
||||
def getOneRandom(distrib: Int): Fu[Option[String]] =
|
||||
$find($query.all skip scala.util.Random.nextInt(distrib), 1) map {
|
||||
_.headOption flatMap (_ str "p")
|
||||
}
|
||||
|
||||
def save(id: ID, pgn: String): Funit =
|
||||
$update.field(id, "p", pgn, upsert = true)
|
||||
|
||||
|
|
Loading…
Reference in New Issue