Esempio n. 1
0
  public void handleVolunteer(String[] fields) {

    // First, add a volunteer to the model when a VOLUNTEER message arrived
    // from the server

    String volunteerString = fields[1];
    String locationString = fields[2];
    int score = Integer.parseInt(fields[3]);

    Log.i("VOLUNTEER: volunteer=%s location=%s score=%d", volunteerString, locationString, score);

    Volunteer volunteer = model.getVolunteerByName(volunteerString);
    Location location = model.getLocationByName(locationString);
    if (location == null) {
      Log.w("Haven't seen volunteer location %s yet", locationString);
      return;
    }

    if (volunteer != null) model.removeVolunteer(volunteer);
    volunteer = new Volunteer(model, volunteerString, score, location);

    // Secondly, if the volunteer just added is "nickname", send a command
    // to
    // the server, whether to walk or to move.
    if (volunteerString.equals(this.nickname)) {

      // use nextRequest() to decide which is the best request to carry on
      Request theNextRequest = nextRequest(volunteer);

      // if nextRequest() can't give out a request, then move to the
      // nearest location with requests
      if (theNextRequest == null) {
        moveToNearest(volunteer);
        return;
      }

      // if nextRequest() gives a request at the same location with
      // "nickname", then walk that requester, otherwise move to that
      // requester
      if (theNextRequest.getStart().equals(volunteer.getCurrentLocation())) {
        String s = "walk " + theNextRequest.getName();
        connector.writeLine(s);
      } else {
        String s = "move " + theNextRequest.getStart().getName();
        connector.writeLine(s);
      }
    }
  }
Esempio n. 2
0
  // When the request returned from nextRequest() is null, call this method to
  // move to the nearest location with requests
  public void moveToNearest(Volunteer volunteer) {

    double min = Double.MAX_VALUE;
    String building = "PMU";

    for (Location location : model.getLocations()) {
      if (location.getRequests().size() > 1) {
        if (distance(volunteer.getCurrentLocation(), location) < min) {
          min = distance(volunteer.getCurrentLocation(), location);
          building = location.getName();
        }
      }
    }

    String s = "move " + building;
    connector.writeLine(s);
  }