Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pla 1183 fix multipoint calculation in playerinfo #1071

Merged
merged 10 commits into from
Feb 25, 2025
4 changes: 2 additions & 2 deletions modules/game/src/main/Game.scala
Original file line number Diff line number Diff line change
Expand Up @@ -501,10 +501,10 @@ case class Game(
// style "copy pasted" from a ts function
def multiPointResult: String =
if (metadata.multiPointState.isEmpty) "-"
else if (finished) finalMultiPointScore()
else if (finished) finalMultiPointScore
else metadata.multiPointState.fold("-")(mps => f"${mps.target}%02d${mps.p1Points}%02d${mps.p2Points}%02d")

def finalMultiPointScore(): String = {
def finalMultiPointScore: String = {
val points2Add: Array[Int] =
if (pointValue.isDefined && winner.isDefined)
if (winner.get.playerIndex == P1) Array(pointValue.get, 0)
Expand Down
53 changes: 26 additions & 27 deletions modules/swiss/src/main/SwissJson.scala
Original file line number Diff line number Diff line change
Expand Up @@ -282,40 +282,39 @@ object SwissJson {
private def multiPointResultsJson(swissPairingGames: Seq[SwissPairingGames]) =
JsArray(
swissPairingGames.flatMap {
pairingGame =>
Seq(
Json.obj(
"target" -> pairingGame.game.metadata.multiPointState.fold(0)(_.target),
"players" -> Json.obj(
"p1" -> Json.obj(
"userId" -> pairingGame.game.p1Player.userId,
),
"p2" -> Json.obj(
"userId" -> pairingGame.game.p2Player.userId,
)
pairingGame => Seq(
Json.obj(
"target" -> pairingGame.game.metadata.multiPointState.fold(0)(_.target),
"players" -> Json.obj(
"p1" -> Json.obj(
"userId" -> pairingGame.game.p1Player.userId,
),
"games" -> JsArray(
pairingGame.multiMatchGames.toList.flatten.reverse.map { game =>
Json.obj(
"id" -> game.id,
"p1UserId" -> game.p1Player.userId,
"startingScore" -> Json.obj(
"p1" -> game.metadata.multiPointState.fold(0)(_.p1Points),
"p2" -> game.metadata.multiPointState.fold(0)(_.p2Points),
)
)
} :+
"p2" -> Json.obj(
"userId" -> pairingGame.game.p2Player.userId,
)
),
"games" -> JsArray(
pairingGame.multiMatchGames.toList.flatten.reverse.map { game =>
Json.obj(
"id" -> pairingGame.game.id,
"p1UserId" -> pairingGame.game.p1Player.userId,
"id" -> game.id,
"p1UserId" -> game.p1Player.userId,
"startingScore" -> Json.obj(
"p1" -> pairingGame.game.metadata.multiPointState.fold(0)(_.p1Points),
"p2" -> pairingGame.game.metadata.multiPointState.fold(0)(_.p2Points),
"p1" -> (if (game.finished) game.finalMultiPointScore.substring(2,4).toInt.toString else game.metadata.multiPointState.fold(0)(_.p1Points)),
"p2" -> (if (game.finished) game.finalMultiPointScore.substring(4,6).toInt.toString else game.metadata.multiPointState.fold(0)(_.p2Points)),
)
)
)
} :+
Json.obj(
"id" -> pairingGame.game.id,
"p1UserId" -> pairingGame.game.p1Player.userId,
"startingScore" -> Json.obj(
"p1" -> (if (pairingGame.game.finished) pairingGame.game.finalMultiPointScore.substring(2,4).toInt.toString else pairingGame.game.metadata.multiPointState.fold(0)(_.p1Points)),
"p2" -> (if (pairingGame.game.finished) pairingGame.game.finalMultiPointScore.substring(4,6).toInt.toString else pairingGame.game.metadata.multiPointState.fold(0)(_.p2Points)),
)
)
)
)
)
}
)

Expand Down
12 changes: 9 additions & 3 deletions ui/swiss/src/view/playerInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,17 @@ function gameResult(p: MultiMatchPairing): string {
}

function multiPointResult(p: MultiMatchPairing, selectedUserId: string, multiPoints: MultiPoint[]): string {
// @TODO: need fix pla-1183-fix-multipoint-calculation-in-playerinfo : pass a ui/game/BaseGame and a ply up to here in order to invoke ui/stratutils/finalMultiPointState() and compute pts correctly (e.g : win multipoint match by timeout but opponent still gets pts by Rule of Gin)
const edgeCasesDisplay = '(*)';
if (!multiPoints) return edgeCasesDisplay;
const round = multiPoints.find(round => round.games?.find(game => game.id === p.g));
if (p.mmGameNb === undefined || !round || round.games.length < 1 || !round.target) return edgeCasesDisplay;
if (
p.mmGameNb === undefined ||
!round ||
round.games.length < 1 ||
!round.target ||
(p.w === undefined && p.mmGameNb == round.games.length)
)
return edgeCasesDisplay;

if (p.isFinalGame && p.w !== undefined) {
return p.w === true
Expand All @@ -237,7 +243,7 @@ function multiPointResult(p: MultiMatchPairing, selectedUserId: string, multiPoi
: round.games[0].startingScore.p2 + ' - ' + round.target;
}

const multiPointMatchIndex = round.games.length - p.mmGameNb - 1;
const multiPointMatchIndex = round.games.length - p.mmGameNb;
if (!round.games[multiPointMatchIndex]) return edgeCasesDisplay;
const [selectedPlayerScore, opponentScore] =
round.games[multiPointMatchIndex].p1UserId === selectedUserId
Expand Down
Loading