feat: Rework draw handling in GameEngine and GameResource
This commit is contained in:
@@ -9,6 +9,5 @@ final case class GameEntry(
|
||||
engine: GameEngine,
|
||||
white: PlayerInfo,
|
||||
black: PlayerInfo,
|
||||
drawOfferedBy: Option[Color] = None,
|
||||
resigned: Boolean = false,
|
||||
)
|
||||
|
||||
@@ -31,7 +31,7 @@ class GameResource(@Inject val registry: GameRegistry, @Inject val objectMapper:
|
||||
// ── mapping ──────────────────────────────────────────────────────────────
|
||||
|
||||
private def statusOf(entry: GameEntry): String =
|
||||
if entry.drawOfferedBy.isDefined then "drawOffered"
|
||||
if entry.engine.pendingDrawOfferBy.isDefined then "drawOffered"
|
||||
else
|
||||
val ctx = entry.engine.context
|
||||
ctx.result match
|
||||
@@ -104,7 +104,7 @@ class GameResource(@Inject val registry: GameRegistry, @Inject val objectMapper:
|
||||
val error = new AtomicReference[Option[String]](None)
|
||||
val obs = new Observer:
|
||||
def onGameEvent(e: GameEvent): Unit = e match
|
||||
case InvalidMoveEvent(_, reason) => error.set(Some(reason))
|
||||
case InvalidMoveEvent(_, reason) => error.set(Some(reason.toString))
|
||||
case _ => ()
|
||||
engine.subscribe(obs)
|
||||
engine.processUserInput(uci)
|
||||
@@ -235,26 +235,16 @@ class GameResource(@Inject val registry: GameRegistry, @Inject val objectMapper:
|
||||
assertGameNotOver(entry)
|
||||
action match
|
||||
case "offer" =>
|
||||
registry.update(entry.copy(drawOfferedBy = Some(entry.engine.context.turn)))
|
||||
entry.engine.offerDraw(entry.engine.context.turn)
|
||||
ok(OkResponseDto())
|
||||
case "accept" =>
|
||||
entry.drawOfferedBy match
|
||||
case None =>
|
||||
throw BadRequestException("NO_DRAW_OFFER", "No draw offer to accept")
|
||||
case Some(offerer) if offerer == entry.engine.context.turn =>
|
||||
throw BadRequestException("CANNOT_ACCEPT_OWN_OFFER", "Cannot accept your own draw offer")
|
||||
case _ =>
|
||||
entry.engine.applyDraw(DrawReason.Agreement)
|
||||
registry.update(entry.copy(drawOfferedBy = None))
|
||||
ok(OkResponseDto())
|
||||
entry.engine.acceptDraw(entry.engine.context.turn)
|
||||
ok(OkResponseDto())
|
||||
case "decline" =>
|
||||
if entry.drawOfferedBy.isEmpty then throw BadRequestException("NO_DRAW_OFFER", "No draw offer to decline")
|
||||
registry.update(entry.copy(drawOfferedBy = None))
|
||||
entry.engine.declineDraw(entry.engine.context.turn)
|
||||
ok(OkResponseDto())
|
||||
case "claim" =>
|
||||
if entry.engine.context.halfMoveClock < 100 then
|
||||
throw BadRequestException("CLAIM_NOT_AVAILABLE", "Fifty-move rule draw is not available")
|
||||
entry.engine.applyDraw(DrawReason.FiftyMoveRule)
|
||||
entry.engine.claimDraw()
|
||||
ok(OkResponseDto())
|
||||
case _ =>
|
||||
throw BadRequestException("INVALID_ACTION", s"Unknown draw action: $action", Some("action"))
|
||||
|
||||
Reference in New Issue
Block a user