Skip to content

Commit

Permalink
support exotic game results in studies too
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Sep 19, 2024
1 parent 5505fed commit 69f1bad
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 21 deletions.
19 changes: 7 additions & 12 deletions modules/relay/src/main/RelayTeams.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,8 @@ final class RelayTeamTable(
JsonStr(Json.stringify(Json.obj("table" -> ordered)))

case class TeamWithPoints(name: String, points: Float = 0):
def add(o: Option[Outcome], as: Color) =
copy(points = points + o.so(_.winner match
case Some(w) if w == as => 1
case None => 0.5f
case _ => 0
))
def add(result: Option[Outcome.GamePoints], as: Color) =
copy(points = points + result.so(_(as).value))
case class Pair[A](a: A, b: A):
def is(p: Pair[A]) = (a == p.a && b == p.b) || (a == p.b && b == p.a)
def map[B](f: A => B) = Pair(f(a), f(b))
Expand All @@ -104,24 +100,23 @@ final class RelayTeamTable(
def add(
chap: ChapterPreview,
playerAndTeam: Pair[(StudyPlayer, TeamName)],
outcome: Option[Outcome]
) =
points: Option[Outcome.GamePoints]
): TeamMatch =
val t0Color = Color.fromWhite(playerAndTeam.a._2 == teams.a.name)
if t0Color.white then playerAndTeam else playerAndTeam.reverse
copy(
games = TeamGame(chap.id, t0Color) :: games,
teams = teams.bimap(_.add(outcome, t0Color), _.add(outcome, !t0Color))
teams = teams.bimap(_.add(points, t0Color), _.add(points, !t0Color))
)
def swap = copy(teams = teams.reverse, games = games.map(_.swap))

def makeTable(chapters: List[ChapterPreview]): List[TeamMatch] =
chapters.reverse.foldLeft(List.empty[TeamMatch]): (table, chap) =>
(for
outcome <- chap.result
points <- chap.points
players <- chap.players
teams <- players.traverse(_.team).map(_.toPair).map(Pair.apply)
m0 = table.find(_.is(teams)) | TeamMatch(teams.map(TeamWithPoints(_)), Nil)
m1 = m0.add(chap, Pair(players.white.player -> teams.a, players.black.player -> teams.b), outcome)
m1 = m0.add(chap, Pair(players.white.player -> teams.a, players.black.player -> teams.b), points)
newTable = m1 :: table.filterNot(_.is(teams))
yield newTable) | table

Expand Down
12 changes: 6 additions & 6 deletions modules/study/src/main/StudyChapterPreview.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ case class ChapterPreview(
check: Option[Chapter.Check],
/* None = No Result PGN tag, the chapter may not be a game
* Some(None) = Result PGN tag is "*", the game is ongoing
* Some(Some(Outcome)) = Game is over with a result
* Some(Some(GamePoints)) = Game is over with a result
*/
result: Option[Option[Outcome]]
points: Option[Option[Outcome.GamePoints]]
):
def finished = result.exists(_.isDefined)
def finished = points.exists(_.isDefined)
def thinkTime = (!finished).so(lastMoveAt.map(at => (nowSeconds - at.toSeconds).toInt))
def fideIds: List[FideId] = players.so(_.mapList(_.fideId)).flatten

Expand Down Expand Up @@ -109,7 +109,7 @@ final class ChapterPreviewApi(
lastMove = denorm.flatMap(_.uci),
lastMoveAt = relay.map(_.lastMoveAt),
check = denorm.flatMap(_.check),
result = tags.outcome.isDefined.option(tags.outcome)
points = tags.points.isDefined.option(tags.points)
)

object federations:
Expand Down Expand Up @@ -155,7 +155,7 @@ object ChapterPreview:
.add("lastMove", c.lastMove)
.add("check", c.check)
.add("thinkTime", c.thinkTime)
.add("status", c.result.map(o => Outcome.showResult(o).replace("1/2", "½")))
.add("status", c.points.map(o => Outcome.showPoints(o).replace("1/2", "½")))

object bson:
import BSONHandlers.given
Expand Down Expand Up @@ -187,5 +187,5 @@ object ChapterPreview:
lastMove = lastPos.flatMap(_.uci),
lastMoveAt = lastMoveAt,
check = lastPos.flatMap(_.check),
result = tags.flatMap(_(_.Result)).map(Outcome.fromResult)
points = tags.map(_.points)
)
2 changes: 1 addition & 1 deletion project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ object Dependencies {
}

object chess {
val version = "16.2.10"
val version = "16.2.12"
val core = "org.lichess" %% "scalachess" % version
val testKit = "org.lichess" %% "scalachess-test-kit" % version % Test
val playJson = "org.lichess" %% "scalachess-play-json" % version
Expand Down
4 changes: 2 additions & 2 deletions ui/analyse/src/study/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export type ToolTab = 'tags' | 'comments' | 'glyphs' | 'serverEval' | 'share' |
export type Visibility = 'public' | 'unlisted' | 'private';
export type ChapterId = string;
export type TeamName = string;
export type OutcomeStr = '1-0' | '0-1' | '½-½';
export type StatusStr = OutcomeStr | '*';
export type PointsStr = '1-0' | '0-1' | -½' | '0-0' | '½-0' | '0-½';
export type StatusStr = PointsStr | '*';
export type ClockCentis = number;
export type BothClocks = [ClockCentis?, ClockCentis?];
export type FideId = number;
Expand Down

0 comments on commit 69f1bad

Please sign in to comment.