Coverage Summary for Class: Lobby (it.polimi.ingsw.server)

Class Class, % Method, % Line, %
Lobby 0% (0/ 1) 0% (0/ 4) 0% (0/ 29)


1 package it.polimi.ingsw.server; 2  3 import it.polimi.ingsw.controller.Controller; 4 import it.polimi.ingsw.model.Game; 5 import it.polimi.ingsw.model.GameMode; 6  7 import java.util.*; 8 import java.util.stream.Collectors; 9  10 /** 11  * Class to Manage Pools of Users waiting for a Game 12  */ 13 class Lobby { 14  /** 15  * A Hashmap to store for each GameMode a List of Connection 16  */ 17  private Map<GameMode, List<Connection>> waitingList = new HashMap<>(); 18  /** 19  * Singleton Pattern 20  */ 21  private final static Lobby instance = new Lobby(); 22  23  /** 24  * Get the Lobby Instance 25  * 26  * @return the instance of Lobby 27  */ 28  public static Lobby getInstance() { 29  return instance; 30  } 31  32  /** 33  * This method is used to set lists in order to start the game 34  * 35  * @param connection connection to add in the lobby 36  * @param username username of the player (connection) 37  * @param mode game mode chosen by player 38  */ 39  40  public synchronized boolean putOnWaiting(Connection connection, String username, GameMode mode) { 41  if (username.length() == 0 || (waitingList.get(mode) != null 42  && waitingList.get(mode).stream().anyMatch(e -> (e.getUsername().equals(username) && e.isActive())))) 43  return false; 44  45  List<Connection> targetList = new ArrayList<>(); 46  47  if (waitingList.get(mode) != null) 48  targetList = waitingList.get(mode).stream().filter(e -> e.isActive()).collect(Collectors.toList()); 49  50  targetList.add(connection); 51  52  if (targetList.size() == mode.getPlayersNum()) { 53  54  Chat chat = new Chat(); 55  Game game = new Game(mode, targetList.stream().map(e -> e.getUsername()).collect(Collectors.toList())); 56  Controller controller = new Controller(game); 57  System.out.print("- Start Game | Mode: " + mode.toString() + " | Players: "); 58  for (Connection x : targetList) { 59  System.out.print(x.getUsername() + " - "); 60  controller.addObservers(x.getUsername(), x); 61  x.addObservers(controller); 62  63  x.addObservers(chat); 64  chat.addObservers(x); 65  } 66  System.out.println(); 67  waitingList.remove(mode); 68  controller.startGame(); 69  } else { 70  waitingList.put(mode, targetList); 71  } 72  return true; 73  } 74 }