public void protocolGameInfo() { /* On vérifie que c'est bien la première fois qu'on reçoit les informations (pour cela on regarde * si l'ensemble des joueurs est vide) */ if (pong.setPlayers.isEmpty()) { /* On place la balle à la bonne position */ int positionBallX = Integer.parseInt(tab[1]); int positionBallY = Integer.parseInt(tab[2]); Ball ball = pong.getBall(); ball.setPosition(new Point(positionBallX, positionBallY)); pong.setBall(ball); /* On instancie tous les joueurs distants */ for (int i = 3; i < tab.length; i++) { String[] curPlayer = tab[i].split(";"); PlayerID playerID = PlayerID.valueOf(curPlayer[0]); int score = Integer.parseInt(curPlayer[1]); int positionRacketX = Integer.parseInt(curPlayer[2]); int positionRacketY = Integer.parseInt(curPlayer[3]); String host = curPlayer[4]; int port = Integer.parseInt(curPlayer[5]); Player player = new Player(playerID, score, positionRacketX, positionRacketY, host, port); pong.setPlayers.add(player); } } }
public void protocolPlayer() { String[] curPlayer = tab[1].split(";"); PlayerID playerID = PlayerID.valueOf(curPlayer[0]); int positionRacketX = Integer.parseInt(curPlayer[2]); int positionRacketY = Integer.parseInt(curPlayer[3]); /* On met à jour la position du joueur */ Iterator<Player> it = pong.setPlayers.iterator(); while (it.hasNext()) { Player player = it.next(); if (player.getPlayerID() == playerID) { Racket racket = player.getRacket(); /* Anti-cheat */ if ((positionRacketX > (racket.getPositionX() + racket.getSpeed()) && racket.getSpeed() > 0) || (positionRacketX < (racket.getPositionX() + racket.getSpeed()) && racket.getSpeed() < 0) || (positionRacketY > (racket.getPositionY() + racket.getSpeed()) && racket.getSpeed() > 0) || (positionRacketY < (racket.getPositionY() + racket.getSpeed()) && racket.getSpeed() < 0)) { errorCheat("Raquette avec coordonnées invalides (cheat)"); } racket.setPosition(new Point(positionRacketX, positionRacketY)); player.setRacket(racket); } } }
public void protocolBall() { /* On met à jour la position de la balle */ int positionBallX = Integer.parseInt(tab[1]); int positionBallY = Integer.parseInt(tab[2]); Ball ball = pong.getBall(); /* Anti-cheat */ if ((positionBallX > (ball.getPositionX() + ball.getSpeedX()) && ball.getSpeedX() > 0) || (positionBallX < (ball.getPositionX() + ball.getSpeedX()) && ball.getSpeedX() < 0) || (positionBallY > (ball.getPositionY() + ball.getSpeedY()) && ball.getSpeedY() > 0) || (positionBallY < (ball.getPositionY() + ball.getSpeedY()) && ball.getSpeedY() < 0)) { errorCheat("Balle avec coordonnées invalides (cheat)"); } ball.setPosition(new Point(positionBallX, positionBallY)); pong.setBall(ball); /* On met à jour les scores */ for (int i = 3; i < tab.length; i++) { String[] curPlayer = tab[i].split(";"); PlayerID playerID = PlayerID.valueOf(curPlayer[0]); int score = Integer.parseInt(curPlayer[1]); Iterator<Player> it = pong.setPlayers.iterator(); while (it.hasNext()) { Player player = it.next(); if (player.getPlayerID() == playerID) { player.setScore(score); } } } }
public void protocolNewPlayer() { /* On instancie un nouveau joueur distant */ String[] curPlayer = tab[1].split(";"); PlayerID playerID = PlayerID.valueOf(curPlayer[0]); int score = Integer.parseInt(curPlayer[1]); int positionRacketX = Integer.parseInt(curPlayer[2]); int positionRacketY = Integer.parseInt(curPlayer[3]); String host = curPlayer[4]; int port = Integer.parseInt(curPlayer[5]); Player player = new Player(playerID, score, positionRacketX, positionRacketY, host, port); pong.setPlayers.add(player); }
/** * Only used when we are trying to export the data to a savegame, and we need to change the round * and step to something other than the current round and step (because we are creating a savegame * at a certain point in history, for example). * * @param currentRound * @param stepDisplayName * @param player */ public synchronized void setRoundAndStep( final int currentRound, final String stepDisplayName, final PlayerID player) { System.out.println("Finding step for: Player: " + player + " at Step: " + stepDisplayName); m_round = currentRound; boolean found = false; for (int i = 0; i < m_steps.size(); i++) { final GameStep step = m_steps.get(i); if (step != null && step.getDisplayName().equalsIgnoreCase(stepDisplayName)) { if ((player == null && step.getPlayerID() == null) || (player != null && player.equals(step.getPlayerID()))) { m_currentIndex = i; found = true; break; } } } if (!found) { m_currentIndex = 0; System.out.println("Step Not Found, will instead use: " + m_steps.get(m_currentIndex)); } else System.out.println("Step Found: " + m_steps.get(m_currentIndex)); }