コード例 #1
0
 public void elaborateShootRequest(int x, int y, String side) {
   String controllerQuery = null;
   if (side.equals("guest")) {
     controllerQuery = cont.shootAtHostSide(x, y);
   }
   if (side.equals("host")) {
     controllerQuery = cont.shootAtGuestSide(x, y);
   }
   for (ServerService s : services) {
     s.sendMessageToClients(controllerQuery);
   }
   if (controllerQuery.startsWith("water")) waterResponse(controllerQuery);
   if (controllerQuery.startsWith("hit")) hitResponse(controllerQuery);
   if (controllerQuery.startsWith("destroyed")) destroyedResponse(controllerQuery);
   if (controllerQuery.startsWith("allShipsDestroyed")) allShipsDestroyed(controllerQuery);
 }
コード例 #2
0
 public NetDaemon(boolean isServer, Game gameData, ConnectionInProgress cip, Controller cont) {
   setDaemon(true);
   playersServerSide = new ArrayList<String>();
   opponentResponse = false;
   serverResponse = false;
   ready = false;
   this.isServer = isServer;
   this.gameData = gameData;
   this.cont = cont;
   if (isServer) clients = cont.getGuestPlayers() + cont.getHostPlayers() - 1;
   clientsJoined = 0;
   players = clients;
   this.cip = cip;
   setPriority(Thread.MIN_PRIORITY);
   services = new ArrayList<ServerService>();
   responseFromServer = null;
   requestToServer = null;
   sSocket = null;
   mw = null;
   shouldRun = true;
 }
コード例 #3
0
 public String[] getServerSidePlayers() {
   if (!isServer) {
     requestToServer.println("serverSidePlayerList");
     while (!serverResponse) {}
     playersServerSide = new ArrayList<String>();
     if (spl == null) return new String[1];
   }
   if (isServer) spl = cont.getServerSidePlayerList();
   StringTokenizer st = new StringTokenizer(spl);
   while (st.hasMoreTokens()) playersServerSide.add(st.nextToken());
   String[] t = new String[playersServerSide.size()];
   for (int l = 0; l < playersServerSide.size(); l++) t[l] = playersServerSide.get(l);
   serverResponse = false;
   return t;
 }
コード例 #4
0
 public boolean placeShipsOnField(
     String field, String alignment, String shipName, int xStartCoordinate, int yStartCoordinate) {
   if (!gameData.shipAlreadyPlaced(shipName, alignment, xStartCoordinate, yStartCoordinate)) {
     if (!isServer) {
       requestToServer.println(
           "placeShip "
               + field
               + " "
               + alignment
               + " "
               + shipName
               + " "
               + xStartCoordinate
               + " "
               + yStartCoordinate);
       return true;
     }
     if (isServer) {
       boolean controllerQuery =
           cont.addShip(shipName, alignment, field, xStartCoordinate, yStartCoordinate);
       if (controllerQuery) {
         gameData.placeShipsOnField(alignment, shipName, xStartCoordinate, yStartCoordinate);
         for (ServerService s : services) {
           s.sendMessageToClients(
               "placeShip "
                   + field
                   + " "
                   + alignment
                   + " "
                   + shipName
                   + " "
                   + xStartCoordinate
                   + " "
                   + yStartCoordinate);
         }
         return true;
       }
     }
   }
   return false;
 }
コード例 #5
0
 public void elaboratePlaceRequest(
     String field, String alignment, String shipName, int xStartCoordinate, int yStartCoordinate) {
   boolean controllerResponse = false;
   controllerResponse =
       cont.addShip(shipName, alignment, field, xStartCoordinate, yStartCoordinate);
   if (controllerResponse) {
     for (ServerService s : services) {
       s.sendMessageToClients(
           "shipSuccessfullyPlaced "
               + field
               + " "
               + alignment
               + " "
               + shipName
               + " "
               + new Integer(xStartCoordinate).toString()
               + " "
               + new Integer(yStartCoordinate).toString());
     }
   }
 }
コード例 #6
0
 public void run() {
   if (isServer) {
     try {
       sSocket = new ServerSocket(portNr);
     } catch (IOException e) {
       e.printStackTrace();
     }
     while (shouldRun) {
       try {
         Socket s = sSocket.accept();
         ServerService c = new ServerService(s, gameData, this, cont);
         services.add(c);
         c.start();
         clientsJoined++;
         System.out.println(
             clientsJoined + " " + cont.getHostPlayers() + " " + cont.getGuestPlayers());
         if (clientsJoined == cont.getHostPlayers() + cont.getGuestPlayers() - 1) {
           System.out.println("ready");
           ready = true;
         }
       } catch (IOException e) {
         e.printStackTrace();
       }
     }
     for (ServerService a : services) a.setShouldRun(false);
   } else {
     while (shouldRun) {
       try {
         String response = responseFromServer.readLine();
         if (response.startsWith("spl")) {
           spl = response.substring(3);
           serverResponse = true;
         }
         if (response.startsWith("opl")) {
           opponentResponse = true;
           opl = response.substring(3);
         }
         if (response.startsWith("gamechallenge")) serverGameData = response;
         if (response.equals("sidefull")) cs.playerNotAdded();
         if (response.equals("playeradded")) cs.playerAdded();
         if (response.startsWith("chat")) {
           parseChatMessage(response);
         }
         if (response.startsWith("water")) {
           waterResponse(response);
         }
         if (response.startsWith("hit")) {
           System.out.println("ship shot");
           hitResponse(response);
         }
         if (response.startsWith("shipSuccessfullyPlaced")) {
           shipPlacedResponse(response);
         }
         if (response.startsWith("destroyed")) {
           destroyedResponse(response);
         }
         if (response.equals("endGame")) {
           closeDown();
         }
         if (response.startsWith("allShipsDestroyed")) allShipsDestroyed(response);
         response = null;
       } catch (IOException e) {
         e.printStackTrace();
       }
     }
   }
 }