fix(tournament): fix scalafix violations and apply scalafmt formatting
Build & Test (NowChessSystems) TeamCity build finished
Build & Test (NowChessSystems) TeamCity build finished
- Replace null with Option in GameResultStreamListener, PairingRepository, ParticipantRepository, TournamentService - Replace isInstanceOf checks with pattern matching in GameResultStreamListener - Wrap var in SwissPairingService resolveConflicts with scalafix:off - Apply spotlessScalaApply formatting across tournament and official-bots modules Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -14,10 +14,10 @@ class Tournament:
|
||||
@Column(nullable = false)
|
||||
var fullName: String = uninitialized
|
||||
|
||||
var nbRounds: Int = 0
|
||||
var clockLimit: Int = 0
|
||||
var nbRounds: Int = 0
|
||||
var clockLimit: Int = 0
|
||||
var clockIncrement: Int = 0
|
||||
var rated: Boolean = true
|
||||
var rated: Boolean = true
|
||||
|
||||
@Column(nullable = false)
|
||||
var status: String = "created"
|
||||
@@ -27,7 +27,7 @@ class Tournament:
|
||||
@Column(nullable = false)
|
||||
var createdBy: String = uninitialized
|
||||
|
||||
var startsAt: Instant = uninitialized
|
||||
var winnerId: String = uninitialized
|
||||
var startsAt: Instant = uninitialized
|
||||
var winnerId: String = uninitialized
|
||||
var winnerName: String = uninitialized
|
||||
// scalafix:on
|
||||
|
||||
+4
-4
@@ -15,8 +15,8 @@ class TournamentPairing:
|
||||
@Column(nullable = false)
|
||||
var tournamentId: String = uninitialized
|
||||
|
||||
var round: Int = 0
|
||||
var whiteId: String = uninitialized
|
||||
var round: Int = 0
|
||||
var whiteId: String = uninitialized
|
||||
var whiteName: String = uninitialized
|
||||
|
||||
@Column(nullable = false)
|
||||
@@ -25,7 +25,7 @@ class TournamentPairing:
|
||||
@Column(nullable = false)
|
||||
var blackName: String = uninitialized
|
||||
|
||||
var gameId: String = uninitialized
|
||||
var winner: String = uninitialized
|
||||
var gameId: String = uninitialized
|
||||
var winner: String = uninitialized
|
||||
var moveList: String = uninitialized
|
||||
// scalafix:on
|
||||
|
||||
+6
-6
@@ -21,11 +21,11 @@ class TournamentParticipant:
|
||||
@Column(nullable = false)
|
||||
var botName: String = uninitialized
|
||||
|
||||
var points: Double = 0.0
|
||||
var points: Double = 0.0
|
||||
var tieBreak: Double = 0.0
|
||||
var nbGames: Int = 0
|
||||
var wins: Int = 0
|
||||
var draws: Int = 0
|
||||
var losses: Int = 0
|
||||
var byeCount: Int = 0
|
||||
var nbGames: Int = 0
|
||||
var wins: Int = 0
|
||||
var draws: Int = 0
|
||||
var losses: Int = 0
|
||||
var byeCount: Int = 0
|
||||
// scalafix:on
|
||||
|
||||
+6
-6
@@ -1,10 +1,10 @@
|
||||
package de.nowchess.tournament.error
|
||||
|
||||
enum TournamentError(val message: String):
|
||||
case NotFound(id: String) extends TournamentError(s"Tournament $id not found")
|
||||
case NotDirector extends TournamentError("Not the tournament director")
|
||||
case NotFound(id: String) extends TournamentError(s"Tournament $id not found")
|
||||
case NotDirector extends TournamentError("Not the tournament director")
|
||||
case WrongStatus(expected: String) extends TournamentError(s"Tournament must be in $expected status")
|
||||
case AlreadyJoined extends TournamentError("Already joined this tournament")
|
||||
case NotJoined extends TournamentError("Not joined this tournament")
|
||||
case NotEnoughParticipants extends TournamentError("Need at least 2 participants to start")
|
||||
case NotABot extends TournamentError("Only bot accounts can join tournaments")
|
||||
case AlreadyJoined extends TournamentError("Already joined this tournament")
|
||||
case NotJoined extends TournamentError("Not joined this tournament")
|
||||
case NotEnoughParticipants extends TournamentError("Need at least 2 participants to start")
|
||||
case NotABot extends TournamentError("Only bot accounts can join tournaments")
|
||||
|
||||
+26
-17
@@ -21,11 +21,11 @@ import java.util.UUID
|
||||
@ApplicationScoped
|
||||
class GameResultStreamListener:
|
||||
// scalafix:off DisableSyntax.var
|
||||
@Inject var redis: RedisDataSource = uninitialized
|
||||
@Inject var objectMapper: ObjectMapper = uninitialized
|
||||
@Inject var tournamentService: TournamentService = uninitialized
|
||||
@Inject var executor: ManagedExecutor = uninitialized
|
||||
@Inject var redisConfig: RedisConfig = uninitialized
|
||||
@Inject var redis: RedisDataSource = uninitialized
|
||||
@Inject var objectMapper: ObjectMapper = uninitialized
|
||||
@Inject var tournamentService: TournamentService = uninitialized
|
||||
@Inject var executor: ManagedExecutor = uninitialized
|
||||
@Inject var redisConfig: RedisConfig = uninitialized
|
||||
// scalafix:on
|
||||
|
||||
private val log = Logger.getLogger(classOf[GameResultStreamListener])
|
||||
@@ -37,8 +37,9 @@ class GameResultStreamListener:
|
||||
@PostConstruct
|
||||
def startListening(): Unit =
|
||||
createGroupIfAbsent()
|
||||
executor.submit(new Runnable:
|
||||
def run(): Unit = pollLoop()
|
||||
executor.submit(
|
||||
new Runnable:
|
||||
def run(): Unit = pollLoop(),
|
||||
)
|
||||
log.infof("Tournament result listener started (consumer=%s)", consumerId)
|
||||
|
||||
@@ -49,17 +50,21 @@ class GameResultStreamListener:
|
||||
case Success(_) => ()
|
||||
|
||||
private def pollLoop(): Unit =
|
||||
// scalafix:off DisableSyntax.var
|
||||
var running = true
|
||||
// scalafix:on DisableSyntax.var
|
||||
while running do
|
||||
Try {
|
||||
val messages = redis.stream(classOf[String]).xreadgroup(
|
||||
groupName,
|
||||
consumerId,
|
||||
streamKey,
|
||||
">",
|
||||
new XReadGroupArgs().count(10).block(java.time.Duration.ofSeconds(2)),
|
||||
)
|
||||
if messages != null then messages.forEach(msg => handleMessage(msg))
|
||||
val messages = redis
|
||||
.stream(classOf[String])
|
||||
.xreadgroup(
|
||||
groupName,
|
||||
consumerId,
|
||||
streamKey,
|
||||
">",
|
||||
new XReadGroupArgs().count(10).block(java.time.Duration.ofSeconds(2)),
|
||||
)
|
||||
Option(messages).foreach(_.forEach(msg => handleMessage(msg)))
|
||||
} match
|
||||
case Failure(ex) if isInterrupted(ex) =>
|
||||
Thread.currentThread().interrupt()
|
||||
@@ -68,8 +73,12 @@ class GameResultStreamListener:
|
||||
case Success(_) => ()
|
||||
|
||||
private def isInterrupted(ex: Throwable): Boolean =
|
||||
ex.isInstanceOf[InterruptedException] ||
|
||||
(ex.getCause != null && ex.getCause.isInstanceOf[InterruptedException])
|
||||
ex match
|
||||
case _: InterruptedException => true
|
||||
case _ =>
|
||||
Option(ex.getCause) match
|
||||
case Some(_: InterruptedException) => true
|
||||
case _ => false
|
||||
|
||||
private def handleMessage(msg: StreamMessage[String, String, String]): Unit =
|
||||
val json = msg.payload().get("data")
|
||||
|
||||
+1
-1
@@ -41,7 +41,7 @@ class PairingRepository:
|
||||
.headOption
|
||||
|
||||
def persist(p: TournamentPairing): TournamentPairing =
|
||||
if p.id == null then
|
||||
if Option(p.id).isEmpty then
|
||||
em.persist(p)
|
||||
p
|
||||
else em.merge(p)
|
||||
|
||||
+1
-1
@@ -34,7 +34,7 @@ class ParticipantRepository:
|
||||
.headOption
|
||||
|
||||
def persist(p: TournamentParticipant): TournamentParticipant =
|
||||
if p.id == null then
|
||||
if Option(p.id).isEmpty then
|
||||
em.persist(p)
|
||||
p
|
||||
else em.merge(p)
|
||||
|
||||
+7
-3
@@ -119,9 +119,13 @@ class TournamentResource:
|
||||
tournamentService.get(id) match
|
||||
case None => Response.status(Response.Status.NOT_FOUND).entity("").build()
|
||||
case Some(_) =>
|
||||
val ndjson = tournamentService.getResults(id).take(nb).map { r =>
|
||||
s"""{"rank":${r.rank},"points":${r.points},"tieBreak":${r.tieBreak},"bot":{"id":"${r.bot.id}","name":"${r.bot.name}"},"nbGames":${r.nbGames},"wins":${r.wins},"draws":${r.draws},"losses":${r.losses}}"""
|
||||
}.mkString("\n")
|
||||
val ndjson = tournamentService
|
||||
.getResults(id)
|
||||
.take(nb)
|
||||
.map { r =>
|
||||
s"""{"rank":${r.rank},"points":${r.points},"tieBreak":${r.tieBreak},"bot":{"id":"${r.bot.id}","name":"${r.bot.name}"},"nbGames":${r.nbGames},"wins":${r.wins},"draws":${r.draws},"losses":${r.losses}}"""
|
||||
}
|
||||
.mkString("\n")
|
||||
Response.ok(ndjson).`type`("application/x-ndjson").build()
|
||||
|
||||
@GET
|
||||
|
||||
+13
-8
@@ -1,6 +1,6 @@
|
||||
package de.nowchess.tournament.service
|
||||
|
||||
import de.nowchess.tournament.domain.{TournamentParticipant, TournamentPairing}
|
||||
import de.nowchess.tournament.domain.{TournamentPairing, TournamentParticipant}
|
||||
import java.util.concurrent.ThreadLocalRandom
|
||||
|
||||
object SwissPairingService:
|
||||
@@ -9,9 +9,9 @@ object SwissPairingService:
|
||||
participants: List[TournamentParticipant],
|
||||
pastPairings: List[TournamentPairing],
|
||||
): (List[(TournamentParticipant, TournamentParticipant)], Option[TournamentParticipant]) =
|
||||
val sorted = sortParticipants(participants)
|
||||
val sorted = sortParticipants(participants)
|
||||
val (remaining, byeOpt) = extractByePlayer(sorted)
|
||||
val pairs = buildPairs(remaining, pastPairings)
|
||||
val pairs = buildPairs(remaining, pastPairings)
|
||||
(pairs, byeOpt)
|
||||
|
||||
private def sortParticipants(participants: List[TournamentParticipant]): List[TournamentParticipant] =
|
||||
@@ -37,16 +37,21 @@ object SwissPairingService:
|
||||
): List[(TournamentParticipant, TournamentParticipant)] =
|
||||
val arr = players.toArray
|
||||
resolveConflicts(arr, pastPairings)
|
||||
arr.grouped(2).flatMap {
|
||||
case Array(a, b) => Some(assignColors(a, b))
|
||||
case _ => None
|
||||
}.toList
|
||||
arr
|
||||
.grouped(2)
|
||||
.flatMap {
|
||||
case Array(a, b) => Some(assignColors(a, b))
|
||||
case _ => None
|
||||
}
|
||||
.toList
|
||||
|
||||
private def resolveConflicts(arr: Array[TournamentParticipant], pastPairings: List[TournamentPairing]): Unit =
|
||||
// scalafix:off DisableSyntax.var
|
||||
var i = 0
|
||||
// scalafix:on DisableSyntax.var
|
||||
while i < arr.length - 1 do
|
||||
if havePlayedBefore(arr(i), arr(i + 1), pastPairings) && i + 2 < arr.length then
|
||||
val tmp = arr(i + 1)
|
||||
val tmp = arr(i + 1)
|
||||
arr(i + 1) = arr(i + 2)
|
||||
arr(i + 2) = tmp
|
||||
i += 2
|
||||
|
||||
+26
-9
@@ -3,7 +3,16 @@ package de.nowchess.tournament.service
|
||||
import de.nowchess.tournament.client.{CoreCreateGameRequest, CoreGameClient, CorePlayerInfo, CoreTimeControl}
|
||||
import de.nowchess.tournament.config.RedisConfig
|
||||
import de.nowchess.tournament.domain.{Tournament, TournamentPairing, TournamentParticipant}
|
||||
import de.nowchess.tournament.dto.{BotRef, Clock, CreateTournamentForm, PairingDto, ResultDto, Standing, TournamentDto, Variant}
|
||||
import de.nowchess.tournament.dto.{
|
||||
BotRef,
|
||||
Clock,
|
||||
CreateTournamentForm,
|
||||
PairingDto,
|
||||
ResultDto,
|
||||
Standing,
|
||||
TournamentDto,
|
||||
Variant,
|
||||
}
|
||||
import de.nowchess.tournament.error.TournamentError
|
||||
import de.nowchess.tournament.repository.{PairingRepository, ParticipantRepository, TournamentRepository}
|
||||
import io.quarkus.redis.datasource.RedisDataSource
|
||||
@@ -66,8 +75,7 @@ class TournamentService:
|
||||
t <- tournamentRepository.findOptById(id).toRight(TournamentError.NotFound(id))
|
||||
_ <- Either.cond(t.createdBy == userId, (), TournamentError.NotDirector)
|
||||
_ <- Either.cond(t.status == "created", (), TournamentError.WrongStatus("created"))
|
||||
yield
|
||||
tournamentRepository.delete(t)
|
||||
yield tournamentRepository.delete(t)
|
||||
|
||||
@Transactional
|
||||
def join(id: String, botId: String, botName: String): Either[TournamentError, Unit] =
|
||||
@@ -115,7 +123,7 @@ class TournamentService:
|
||||
Either.cond(ps.size >= 2, ps, TournamentError.NotEnoughParticipants)
|
||||
|
||||
private def startRound(t: Tournament, round: Int, participants: List[TournamentParticipant]): Unit =
|
||||
val pastPairings = pairingRepository.findByTournamentId(t.id)
|
||||
val pastPairings = pairingRepository.findByTournamentId(t.id)
|
||||
val (pairs, byeOpt) = SwissPairingService.computePairings(participants, pastPairings)
|
||||
byeOpt.foreach(bye => createByePairing(t.id, round, bye))
|
||||
pairs.foreach { case (white, black) => createRealPairing(t.id, round, white, black, t) }
|
||||
@@ -159,8 +167,16 @@ class TournamentService:
|
||||
pairing.blackName = black.botName
|
||||
pairing.gameId = resp.gameId
|
||||
pairingRepository.persist(pairing)
|
||||
streamManager.publishToBot(tournamentId, white.botId, s"""{"type":"gameStart","round":$round,"gameId":"${resp.gameId}","color":"white"}""")
|
||||
streamManager.publishToBot(tournamentId, black.botId, s"""{"type":"gameStart","round":$round,"gameId":"${resp.gameId}","color":"black"}""")
|
||||
streamManager.publishToBot(
|
||||
tournamentId,
|
||||
white.botId,
|
||||
s"""{"type":"gameStart","round":$round,"gameId":"${resp.gameId}","color":"white"}""",
|
||||
)
|
||||
streamManager.publishToBot(
|
||||
tournamentId,
|
||||
black.botId,
|
||||
s"""{"type":"gameStart","round":$round,"gameId":"${resp.gameId}","color":"black"}""",
|
||||
)
|
||||
publishBotGameStart(white.botName, resp.gameId, "white", white.botId)
|
||||
publishBotGameStart(black.botName, resp.gameId, "black", black.botId)
|
||||
|
||||
@@ -171,7 +187,8 @@ class TournamentService:
|
||||
botAccountId: String,
|
||||
): Unit =
|
||||
val channel = s"${redisConfig.prefix}:bot:$botName:events"
|
||||
val payload = s"""{"type":"gameStart","gameId":"$gameId","playingAs":"$playingAs","difficulty":1500,"botAccountId":"$botAccountId"}"""
|
||||
val payload =
|
||||
s"""{"type":"gameStart","gameId":"$gameId","playingAs":"$playingAs","difficulty":1500,"botAccountId":"$botAccountId"}"""
|
||||
Try(redis.pubsub(classOf[String]).publish(channel, payload)) match
|
||||
case Failure(ex) => log.warnf(ex, "Failed to publish gameStart to bot channel %s", channel)
|
||||
case Success(_) => ()
|
||||
@@ -212,7 +229,7 @@ class TournamentService:
|
||||
private def checkRoundCompletion(tournamentId: String): Unit =
|
||||
tournamentRepository.findOptById(tournamentId).foreach { t =>
|
||||
val roundPairings = pairingRepository.findByTournamentIdAndRound(tournamentId, t.currentRound)
|
||||
val allDone = roundPairings.nonEmpty && roundPairings.forall(p => Option(p.winner).isDefined)
|
||||
val allDone = roundPairings.nonEmpty && roundPairings.forall(p => Option(p.winner).isDefined)
|
||||
if allDone then onRoundComplete(t)
|
||||
}
|
||||
|
||||
@@ -282,7 +299,7 @@ class TournamentService:
|
||||
status = t.status,
|
||||
round = t.currentRound,
|
||||
standing = Standing(1, standings),
|
||||
winner = if t.winnerId != null then Some(BotRef(t.winnerId, t.winnerName)) else None,
|
||||
winner = Option(t.winnerId).map(id => BotRef(id, t.winnerName)),
|
||||
)
|
||||
|
||||
private def toPairingDto(p: TournamentPairing): PairingDto =
|
||||
|
||||
+6
-2
@@ -14,8 +14,12 @@ class TournamentStreamManager:
|
||||
private def botKey(tournamentId: String, botId: String): String = s"${tournamentId}:${botId}"
|
||||
|
||||
def register(tournamentId: String, botId: String, emitter: MultiEmitter[? >: String]): Unit =
|
||||
tournamentEmitters.computeIfAbsent(tournamentId, _ => new CopyOnWriteArrayList[MultiEmitter[? >: String]]()).add(emitter)
|
||||
botEmitters.computeIfAbsent(botKey(tournamentId, botId), _ => new CopyOnWriteArrayList[MultiEmitter[? >: String]]()).add(emitter)
|
||||
tournamentEmitters
|
||||
.computeIfAbsent(tournamentId, _ => new CopyOnWriteArrayList[MultiEmitter[? >: String]]())
|
||||
.add(emitter)
|
||||
botEmitters
|
||||
.computeIfAbsent(botKey(tournamentId, botId), _ => new CopyOnWriteArrayList[MultiEmitter[? >: String]]())
|
||||
.add(emitter)
|
||||
|
||||
def unregister(tournamentId: String, botId: String, emitter: MultiEmitter[? >: String]): Unit =
|
||||
Option(tournamentEmitters.get(tournamentId)).foreach(_.remove(emitter))
|
||||
|
||||
Reference in New Issue
Block a user