diff --git a/src/main/scala/wow/doge/mygame/MainApp.scala b/src/main/scala/wow/doge/mygame/MainApp.scala index 09fdb43..12e9dfd 100644 --- a/src/main/scala/wow/doge/mygame/MainApp.scala +++ b/src/main/scala/wow/doge/mygame/MainApp.scala @@ -34,7 +34,6 @@ import monix.bio.Task import monix.bio.UIO import monix.eval.Coeval import monix.execution.cancelables.CompositeCancelable -import monix.execution.exceptions.DummyException import monix.reactive.Observable import monix.{eval => me} import scalafx.scene.control.Label @@ -83,6 +82,7 @@ import wow.doge.mygame.utils.MonixDirectoryWatcher.ModifyEvent import wow.doge.mygame.utils.controls.JFXProgressBar import wow.doge.mygame.utils.wrappers.jme.AssetManager import wow.doge.mygame.utils.wrappers.jme.PhysicsSpace + class MainApp( logger: Logger[Task], jmeThread: JmeScheduler, @@ -151,22 +151,18 @@ class MainApp( .executeOn(gameApp.scheduler.value) } yield fib - // val k = new FunctionK[Task, UIO] { - - // override def apply[A](fa: monix.bio.Task[A]): monix.bio.UIO[A] = - // fa.hideErrors - - // } - def gameInit( tickEventBus: GameEventBus[TickEvent] ): Resource[UIO, Either[AppError, Fiber[Nothing, Unit]]] = for { - r1 <- wire[GameAppResource].resource.evalMap { - case Right(gameApp -> gameAppFib) => - eval(tickEventBus, gameApp, gameAppFib).attempt - case Left(error) => IO.terminate(new Exception(error.toString)) - } + r1 <- wire[GameAppResource].resource.evalMap(e => + IO.fromEither(e) + .flatMap { + case (gameApp -> gameAppFib) => + eval(tickEventBus, gameApp, gameAppFib) + } + .attempt + ) dirWatcher <- Resource.liftF( MonixDirectoryWatcher( os.pwd / "assets" / "scripts" diff --git a/src/main/scala/wow/doge/mygame/game/subsystems/input/GameInputHandler.scala b/src/main/scala/wow/doge/mygame/game/subsystems/input/GameInputHandler.scala index d6bca6c..650d919 100644 --- a/src/main/scala/wow/doge/mygame/game/subsystems/input/GameInputHandler.scala +++ b/src/main/scala/wow/doge/mygame/game/subsystems/input/GameInputHandler.scala @@ -144,28 +144,28 @@ object GameInputHandler { case PlayerMovementInput.WalkLeft => me.Task( playerEventBus ! EventBus.Publish( - PlayerMovementEvent.PlayerMovedLeft(pressed = action.value), + PlayerMovementEvent.PlayerMovedLeft(action.value), name ) ) case PlayerMovementInput.WalkRight => me.Task( playerEventBus ! EventBus.Publish( - PlayerMovementEvent.PlayerMovedRight(pressed = action.value), + PlayerMovementEvent.PlayerMovedRight(action.value), name ) ) case PlayerMovementInput.WalkForward => me.Task( playerEventBus ! EventBus.Publish( - PlayerMovementEvent.PlayerMovedForward(pressed = action.value), + PlayerMovementEvent.PlayerMovedForward(action.value), name ) ) case PlayerMovementInput.WalkBackward => me.Task( playerEventBus ! EventBus.Publish( - PlayerMovementEvent.PlayerMovedBackward(pressed = action.value), + PlayerMovementEvent.PlayerMovedBackward(action.value), name ) ) diff --git a/src/main/scala/wow/doge/mygame/subsystems/moddingsystem/ModdingSystem.scala b/src/main/scala/wow/doge/mygame/subsystems/moddingsystem/ModdingSystem.scala index 9e94528..b6cf738 100644 --- a/src/main/scala/wow/doge/mygame/subsystems/moddingsystem/ModdingSystem.scala +++ b/src/main/scala/wow/doge/mygame/subsystems/moddingsystem/ModdingSystem.scala @@ -44,7 +44,7 @@ object ModdingSystem { } def readPluginsList(dir: os.Path): IO[Error, ArraySeq[Plugin]] = - IO(parse(os.read(dir / "plugins.json"))) + IO(os.read(dir / "plugins.json")) .onErrorHandleWith { case _: FileNotFoundException => IO.raiseError(FileNotFound(dir / "plugins.json")) @@ -52,16 +52,16 @@ object ModdingSystem { IO.raiseError(FileNotFound(dir / "plugins.json")) } .flatMap(files => - IO.fromEither(files) - .map(_.as[ArraySeq[Plugin]]) + IO.fromEither(parse(files)) .mapError(ParseFailure) + .map(_.as[ArraySeq[Plugin]]) ) .flatMap(result => IO.fromEither(result).mapError(DecodingFailure)) - def findPluginFiles(dir: os.Path): View[os.Path] = - os.list(dir) - .view - .filter(f => f.ext === "json" && f.baseName.endsWith("plugin")) + // def findPluginFiles(dir: os.Path): View[os.Path] = + // os.list(dir) + // .view + // .filter(f => f.ext === "json" && f.baseName.endsWith("plugin")) def findAndReadPluginFiles( dir: os.Path, diff --git a/src/main/scala/wow/doge/mygame/subsystems/scriptsystem/MonixScriptCompiler.scala b/src/main/scala/wow/doge/mygame/subsystems/scriptsystem/MonixScriptCompiler.scala index 2ae5ed1..5a966d3 100644 --- a/src/main/scala/wow/doge/mygame/subsystems/scriptsystem/MonixScriptCompiler.scala +++ b/src/main/scala/wow/doge/mygame/subsystems/scriptsystem/MonixScriptCompiler.scala @@ -44,6 +44,19 @@ trait Requestable[A] { _ <- queue.offer(req) res <- d.get.timeout(timeout).map(_.get) } yield res + + // def request[T]( + // compileRequest: Deferred[Task, T] => A + // )(implicit timeout: FiniteDuration): IO[AppError, T] = + // for { + // d <- Deferred[Task, T].hideErrors + // req = compileRequest(d) + // _ <- queue.offer(req).hideErrors + // res <- + // d.get.hideErrors + // .timeout(timeout) + // .flatMap(o => IO.fromOption(o, AppError.TimeoutError("Timed out"))) + // } yield res } class ScriptCompiler private ( diff --git a/src/test/scala/wow/doge/mygame/WebsocketTest.scala b/src/test/scala/wow/doge/mygame/WebsocketTest.scala index 6b42eb1..0e4d385 100644 --- a/src/test/scala/wow/doge/mygame/WebsocketTest.scala +++ b/src/test/scala/wow/doge/mygame/WebsocketTest.scala @@ -7,6 +7,8 @@ import sttp.client3.asynchttpclient.monix._ import monix.eval.Task import monix.reactive.Observable import scala.concurrent.duration.Duration +import scala.collection.immutable.ArraySeq +import monix.reactive.Consumer class WebsocketTest { // : Task[Response[Either[String, Observable[Array[Byte]]]]] @@ -15,13 +17,25 @@ class WebsocketTest { val response = basicRequest .post(uri"...") - .response( - asStream(MonixStreams)( - _.doOnNext(i => Task(println(s"$i"))).completedL - ) - ) + .response(asStreamUnsafe(MonixStreams)) .readTimeout(Duration.Inf) .send(backend) - response + response.map(_.body.map(_.map(ArraySeq.unsafeWrapArray))) } + + val MB = 1024 * 1024 + def consumer(contentLength: Long): Consumer[ArraySeq[Byte], Long] = + Consumer.foldLeftEval(0L) { + case (sum, data) => + val newSum = sum + data.length + for { + _ <- Task( + pprint.log( + s"Bytes downloaded = ${newSum * 1f / MB}MB . Percent done = ${(newSum * 100f / contentLength) + .formatted("%.2f")}" + ) + ) + } yield newSum + } + }