Skip to content

Commit

Permalink
add game source to insights dimensions - closes #16363
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Nov 8, 2024
1 parent 623f4cd commit 121dd6a
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 11 deletions.
3 changes: 2 additions & 1 deletion modules/core/src/main/game/misc.scala
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,14 @@ enum Source(val id: Int) derives Eq:
case Arena extends Source(id = 5)
case Position extends Source(id = 6)
case Import extends Source(id = 7)
case ImportLive extends Source(id = 9)
case ImportLive extends Source(id = 9) // wut?
case Simul extends Source(id = 10)
case Pool extends Source(id = 12)
case Swiss extends Source(id = 13)

object Source:
val byId = values.mapBy(_.id)
val byName = values.mapBy(_.name)
val searchable = List(Lobby, Friend, Ai, Position, Arena, Simul, Pool, Swiss)
val expirable = Set(Lobby, Arena, Pool, Swiss)
def apply(id: Int): Option[Source] = byId.get(id)
Expand Down
6 changes: 3 additions & 3 deletions modules/game/src/main/BSONHandlers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ object BSONHandlers:

given BSONHandler[GameRule] = valueMapHandler[String, GameRule](GameRule.byKey)(_.toString)

given BSONHandler[Source] = valueMapHandler[Int, Source](Source.byId)(_.id)
given sourceHandler: BSONHandler[Source] = valueMapHandler[Int, Source](Source.byId)(_.id)

private[game] given crazyhouseDataHandler: BSON[Crazyhouse.Data] with
import Crazyhouse.*
Expand Down Expand Up @@ -221,9 +221,9 @@ object BSONHandlers:
F.status -> o.status,
F.turns -> o.chess.ply,
F.startedAtTurn -> w.intO(o.chess.startedAtPly.value),
F.clock -> (o.chess.clock.flatMap { c =>
F.clock -> o.chess.clock.flatMap { c =>
clockBSONWrite(o.createdAt, c).toOption
}),
},
F.daysPerTurn -> o.daysPerTurn,
F.moveTimes -> o.binaryMoveTimes,
F.whiteClockHistory -> clockHistory(Color.White, o.clockHistory, o.chess.clock, o.flagged),
Expand Down
4 changes: 4 additions & 0 deletions modules/insight/src/main/BSONHandlers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import lila.db.BSON
import lila.db.dsl.{ *, given }
import lila.rating.BSONHandlers.perfTypeIdHandler
import lila.rating.PerfType
import lila.core.game.Source
import lila.game.BSONHandlers.sourceHandler

object BSONHandlers:

Expand Down Expand Up @@ -102,6 +104,7 @@ object BSONHandlers:
ratingDiff = r.get[IntRatingDiff](ratingDiff),
analysed = r.boolD(analysed),
provisional = r.boolD(provisional),
source = r.getO[Source](source),
date = r.date(date)
)
def writes(w: BSON.Writer, e: InsightEntry) =
Expand All @@ -124,5 +127,6 @@ object BSONHandlers:
ratingDiff -> e.ratingDiff,
analysed -> w.boolO(e.analysed),
provisional -> w.boolO(e.provisional),
source -> e.source,
date -> e.date
)
22 changes: 20 additions & 2 deletions modules/insight/src/main/InsightDimension.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import lila.db.dsl.{ *, given }
import lila.insight.BSONHandlers.given
import lila.insight.InsightEntry.BSONFields as F
import lila.rating.BSONHandlers.perfTypeIdHandler
import lila.game.BSONHandlers.sourceHandler
import lila.rating.PerfType
import lila.core.game.Source

