public void serverTCPConnect(String serverIP, int serverPort) throws Exception { this.mode = MODE_CLIENT; this.gameRunning = false; String result = NET.serverTCPConnect(serverIP, serverPort); this.playerID = Integer.parseInt(result.split("\\|")[0]); this.teamID = Integer.parseInt(result.split("\\|")[1]); }
/** * Notify all the clients that the WaitingRoom has changed because a new player joined or a * current player disconnected */ public void updatedPlayers() { WaitingRoom r = new WaitingRoom(); // Send the info to all the connected clients NET.sendSignals("WAITINGROOM " + serialize(r)); }
public static CtrlDomain getInstance() { if (INSTANCE == null) { INSTANCE = new CtrlDomain(); INSTANCE.NET = CtrlNet.getInstance(); INSTANCE.GAME = CtrlGame.getInstance(); } return INSTANCE; }
/** * A disconnection has been detected. Notify the UI if in client mode or notify the rest of the * clients if in server mode. * * @param conn */ public void disconnectionDetected(TCPConnection conn) { conn.close(); if (mode == CtrlDomain.MODE_CLIENT) { // If in client mode, notify the UI about the shutdown shutdownUI(); } else if (mode == CtrlDomain.MODE_SERVER && !gameRunning) { // This happens when there is a disconnection in the WaitingRoom CtrlNet.getInstance().removePlayer(conn); updatedPlayers(); } else if (mode == CtrlDomain.MODE_SERVER && gameRunning) { // This happens when there is a disconnection while playing a game CtrlNet.getInstance().sendShutdown(); } }
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()); } }
public void serverTCPDisconnectClients() { this.mode = 0; this.gameRunning = false; NET.serverTCPDisconnectClients(); }
public void serverTCPStop() { NET.serverTCPStop(); }
public void serverTCPStart() throws Exception { this.mode = MODE_SERVER; this.gameRunning = false; NET.serverTCPStart(serverNumTeams, serverNumTurns); }
public void setWifiManager(WifiManager systemService) { NET.setWifiManager(systemService); }
public void serverUDPStop() { NET.serverUDPStop(); }
public void serverUDPFind(Handler handler) { NET.serverUDPFind(handler); }
public void serverUDPStart() { NET.serverUDPStart(); }