Skip to content

Commit

Permalink
Define first player for #708 and for #738 (#866)
Browse files Browse the repository at this point in the history
* Add plugin sbt-bloop

* Disambiguate firstPlayer - startPlayer #708 #738
  • Loading branch information
ddugovic authored Sep 28, 2024
1 parent c529bf5 commit ff04ccc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
4 changes: 3 additions & 1 deletion modules/game/src/main/Game.scala
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ case class Game(

def opponent(c: Color): Player = player(!c)

lazy val firstColor = Color.fromSente(sentePlayer before gotePlayer)
// View handicap games from the handicap receiver's perspective
// View other games from the dominant (title/rating) player's perspective
lazy val firstColor = if (isHandicap) !startColor else Color.fromSente(sentePlayer before gotePlayer)
def firstPlayer = player(firstColor)
def secondPlayer = player(!firstColor)

Expand Down
25 changes: 17 additions & 8 deletions modules/game/src/main/Player.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ case class Player(
color: Color,
engineConfig: Option[EngineConfig] = None,
isBot: Boolean = false,
hasTitle: Boolean = false,
isWinner: Option[Boolean] = None,
isOfferingDraw: Boolean = false,
lastDrawOffer: Option[Int] = None,
Expand Down Expand Up @@ -82,11 +83,13 @@ case class Player(
}

def before(other: Player) =
((rating, id), (other.rating, other.id)) match {
case ((Some(a), _), (Some(b), _)) if a != b => a > b
case ((Some(_), _), (None, _)) => true
case ((None, _), (Some(_), _)) => false
case ((_, a), (_, b)) => a < b
((isHuman, hasTitle, rating, id), (other.isHuman, other.hasTitle, other.rating, other.id)) match {
case ((a, _, _, _), (b, _, _, _)) if a != b => a > b
case ((_, a, _, _), (_, b, _, _)) if a != b => a > b
case ((_, _, Some(a), _), (_, _, Some(b), _)) if a != b => a > b
case ((_, _, Some(_), _), (_, _, None, _)) => true
case ((_, _, None, _), (_, _, Some(_), _)) => false
case ((_, _, _, a), (_, _, _, b)) => a < b
}

def ratingAfter = rating map (_ + ~ratingDiff)
Expand Down Expand Up @@ -119,7 +122,8 @@ object Player {
userId: User.ID,
rating: Int,
provisional: Boolean,
isBot: Boolean
isBot: Boolean,
hasTitle: Boolean
): Player =
Player(
id = IdGenerator.player(color),
Expand All @@ -128,7 +132,8 @@ object Player {
userId = userId.some,
rating = rating.some,
provisional = provisional,
isBot = isBot
isBot = isBot,
hasTitle = hasTitle
)

def make(
Expand All @@ -143,7 +148,8 @@ object Player {
userId = u.id,
rating = perf.intRating,
provisional = perf.glicko.provisional,
isBot = u.isBot
isBot = u.isBot,
hasTitle = u.hasTitle
)
}

Expand All @@ -167,6 +173,7 @@ object Player {
val aiLevel = "ai"
val aiEngine = "a"
val isBot = "b"
val hasTitle = "t"
val isOfferingDraw = "od"
val lastDrawOffer = "ld"
val proposeTakebackAt = "ta"
Expand Down Expand Up @@ -222,6 +229,7 @@ object Player {
)
),
isBot = r boolD isBot,
hasTitle = r boolD hasTitle,
isWinner = win,
isOfferingDraw = r boolD isOfferingDraw,
lastDrawOffer = r intO lastDrawOffer,
Expand All @@ -242,6 +250,7 @@ object Player {
aiLevel -> p.aiLevel,
aiEngine -> p.aiEngine.map(_.code),
isBot -> w.boolO(p.isBot),
hasTitle -> w.boolO(p.hasTitle),
isOfferingDraw -> w.boolO(p.isOfferingDraw),
lastDrawOffer -> p.lastDrawOffer,
isOfferingPause -> w.boolO(p.isOfferingPause),
Expand Down
2 changes: 1 addition & 1 deletion modules/tournament/src/main/AutoPairing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ final class AutoPairing(
}

private def makePlayer(color: Color, player: Player) =
GamePlayer.make(color, player.userId, player.rating, provisional = player.provisional, isBot = false)
GamePlayer.make(color, player.userId, player.rating, provisional = player.provisional, isBot = false, hasTitle = false)

private def usernameOf(userId: User.ID) =
lightUserApi.sync(userId).fold(userId)(_.name)
Expand Down

0 comments on commit ff04ccc

Please sign in to comment.