diff --git a/knockoutwhistweb/app/controllers/HomeController.scala b/knockoutwhistweb/app/controllers/HomeController.scala index e4f5bb6..b8435f6 100644 --- a/knockoutwhistweb/app/controllers/HomeController.scala +++ b/knockoutwhistweb/app/controllers/HomeController.scala @@ -55,32 +55,35 @@ class HomeController @Inject()(val controllerComponents: ControllerComponents) e def ingame(id: String): Action[AnyContent] = { val uuid: UUID = UUID.fromString(id) - if (PodGameManager.identify(uuid).isEmpty) { - return Action { implicit request => - NotFound(views.html.tui.apply(List(Html(s"
Session with id $id not found!
")))) - } - } else { - val session = PodGameManager.identify(uuid).get - val player = session.asInstanceOf[SimpleSession].player - val logic = null - if (logic.getCurrentState == Lobby) { - - } else if (logic.getCurrentState == InGame) { - return Action { implicit request => - Ok(views.html.ingame.apply(player, logic)) - } - } else if (logic.getCurrentState == SelectTrump) { - return Action { implicit request => - Ok(views.html.selecttrump.apply(player, logic)) - } - } else if (logic.getCurrentState == TieBreak) { - return Action { implicit request => - Ok(views.html.tie.apply(player, logic)) - } - } - } Action { implicit request => - InternalServerError("Oops") + NotFound(views.html.tui.apply(List(Html(s"Session with id $id not found!
")))) } +// if (PodGameManager.identify(uuid).isEmpty) { +// return Action { implicit request => +// NotFound(views.html.tui.apply(List(Html(s"Session with id $id not found!
")))) +// } +// } else { +// val session = PodGameManager.identify(uuid).get +// val player = session.asInstanceOf[SimpleSession].player +// val logic = BaseGameLogic(null) +// if (logic.getCurrentState == Lobby) { +// +// } else if (logic.getCurrentState == InGame) { +// return Action { implicit request => +// Ok(views.html.ingame.apply(player, logic)) +// } +// } else if (logic.getCurrentState == SelectTrump) { +// return Action { implicit request => +// Ok(views.html.selecttrump.apply(player, logic)) +// } +// } else if (logic.getCurrentState == TieBreak) { +// return Action { implicit request => +// Ok(views.html.tie.apply(player, logic)) +// } +// } +// } +// Action { implicit request => +// InternalServerError("Oops") +// } } } \ No newline at end of file diff --git a/knockoutwhistweb/app/exceptions/NotHostException.java b/knockoutwhistweb/app/exceptions/NotHostException.java new file mode 100644 index 0000000..490d619 --- /dev/null +++ b/knockoutwhistweb/app/exceptions/NotHostException.java @@ -0,0 +1,7 @@ +package exceptions; + +public class NotHostException extends RuntimeException { + public NotHostException(String message) { + super(message); + } +} diff --git a/knockoutwhistweb/app/exceptions/NotInThisGameException.java b/knockoutwhistweb/app/exceptions/NotInThisGameException.java new file mode 100644 index 0000000..e33b706 --- /dev/null +++ b/knockoutwhistweb/app/exceptions/NotInThisGameException.java @@ -0,0 +1,7 @@ +package exceptions; + +public class NotInThisGameException extends RuntimeException { + public NotInThisGameException(String message) { + super(message); + } +} diff --git a/knockoutwhistweb/app/exceptions/NotInteractableException.java b/knockoutwhistweb/app/exceptions/NotInteractableException.java new file mode 100644 index 0000000..e4642c9 --- /dev/null +++ b/knockoutwhistweb/app/exceptions/NotInteractableException.java @@ -0,0 +1,7 @@ +package exceptions; + +public class NotInteractableException extends RuntimeException { + public NotInteractableException(String message) { + super(message); + } +} diff --git a/knockoutwhistweb/app/logic/game/GameLobby.scala b/knockoutwhistweb/app/logic/game/GameLobby.scala new file mode 100644 index 0000000..6f20ae9 --- /dev/null +++ b/knockoutwhistweb/app/logic/game/GameLobby.scala @@ -0,0 +1,69 @@ +package logic.game + +import de.knockoutwhist.control.GameLogic +import de.knockoutwhist.events.player.PlayerEvent +import de.knockoutwhist.player.Playertype.HUMAN +import de.knockoutwhist.player.{AbstractPlayer, PlayerFactory} +import de.knockoutwhist.rounds.Match +import de.knockoutwhist.utils.events.{EventListener, SimpleEvent} +import exceptions.{NotHostException, NotInThisGameException, NotInteractableException} +import model.sessions.UserSession +import model.users.User + +import java.util.UUID +import scala.collection.mutable.ListBuffer + +class GameLobby(val logic: GameLogic, val id: String, internalId: UUID) extends EventListener{ + logic.addListener(this) + logic.createSession() + + val users: Map[UUID, UserSession] = Map() + + override def listen(event: SimpleEvent): Unit = { + event match { + case event: PlayerEvent => + users.get(event.playerId).foreach(session => session.updatePlayer(event)) + case event: SimpleEvent => + users.values.foreach(session => session.updatePlayer(event)) + } + } + + def startGame(user: User): Unit = { + val sessionOpt = users.get(user.id) + if (sessionOpt.isEmpty) { + throw new NotInThisGameException("You are not in this game!") + } + if (!sessionOpt.get.host) { + throw new NotHostException("Only the host can start the game!") + } + val playerNamesList = ListBuffer[AbstractPlayer]() + users.values.foreach { player => + playerNamesList += PlayerFactory.createPlayer(player.name, player.id, HUMAN) + } + logic.createMatch(playerNamesList.toList) + logic.controlMatch() + } + + def playCard(user: User, card: Int): Unit = { + val sessionOpt = users.get(user.id) + if (sessionOpt.isEmpty) { + throw new NotInThisGameException("You are not in this game!") + } + if (!sessionOpt.get.canInteract) { + throw new NotInteractableException("You can't play a card!") + } + + } + + + //------------------- + + private def getMatch: Match = { + val matchOpt = logic.getCurrentMatch + if (matchOpt.isEmpty) { + throw new IllegalStateException("No match is currently running!") + } + matchOpt.get + } + +} diff --git a/knockoutwhistweb/app/model/game/GameLobby.scala b/knockoutwhistweb/app/model/game/GameLobby.scala deleted file mode 100644 index 3e4767b..0000000 --- a/knockoutwhistweb/app/model/game/GameLobby.scala +++ /dev/null @@ -1,14 +0,0 @@ -package model.game - -import de.knockoutwhist.control.GameLogic -import de.knockoutwhist.utils.events.{EventListener, SimpleEvent} - -class GameLobby(val logic: GameLogic) extends EventListener{ - logic.addListener(this) - logic.createSession() - - - override def listen(event: SimpleEvent): Unit = { - - } -} diff --git a/knockoutwhistweb/app/model/sessions/PlayerSession.scala b/knockoutwhistweb/app/model/sessions/PlayerSession.scala index 87248f3..95c39f5 100644 --- a/knockoutwhistweb/app/model/sessions/PlayerSession.scala +++ b/knockoutwhistweb/app/model/sessions/PlayerSession.scala @@ -1,6 +1,5 @@ package model.sessions -import de.knockoutwhist.player.AbstractPlayer import de.knockoutwhist.utils.events.SimpleEvent import java.util.UUID @@ -9,7 +8,6 @@ trait PlayerSession { def id: UUID def name: String - def player: AbstractPlayer def updatePlayer(event: SimpleEvent): Unit } diff --git a/knockoutwhistweb/app/model/sessions/UserSession.scala b/knockoutwhistweb/app/model/sessions/UserSession.scala index 1019b59..ccfd172 100644 --- a/knockoutwhistweb/app/model/sessions/UserSession.scala +++ b/knockoutwhistweb/app/model/sessions/UserSession.scala @@ -1,8 +1,28 @@ package model.sessions -import de.knockoutwhist.player.AbstractPlayer +import de.knockoutwhist.events.player.{RequestCardEvent, RequestTieChoiceEvent, RequestTrumpSuitEvent} +import de.knockoutwhist.utils.events.SimpleEvent +import model.users.User + import java.util.UUID -class UserSession(id: UUID, player: AbstractPlayer) extends SimpleSession(id, player) { +class UserSession(user: User, val host: Boolean) extends PlayerSession { + var canInteract: Boolean = false + override def updatePlayer(event: SimpleEvent): Unit = { + event match { + case event: RequestTrumpSuitEvent => + canInteract = true + case event: RequestTieChoiceEvent => + canInteract = true + case event: RequestCardEvent => + canInteract = true + case _ => + } + } + + override def id: UUID = user.id + + override def name: String = user.name + } diff --git a/knockoutwhistweb/conf/application.conf b/knockoutwhistweb/conf/application.conf index d6d372a..c9d8a3d 100644 --- a/knockoutwhistweb/conf/application.conf +++ b/knockoutwhistweb/conf/application.conf @@ -6,9 +6,9 @@ auth { issuer = "knockoutwhistweb" audience = "ui" # ${?PUBLIC_KEY_FILE} - privateKeyFile = "D:\\Workspaces\\Gitops\\rsa512-private.pem" + privateKeyFile = "/home/janis/Workspaces/IntelliJ/KnockOutWhist/Gitops/rsa512-private.pem" privateKeyPem = ${?PUBLIC_KEY_PEM} #${?PUBLIC_KEY_FILE} - publicKeyFile = "D:\\Workspaces\\Gitops\\rsa512-public.pem" + publicKeyFile = "/home/janis/Workspaces/IntelliJ/KnockOutWhist/Gitops/rsa512-public.pem" publicKeyPem = ${?PUBLIC_KEY_PEM} }