Class GameState

java.lang.Object
it.polimi.ingsw.server.event_sourcing.Aggregate
it.polimi.ingsw.server.model.game_logic.GameState
All Implemented Interfaces:
GameStateCommonInterface
Direct Known Subclasses:
ExpertGameState

public class GameState extends Aggregate implements GameStateCommonInterface
  • Field Details

    • numberOfPlayers

      protected int numberOfPlayers
    • strategy

      protected NumberOfPlayersStrategy strategy
    • numberOfStudentsInEachCloud

      private int numberOfStudentsInEachCloud
    • numberOfStudentsInTheEntrance

      private int numberOfStudentsInTheEntrance
    • round

      protected Round round
    • schoolBoardIdsToCardPlayedThisRound

      protected Map<Integer,Card> schoolBoardIdsToCardPlayedThisRound
    • schoolBoardIdsToLastCardPlayed

      protected Map<Integer,Card> schoolBoardIdsToLastCardPlayed
    • archipelagos

      protected List<Archipelago> archipelagos
    • schoolBoards

      protected List<SchoolBoard> schoolBoards
    • clouds

      protected List<List<Color>> clouds
    • studentFactory

      public StudentFactory studentFactory
    • motherNaturePosition

      protected Archipelago motherNaturePosition
    • currentPlayerSchoolBoardId

      protected int currentPlayerSchoolBoardId
  • Constructor Details

  • Method Details

    • initGameState

      public void initGameState(InitGameStateEvent event) throws GameStateInitializationFailureException
      This method initializes the gameState in all its parts according to the game's rules
      Parameters:
      event - is the event linked the initialization
      Throws:
      GameStateInitializationFailureException - if an error occurs during the initialization
    • chooseStrategy

      protected void chooseStrategy()
      This method is used to select the correct strategy according to the number of players
    • initializeClouds

      private List<List<Color>> initializeClouds()
      This method initializes the List of Lists of Color representing the clouds
    • initializeArchipelagos

      private List<Archipelago> initializeArchipelagos() throws EmptyStudentSupplyException
      This method initializes all the archipelagos adding motherNature and the students as the rulebook commands
      Returns:
      a List of Archipelago containing all the already initialized and ready to use archipelagos of the game
      Throws:
      EmptyStudentSupplyException - if the studentSupply representing the bag is empty
    • createArchipelago

      protected Archipelago createArchipelago(int code)
      This method creates an archipelago with the inputted code
      Parameters:
      code - is the starting islandCode of the archipelago
      Returns:
      the newly created archipelago
    • initializeSchoolBoards

      protected List<SchoolBoard> initializeSchoolBoards() throws EmptyStudentSupplyException
      This method initializes the schoolBoards according to the appropriate strategy depending on the number of players.
      Returns:
      a List of SchoolBoard containing the already initialized schoolBoards, students in the entrance included.
      Throws:
      EmptyStudentSupplyException - if the studentSupply representing the bag is empty and cannot fulfill the initialization process
    • playCard

      public void playCard(Card card) throws InvalidCardPlayedException, CardIsNotInTheDeckException
      The current player plays the given card
      Parameters:
      card - the card to be played by the current player
      Throws:
      CardIsNotInTheDeckException - if the current player does not actually own the card to be played.
      InvalidCardPlayedException - if another player already played the same card in this round, and it is not the final round.
    • playCardHandler

      public void playCardHandler(PlayCardEvent event) throws CardIsNotInTheDeckException, InvalidCardPlayedException
      Throws:
      CardIsNotInTheDeckException
      InvalidCardPlayedException
    • apply

      public void apply(Event event) throws EventSourcingException
      Description copied from class: Aggregate
      Applies an event, given the Event to apply This method will call using reflection the appropriate handler that manage the event
      Overrides:
      apply in class Aggregate
      Parameters:
      event - to apply
      Throws:
      EventSourcingException - if there is some problem finding the appropriate handler
    • fillCloud

      public void fillCloud(int cloudIndex) throws EmptyStudentSupplyException, IllegalArgumentException
      This method gets int cloudIndex in input identifying a cloud and fills the cloud with students taken from the studentSupply
      Parameters:
      cloudIndex - is the index of the cloud to fill with students
      Throws:
      IllegalArgumentException - if the cloudIndex parameter is not valid
      EmptyStudentSupplyException - if the student supply representing the bag cannot fulfill the request for students
    • fillCloudHandler

      public void fillCloudHandler(FillCloudEvent event) throws EmptyStudentSupplyException, IllegalArgumentException
      Throws:
      EmptyStudentSupplyException
      IllegalArgumentException
    • addEvent

      private void addEvent(Event event)
    • updateSeed

      private void updateSeed(Event event)
    • fillClouds

      public void fillClouds() throws EmptyStudentSupplyException
      Fills every cloud with students //@throws FullCloudException if one or more of the clouds are not completely empty before being refilled
      Throws:
      EmptyStudentSupplyException - if the studentSupply cannot fulfill the demand for students to refill all the clouds
    • grabStudentsFromCloud

      public void grabStudentsFromCloud(int cloudIndex) throws EmptyCloudException
      The current player grabs all the students from a cloud and puts them in the entrance
      Parameters:
      cloudIndex - is the index of the cloud to pick the students from
      Throws:
      EmptyCloudException - if the cloud is empty
    • grabStudentsFromCloudHandler

      public void grabStudentsFromCloudHandler(GrabStudentsFromCloudEvent event) throws EmptyCloudException
      Throws:
      EmptyCloudException
    • moveStudentFromEntranceToDiningRoom

      public void moveStudentFromEntranceToDiningRoom(Color student) throws StudentNotInTheEntranceException, FullDiningRoomLaneException
      The current player moves a student from the entrance to the dining room
      Parameters:
      student - represents a student that the player wants to move from the entrance to the diningRoom
      Throws:
      IllegalArgumentException - if(student == null)
      StudentNotInTheEntranceException - if the student that the player is trying to move is not actually in the entrance
      FullDiningRoomLaneException
    • moveStudentFromEntranceToDiningHandler

      public void moveStudentFromEntranceToDiningHandler(MoveStudentsFromEntranceToDiningEvent event) throws StudentNotInTheEntranceException, FullDiningRoomLaneException
      Throws:
      StudentNotInTheEntranceException
      FullDiningRoomLaneException
    • assignProfessor

      public Map<Color,Integer> assignProfessor(Color professor)
      The method gets a color in input and checks if the current player should get the professor corresponding to the inputted color and if another player should lose the corresponding professor
      Parameters:
      professor - indicates the color of the professor the player should get and/or remove from another player
    • assignProfessorHandler

      public Map<Color,Integer> assignProfessorHandler(AssignProfessorEvent event)
    • compareCurrentPlayersStudentsNumberWithOthersMax

      protected boolean compareCurrentPlayersStudentsNumberWithOthersMax(int currentPlayerNumberOfStudentsInDiningRoomLane, int otherSchoolBoardsMaxStudentsInDiningRoomLane)
    • moveStudentFromEntranceToArchipelago

      public void moveStudentFromEntranceToArchipelago(Color student, List<Integer> archipelagoIslandCodes) throws StudentNotInTheEntranceException
      The current player moves a student from the entrance to an archipelago
      Parameters:
      student - represents a student of a certain color that the player wants to move from the entrance to an archipelago
      archipelagoIslandCodes - represents the islandCodes of the archipelago into which the student is being moved
      Throws:
      IllegalArgumentException - if(student == null || archipelagoIslandCodes == null || archipelagoIslandCodes contains null || archipelagoIslandCodes is not an identifier of an actual archipelago)
      StudentNotInTheEntranceException - if the student that the player is trying to move is not actually in the entrance
    • moveStudentFromEntranceToArchipelagoHandler

      public void moveStudentFromEntranceToArchipelagoHandler(MoveStudentFromEntranceToArchipelagoEvent event) throws StudentNotInTheEntranceException
      Throws:
      StudentNotInTheEntranceException
    • moveMotherNatureOneStepClockwise

      public void moveMotherNatureOneStepClockwise()
      Shifts mother nature's position one step clockwise
    • moveMotherNatureNStepsClockwise

      public void moveMotherNatureNStepsClockwise(int numberOfSteps) throws InvalidNumberOfStepsException
      Shifts mother nature's position by the provided numberOfSteps
      Parameters:
      numberOfSteps - has to be a positive integer
      Throws:
      InvalidNumberOfStepsException - if (numberOfSteps invalid input: '<'= 0 || numberOfSteps > maxStepsAllowed) where maxStepsAllowed depends on the card played in the round
    • moveMotherNatureNStepsClockwiseHandler

      public void moveMotherNatureNStepsClockwiseHandler(MoveMotherNatureNStepsClockwiseEvent event) throws InvalidNumberOfStepsException
      Throws:
      InvalidNumberOfStepsException
    • conquerArchipelago

      public void conquerArchipelago(int schoolBoardId)
      The current player conquers the archipelago mother nature is currently in, placing a tower of his own color and substituting any tower that was previously placed on that archipelago
    • conquerArchipelagoHandler

      public void conquerArchipelagoHandler(ConquerArchipelagoEvent event)
    • mergeWithPrevious

      public boolean mergeWithPrevious()
      Merges the archipelago mother nature is currently in with the archipelago on the left (one step counter-clockwise with respect to mother nature's position)
      Returns:
      true if the archipelagos merged, false otherwise.
    • mergeWithPreviousHandler

      public boolean mergeWithPreviousHandler(MergeWithPreviousEvent event)
    • mergeWithNext

      public boolean mergeWithNext()
      Merges the archipelago mother nature is currently on with the archipelago on the right (one step clockwise with respect to mother nature's position)
      Returns:
      true if the archipelagos merged, false otherwise.
    • mergeWithNextHandler

      public boolean mergeWithNextHandler(MergeWithNextEvent event)
    • checkWinners

      public Map<Integer,Boolean> checkWinners()
      This method checks the winning conditions and returns a map linking every schoolBord ID to a boolean indicating if the schoolBoard is a winner or not
      Returns:
      a map schoolBoardId -> isWinner
    • onlyThreeArchipelagoLeft

      private boolean onlyThreeArchipelagoLeft()
    • setRoundOrder

      public void setRoundOrder(List<Integer> roundOrder)
    • setRoundOrderHandler

      public void setRoundOrderHandler(SetRoundOrderEvent event)
    • resetRoundIterator

      public void resetRoundIterator()
    • resetRoundIteratorHandler

      public void resetRoundIteratorHandler(ResetRoundIteratorEvent event)
    • increaseRoundCount

      public void increaseRoundCount()
    • increaseRoundCountHandler

      public void increaseRoundCountHandler(IncreaseRoundCountEvent event)
    • getCurrentRound

      public int getCurrentRound()
    • setCurrentPhase

      public void setCurrentPhase(Phase currentPhase)
      This method sets the current phase of the turn
      Throws:
      IllegalArgumentException - if(currentPhase == null)
    • setCurrentPhaseHandler

      public void setCurrentPhaseHandler(SetCurrentPhaseEvent event)
    • setCurrentPlayerSchoolBoardId

      public void setCurrentPlayerSchoolBoardId(int schoolBoardId)
      Parameters:
      schoolBoardId - has to be a valid id of an existing schoolBoard
    • setCurrentPlayerSchoolBoardIdHandler

      public void setCurrentPlayerSchoolBoardIdHandler(SetCurrentPlayerSchoolBoardIDEvent event)
    • setLastRoundTrue

      public void setLastRoundTrue()
      This method sets the last round flag to true
    • getSchoolBoardIDFromTowerColor

      public List<Integer> getSchoolBoardIDFromTowerColor(TowerColor towerColor)
      This method returns the list of schoolBoards that have the inputted towerColor
      Parameters:
      towerColor - is the towerColor of interest
      Returns:
      a list of integer representing the list of schoolBoards that have the inputted towerColor
    • getNumberOfTowersPlaced

      private int getNumberOfTowersPlaced(int schoolBoardId)
    • getAllowedStepsNumber

      protected int getAllowedStepsNumber()
    • getSchoolBoardFromSchoolBoardId

      protected SchoolBoard getSchoolBoardFromSchoolBoardId(int schoolBoardId)
      This method returns the corresponding schoolBoard to the inputted schoolBoardId
      Parameters:
      schoolBoardId - has to be an existing schoolBoardId
      Returns:
      the reference to the schoolBoard corresponding to the inputted schoolBoardId
    • getPreviousArchipelago

      protected Archipelago getPreviousArchipelago()
    • getNextArchipelago

      private Archipelago getNextArchipelago()
    • getArchipelagoFromIslandCodes

      protected Optional<Archipelago> getArchipelagoFromIslandCodes(List<Integer> archipelagoIslandCodes)
    • getArchipelagoFromSingleIslandCode

      public Optional<Archipelago> getArchipelagoFromSingleIslandCode(int archipelagoIslandCode)
    • getCurrentPlayerSchoolBoard

      protected SchoolBoard getCurrentPlayerSchoolBoard()
    • getInfluence

      public Optional<Map<Integer,Integer>> getInfluence(List<Integer> archipelagoIslandCodes)
      This method gets an archipelago in input and returns a map where every entry links a schoolBoard with its influence on the inputted archipelago
      Parameters:
      archipelagoIslandCodes - is a List of Integer uniquely identifying an archipelago
      Returns:
      a Map Integer, Integer where the key is the schoolBoardId and the value is the influence on the inputted archipelago
    • isLastTurnInThisRound

      public boolean isLastTurnInThisRound()
    • getNextTurn

      public int getNextTurn()
    • getNextTurnHandler

      public int getNextTurnHandler(GetNextTurnEvent event)
    • getNumberOfStudentsInTheEntrance

      public int getNumberOfStudentsInTheEntrance()
    • getInitialNumberOfStudentsInTheEntrance

      public int getInitialNumberOfStudentsInTheEntrance()
    • getNumberOfMovableStudents

      public int getNumberOfMovableStudents()
    • getCurrentPhase

      public Phase getCurrentPhase()
    • getMotherNaturePositionIslandCodes

      public List<Integer> getMotherNaturePositionIslandCodes()
    • getClouds

      public List<List<Color>> getClouds()
    • getNumberOfPlayers

      public int getNumberOfPlayers()
    • getSchoolBoardIds

      public Set<Integer> getSchoolBoardIds()
      This method returns a set containing all the schoolBoard IDs contained in this gameState
      Returns:
      a Set of Integer containing all the schoolBoard IDs contained in this gameState
    • getSchoolBoardIdsToCardPlayedThisRound

      public Map<Integer,Card> getSchoolBoardIdsToCardPlayedThisRound()
      This method returns a map that links every schoolBoard to the card it played in this round
      Returns:
      a map that links every schoolBoard to the card it played in this round
    • resetSchoolBoardIdsToCardsPlayerThisRound

      public void resetSchoolBoardIdsToCardsPlayerThisRound()
    • resetSchoolBoardIdsToCardsPlayerThisRoundHandler

      public void resetSchoolBoardIdsToCardsPlayerThisRoundHandler(ResetSchoolBoardIdsToCardsPlayerThisRoundEvent event)
    • getCurrentPlayerSchoolBoardId

      public int getCurrentPlayerSchoolBoardId()
      This method returns the current player schoolBoardId
      Returns:
      an int representing the current player schoolBoard ID
    • setActionPhaseSubTurn

      public void setActionPhaseSubTurn(ActionPhaseSubTurn actionPhaseSubTurn)
      This method sets the ActionPhaseSubTurn to the inputted actionPhaseSubTurn
      Parameters:
      actionPhaseSubTurn - is the sub-turn the gameState will be changed to
    • setActionPhaseSubTurnHandler

      public void setActionPhaseSubTurnHandler(SetActionPhaseSubTurnEvent event)
    • getActionPhaseSubTurn

      public ActionPhaseSubTurn getActionPhaseSubTurn()
      This method returns the current ActionPhaseSubTurn in which the gameState is currently in
      Returns:
      an ActionPhaseSubTurn representing the current ActionPhaseSubTurn in which the gameState is currently in
    • getTowerColorFromSchoolBoardId

      public Optional<TowerColor> getTowerColorFromSchoolBoardId(Integer schoolBoardId)
      This method returns an Optional wrapping the towerColor of the inputted schoolBoardId
      Parameters:
      schoolBoardId - is the ID that represents a schoolBoard
      Returns:
      the towerColor of the inputted schoolBoardId, if the schoolBoardId is valid, an empty Optional otherwise
    • isLastRound

      public boolean isLastRound()
      This method returns true if this is the last round, false otherwise
      Returns:
      true if this is the last round, false otherwise
    • lightify

      public LightGameState lightify()
      This method returns the light version of the GameState, with all the useful information to be sent over the network
      Returns:
      a LightGameState representing the light version of the GameState, with all the useful information to be sent over the network
    • getMostInfluentSchoolBoardId

      public Optional<Integer> getMostInfluentSchoolBoardId(List<Integer> archipelagoIslandCodes)
      This method verifies if there is a schoolBoard that is more influent than all the others on the archipelago on which motherNature is, and returns its schoolBoardId
      Returns:
      an integer representing the schoolBoardId of the most influent player on the archipelago on which motherNature is
    • setMotherNaturePositionForTesting

      public void setMotherNaturePositionForTesting(Archipelago motherNaturePosition)
    • getCurrentPlayerSchoolBoardForTesting

      public SchoolBoard getCurrentPlayerSchoolBoardForTesting()
    • getArchipelagosForTesting

      public List<Archipelago> getArchipelagosForTesting()
    • getSchoolBoardIdsToCardsPlayedThisRoundForTesting

      public Map<Integer,Card> getSchoolBoardIdsToCardsPlayedThisRoundForTesting()