Example #1
0
  private void parserController(String str) {
    L.d(str);
    String[] actionContent = str.split(" ", 2);
    String[] args = null;
    if (actionContent.length > 1) {
      args = actionContent[1].split(",");
    }

    if (actionContent[0].equals("WAITINGROOM")) {
      // The name of the players and the team they belong
      // Only used in the *Waiting classes (JoinGameWaiting and NewGameWaiting)
      // The class sent with the info is WaitingRoom

      WaitingRoom room = (WaitingRoom) unserialize(args[0]);

      // Populate the remaining info that the server left blank
      room.currentPlayerID = this.playerID;
      room.currentTeamID = this.teamID;

      // Update the client UI
      Message msg = new Message();
      Bundle b = new Bundle();
      b.putString("type", "WAITINGROOM");
      b.putSerializable("room", room);
      msg.setData(b);
      handlerUI.sendMessage(msg);

    } else if (actionContent[0].equals("SHUTDOWN")) {
      // The server is closing the connection

      shutdownUI();
    } else if (actionContent[0].equals("STARTGAME")) {
      // The server started the game
      // Clients must leave the *Waiting view and change it to the Game

      this.gameRunning = true;

      Message msg = new Message();
      Bundle b = new Bundle();
      b.putString("type", "STARTGAME");
      msg.setData(b);
      handlerUI.sendMessage(msg);

    } else if (actionContent[0].equals("UPDATEDBOARD")) {
      // A client sent a new board
      // Send this new board to all the clients

      Board b = (Board) unserialize(args[0]);
      NET.sendUpdatedBoardClients(b);
      // NET.sendSignals(serialize(b));

    } else if (actionContent[0].equals("UPDATEBOARD")) {
      // The server sent a new board
      // Update the UI

      if (!myTurn) {
        GAME.setBoard((Board) unserialize(args[0]));
        L.d(" Rebut board amb gameover a " + GAME.getBoardToSend().isGameOver());
      }

    } else if (actionContent[0].equals("UPDATEMYTURN")) {
      // The server sent new information about my turn

      this.myTurns = Integer.parseInt(((String) args[0]));
      this.myTurn = (this.myTurns > 0) ? true : false;
      L.d("Turns updated. myTurn " + this.myTurn + " myTurns " + this.myTurns);

    } else if (actionContent[0].equals("TURNFINISHED")) {
      // A client just finished playing all the turns
      // Notify all the clients with the new turn

      incrementCurrentTurn();

      // Save the board sent by the finished player
      GAME.setBoard((Board) unserialize(args[0]));

      // Update the current turn of the board
      GAME.getBoardToSend().setCurrentTurnPlayer(NET.serverTCPGetConnectedPlayer(getCurrentTurn()));
      GAME.getBoardToSend().refreshCurrentPieceColor();

      // Send the new board to all the clients
      NET.sendUpdatedBoardClients(GAME.getBoardToSend());

      try {
        Thread.sleep(500);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }

      // Send the new turn
      NET.sendTurns(getCurrentTurn());
    }
  }