update user ranking every 10 minutes
This commit is contained in:
parent
4a52457fbb
commit
8c0fce29a8
|
@ -29,7 +29,8 @@ final class Env(
|
||||||
bookmarkApi = Env.bookmark.api,
|
bookmarkApi = Env.bookmark.api,
|
||||||
eloCalculator = Env.round.eloCalculator,
|
eloCalculator = Env.round.eloCalculator,
|
||||||
relationApi = Env.relation.api,
|
relationApi = Env.relation.api,
|
||||||
postApi = Env.forum.postApi) _
|
postApi = Env.forum.postApi,
|
||||||
|
getRank = Env.user.ranking.get) _
|
||||||
|
|
||||||
system.actorOf(Props(new actor.Renderer), name = RendererName)
|
system.actorOf(Props(new actor.Renderer), name = RendererName)
|
||||||
|
|
||||||
|
|
|
@ -2,11 +2,11 @@ package lila.app
|
||||||
package mashup
|
package mashup
|
||||||
|
|
||||||
import chess.{ EloCalculator, Color }
|
import chess.{ EloCalculator, Color }
|
||||||
import lila.game.{ GameRepo, Game }
|
|
||||||
import lila.user.{ User, UserRepo, Context, EloChart }
|
|
||||||
import lila.bookmark.BookmarkApi
|
import lila.bookmark.BookmarkApi
|
||||||
import lila.relation.RelationApi
|
|
||||||
import lila.forum.PostApi
|
import lila.forum.PostApi
|
||||||
|
import lila.game.{ GameRepo, Game }
|
||||||
|
import lila.relation.RelationApi
|
||||||
|
import lila.user.{ User, UserRepo, Context, EloChart }
|
||||||
|
|
||||||
case class UserInfo(
|
case class UserInfo(
|
||||||
user: User,
|
user: User,
|
||||||
|
@ -27,18 +27,15 @@ case class UserInfo(
|
||||||
|
|
||||||
object UserInfo {
|
object UserInfo {
|
||||||
|
|
||||||
private val rankMinElo = 1800
|
|
||||||
|
|
||||||
def apply(
|
def apply(
|
||||||
countUsers: () ⇒ Fu[Int],
|
countUsers: () ⇒ Fu[Int],
|
||||||
bookmarkApi: BookmarkApi,
|
bookmarkApi: BookmarkApi,
|
||||||
eloCalculator: EloCalculator,
|
eloCalculator: EloCalculator,
|
||||||
relationApi: RelationApi,
|
relationApi: RelationApi,
|
||||||
postApi: PostApi)(user: User, ctx: Context): Fu[UserInfo] =
|
postApi: PostApi,
|
||||||
((user.elo >= rankMinElo) ?? {
|
getRank: String ⇒ Fu[Option[Int]])(user: User, ctx: Context): Fu[UserInfo] =
|
||||||
UserRepo rank user flatMap { rank ⇒
|
(getRank(user.id) flatMap {
|
||||||
countUsers() map { nbUsers ⇒ (rank -> nbUsers).some }
|
_ ?? { rank ⇒ countUsers() map { nb ⇒ (rank -> nb).some } }
|
||||||
}
|
|
||||||
}) zip
|
}) zip
|
||||||
((ctx is user) ?? {
|
((ctx is user) ?? {
|
||||||
GameRepo count (_ notFinished user.id) map (_.some)
|
GameRepo count (_ notFinished user.id) map (_.some)
|
||||||
|
|
|
@ -25,4 +25,10 @@ object AsyncCache {
|
||||||
timeToIdle: Duration = Duration.Zero) = new AsyncCache(
|
timeToIdle: Duration = Duration.Zero) = new AsyncCache(
|
||||||
cache = LruCache(maxCapacity, initialCapacity, timeToLive, timeToIdle),
|
cache = LruCache(maxCapacity, initialCapacity, timeToLive, timeToIdle),
|
||||||
f = f)
|
f = f)
|
||||||
|
|
||||||
|
def single[V](
|
||||||
|
f: ⇒ Fu[V],
|
||||||
|
timeToLive: Duration = Duration.Zero) = new AsyncCache[Boolean, V](
|
||||||
|
cache = LruCache(timeToLive = timeToLive),
|
||||||
|
f = _ ⇒ f)
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,7 @@ final class Env(
|
||||||
val EloUpdaterFloor = config getInt "elo_updater.floor"
|
val EloUpdaterFloor = config getInt "elo_updater.floor"
|
||||||
val CachedNbTtl = config duration "cached.nb.ttl"
|
val CachedNbTtl = config duration "cached.nb.ttl"
|
||||||
val OnlineTtl = config duration "online.ttl"
|
val OnlineTtl = config duration "online.ttl"
|
||||||
|
val RankingTtl = config duration "ranking.ttl"
|
||||||
val CollectionUser = config getString "collection.user"
|
val CollectionUser = config getString "collection.user"
|
||||||
val CollectionHistory = config getString "collection.history"
|
val CollectionHistory = config getString "collection.history"
|
||||||
}
|
}
|
||||||
|
@ -34,6 +35,8 @@ final class Env(
|
||||||
|
|
||||||
lazy val onlineUserIdMemo = new ExpireSetMemo(ttl = OnlineTtl)
|
lazy val onlineUserIdMemo = new ExpireSetMemo(ttl = OnlineTtl)
|
||||||
|
|
||||||
|
lazy val ranking = new Ranking(ttl = RankingTtl)
|
||||||
|
|
||||||
val forms = DataForm
|
val forms = DataForm
|
||||||
|
|
||||||
def usernameOption(id: String): Fu[Option[String]] = cached username id
|
def usernameOption(id: String): Fu[Option[String]] = cached username id
|
||||||
|
|
24
modules/user/src/main/Ranking.scala
Normal file
24
modules/user/src/main/Ranking.scala
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
package lila.user
|
||||||
|
|
||||||
|
import scala.concurrent.duration._
|
||||||
|
|
||||||
|
import play.api.libs.json.Json
|
||||||
|
|
||||||
|
import lila.db.api._
|
||||||
|
import lila.db.Implicits._
|
||||||
|
import lila.memo.AsyncCache
|
||||||
|
import tube.userTube
|
||||||
|
|
||||||
|
final class Ranking(ttl: Duration) {
|
||||||
|
|
||||||
|
def get(id: String): Fu[Option[Int]] = cache(true) map (_ get id)
|
||||||
|
|
||||||
|
private val cache = AsyncCache.single(compute, timeToLive = ttl)
|
||||||
|
|
||||||
|
private def compute: Fu[Map[String, Int]] =
|
||||||
|
$primitive(
|
||||||
|
UserRepo.enabledQuery ++ Json.obj("elo" -> $gt(User.STARTING_ELO)),
|
||||||
|
"_id",
|
||||||
|
_ sort UserRepo.sortEloDesc
|
||||||
|
)(_.asOpt[String]) map { _.pp.zipWithIndex.toMap }
|
||||||
|
}
|
3
todo
3
todo
|
@ -59,6 +59,9 @@ check team average elo http://en.lichess.org/inbox/benuegti#bottom
|
||||||
if game ends during move -> bug
|
if game ends during move -> bug
|
||||||
challenge friends
|
challenge friends
|
||||||
computer players ranks once a day
|
computer players ranks once a day
|
||||||
|
lobby don't show duplicated entries
|
||||||
|
prevent double lobby entry per browser
|
||||||
|
players world map
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue