Coverage Summary for Class: AppCLI (it.polimi.ingsw.view.CLI)

Class Class, % Method, % Line, %
AppCLI 0% (0/ 1) 0% (0/ 7) 0% (0/ 90)


1 package it.polimi.ingsw.view.CLI; 2  3 import it.polimi.ingsw.utils.Observable; 4 import it.polimi.ingsw.utils.Observer; 5 import it.polimi.ingsw.view.socket.AppInterface; 6 import it.polimi.ingsw.view.socket.Connection; 7 import it.polimi.ingsw.view.socket.Parser; 8  9 import java.util.Scanner; 10  11 /** 12  * CLI Application 13  */ 14 public class AppCLI extends Observable<String> implements Observer<String>, AppInterface { 15  /** 16  * Socket connection 17  */ 18  private Connection connection; 19  /** 20  * Status Request, if a response of the request is needed 21  */ 22  private Boolean statusRequest; 23  /** 24  * CLI Scanner 25  */ 26  private final Scanner scanner; 27  /** 28  * Printer 29  */ 30  private ViewPrinter printer; 31  /** 32  * Player username 33  */ 34  private String username; 35  36  /** 37  * AppCLI Constructor 38  * 39  * @param scanner scanner 40  */ 41  public AppCLI(Scanner scanner) { 42  this.scanner = scanner; 43  } 44  45  /** 46  * Set Socket Connection 47  */ 48  private void setServer() { 49  String in = ""; 50  connection = null; 51  while (!in.toUpperCase().equals("QUIT")) { 52  ViewPrinter.clearConsole(); 53  ViewPrinter.printLogo(); 54  55  System.out.println(" Type QUIT to exit from server setup"); 56  System.out.print(" Server IP & Port: "); 57  in = scanner.nextLine(); 58  try { 59  String[] in2 = in.split(" "); 60  if (in2.length == 2) { 61  System.out.println(" Connecting.."); 62  connection = new Connection(in2[0], Integer.parseInt(in2[1])); 63  Parser parser = new Parser(); 64  printer = new ViewPrinter(parser); 65  66  parser.addObservers(printer); 67  this.addObservers(connection); 68  connection.addObservers(this); 69  connection.addObservers(parser); 70  connection.setMaster(this); 71  new Thread(connection).start(); 72  break; 73  } 74  } catch (Exception e) { 75  // fail parsing 76  } 77  } 78  } 79  80  /** 81  * Set Socket/Player Username 82  */ 83  private void setUsername() { 84  ViewPrinter.clearConsole(); 85  ViewPrinter.printLogo(); 86  while (connection != null && connection.getStatus()) { 87  System.out.print(" Insert username: "); 88  String in = scanner.nextLine(); 89  if (in.length() != 0) 90  try { 91  statusRequest = null; 92  notify(in); 93  System.out.println(" Waiting for server response..."); 94  while (statusRequest == null) { 95  Thread.sleep(300); 96  } 97  if (statusRequest == true) { 98  username = in; 99  printer.setUsername(username); 100  break; 101  } else 102  System.out.println(" Username not available"); 103  104  } catch (Exception e) { 105  } 106  } 107  } 108  109  /** 110  * Set Game Mode 111  */ 112  private void setMode() { 113  114  ViewPrinter.clearConsole(); 115  ViewPrinter.printLogo(); 116  117  while (connection != null && connection.getStatus()) { 118  System.out.print(" Choose a game mode (Type 1 or 2)\n 1) Two players\n 2) Three players\n "); 119  String in = scanner.nextLine(); 120  if (in.equals("1") || in.equals("2")) 121  try { 122  statusRequest = null; 123  notify(in.equals("1") ? "TWO" : "THREE"); 124  System.out.println(" Waiting for server response..."); 125  while (statusRequest == null) { 126  Thread.sleep(300); 127  } 128  if (statusRequest == true) 129  break; 130  } catch (Exception e) { 131  // fail parsing 132  } 133  } 134  } 135  136  /** 137  * CLI Start Application 138  */ 139  public void start() { 140  String in = ""; 141  while (!in.toUpperCase().equals("QUIT")) { 142  setServer(); 143  setMode(); 144  setUsername(); 145  ViewPrinter.clearConsole(); 146  ViewPrinter.printLogo(); 147  148  if (connection != null && connection.getStatus()) { 149  System.out.println(" Waiting for other players..."); 150  printer.addObservers(connection); 151  printer.setStatus(true); 152  } 153  154  while (!in.toUpperCase().equals("QUIT") && connection != null && connection.getStatus()) { 155  in = scanner.nextLine(); 156  printer.useAction(in); 157  } 158  159  if (connection != null && connection.getStatus()) 160  connection.close(); 161  System.out.print("\n Type QUIT to close the CLI, or any other key to play again.\n "); 162  in = scanner.nextLine(); 163  } 164  } 165  166  /** 167  * Update about request status from server 168  */ 169  @Override 170  public void update(String in) { 171  if (in.equals("ok")) 172  statusRequest = true; 173  174  if (in.equals("ko")) 175  statusRequest = false; 176  } 177  178  @Override 179  public void onDisconnection() { 180  System.out.println("\n Network error, Try later."); 181  } 182 }