enum InsightDimension[A](
val key: String,
Expand Down Expand Up @@ -231,6 +233,15 @@ enum InsightDimension[A](
"Time left on the player clock, accounting for increment. 100% = full clock, 0% = flagging."
)

case GameSource
extends InsightDimension[Source](
"source",
"Game source",
F.source,
InsightPosition.Game,
"How the game was created."
)

object InsightDimension:

def requiresStableRating(d: InsightDimension[?]) = d match
Expand Down Expand Up @@ -260,6 +271,10 @@ object InsightDimension:
case ClockPercentRange => lila.insight.ClockPercentRange.all.toList
case Blur => lila.insight.Blur.values.toIndexedSeq
case TimeVariance => lila.insight.TimeVariance.values.toIndexedSeq
case GameSource =>
Source.values.toIndexedSeq.filter:
case Source.Ai | Source.Import | Source.ImportLive => false
case _ => true

def valueByKey[X](d: InsightDimension[X], key: String): Option[X] = d match
case Period => key.toIntOption.map(lila.insight.Period.apply)
Expand All @@ -284,6 +299,7 @@ object InsightDimension:
case ClockPercentRange => key.toIntOption.flatMap(lila.insight.ClockPercentRange.byPercent.get)
case Blur => lila.insight.Blur(key == "true").some
case TimeVariance => key.toFloatOption.map(lila.insight.TimeVariance.byId)
case GameSource => Source.byName.get(key)

def valueToJson[X](d: InsightDimension[X])(v: X)(using Translate): JsObject =
Json.obj(
Expand All @@ -292,7 +308,7 @@ object InsightDimension:
)

def valueKey[X](d: InsightDimension[X])(v: X): String =
(d match
d.match
case Date => v.toString
case Period => v.days.toString
case Perf => v.key
Expand All @@ -315,7 +331,8 @@ object InsightDimension:
case ClockPercentRange => v.bottom.toInt
case Blur => v.id
case TimeVariance => v.id
).toString
case GameSource => v.name
.toString

def valueJson[X](d: InsightDimension[X])(v: X)(using Translate): JsValue =
d match
Expand All @@ -341,6 +358,7 @@ object InsightDimension:
case ClockPercentRange => JsString(v.name)
case Blur => JsString(v.name)
case TimeVariance => JsString(v.name)
case GameSource => JsString(v.toString)

def filtersOf[X](d: InsightDimension[X], selected: List[X]): Bdoc =

Expand Down
3 changes: 3 additions & 0 deletions modules/insight/src/main/InsightEntry.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lila.insight

import lila.common.SimpleOpening
import lila.core.game.Source

case class InsightEntry(
id: String, // gameId + w/b
Expand All @@ -20,6 +21,7 @@ case class InsightEntry(
ratingDiff: IntRatingDiff,
analysed: Boolean,
provisional: Boolean,
source: Option[Source],
date: Instant
)

Expand Down Expand Up @@ -48,4 +50,5 @@ case object InsightEntry:
val ratingDiff = "rd"
val analysed = "a"
val provisional = "pr"
val source = "so"
val date = "d"
2 changes: 2 additions & 0 deletions modules/insight/src/main/JsonQuestion.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ case class JsonQuestion(
case ClockPercentRange.key => build(ClockPercentRange)
case Blur.key => build(Blur)
case TimeVariance.key => build(TimeVariance)
case GameSource.key => build(GameSource)
case _ => none
}
.filterNot(_.isEmpty)
Expand Down Expand Up @@ -75,6 +76,7 @@ case class JsonQuestion(
case AccuracyPercentRange.key => build(AccuracyPercentRange)
case Blur.key => build(Blur)
case TimeVariance.key => build(TimeVariance)
case GameSource.key => build(GameSource)
case _ => none
yield question

Expand Down
3 changes: 2 additions & 1 deletion modules/insight/src/main/JsonView.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ final class JsonView:
dimWrites.writes(D.Period),
dimWrites.writes(D.Perf),
dimWrites.writes(D.Color),
dimWrites.writes(D.OpponentStrength)
dimWrites.writes(D.OpponentStrength),
dimWrites.writes(D.GameSource)
)
),
Categ(
Expand Down
1 change: 1 addition & 0 deletions modules/insight/src/main/PovToEntry.scala
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ final private class PovToEntry(
ratingDiff = ~pov.player.ratingDiff,
analysed = analysis.isDefined,
provisional = provisional,
source = game.source,
date = game.createdAt
)

Expand Down
8 changes: 4 additions & 4 deletions ui/insight/src/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ export function vert(ctrl: Ctrl, attrs: any = null) {
),
h(
'tbody',
answer.xAxis.categories.map((c, i) => {
return h('tr', [
answer.xAxis.categories.map((c, i) =>
h('tr', [
h('th', formatSerieName(answer.xAxis.dataType, c)),
...answer.series.map(serie => h('td.data', formatNumber(serie.dataType, serie.data[i]))),
h('td.size', formatNumber(answer.sizeSerie.dataType, answer.sizeSerie.data[i])),
]);
}),
]),
),
),
]),
);
Expand Down

0 comments on commit 121dd6a

Please sign in to comment.