This commit is contained in:
Anibus
2020-11-29 03:13:10 +03:00
parent ec519889dc
commit 460e95c824
5 changed files with 149 additions and 69 deletions
+31 -7
View File
@@ -1,6 +1,6 @@
package actors
import actors.UserActor.{GetName, HostLeaveLobby, LobbyFatal, RefreshLobbyInfo, SecondPlayerLeaveLobby, SetUserAsHost, SetUserAsObs, SetUserAsSecondPlayer}
import actors.UserActor.{BanMapMessage, GetName, HostLeaveLobby, LobbyFatal, Message, RefreshLobbyInfo, SecondPlayerLeaveLobby, SendBanMapMessage, SendMessage, SetUserAsHost, SetUserAsObs, SetUserAsSecondPlayer}
import akka.actor.{Actor, ActorLogging, ActorRef}
import akka.event.LoggingReceive
import com.typesafe.scalalogging.LazyLogging
@@ -65,20 +65,35 @@ class LobbieActor(hostUser: LobbyUser) extends Actor with LazyLogging {
case BanMap(mapName: String) =>
// notify watchers
logger.info(s"Ban map $mapName by ${sender.path.name}")
if(status == Draft()){
if((playerTurn == 1 && sender == host.actorRef) ||
(playerTurn == 2 && secondPlayer.exists(_.actorRef == sender))){
mapsLobby.find(p => p.map == mapName).foreach(_.isBanned = true)
if (playerTurn== 1) playerTurn = 2 else playerTurn = 1
if(mapsLobby.count(_.isBanned == false) == 1){
status = Finish()
mapsLobby.find(p => p.map == mapName) match {
case Some(map) =>
if(!map.isBanned){
map.isBanned = true
if (playerTurn== 1) playerTurn = 2 else playerTurn = 1
if(mapsLobby.count(_.isBanned == false) == 1){
status = Finish()
}
users.foreach(_ ! RefreshLobbyInfo(getLobbyInfoResponse))
val senderName: String = if (sender == host.actorRef) {
host.name
} else secondPlayer.get.name
users.foreach(_ ! SendBanMapMessage(BanMapMessage(senderName, mapName)))
} else {
logger.warn(s"User ban map $mapName, but map already banned")
}
case None =>
logger.error(s"Map $mapName not exist")
}
users.foreach(_ ! RefreshLobbyInfo(getLobbyInfoResponse))
} else{
logger.warn(s"Player ${sender.path.name} ban map, but turn is $playerTurn")
}
} else {
logger.warn(s"Player ${sender.path.name} ban map, but it is '$status' status")
logger.warn(s"Player ${sender.path.name} ban map, but lobby status is '$status' status")
}
case SetReady =>
// notify watchers
@@ -112,6 +127,13 @@ class LobbieActor(hostUser: LobbyUser) extends Actor with LazyLogging {
secondPlayer = None
users.foreach(_ ! RefreshLobbyInfo(getLobbyInfoResponse))
}
case MessageForLobby(message) =>
if (sender() == host.actorRef) {
users.foreach(_ ! SendMessage(Message(host.name, message)))
}else if (secondPlayer.exists(sp => sp.actorRef == sender())){
users.foreach(_ ! SendMessage(Message(secondPlayer.get.name, message)))
}
case JoinLobbyAsPlayer(sp) =>
logger.info(s"User ${sender.path.name} join lobby ${self.path.name} as player")
if (secondPlayer.isEmpty) {
@@ -177,6 +199,8 @@ case object SetReady
case object SetNotReady
case class MessageForLobby(message: String)
case object InfoQuery
case class UserInfo(name: String, isReady: Boolean)