forked from nova/jmonkey-test
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
2.4 KiB
87 lines
2.4 KiB
package wow.doge.mygame
|
|
import akka.actor.typed.scaladsl.Behaviors
|
|
import wow.doge.mygame.game.GameApp
|
|
import akka.actor.typed.Behavior
|
|
import wow.doge.mygame.game.GameAppActor
|
|
import cats.effect.Resource
|
|
import akka.actor.typed.ActorSystem
|
|
import monix.bio.Task
|
|
import wow.doge.mygame.game.GameModule
|
|
import io.odin._
|
|
import io.odin.syntax._
|
|
import wow.doge.mygame.executors.ExecutorsModule
|
|
import akka.actor.typed.scaladsl.ActorContext
|
|
import wow.doge.mygame.executors.Schedulers
|
|
import com.softwaremill.macwire._
|
|
|
|
trait MainModule extends GameModule with ExecutorsModule {
|
|
def actorSystemResource(
|
|
logger: Logger[Task],
|
|
app: GameApp,
|
|
schedulers: Schedulers
|
|
): Resource[Task, ActorSystem[RootActor.Command]] =
|
|
Resource.make(logger.info("Creating Actor System") >> Task {
|
|
ActorSystem(RootActor(app, schedulers), name = "GameActorSystem")
|
|
})(sys =>
|
|
logger.info("Shutting down actor system") >> Task(
|
|
sys.terminate()
|
|
)
|
|
)
|
|
}
|
|
|
|
object MainModule {
|
|
|
|
// import cats.implicits._
|
|
import scala.concurrent.duration._
|
|
val DefaultFileLogger: Resource[Task, Logger[Task]] =
|
|
fileLogger[Task](
|
|
"log.log"
|
|
).withAsync(timeWindow = 1.seconds)
|
|
}
|
|
|
|
object RootActor {
|
|
sealed trait Command
|
|
final case object Start extends Command
|
|
final case object Stop extends Command
|
|
|
|
final case class State(initialized: Boolean = false)
|
|
def apply(
|
|
app: GameApp,
|
|
schedulers: Schedulers,
|
|
state: State = State()
|
|
): Behavior[Command] =
|
|
Behaviors.setup { ctx =>
|
|
ctx.log.info("Hello from root actor")
|
|
wire[RootActor].receive(state)
|
|
}
|
|
}
|
|
|
|
class RootActor(
|
|
ctx: ActorContext[RootActor.Command],
|
|
app: GameApp,
|
|
schedulers: Schedulers
|
|
) {
|
|
import RootActor._
|
|
def receive(state: State): Behavior[Command] =
|
|
Behaviors.receiveMessage(msg =>
|
|
msg match {
|
|
case Start =>
|
|
if (!state.initialized) {
|
|
ctx.log.info("Starting GameAppActor")
|
|
val _ = ctx.spawn(
|
|
wireWith(GameAppActor.apply _),
|
|
"gameAppActor"
|
|
// DispatcherSelector.fromConfig("jme-dispatcher")
|
|
)
|
|
receive(state.copy(initialized = true))
|
|
} else {
|
|
ctx.log.warn("Already Initialized")
|
|
Behaviors.same
|
|
}
|
|
case Stop =>
|
|
ctx.log.info("Stopping")
|
|
Behaviors.stopped
|
|
|
|
}
|
|
)
|
|
}
|