Kick button, show player turn, can't join if 2-nd player alreay join

This commit is contained in:
Anibus
2020-11-22 12:39:56 +03:00
parent 6398f44d18
commit f94bf511a3
5 changed files with 100 additions and 41 deletions
+45 -12
View File
@@ -1,6 +1,6 @@
package actors
import actors.UserActor.{GetName, HostLeaveLobby, RefreshLobbyInfo, SetUserAsHost}
import actors.UserActor.{GetName, HostLeaveLobby, LobbyFatal, RefreshLobbyInfo, SecondPlayerLeaveLobby, SetUserAsHost, SetUserAsObs, SetUserAsSecondPlayer}
import akka.actor.{Actor, ActorLogging, ActorRef}
import akka.event.LoggingReceive
import com.typesafe.scalalogging.LazyLogging
@@ -49,7 +49,14 @@ class LobbieActor(hostUser: LobbyUser) extends Actor with LazyLogging {
}
hostUser.actorRef.tell(
SetUserAsHost(LobbyInfo(hostUser.name, "", firstPlayerReady, secondPlayerReady, self.path.name, status.toString(), mapsLobby)), this.self)
SetUserAsHost(LobbyInfo(
UserInfo(hostUser.name,firstPlayerReady),
UserInfo("", secondPlayerReady),
self.path.name,
status.toString(),
playerTurn,
mapsLobby)),
this.self)
implicit val timeout: Timeout = 1.second
@@ -98,21 +105,38 @@ class LobbieActor(hostUser: LobbyUser) extends Actor with LazyLogging {
}
users.foreach(_ ! RefreshLobbyInfo(getLobbyInfoResponse))
case KickSecondPlayer =>
if (sender() == host.actorRef) {
secondPlayer.foreach(player => player.actorRef ! LobbyFatal("You were kicked from lobby!"))
secondPlayerReady = false
secondPlayer = None
users.foreach(_ ! RefreshLobbyInfo(getLobbyInfoResponse))
}
case JoinLobbyAsPlayer(sp) =>
logger.info(s"User ${sender.path.name} join lobby ${self.path.name} as player")
secondPlayer = Some(sp)
users = users + sp.actorRef
sp.actorRef.tell(
SetUserAsHost(LobbyInfo(hostUser.name, sp.name, firstPlayerReady, secondPlayerReady, self.path.name, status.toString(), mapsLobby)), this.self)
users.foreach(_ ! RefreshLobbyInfo(getLobbyInfoResponse))
if (secondPlayer.isEmpty) {
secondPlayer = Some(sp)
users = users + sp.actorRef
sp.actorRef.tell(
SetUserAsSecondPlayer(getLobbyInfoResponse), this.self)
users.foreach(_ ! RefreshLobbyInfo(getLobbyInfoResponse))
} else {
sp.actorRef ! LobbyFatal("Lobby already full")
}
case WatchLobby(_) =>
// add the watcher to the list
users = users + sender
sender ! SetUserAsObs(getLobbyInfoResponse)
case LeaveLobby =>
users = users - sender
if(secondPlayer.exists(sp => sp.actorRef == sender)) secondPlayer = None
if(host.actorRef == sender()){
users.foreach(_ ! HostLeaveLobby)
context.stop(self)
}else if(secondPlayer.exists(_.actorRef == sender())){
users.foreach(_ ! SecondPlayerLeaveLobby)
context.stop(self)
}
if (users.isEmpty) {
logger.info(s"Stop lobby ${self.path.name}")
@@ -125,7 +149,13 @@ class LobbieActor(hostUser: LobbyUser) extends Actor with LazyLogging {
private def getLobbyInfoResponse: LobbyInfo = {
val user2Name = secondPlayer.map(_.name).getOrElse("")
LobbyInfo(host.name, user2Name, firstPlayerReady, secondPlayerReady, self.path.name, status.toString(), mapsLobby)
LobbyInfo(
UserInfo(host.name, firstPlayerReady),
UserInfo(user2Name, secondPlayerReady),
self.path.name,
status.toString(),
playerTurn,
mapsLobby)
}
}
@@ -139,6 +169,8 @@ case class WatchLobby(lobbyName: String)
case class JoinLobbyAsPlayer(lobbyUser: LobbyUser)
case object KickSecondPlayer
case object LeaveLobby
case object SetReady
@@ -147,12 +179,13 @@ case object SetNotReady
case object InfoQuery
case class LobbyInfo(user1Name: String,
user2Name: String,
user1Ready: Boolean,
user2Ready: Boolean,
case class UserInfo(name: String, isReady: Boolean)
case class LobbyInfo(user1Info: UserInfo,
user2Info: UserInfo,
lobbyActorName: String,
status: String,
playerTurn: BigDecimal,
maps: Set[DeciderMap])
class LobbyStatus