From 21320b158bcc01aab334e8d909d71b2f9d311578 Mon Sep 17 00:00:00 2001 From: Thanh Le Date: Tue, 12 Mar 2024 10:54:19 +0700 Subject: [PATCH] Refactor Lila.Move => Lila.Response --- app/src/main/scala/Executor.scala | 2 +- app/src/main/scala/LilaClient.scala | 4 ++-- app/src/main/scala/model.scala | 2 +- app/src/test/scala/ExecutorTest.scala | 18 +++++++++--------- app/src/test/scala/Helper.scala | 2 +- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/app/src/main/scala/Executor.scala b/app/src/main/scala/Executor.scala index c9faa51..6de38f6 100644 --- a/app/src/main/scala/Executor.scala +++ b/app/src/main/scala/Executor.scala @@ -66,7 +66,7 @@ object Executor: response.uci match case Some(uci) => state.remove(task.id) -> (monitor.success(task) >> - client.send(Lila.Move(task.request.id, task.request.moves, uci))) + client.send(Lila.Response(task.request.id, task.request.moves, uci))) case _ => val (newState, io) = task.clearAssignedKey match case None => diff --git a/app/src/main/scala/LilaClient.scala b/app/src/main/scala/LilaClient.scala index 19f18e6..22c59eb 100644 --- a/app/src/main/scala/LilaClient.scala +++ b/app/src/main/scala/LilaClient.scala @@ -4,10 +4,10 @@ import cats.effect.IO import io.chrisdavenport.rediculous.RedisPubSub trait LilaClient: - def send(move: Lila.Move): IO[Unit] + def send(move: Lila.Response): IO[Unit] object LilaClient: def apply(pubsub: RedisPubSub[IO]): LilaClient = new LilaClient: - def send(move: Lila.Move): IO[Unit] = pubsub.publish("fishnet-in", move.write).void + def send(move: Lila.Response): IO[Unit] = pubsub.publish("fishnet-in", move.write).void diff --git a/app/src/main/scala/model.scala b/app/src/main/scala/model.scala index 679087b..956561d 100644 --- a/app/src/main/scala/model.scala +++ b/app/src/main/scala/model.scala @@ -70,7 +70,7 @@ object Lila: import ChessCirceCodecs.given - case class Move(gameId: GameId, moves: String, uci: Uci): + case class Response(gameId: GameId, moves: String, uci: Uci): def sign = moves.takeRight(20).replace(" ", "") def write = s"$gameId $sign ${uci.uci}" diff --git a/app/src/test/scala/ExecutorTest.scala b/app/src/test/scala/ExecutorTest.scala index 20fd442..3b28ff4 100644 --- a/app/src/test/scala/ExecutorTest.scala +++ b/app/src/test/scala/ExecutorTest.scala @@ -67,8 +67,8 @@ object ExecutorTest extends SimpleIOSuite: _ <- executor.add(request) acquired <- executor.acquire(key) _ <- executor.move(acquired.get.id, key, validMove) - move <- ref.get.map(_.head) - yield expect.same(move, Lila.Move(request.id, request.moves, chess.format.Uci.Move("e2e4").get)) + response <- ref.get.map(_.head) + yield expect.same(response, Lila.Response(request.id, request.moves, chess.format.Uci.Move("e2e4").get)) test("post move after timeout should not send move"): for @@ -90,8 +90,8 @@ object ExecutorTest extends SimpleIOSuite: _ <- executor.clean(Instant.now.plusSeconds(37)) acquired <- executor.acquire(key) _ <- executor.move(acquired.get.id, key, validMove) - move <- ref.get.map(_.head) - yield expect.same(move, Lila.Move(request.id, request.moves, chess.format.Uci.Move("e2e4").get)) + response <- ref.get.map(_.head) + yield expect.same(response, Lila.Response(request.id, request.moves, chess.format.Uci.Move("e2e4").get)) test("post an invalid move should not send move"): for @@ -168,7 +168,7 @@ object ExecutorTest extends SimpleIOSuite: def createExecutor(config: ExecutorConfig = ExecutorConfig(300)): IO[Executor] = createLilaClient.flatMap(ioExecutor(_)(noopMonitor, config)) - def createExecutor(ref: Ref[IO, List[Lila.Move]])(config: ExecutorConfig): IO[Executor] = + def createExecutor(ref: Ref[IO, List[Lila.Response]])(config: ExecutorConfig): IO[Executor] = ioExecutor(createLilaClient(ref))(noopMonitor, config) def ioExecutor(client: LilaClient)(monitor: Monitor, config: ExecutorConfig): IO[Executor] = @@ -179,10 +179,10 @@ object ExecutorTest extends SimpleIOSuite: def createLilaClient: IO[LilaClient] = emptyMovesRef.map(createLilaClient) - def createLilaClient(ref: Ref[IO, List[Lila.Move]]): LilaClient = + def createLilaClient(ref: Ref[IO, List[Lila.Response]]): LilaClient = new LilaClient: - def send(move: Lila.Move): IO[Unit] = + def send(move: Lila.Response): IO[Unit] = ref.update(_ :+ move) - def emptyMovesRef: IO[Ref[IO, List[Lila.Move]]] = - Ref.of[IO, List[Lila.Move]](Nil) + def emptyMovesRef: IO[Ref[IO, List[Lila.Response]]] = + Ref.of[IO, List[Lila.Response]](Nil) diff --git a/app/src/test/scala/Helper.scala b/app/src/test/scala/Helper.scala index e16df79..8a8fd68 100644 --- a/app/src/test/scala/Helper.scala +++ b/app/src/test/scala/Helper.scala @@ -11,7 +11,7 @@ object Helper: val noopLilaClient: LilaClient = new LilaClient: - def send(move: Lila.Move): IO[Unit] = IO.unit + def send(move: Lila.Response): IO[Unit] = IO.unit val noopStateRepository: StateRepository = new StateRepository: