コード例 #1
0
ファイル: GameService.java プロジェクト: upost/Pandemia
  // LocationListener impl
  @Override
  public void onLocationChanged(Location location) {
    String topic = topic(location);
    if (!topic.equals(currentTopic)) {
      Log.d(LOG_TAG, "new grid location: " + topic);
      if (sendInfectionMessage(true)) {
        currentTopic = topic;
        subscribeToCurrentTopic();
      }
    }

    int thisRegion = calcRegionCode(location);
    if (thisRegion != region || bioHazards.isEmpty()) {
      Log.d(
          LOG_TAG,
          "new region or no biohazards in region " + thisRegion + " - generating biohazards");
      region = thisRegion;
      createBiohazards(location);
      EventBus.getDefault().post(new MapEvent(MapEvent.Type.REGION_UPDATED, region, bioHazards));
    }

    for (Biohazard b : bioHazards) {
      if (b.getLocation().distanceTo(location) < BIOHAZARD_INFECTION_RADIUS) {
        Virus v = VirusFactory.fromBiohazard(b.getSeed(), b.getId());
        if (gameDatabase.findVirus(v.getId()) == null) {
          Log.d(LOG_TAG, "infection by approaching biohazard " + b.getId());
          gameDatabase.addVisitedBiohazard(b.getId());
          addVirus(v);
        }
      }
    }
  }
コード例 #2
0
ファイル: GameService.java プロジェクト: upost/Pandemia
  @Override
  public void onCreate() {
    super.onCreate();

    vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

    gameDatabase = new GameDatabase(this);

    // init game DB
    if (gameDatabase.getViruses().isEmpty()) {
      Virus v = VirusFactory.createMutation(null);
      Log.d(LOG_TAG, "new virus: " + v.getId());
      gameDatabase.addVirus(v);
      EventBus.getDefault().post(new GameEvent(GameEvent.Type.NEW_VIRUS, v.getId(), 0));
    }
    clientId = gameDatabase.getSetting("clientId", null);
    if (clientId == null) {
      clientId = UUID.randomUUID().toString();
      gameDatabase.putSetting("clientId", clientId);
      Log.i(LOG_TAG, "persisted new clientId: " + clientId);
    }

    // MQTT
    mqttClient = new MqttAndroidClient(this, BROKER_URI, clientId);
    mqttClient.setCallback(this);

    // Location
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
      locationManager.requestLocationUpdates(
          LocationManager.GPS_PROVIDER,
          MIN_LOCATION_UPDATE_INTERVAL_MS,
          MIN_LOCATION_UPDATE_DISTANCE_M,
          this);
    else
      locationManager.requestLocationUpdates(
          LocationManager.NETWORK_PROVIDER,
          MIN_LOCATION_UPDATE_INTERVAL_MS,
          MIN_LOCATION_UPDATE_DISTANCE_M,
          this);

    findBestLastLocation();

    try {
      MqttConnectOptions options = new MqttConnectOptions();
      options.setKeepAliveInterval(0); // no keepalive pings
      mqttClient.connect(options, null, this);
      Log.d(LOG_TAG, "connected to MQTT broker as " + clientId);
    } catch (MqttException e) {
      Log.e(LOG_TAG, "could not connect to MQTT broker at " + BROKER_URI);
    }

    EventBus.getDefault().register(this);
  }
コード例 #3
0
ファイル: GameService.java プロジェクト: upost/Pandemia
 @Override
 public void messageArrived(String topic, MqttMessage message) throws Exception {
   if (topic.equals(currentTopic)) {
     String msg = new String(message.getPayload());
     Log.v(LOG_TAG, "mqtt message arrived: " + msg);
     // process message
     JSONObject o = new JSONObject(msg);
     String sender = o.optString("sender", null);
     // ignore our own messages, so check sender
     if (sender != null && !sender.equals(clientId)) {
       // somebody else sent their viruses
       JSONArray a = o.getJSONArray("viruses");
       for (int i = 0; i < a.length(); i++) {
         Virus v = VirusFactory.fromJSON(a.getJSONObject(i));
         addVirus(v);
       }
       // was the other user just entering our region?
       if (o.optString("entering", null) != null) {
         // send our infection
         sendInfectionMessage(false);
       }
     }
   }
 }