migrate practice module

rm0193-mapreduce
Thibault Duplessis 2019-12-03 19:16:42 -06:00
parent 42b9982847
commit 37b6c875b8
8 changed files with 40 additions and 47 deletions

View File

@ -411,11 +411,6 @@ memo {
config = flag
}
}
practice {
collection {
progress = practice_progress
}
}
redis {
uri = "redis://127.0.0.1"
# uri = "redis-socket:///var/run/redis/redis-server.sock"

View File

@ -42,9 +42,12 @@ object config {
implicit val domainLoader = strLoader(Domain.unsafe)
implicit val netLoader = AutoConfig.loader[NetConfig]
implicit val listLoader: ConfigLoader[List[String]] = ConfigLoader { c => k =>
implicit val strListLoader: ConfigLoader[List[String]] = ConfigLoader { c => k =>
c.getStringList(k).asScala.toList
}
implicit def listLoader[A](implicit l: ConfigLoader[A]): ConfigLoader[List[A]] = ConfigLoader { c => k =>
c.getConfigList(k).asScala.toList map { l.load(_) }
}
def strLoader[A](f: String => A): ConfigLoader[A] = ConfigLoader(_.getString) map f
def intLoader[A](f: Int => A): ConfigLoader[A] = ConfigLoader(_.getInt) map f

View File

@ -23,19 +23,19 @@ trait Handlers {
def isoHandler[A, B](to: A => B, from: B => A)(implicit handler: BSONHandler[B]): BSONHandler[A] =
isoHandler(Iso(from, to))
def stringIsoHandler[A](implicit iso: StringIso[A]): BSONHandler[A] = isoHandler[A, String](iso)
def stringIsoHandler[A](implicit iso: StringIso[A]): BSONHandler[A] = BSONStringHandler.as[A](iso.from, iso.to)
def stringAnyValHandler[A](to: A => String, from: String => A): BSONHandler[A] = stringIsoHandler(Iso(from, to))
def intIsoHandler[A](implicit iso: IntIso[A]): BSONHandler[A] = isoHandler[A, Int](iso)
def intIsoHandler[A](implicit iso: IntIso[A]): BSONHandler[A] = BSONIntegerHandler.as[A](iso.from, iso.to)
def intAnyValHandler[A](to: A => Int, from: Int => A): BSONHandler[A] = intIsoHandler(Iso(from, to))
def booleanIsoHandler[A](implicit iso: BooleanIso[A]): BSONHandler[A] = isoHandler[A, Boolean](iso)
def booleanIsoHandler[A](implicit iso: BooleanIso[A]): BSONHandler[A] = BSONBooleanHandler.as[A](iso.from, iso.to)
def booleanAnyValHandler[A](to: A => Boolean, from: Boolean => A): BSONHandler[A] = booleanIsoHandler(Iso(from, to))
def doubleIsoHandler[A](implicit iso: DoubleIso[A]): BSONHandler[A] = isoHandler[A, Double](iso)
def doubleIsoHandler[A](implicit iso: DoubleIso[A]): BSONHandler[A] = BSONDoubleHandler.as[A](iso.from, iso.to)
def doubleAnyValHandler[A](to: A => Double, from: Double => A): BSONHandler[A] = doubleIsoHandler(Iso(from, to))
def dateIsoHandler[A](implicit iso: Iso[DateTime, A]): BSONHandler[A] = isoHandler[A, DateTime](iso)
def dateIsoHandler[A](implicit iso: Iso[DateTime, A]): BSONHandler[A] = BSONJodaDateTimeHandler.as[A](iso.from, iso.to)
def quickHandler[T](read: PartialFunction[BSONValue, T], write: T => BSONValue): BSONHandler[T] = new BSONHandler[T] {
def readTry(bson: BSONValue) = read.andThen(Success(_)).applyOrElse(

View File

@ -2,15 +2,16 @@ package lila.practice
import lila.db.BSON
import lila.db.dsl._
import lila.study.Chapter
import lila.study.BSONHandlers._
import reactivemongo.api.bson._
object BSONHandlers {
import PracticeProgress.NbMoves
import PracticeProgress.{ NbMoves, ChapterNbMoves }
private implicit val nbMovesHandler = intIsoHandler(PracticeProgress.nbMovesIso)
private implicit val chapterNbMovesHandler = BSON.MapValue.MapHandler[Chapter.Id, NbMoves]
private implicit val nbMovesHandler: BSONHandler[NbMoves] = intIsoHandler(PracticeProgress.nbMovesIso)
private implicit val chapterNbMovesHandler: BSONHandler[ChapterNbMoves] =
implicitly[BSONHandler[Map[lila.study.Chapter.Id, NbMoves]]]
implicit val practiceProgressIdHandler = stringAnyValHandler[PracticeProgress.Id](_.value, PracticeProgress.Id.apply)
implicit val practiceProgressHandler = Macros.handler[PracticeProgress]

View File

@ -1,37 +1,26 @@
package lila.practice
import akka.actor._
import com.typesafe.config.Config
import com.softwaremill.macwire._
import play.api.Configuration
import lila.common.config._
final class Env(
config: Config,
configStore: lila.memo.ConfigStore.Builder,
appConfig: Configuration,
configStoreApi: lila.memo.ConfigStore.Builder,
studyApi: lila.study.StudyApi,
asyncCache: lila.memo.AsyncCache.Builder,
db: lila.db.Env
) {
private val CollectionProgress = config getString "collection.progress"
private lazy val coll = db(CollName("practice_progress"))
lazy val api = new PracticeApi(
coll = db(CollectionProgress),
configStore = configStore[PracticeConfig]("practice", logger),
asyncCache = asyncCache,
studyApi = studyApi
)
import PracticeConfig.loader
private lazy val configStore = configStoreApi[PracticeConfig]("practice", logger)
lazy val api: PracticeApi = wire[PracticeApi]
lila.common.Bus.subscribeFun("study") {
case lila.study.actorApi.SaveStudy(study) => api.structure onSave study
}
}
object Env {
lazy val current: Env = "practice" boot new Env(
config = lila.common.PlayApp loadConfig "practice",
configStore = lila.memo.Env.current.configStore,
studyApi = lila.study.Env.current.api,
asyncCache = lila.memo.Env.current.asyncCache,
db = lila.db.Env.current
)
}

View File

@ -81,7 +81,7 @@ final class PracticeApi(
coll.uno[PracticeProgress]($id(user.id)) map { _ | PracticeProgress.empty(PracticeProgress.Id(user.id)) }
private def save(p: PracticeProgress): Funit =
coll.update($id(p.id), p, upsert = true).void
coll.update.one($id(p.id), p, upsert = true).void
def setNbMoves(user: User, chapterId: Chapter.Id, score: NbMoves) = {
get(user) flatMap { prog =>
@ -94,6 +94,6 @@ final class PracticeApi(
}
def reset(user: User) =
coll.remove($id(user.id)).void
coll.delete.one($id(user.id)).void
}
}

View File

@ -1,6 +1,8 @@
package lila.practice
import io.methvin.play.autoconfig._
import lila.study.Study
import lila.common.config._
case class PracticeConfig(
sections: List[PracticeConfigSection]
@ -11,6 +13,11 @@ case class PracticeConfig(
object PracticeConfig {
val empty = PracticeConfig(Nil)
private implicit val studyLoader = AutoConfig.loader[PracticeConfigStudy]
private implicit val studiesLoader = listLoader[List[PracticeConfigStudy]]
private implicit val sectionLoader = AutoConfig.loader[PracticeConfigSection]
implicit val loader = AutoConfig.loader[PracticeConfig]
}
case class PracticeConfigSection(

View File

@ -1,7 +1,5 @@
package lila.practice
import scala.collection.breakOut
import lila.study.{ Study, Chapter }
case class PracticeStructure(
@ -12,16 +10,16 @@ case class PracticeStructure(
sections.flatMap(_ study id).headOption
lazy val studiesByIds: Map[Study.Id, PracticeStudy] =
sections.flatMap(_.studies).map { s =>
sections.view.flatMap(_.studies).map { s =>
s.id -> s
}(breakOut)
}.toMap
lazy val sectionsByStudyIds: Map[Study.Id, PracticeSection] =
sections.flatMap { sec =>
sections.view.flatMap { sec =>
sec.studies.map { stu =>
stu.id -> sec
}
}(breakOut)
}.toMap
lazy val chapterIds: List[Chapter.Id] = sections.flatMap(_.studies).flatMap(_.chapterIds)
@ -39,9 +37,9 @@ case class PracticeSection(
) {
lazy val studiesByIds: Map[Study.Id, PracticeStudy] =
studies.map { s =>
studies.view.map { s =>
s.id -> s
}(breakOut)
}.toMap
def study(id: Study.Id): Option[PracticeStudy] = studiesByIds get id
}