activity: generalize puzzles score

This commit is contained in:
Thibault Duplessis 2017-07-18 19:54:11 +02:00
parent 1eac8c8cf0
commit 72a582b221
3 changed files with 27 additions and 45 deletions

View file

@ -38,6 +38,12 @@ object Activity {
case class RatingProg(before: Rating, after: Rating) {
def +(o: RatingProg) = copy(after = o.after)
}
object RatingProg {
def +(rp1O: Option[RatingProg], rp2O: Option[RatingProg]) = (rp1O, rp2O) match {
case (Some(rp1), Some(rp2)) => Some(rp1 + rp2)
case _ => rp2O orElse rp1O
}
}
case class Games(value: Map[PerfType, Score]) extends AnyVal {
def add(pt: PerfType, score: Score) = copy(
@ -51,10 +57,16 @@ object Activity {
win = win + s.win,
loss = loss + s.loss,
draw = draw + s.draw,
rp = (rp, s.rp) match {
case (Some(rp1), Some(rp2)) => Some(rp1 + rp2)
case (rp1, rp2) => rp2 orElse rp1
}
rp = RatingProg.+(rp, s.rp)
)
def size = win + loss + draw
}
object Score {
def make(res: Option[Boolean], rp: Option[RatingProg]) = Score(
win = res.has(true) ?? 1,
loss = res.has(false) ?? 1,
draw = res.isEmpty ?? 1,
rp = rp
)
}
implicit val ScoreZero = Zero.instance(Score(0, 0, 0, none))
@ -77,16 +89,10 @@ object Activity {
case class GameId(value: String) extends AnyVal
implicit val CompsZero = Zero.instance(CompAnalysis(Nil))
case class Puzzles(win: PuzzleList, loss: PuzzleList, ratingProg: Option[RatingProg])
case class PuzzleList(latest: List[PuzzleId], total: Int) {
def +(id: PuzzleId) = PuzzleList(
latest = (id :: latest) take 10,
total = total + 1
)
case class Puzzles(score: Score) extends AnyVal {
def +(s: Score) = Puzzles(score + s)
}
case class PuzzleId(value: Int) extends AnyVal
implicit val PuzzleListZero = Zero.instance(PuzzleList(Nil, 0))
implicit val PuzzlesZero = Zero.instance(Puzzles(PuzzleListZero.zero, PuzzleListZero.zero, none))
implicit val PuzzlesZero = Zero.instance(Puzzles(ScoreZero.zero))
def make(userId: User.ID) = Activity(
id = Id today userId,

View file

@ -10,11 +10,8 @@ private object ActivityAggregation {
def addGame(game: Game, userId: String)(a: Activity): Option[Activity] = for {
pt <- game.perfType
player <- game playerByUserId userId
won = game.winnerColor map (player.color ==)
score = Score(
win = won.has(true) ?? 1,
loss = won.has(false) ?? 1,
draw = won.isEmpty ?? 1,
score = Score.make(
res = game.winnerColor map (player.color ==),
rp = player.rating map { before =>
RatingProg(Rating(before), Rating(before + ~player.ratingDiff))
}
@ -29,15 +26,9 @@ private object ActivityAggregation {
a.copy(posts = a.posts.+(Posts.PostId(post.id), Posts.TopicId(topic.id)))
}
def addPuzzle(res: lila.puzzle.Puzzle.UserResult)(a: Activity) = a.copy(puzzles = {
val p = a.puzzles
val id = PuzzleId(res.puzzleId)
p.copy(
win = if (res.result.win) p.win + id else p.win,
loss = if (res.result.loss) p.loss + id else p.loss,
ratingProg = p.ratingProg.fold(RatingProg(Rating(res.rating._1), Rating(res.rating._2))) { rp =>
rp.copy(after = Rating(res.rating._2))
} some
)
}).some
def addPuzzle(res: lila.puzzle.Puzzle.UserResult)(a: Activity) =
a.copy(puzzles = a.puzzles + Score.make(
res = res.result.win.some,
rp = RatingProg(Rating(res.rating._1), Rating(res.rating._2)).some
)).some
}

View file

@ -64,22 +64,7 @@ private object BSONHandlers {
private implicit val postsMapHandler = MapValue.MapHandler[Posts.TopicId, List[Posts.PostId]]
private implicit val postsHandler = isoHandler[Posts, Map[Posts.TopicId, List[Posts.PostId]], Bdoc]((p: Posts) => p.posts, Posts.apply _)
private implicit val puzzleIdHandler = intAnyValHandler[PuzzleId](_.value, PuzzleId.apply)
private implicit val puzzleListHandler = Macros.handler[PuzzleList]
private implicit val puzzlesHandler = new lila.db.BSON[Puzzles] {
def reads(r: lila.db.BSON.Reader) = Puzzles(
win = r.getD[PuzzleList]("w"),
loss = r.getD[PuzzleList]("l"),
ratingProg = r.getO[RatingProg]("r")
)
def writes(w: lila.db.BSON.Writer, o: Puzzles) = BSONDocument(
"w" -> w.zero(o.win),
"l" -> w.zero(o.loss),
"r" -> o.ratingProg
)
}
private implicit val puzzlesHandler = Macros.handler[Puzzles]
implicit val activityHandler = new lila.db.BSON[Activity] {