Skip to content

Commit

Permalink
Refactor Lila.Move => Lila.Response
Browse files Browse the repository at this point in the history
  • Loading branch information
lenguyenthanh committed Mar 12, 2024
1 parent 8c7f667 commit 21320b1
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion app/src/main/scala/Executor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/scala/LilaClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion app/src/main/scala/model.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

Expand Down
18 changes: 9 additions & 9 deletions app/src/test/scala/ExecutorTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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] =
Expand All @@ -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)
2 changes: 1 addition & 1 deletion app/src/test/scala/Helper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 21320b1

Please sign in to comment.