Skip to content

Commit

Permalink
Merge pull request #146 from iuginP/develop
Browse files Browse the repository at this point in the history
Sprint 8 - closed
  • Loading branch information
iuginP authored Aug 21, 2018
2 parents 84e7f9a + 265b963 commit 7fa47b8
Show file tree
Hide file tree
Showing 52 changed files with 325 additions and 260 deletions.
1 change: 0 additions & 1 deletion client/project/build.properties

This file was deleted.

2 changes: 0 additions & 2 deletions client/project/plugins.sbt

This file was deleted.

30 changes: 21 additions & 9 deletions client/src/main/scala/it/cwmp/client/ClientMain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package it.cwmp.client

import akka.actor.{ActorSystem, Props}
import com.typesafe.config.ConfigFactory
import it.cwmp.client.controller.{ApiClientActor, ClientControllerActor}
import it.cwmp.services.{VertxInstance, discovery}
import it.cwmp.client.controller.actors.{ApiClientActor, ClientControllerActor}
import it.cwmp.services.wrapper.{AuthenticationApiWrapper, DiscoveryApiWrapper, RoomsApiWrapper}
import it.cwmp.services.{VertxInstance, discovery}
import it.cwmp.utils.{HostAndPortArguments, Logging}
import it.cwmp.view.OneAddressInput
import it.cwmp.view.TwoAddressesInput

import scala.util.Failure

