예제 #1
0
  /**
   * Handles a message that was received from a robot.
   *
   * @param address IP address of the sender
   * @param teamNumber team number belonging to the port on which the message was received
   * @param message received message
   */
  public void receiveMessage(
      final String address, final int teamNumber, final SPLStandardMessage message) {
    int changed = 0;

    // update the team info if no GameController info is available
    if (lastGameControlData == null) {
      synchronized (teamNumbers) {
        boolean exists = false;
        for (int i = 0; i < 2; i++) {
          if (teamNumbers[i] == teamNumber) {
            exists = true;
            break;
          }
        }
        if (!exists) {
          for (int i = 0; i < 2; i++) {
            if (teamNumbers[i] == 0) {
              teamNumbers[i] = teamNumber;

              // (re)load plugins
              PluginLoader.getInstance().update(teamNumber);

              changed = i + 1 | CHANGED_OTHER;
              break;
            }
          }
        }
      }
    }

    // create the robot state if it does not yet exist
    RobotState r;
    synchronized (robotsByAddress) {
      r = robotsByAddress.get(address);
      if (r == null) {
        r = new RobotState(address, teamNumber);

        robotsByAddress.put(address, r);
      }

      Collection<RobotState> set = robots.get(teamNumber);
      if (set == null) {
        set = new HashSet<>();
        robots.put(teamNumber, set);
      }
      if (set.add(r)) {
        if (teamNumbers[TEAM_LEFT] == teamNumber) {
          changed |= CHANGED_LEFT;
        } else if (teamNumbers[TEAM_RIGHT] == teamNumber) {
          changed |= CHANGED_RIGHT;
        }
      }
    }

    // let the robot state handle the message
    r.registerMessage(message);

    // send events
    sendEvents(changed);
  }
예제 #2
0
  /**
   * Updates info about the game with a message from the GameController.
   *
   * @param data data sent by the GameController
   */
  public void updateGameData(final GameControlData data) {
    int changed = 0;

    if (data == null) {
      if (lastGameControlData != null) {
        synchronized (teamNumbers) {
          teamNumbers[TEAM_LEFT] = 0;
          teamNumbers[TEAM_RIGHT] = 0;
          int s = 0;
          for (final Entry<Integer, Collection<RobotState>> entry : robots.entrySet()) {
            if (!entry.getValue().isEmpty()) {
              teamNumbers[s++] = entry.getKey();
              if (s == 2) {
                break;
              }
            }
          }
        }
        changed = CHANGED_LEFT | CHANGED_RIGHT | CHANGED_OTHER;
        Logger.getInstance().createLogfile();
      }
    } else {
      if (lastGameControlData == null) {
        synchronized (teamNumbers) {
          teamNumbers[TEAM_LEFT] = data.team[0].teamNumber;
          teamNumbers[TEAM_RIGHT] = data.team[1].teamNumber;
        }
        changed = CHANGED_LEFT | CHANGED_RIGHT | CHANGED_OTHER;
      } else {
        synchronized (teamNumbers) {
          if (data.team[0].teamNumber != teamNumbers[TEAM_LEFT]) {
            teamNumbers[TEAM_LEFT] = data.team[0].teamNumber;
            changed = CHANGED_LEFT | CHANGED_OTHER;
          }
          if (data.team[1].teamNumber != teamNumbers[TEAM_RIGHT]) {
            teamNumbers[TEAM_RIGHT] = data.team[1].teamNumber;
            changed = CHANGED_RIGHT | CHANGED_OTHER;
          }
        }
      }

      teamColors.put((int) data.team[0].teamNumber, (int) data.team[0].teamColor);
      teamColors.put((int) data.team[1].teamNumber, (int) data.team[1].teamColor);

      // Update penalties
      for (final TeamInfo team : data.team) {
        final Collection<RobotState> teamRobots = robots.get((int) team.teamNumber);
        if (teamRobots != null) {
          for (final RobotState r : teamRobots) {
            if (r.getPlayerNumber() != null && r.getPlayerNumber() <= team.player.length) {
              r.setPenalty(team.player[r.getPlayerNumber() - 1].penalty);
            }
          }
        }
      }

      // Open a new logfile for the current GameController state if the
      // state changed from or to initial/finished
      final StringBuilder logfileName;
      if ((data.team[0].teamNumber == 98 || data.team[0].teamNumber == 99)
          && (data.team[1].teamNumber == 98 || data.team[1].teamNumber == 99)) {
        logfileName = new StringBuilder("Drop-in_");
        if (data.firstHalf == GameControlData.C_TRUE) {
          logfileName.append("1st");
        } else {
          logfileName.append("2nd");
        }
        logfileName.append("Half");
      } else {
        if (data.firstHalf == GameControlData.C_TRUE) {
          logfileName =
              new StringBuilder(getTeamName((int) data.team[0].teamNumber, false, false))
                  .append("_")
                  .append(getTeamName((int) data.team[1].teamNumber, false, false))
                  .append("_1st");
        } else {
          logfileName =
              new StringBuilder(getTeamName((int) data.team[1].teamNumber, false, false))
                  .append("_")
                  .append(getTeamName((int) data.team[0].teamNumber, false, false))
                  .append("_2nd");
        }
        logfileName.append("Half");
      }
      if (data.gameState == GameControlData.STATE_READY
          && (lastGameControlData == null
              || lastGameControlData.gameState == GameControlData.STATE_INITIAL)) {
        Logger.getInstance().createLogfile(logfileName.toString());
      } else if (data.gameState == GameControlData.STATE_INITIAL
          && (lastGameControlData == null
              || lastGameControlData.gameState != GameControlData.STATE_INITIAL)) {
        Logger.getInstance().createLogfile(logfileName.append("_initial").toString());
      } else if (data.gameState == GameControlData.STATE_FINISHED
          && (lastGameControlData == null
              || lastGameControlData.gameState != GameControlData.STATE_FINISHED)) {
        Logger.getInstance().createLogfile(logfileName.append("_finished").toString());
      }

      if (changed != 0) {
        // (re)load plugins
        PluginLoader.getInstance()
            .update((int) data.team[0].teamNumber, (int) data.team[1].teamNumber);
      }
    }
    lastGameControlData = data;

    // Log the GameController data
    if (data != null || changed != 0) {
      Logger.getInstance().log(data);
    }

    // send events
    sendEvents(changed);
  }