fix(official-bots): replace null with Option to pass scalafix DisableSyntax.null
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+25
-19
@@ -103,7 +103,7 @@ class TournamentBotGamePlayer:
|
|||||||
log.infof("Playing game %s as %s", gameId, color)
|
log.infof("Playing game %s as %s", gameId, color)
|
||||||
val stream = openGameStream(cfg, gameId)
|
val stream = openGameStream(cfg, gameId)
|
||||||
maybeMoveFromCurrentState(cfg, gameId, color)
|
maybeMoveFromCurrentState(cfg, gameId, color)
|
||||||
if stream != null then consumeGameStream(cfg, gameId, color, stream)
|
stream.foreach(consumeGameStream(cfg, gameId, color, _))
|
||||||
activeGames.remove(gameId)
|
activeGames.remove(gameId)
|
||||||
} match
|
} match
|
||||||
case Failure(ex) => log.errorf(ex, "Game %s crashed", gameId); activeGames.remove(gameId)
|
case Failure(ex) => log.errorf(ex, "Game %s crashed", gameId); activeGames.remove(gameId)
|
||||||
@@ -125,15 +125,19 @@ class TournamentBotGamePlayer:
|
|||||||
val reader = new BufferedReader(new InputStreamReader(stream))
|
val reader = new BufferedReader(new InputStreamReader(stream))
|
||||||
// scalafix:off DisableSyntax.var
|
// scalafix:off DisableSyntax.var
|
||||||
var done = false
|
var done = false
|
||||||
var line = reader.readLine()
|
|
||||||
// scalafix:on DisableSyntax.var
|
// scalafix:on DisableSyntax.var
|
||||||
while line != null && running && !done do
|
Iterator
|
||||||
parse(line).foreach: node =>
|
.continually(reader.readLine())
|
||||||
node.path("type").asText() match
|
.map(Option(_))
|
||||||
case "move" => maybeMove(cfg, gameId, color, node.path("turn").asText(), "ongoing", node.path("fen").asText())
|
.takeWhile(opt => opt.isDefined && running && !done)
|
||||||
case "gameEnd" => log.infof("Game %s ended — status=%s", gameId, node.path("status").asText()); done = true
|
.flatten
|
||||||
case _ => ()
|
.foreach { line =>
|
||||||
line = reader.readLine()
|
parse(line).foreach: node =>
|
||||||
|
node.path("type").asText() match
|
||||||
|
case "move" => maybeMove(cfg, gameId, color, node.path("turn").asText(), "ongoing", node.path("fen").asText())
|
||||||
|
case "gameEnd" => log.infof("Game %s ended — status=%s", gameId, node.path("status").asText()); done = true
|
||||||
|
case _ => ()
|
||||||
|
}
|
||||||
|
|
||||||
private def maybeMove(
|
private def maybeMove(
|
||||||
cfg: TournamentBotConfig,
|
cfg: TournamentBotConfig,
|
||||||
@@ -172,14 +176,14 @@ class TournamentBotGamePlayer:
|
|||||||
node
|
node
|
||||||
}.getOrElse(None)
|
}.getOrElse(None)
|
||||||
|
|
||||||
private def openGameStream(cfg: TournamentBotConfig, gameId: String): InputStream =
|
private def openGameStream(cfg: TournamentBotConfig, gameId: String): Option[InputStream] =
|
||||||
Try {
|
Try {
|
||||||
val response = authed(cfg, target(cfg).path("game").path(gameId).path("stream"))
|
val response = authed(cfg, target(cfg).path("game").path(gameId).path("stream"))
|
||||||
.header("Accept", "application/x-ndjson")
|
.header("Accept", "application/x-ndjson")
|
||||||
.get()
|
.get()
|
||||||
if response.getStatus == 200 then response.readEntity(classOf[InputStream])
|
if response.getStatus == 200 then Some(response.readEntity(classOf[InputStream]))
|
||||||
else { log.warnf("Game stream %s returned status %d", gameId, response.getStatus); response.close(); null }
|
else { log.warnf("Game stream %s returned status %d", gameId, response.getStatus); response.close(); None }
|
||||||
}.getOrElse(null)
|
}.getOrElse(None)
|
||||||
|
|
||||||
private def engine(cfg: TournamentBotConfig): Bot =
|
private def engine(cfg: TournamentBotConfig): Bot =
|
||||||
botController.getBot(cfg.difficulty).orElse(botController.getBot("medium")).get
|
botController.getBot(cfg.difficulty).orElse(botController.getBot("medium")).get
|
||||||
@@ -196,12 +200,14 @@ class TournamentBotGamePlayer:
|
|||||||
|
|
||||||
private def forEachLine(stream: InputStream)(handle: String => Unit): Unit =
|
private def forEachLine(stream: InputStream)(handle: String => Unit): Unit =
|
||||||
val reader = new BufferedReader(new InputStreamReader(stream))
|
val reader = new BufferedReader(new InputStreamReader(stream))
|
||||||
// scalafix:off DisableSyntax.var
|
Iterator
|
||||||
var line: String = reader.readLine()
|
.continually(reader.readLine())
|
||||||
// scalafix:on DisableSyntax.var
|
.map(Option(_))
|
||||||
while line != null && running do
|
.takeWhile(opt => opt.isDefined && running)
|
||||||
Try(handle(line)).failed.foreach(ex => log.warnf(ex, "Error handling stream line"))
|
.flatten
|
||||||
line = reader.readLine()
|
.foreach { line =>
|
||||||
|
Try(handle(line)).failed.foreach(ex => log.warnf(ex, "Error handling stream line"))
|
||||||
|
}
|
||||||
|
|
||||||
private def toUci(move: Move): String =
|
private def toUci(move: Move): String =
|
||||||
val base = s"${move.from}${move.to}"
|
val base = s"${move.from}${move.to}"
|
||||||
|
|||||||
Reference in New Issue
Block a user