lila/modules/socket/src/main/UserLagCache.scala

32 lines
782 B
Scala
Raw Normal View History

package lila.socket
import chess.Centis
2019-12-24 13:01:35 -07:00
import com.github.blemale.scaffeine.Cache
import scala.concurrent.duration._
object UserLagCache {
2019-11-25 14:36:39 -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]()
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
}
)
def get(userId: String): Option[Centis] = cache.getIfPresent(userId)
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
}