2017-06-17 12:11:49 -06:00
|
|
|
package lila.socket
|
|
|
|
|
|
|
|
import chess.Centis
|
2019-12-24 13:01:35 -07:00
|
|
|
import com.github.blemale.scaffeine.Cache
|
2017-06-17 12:11:49 -06:00
|
|
|
import scala.concurrent.duration._
|
|
|
|
|
|
|
|
object UserLagCache {
|
2019-11-25 14:36:39 -07:00
|
|
|
|
2019-12-27 12:49:59 -07:00
|
|
|
private val cache: Cache[String, Centis] = lila.memo.CacheApi.scaffeineNoScheduler
|
2019-07-09 10:22:45 -06:00
|
|
|
.expireAfterWrite(15 minutes)
|
2020-07-07 02:34:48 -06:00
|
|
|
.build[String, Centis]()
|
2017-06-17 12:11:49 -06:00
|
|
|
|
2019-12-24 13:01:35 -07:00
|
|
|
def put(userId: String, lag: Centis): Unit =
|
|
|
|
if (lag.centis >= 0)
|
2020-05-05 22:11:15 -06:00
|
|
|
cache.put(
|
|
|
|
userId,
|
|
|
|
cache.getIfPresent(userId).fold(lag) {
|
|
|
|
_ avg lag
|
|
|
|
}
|
|
|
|
)
|
2017-06-17 12:11:49 -06:00
|
|
|
|
2017-07-05 10:13:01 -06:00
|
|
|
def get(userId: String): Option[Centis] = cache.getIfPresent(userId)
|
2017-07-03 22:43:30 -06:00
|
|
|
|
2020-05-05 22:11:15 -06:00
|
|
|
def getLagRating(userId: String): Option[Int] =
|
|
|
|
get(userId) map {
|
|
|
|
case i if i <= Centis(15) => 4
|
|
|
|
case i if i <= Centis(30) => 3
|
|
|
|
case i if i <= Centis(50) => 2
|
|
|
|
case _ => 1
|
|
|
|
}
|
2018-12-15 02:23:41 -07:00
|
|
|
}
|