Module AM37

Class Lobby

java.lang.Object
it.polimi.ingsw.network.server.Lobby
All Implemented Interfaces:
EndGameListener, PlayerLostListener

public class Lobby
extends java.lang.Object
implements PlayerLostListener, EndGameListener
The place where a game is created

A Lobby object is a container for a group of players: once created, the owner can select the size and, when full, start a new game

  • Constructor Summary

    Constructors 
    Constructor Description
    Lobby​(Server server, java.lang.String roomName, User user, int numOfPlayers)
    Default constructor
  • Method Summary

    Modifier and Type Method Description
    void addUser​(User user)
    Adds a user to the room
    void askGods​(java.util.List<GodData> godData)
    Asks the "owner" to choose the gods for the game
    void askToChooseGod​(java.lang.String username)
    Asks a player to choose its personal god for the game
    void assignGod​(java.lang.String username, GodData godData)
    Assigns a god to the player who chose it
    void broadcastMessage​(Message message)
    Sends a message to all the users in the room
    void chooseGods​(java.util.List<GodData> gods)
    Receives the list of chosen gods from the lobby owner and, if correct, asks the next player to choose its god
    void createGame()
    Creates and starts a new game
    boolean gameStarted()
    gameStarted getter
    java.lang.String getRoomName()
    roomName getter
    MessageManagerParser getRoomParser()
    messageParser getter
    boolean hasLost​(User user)
    Determines if a given user has lost the game in this lobby
    java.util.List<java.lang.String> lobbyInfo()
    Creates a list containing this lobby information, with the following format: lobby name number of users in the lobby number of available slots list of users in the lobby
    void onEndGame​(java.lang.String name)
    Sends a WinnerDeclaredEvent to all users, deletes the saved game, moves all the player to the waiting room and removes the lobby from the server's lobby list
    void onPlayerLoss​(java.lang.String username, java.util.List<Cell> gameBoard)
    Removes the user which lost from the in-game players, and overrides the saved file
    void reloadMatch​(boolean wantToReload)
    Based on the user choice, reloads a previously saved match or creates a new one
    void removeUser​(User user)
    Removes a user from the lobby
    void selectStartingPlayer​(java.lang.String username)
    Rotates the players' list, based on the first player chosen, then starts the game
    void sendMessage​(java.lang.String username, Message message)
    Sends a message to a given user

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Lobby

      public Lobby​(Server server, java.lang.String roomName, User user, int numOfPlayers)
      Default constructor
      Parameters:
      server - the server object
      roomName - the room name
      numOfPlayers - the lobby size
      user - the first use
  • Method Details

    • getRoomName

      public java.lang.String getRoomName()
      roomName getter
      Returns:
      the lobby name
    • getRoomParser

      public MessageManagerParser getRoomParser()
      messageParser getter
      Returns:
      this lobby's message parser
    • hasLost

      public boolean hasLost​(User user)
      Determines if a given user has lost the game in this lobby
      Parameters:
      user - the user to check for loss
      Returns:
      true if the player has lost
    • sendMessage

      public void sendMessage​(java.lang.String username, Message message)
      Sends a message to a given user

      The message is sent only if the recipient is in the same room of the sender.

      Parameters:
      username - the message recipient, as a string
      message - the message to send
    • broadcastMessage

      public void broadcastMessage​(Message message)
      Sends a message to all the users in the room
      Parameters:
      message - the message to send
    • addUser

      public void addUser​(User user) throws RoomFullException, InvalidUsernameException
      Adds a user to the room
      Parameters:
      user - the user to add
      Throws:
      RoomFullException - if the room is full
      InvalidUsernameException - if the username is already taken in the lobby
    • reloadMatch

      public void reloadMatch​(boolean wantToReload)
      Based on the user choice, reloads a previously saved match or creates a new one
      Parameters:
      wantToReload - the user choice
    • chooseGods

      public void chooseGods​(java.util.List<GodData> gods)
      Receives the list of chosen gods from the lobby owner and, if correct, asks the next player to choose its god
      Parameters:
      gods - the list of chosen gods
    • askGods

      public void askGods​(java.util.List<GodData> godData)
      Asks the "owner" to choose the gods for the game
      Parameters:
      godData - the list of all the available gods
    • askToChooseGod

      public void askToChooseGod​(java.lang.String username)
      Asks a player to choose its personal god for the game
      Parameters:
      username - the user's username to ask to choose to
    • assignGod

      public void assignGod​(java.lang.String username, GodData godData)
      Assigns a god to the player who chose it
      Parameters:
      username - the player's username
      godData - the chosen god
    • selectStartingPlayer

      public void selectStartingPlayer​(java.lang.String username)
      Rotates the players' list, based on the first player chosen, then starts the game
      Parameters:
      username - the first player's username
    • createGame

      public void createGame()
      Creates and starts a new game
    • gameStarted

      public boolean gameStarted()
      gameStarted getter
      Returns:
      true if the game started
    • removeUser

      public void removeUser​(User user)
      Removes a user from the lobby
      Parameters:
      user - the user to remove
    • lobbyInfo

      public java.util.List<java.lang.String> lobbyInfo()
      Creates a list containing this lobby information, with the following format:
      • lobby name
      • number of users in the lobby
      • number of available slots
      • list of users in the lobby
      Returns:
      a list containing the lobby information as described above
    • onPlayerLoss

      public void onPlayerLoss​(java.lang.String username, java.util.List<Cell> gameBoard)
      Removes the user which lost from the in-game players, and overrides the saved file
      Specified by:
      onPlayerLoss in interface PlayerLostListener
      Parameters:
      username - the loser's username
      gameBoard - the changed gameBoard without the loser's workers as a list of cells
    • onEndGame

      public void onEndGame​(java.lang.String name)
      Sends a WinnerDeclaredEvent to all users, deletes the saved game, moves all the player to the waiting room and removes the lobby from the server's lobby list
      Specified by:
      onEndGame in interface EndGameListener
      Parameters:
      name - the winner's username