Skip to content

Commit

Permalink
send more games to irwin
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Aug 23, 2018
1 parent 7c10745 commit c8baf65
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 31 deletions.
12 changes: 0 additions & 12 deletions modules/game/src/main/GameRepo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -430,16 +430,4 @@ object GameRepo {
).sort(Query.sortCreated)
.cursor[Game](ReadPreference.secondaryPreferred)
.list(nb)

def recentStandardAnalysedGamesByUserId(userId: User.ID, nb: Int) =
coll.find(
Query.finished
++ Query.variantStandard
++ Query.rated
++ Query.analysed(true)
++ Query.user(userId)
++ Query.turnsGt(20)
).sort(Query.sortCreated)
.cursor[Game](ReadPreference.secondaryPreferred)
.list(nb)
}
45 changes: 33 additions & 12 deletions modules/irwin/src/main/IrwinApi.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package lila.irwin

import org.joda.time.DateTime
import reactivemongo.api.ReadPreference
import reactivemongo.bson._

import lila.analyse.Analysis.Analyzed
import lila.analyse.AnalysisRepo
import lila.db.dsl._
import lila.game.{ Pov, GameRepo }
import lila.game.{ Game, Pov, GameRepo, Query }
import lila.report.{ Report, Mod, Suspect, Reporter, SuspectId, ModId }
import lila.tournament.{ Tournament, TournamentTop }
import lila.user.{ User, UserRepo }
import lila.analyse.AnalysisRepo

final class IrwinApi(
reportColl: Coll,
Expand Down Expand Up @@ -83,14 +84,15 @@ final class IrwinApi(
insert(suspect, _.Moderator)
}

private[irwin] def insert(suspect: Suspect, origin: Origin.type => Origin): Funit =
getGames(suspect) map { games =>
bus.publish(IrwinRequest(
suspect = suspect,
origin = origin(Origin),
games = games
), 'irwin)
}
private[irwin] def insert(suspect: Suspect, origin: Origin.type => Origin): Funit = for {
analyzed <- getAnalyzedGames(suspect, 15)
more <- getMoreGames(suspect, 20 - analyzed.size)
all = analyzed.map { a => a.game -> a.analysis.some } ::: more.map(_ -> none)
} yield bus.publish(IrwinRequest(
suspect = suspect,
origin = origin(Origin),
games = all
), 'irwin)

private[irwin] def fromTournamentLeaders(leaders: Map[Tournament, TournamentTop]): Funit =
lila.common.Future.applySequentially(leaders.toList) {
Expand All @@ -110,8 +112,27 @@ final class IrwinApi(
insert(Suspect(user), _.Leaderboard)
}

private def getGames(suspect: Suspect): Fu[List[Analyzed]] =
GameRepo.recentStandardAnalysedGamesByUserId(suspect.id.value, 20) flatMap AnalysisRepo.associateToGames
import lila.game.BSONHandlers._

private def baseQuery(suspect: Suspect) =
Query.finished ++
Query.variantStandard ++
Query.rated ++
Query.user(suspect.id.value) ++
Query.turnsGt(20) ++
Query.createdSince(DateTime.now minusMonths 6)

private def getAnalyzedGames(suspect: Suspect, nb: Int): Fu[List[Analyzed]] =
GameRepo.coll.find(baseQuery(suspect) ++ Query.analysed(true))
.sort(Query.sortCreated)
.cursor[Game](ReadPreference.secondaryPreferred)
.list(nb)
.flatMap(AnalysisRepo.associateToGames)

private def getMoreGames(suspect: Suspect, nb: Int): Fu[List[Game]] = (nb > 0) ??
GameRepo.coll.find(baseQuery(suspect) ++ Query.analysed(false))
.sort(Query.sortCreated).cursor[Game](ReadPreference.secondaryPreferred)
.list(nb)
}

object notification {
Expand Down
5 changes: 3 additions & 2 deletions modules/irwin/src/main/IrwinRequest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package lila.irwin

import lila.report.Suspect
import lila.user.User
import lila.analyse.Analysis.Analyzed
import lila.game.Game
import lila.analyse.Analysis

case class IrwinRequest(
suspect: Suspect,
origin: IrwinRequest.Origin,
games: List[Analyzed]
games: List[(Game, Option[Analysis])]
)

object IrwinRequest {
Expand Down
12 changes: 7 additions & 5 deletions modules/irwin/src/main/IrwinStream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,18 @@ final class IrwinStream(system: ActorSystem) {
"games" -> req.suspect.user.count.rated
),
"games" -> req.games.map {
case Analyzed(game, analysis) => Json.obj(
case (game, analysis) => Json.obj(
"id" -> game.id,
"white" -> game.whitePlayer.userId,
"black" -> game.blackPlayer.userId,
"pgn" -> game.pgnMoves.mkString(" "),
"emts" -> game.clockHistory.isDefined ?? game.moveTimes.map(_.map(_.centis)),
"analysis" -> analysis.infos.map { info =>
info.cp.map { cp => Json.obj("cp" -> cp.value) } orElse
info.mate.map { mate => Json.obj("mate" -> mate.value) } getOrElse
JsNull
"analysis" -> analysis.map {
_.infos.map { info =>
info.cp.map { cp => Json.obj("cp" -> cp.value) } orElse
info.mate.map { mate => Json.obj("mate" -> mate.value) } getOrElse
JsNull
}
}
)
}
Expand Down

0 comments on commit c8baf65

Please sign in to comment.