Example #1
0
  // Read Packet for GameStateVisualizer
  public boolean readPacket(DatagramPacket packet, MainGUI gui) {
    ByteBuffer packetData = ByteBuffer.allocate(GameState.packet_size * 3);
    packetData.order(Constants.NETWORK_BYTEORDER);
    packetData.rewind();
    packetData.put(packet.getData());

    packetData.rewind();

    byte[] header = new byte[4];
    packetData.get(header);
    if (!GameState.STRUCT_HEADER.equals(new String(header))) {
      if (gui.getDebug()) {
        System.out.println("Bad header");
      }
      return false;
    }

    int version = packetData.getInt();
    if (version != 6 && version != 7) {
      if (gui.getDebug()) {
        System.out.println("Bad packet version: " + version + " expecting: 6 or 7");
      }
      return false;
    }

    packetData.get(); // playersPerTeam
    byte gameState = packetData.get(); // gameState
    byte firstHalf = packetData.get(); // firstHalf
    packetData.get(); // kickOffTeam
    byte secondaryState = packetData.get(); // secondaryState
    packetData.get(); // dropInTeam
    packetData.getShort(); // dropInTime
    int secsRemaining = packetData.getInt(); // secsRemaining

    // team blue
    byte team1Number = packetData.get(); // teamNumber
    packetData.get(); // teamColor
    if (version == 7) packetData.get();
    int team1Score = version == 6 ? packetData.getShort() : packetData.get(); // score
    packetData.position(packetData.position() + Constants.MAX_NUM_PLAYERS * 4);
    // team red
    byte team2Number = packetData.get(); // teamNumber
    packetData.get(); // teamColor
    if (version == 7) packetData.get();
    int team2Score = version == 6 ? packetData.getShort() : packetData.get(); // score
    this.setVersion(version);
    this.setGameState(gameState);
    this.setSecondaryGameState(secondaryState);
    this.setHalf(firstHalf);
    this.setEstimatedSecs(secsRemaining, false);
    this.getTeam(Constants.TEAM_BLUE).setTeamNumber(team1Number);
    this.getTeam(Constants.TEAM_BLUE).setTeamScore((byte) team1Score);
    this.getTeam(Constants.TEAM_RED).setTeamNumber(team2Number);
    this.getTeam(Constants.TEAM_RED).setTeamScore((byte) team2Score);
    return true;
  }