Expand All @@ -16,6 +16,10 @@ import scala.util.Failure
object ClientMain extends App with VertxInstance with Logging {

val APP_NAME = "CellWarsClient"
val INITIAL_GUI_INSERTION_MESSAGE =
"""First pair -> DiscoveryService host-port
|Second pair -> Your host IP, port will be ignored
""".stripMargin

private val COMMAND_LINE_ARGUMENTS_ERROR =
"""
Expand All @@ -27,22 +31,30 @@ object ClientMain extends App with VertxInstance with Logging {
try {
val hostPortPairs = HostAndPortArguments(args, 1, COMMAND_LINE_ARGUMENTS_ERROR).pairs
val discoveryService = hostPortPairs.head
val myHostIP = hostPortPairs(1)._1

launch(discoveryService._1, discoveryService._2)
launch(discoveryService._1, discoveryService._2, myHostIP)
} catch {
case _: IllegalArgumentException =>
OneAddressInput(APP_NAME, "You should insert DiscoveryService host-port", discoveryServiceHostPortPair => {
launch(discoveryServiceHostPortPair._1, discoveryServiceHostPortPair._2)
})(defaultPort = discovery.Service.DEFAULT_PORT.toString)
TwoAddressesInput(APP_NAME, INITIAL_GUI_INSERTION_MESSAGE)(discoveryAndMyHostPortPairs => {
val discoveryService = discoveryAndMyHostPortPairs._1
val myHostIP = discoveryAndMyHostPortPairs._2._1

launch(discoveryService._1, discoveryService._2, myHostIP)
},
_ => System.exit(0)
)(firstDefaultPort = discovery.Service.DEFAULT_PORT.toString,
secondDefaultPort = 0.toString)
}

/**
* Launches client application
*
* @param discoveryHost the discovery to contact for services
* @param discoveryPort the discovery port on which it listens
* @param myHost the host on which the client is executed
*/
private def launch(discoveryHost: String, discoveryPort: String): Unit = {
private def launch(discoveryHost: String, discoveryPort: String, myHost: String): Unit = {
val config = ConfigFactory.parseString("akka.remote.netty.tcp.port=0").withFallback(ConfigFactory.load())
val system = ActorSystem(APP_NAME, config)
val discoveryApiWrapper: DiscoveryApiWrapper = DiscoveryApiWrapper(discoveryHost, discoveryPort.toInt)
Expand All @@ -59,7 +71,7 @@ object ClientMain extends App with VertxInstance with Logging {
AuthenticationApiWrapper(authenticationHost, authenticationPort),
RoomsApiWrapper(roomsHost, roomsPort))), ApiClientActor.getClass.getName)
log.info(s"Initializing the client controller actor...")
system.actorOf(Props(ClientControllerActor(apiClientActor)), ClientControllerActor.getClass.getName)
system.actorOf(Props(ClientControllerActor(apiClientActor, myHost)), ClientControllerActor.getClass.getName)
log.info("Client up and running!")
}) andThen {
case Failure(ex) => log.info("Error discovering services", ex)
Expand Down
45 changes: 0 additions & 45 deletions client/src/main/scala/it/cwmp/client/GameMain.scala

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package it.cwmp.client.controller

import java.net.InetAddress

import it.cwmp.client.controller.ParticipantListReceiver.ADDRESS_TOKEN_LENGTH
import it.cwmp.model.{Address, Participant}
import it.cwmp.services.VertxInstance
Expand All @@ -20,6 +18,8 @@ trait ParticipantListReceiver extends VertxInstance with Logging {

private var deploymentID: Option[String] = None

protected def hostname: String

/**
* Listens for a list of participants
*
Expand All @@ -31,7 +31,7 @@ trait ParticipantListReceiver extends VertxInstance with Logging {
val verticle = RoomReceiverServiceVerticle(token, participants => onListReceived(participants))
vertx.deployVerticleFuture(verticle)
.andThen { case Success(id) => deploymentID = id }
.map(_ => Address(s"http://${InetAddress.getLocalHost.getHostAddress}:${verticle.port}"
.map(_ => Address(s"http://$hostname:${verticle.port}"
+ createParticipantReceiverUrl(token)))
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package it.cwmp.client.controller
package it.cwmp.client.controller.actors

import akka.actor.Actor
import it.cwmp.client.controller.messages.AuthenticationRequests.{LogIn, SignUp}
import it.cwmp.client.controller.messages.AuthenticationResponses.{LogInFailure, LogInSuccess, SignUpFailure, SignUpSuccess}
import it.cwmp.client.controller.messages.RoomsRequests._
import it.cwmp.client.controller.messages.RoomsResponses._
import it.cwmp.services.wrapper.{AuthenticationApiWrapper, DiscoveryApiWrapper, RoomsApiWrapper}
import it.cwmp.client.controller.actors.messages.AuthenticationRequests.{LogIn, SignUp}
import it.cwmp.client.controller.actors.messages.AuthenticationResponses.{LogInFailure, LogInSuccess, SignUpFailure, SignUpSuccess}
import it.cwmp.client.controller.actors.messages.RoomsRequests._
import it.cwmp.client.controller.actors.messages.RoomsResponses._
import it.cwmp.services.wrapper.{AuthenticationApiWrapper, RoomsApiWrapper}
import it.cwmp.utils.Utils.stringToOption

import scala.concurrent.ExecutionContext.Implicits.global
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package it.cwmp.client.controller
package it.cwmp.client.controller.actors

import akka.actor.{Actor, ActorRef, Props}
import it.cwmp.client.controller.AlertMessages.{Error, Info}
import it.cwmp.client.controller.ClientControllerActor._
import it.cwmp.client.controller.PlayerActor.{PrepareForGame, RetrieveAddress, RetrieveAddressResponse}
import it.cwmp.client.controller.ViewVisibilityMessages.{Hide, Show}
import it.cwmp.client.controller.game.{CellWorldGenerationStrategy, GameConstants}
import it.cwmp.client.controller.messages.AuthenticationRequests.{GUILogOut, LogIn, SignUp}
import it.cwmp.client.controller.messages.AuthenticationResponses.{LogInFailure, LogInSuccess, SignUpFailure, SignUpSuccess}
import it.cwmp.client.controller.messages.Initialize
import it.cwmp.client.controller.messages.RoomsRequests._
import it.cwmp.client.controller.messages.RoomsResponses._
import it.cwmp.client.view.authentication.AuthenticationViewActor
import it.cwmp.client.view.game.GameViewActor
import it.cwmp.client.view.room.RoomViewActor
import it.cwmp.client.view.room.RoomViewActor.{FoundOpponents, ShowToken, WaitingForOthers}
import it.cwmp.client.controller.ParticipantListReceiver
import it.cwmp.client.controller.actors.ClientControllerActor._
import it.cwmp.client.controller.actors.PlayerActor.{PrepareForGame, RetrieveAddress, RetrieveAddressResponse}
import it.cwmp.client.controller.actors.common.AlertMessages.{Error, Info}
import it.cwmp.client.controller.actors.common.ViewVisibilityMessages.{Hide, Show}
import it.cwmp.client.controller.actors.messages.AuthenticationRequests.{GUILogOut, LogIn, SignUp}
import it.cwmp.client.controller.actors.messages.AuthenticationResponses.{LogInFailure, LogInSuccess, SignUpFailure, SignUpSuccess}
import it.cwmp.client.controller.actors.messages.Initialize
import it.cwmp.client.controller.actors.messages.RoomsRequests._
import it.cwmp.client.controller.actors.messages.RoomsResponses._
import it.cwmp.client.controller.actors.view.RoomViewActor.{FoundOpponents, ShowToken, WaitingForOthers}
import it.cwmp.client.controller.actors.view.{AuthenticationViewActor, GameViewActor, RoomViewActor}
import it.cwmp.client.controller.game.GameConstants
import it.cwmp.client.controller.game.generation.CellWorldGenerationStrategy
import it.cwmp.model.{Address, Participant}
import it.cwmp.utils.Logging

Expand All @@ -29,7 +29,7 @@ import scala.util.{Failure, Success}
* @author Eugenio Pierfederici
* @author contributor Enrico Siboni
*/
case class ClientControllerActor(private val apiClientActor: ActorRef) extends Actor with ParticipantListReceiver with Logging {
case class ClientControllerActor(private val apiClientActor: ActorRef, protected val hostname: String) extends Actor with ParticipantListReceiver with Logging {

private val UNKNOWN_ERROR = "Unknown Error"

Expand Down Expand Up @@ -231,11 +231,6 @@ case class ClientControllerActor(private val apiClientActor: ActorRef) extends A
log.error(s"${errorMessage.getOrElse(UNKNOWN_ERROR)}")
}

/**
* @return the behaviour to enable when user is playing
*/
private def inGameBehaviour: Receive = Actor.emptyBehavior // TODO: we can remove thi behaviour, after starting the game there's nothing more to do

/**
* @return the Future containing the address of one-time server that will receive the participants
*/
Expand Down Expand Up @@ -264,7 +259,7 @@ case class ClientControllerActor(private val apiClientActor: ActorRef) extends A
private def onSuccessFindingOpponents(participants: List[Participant]): Unit = {
roomViewActor ! FoundOpponents
log.info(s"Setting the behaviour 'in-game'")
context.become(inGameBehaviour)
context.become(Actor.emptyBehavior)
roomViewActor ! Hide
playerActor ! PrepareForGame(participants,
CellWorldGenerationStrategy(GameViewActor.VIEW_SIZE, GameViewActor.VIEW_SIZE, GameConstants.PASSIVE_CELLS_NUMBER))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package it.cwmp.client.controller
package it.cwmp.client.controller.actors

import akka.actor.{Actor, ActorRef, AddressFromURIString, Props, Stash}
import akka.cluster.Cluster
import akka.cluster.ClusterEvent._
import akka.cluster.ddata.DistributedData
import it.cwmp.client.controller.PlayerActor.{GameEnded, PrepareForGame, RetrieveAddress, RetrieveAddressResponse}
import it.cwmp.client.controller.game.GenerationStrategy
import it.cwmp.client.controller.messages.{Initialize, Request, Response}
import it.cwmp.client.controller.actors.PlayerActor.{PrepareForGame, RetrieveAddress, RetrieveAddressResponse}
import it.cwmp.client.controller.actors.messages.{Initialize, Request, Response}
import it.cwmp.client.controller.actors.view.GameViewActor
import it.cwmp.client.controller.actors.view.GameViewActor.{NewWorld, ShowGUIWithName}
import it.cwmp.client.controller.game.generation.GenerationStrategy
import it.cwmp.client.model.game.distributed.AkkaDistributedState
import it.cwmp.client.model.game.distributed.impl.MergingStateCellWorld
import it.cwmp.client.model.game.impl.CellWorld
import it.cwmp.client.view.game.GameViewActor
import it.cwmp.client.view.game.GameViewActor._
import it.cwmp.model.Participant
import it.cwmp.utils.Logging

Expand Down Expand Up @@ -69,6 +69,7 @@ case class PlayerActor() extends Actor with Stash with Logging {
roomSize = participants.size
cluster.join(AddressFromURIString(participants.head.address)) // all join first player cluster
if (getAddress == participants.head.address) { // first player injects start world
log.debug("First World injection by : " + getAddress)
distributedState.initialize(worldGenerationStrategy(participants))
}
playerName = participants.find(participant => participant.address == getAddress).get.username
Expand All @@ -95,25 +96,11 @@ case class PlayerActor() extends Actor with Stash with Logging {
* Starts the game
*/
private def startGame(): Unit = {
context.become(inGameBehaviour)
context.become(distributedState.distributedStateBehaviour)
gameViewActor ! ShowGUIWithName(playerName)
unstashAll() // un-stash distributed change messages
}

/**
* @return the behaviour of the actor when it's in game
*/
private def inGameBehaviour: Receive =
distributedState.distributedStateBehaviour orElse {
// TODO: remove this part of receive because we cannot come back to rooms view without stopping acotrSystem
case GameEnded => backToLobbyAction()
}

/**
* The action to do when the game is ended
*/
private def backToLobbyAction(): Unit = context.become(beforeInGameBehaviour orElse clusterBehaviour)

/**
* Action that will be executed every time the world will be updated
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package it.cwmp.client.controller
package it.cwmp.client.controller.actors.common

import akka.actor.Actor.Receive
import it.cwmp.client.controller.AlertMessages._
import it.cwmp.client.controller.actors.common.AlertMessages._
import it.cwmp.client.view.FXAlertsController

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package it.cwmp.client.controller
package it.cwmp.client.controller.actors.common

import akka.actor.Actor.Receive
import it.cwmp.client.controller.ViewVisibilityMessages.{Hide, Show}
import it.cwmp.client.controller.actors.common.ViewVisibilityMessages.{Hide, Show}
import it.cwmp.client.view.{FXRunOnUIThread, FXViewController}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package it.cwmp.client.controller.messages
package it.cwmp.client.controller.actors.messages

/**
* Collection of Authentication request messages
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package it.cwmp.client.controller.messages
package it.cwmp.client.controller.actors.messages

/**
* Collection of Authentication response messages
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package it.cwmp.client.controller.messages
package it.cwmp.client.controller.actors.messages

/**
* Identifies a request message
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package it.cwmp.client.controller.messages
package it.cwmp.client.controller.actors.messages

import it.cwmp.model.Address

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package it.cwmp.client.controller.messages
package it.cwmp.client.controller.actors.messages

/**
* The collection of Rooms response messages
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package it.cwmp.client.view.authentication
package it.cwmp.client.controller.actors.view

import it.cwmp.client.controller.messages.AuthenticationRequests.{LogIn, SignUp}
import it.cwmp.client.view.FXServiceViewActor
import it.cwmp.client.view.authentication.AuthenticationViewActor.{LOGGING_IN_MESSAGE, SIGNING_UP_MESSAGE}
import it.cwmp.client.controller.actors.messages.AuthenticationRequests.{LogIn, SignUp}
import it.cwmp.client.controller.actors.view.AuthenticationViewActor._
import it.cwmp.client.view.authentication.{AuthenticationFXController, AuthenticationStrategy}

/**
* Actor assigned to the management of the display of the authentication screen and of the events generated by it.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package it.cwmp.client.view
package it.cwmp.client.controller.actors.view

import akka.actor.{Actor, ActorRef}
import it.cwmp.client.controller.messages.Initialize
import it.cwmp.client.controller.{ActorAlertManagement, ActorViewVisibilityManagement}
import it.cwmp.client.controller.actors.common.{ActorAlertManagement, ActorViewVisibilityManagement}
import it.cwmp.client.controller.actors.messages.Initialize
import it.cwmp.client.view.{FXAlertsController, FXInputViewController, FXViewController}

/**
* A base class representing a Service View actor with JavaFX underlying
Expand Down
Loading

0 comments on commit 7fa47b8

Please sign in to comment.