Esempio n. 1
0
  /**
   * Tell all of the players and the server that the game has ended
   *
   * @param winner The winner of the game
   */
  public void notifyEndGame(String winner) {
    // Tell the other players that the game has ended
    // The players show the winner when they receive this message
    gameUpdateHandler.sendWinner(winner);

    // Tell the server that the game has ended
    ImmutableMap query =
        ImmutableMap.of(
            "gameId", String.valueOf(GameSettings.getGameId()),
            "token", GameSettings.getGameToken(),
            "winnerId", winner);

    dataServer.sendRequest(
        ServerCommand.END_GAME,
        query,
        new HttpConnector.Callback() {
          @Override
          public void handleResult(String data) {
            try {
              JSONObject result = new JSONObject(data);
              // TODO check for errors
              // The host shows the winner here
              onEndGame();

            } catch (JSONException e) {
              e.printStackTrace();
            }
          }
        });
  }
Esempio n. 2
0
  // region Initialization
  // ---------------------------------------------------------------------------------------------
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState != null) {
      // If the application closed accidentally the player is set to spectator mode
      // At this point the game is corrupt and a lot of problems will arise
      // If this happens to the host it is extremely problematic
      // TODO make the host end the game if his game is corrupt
      GameSettings.loadFromBundle(savedInstanceState);
      GameSettings.setSpectate(true);
      Toast.makeText(this, getString(R.string.game_activity_closed), Toast.LENGTH_SHORT).show();
    }

    // Content of Activity
    // -----------------------------------------------------------------------------------------
    setContentView(R.layout.activity_game);

    // Sensor tracking
    // -----------------------------------------------------------------------------------------
    // Initialize sensorDataObservable and proximityObserver
    acceleration = 0;
    accelerationCount = 0;
    turns = 0;
    sensorDataObservable = new SensorDataObservable(this);
    isBellRinging = false;

    // Location tracking
    // -----------------------------------------------------------------------------------------
    // Initialize the stored locations to 0,0
    snappedGpsLoc = new LatLng(0, 0);
    gpsLoc = new LatLng(0, 0);
    if (savedInstanceState != null)
      travelledDistance = savedInstanceState.getDouble("travelledDistance", 0.0);
    else travelledDistance = 0.0;

    // Initialize location listener
    locationListener =
        new CustomLocationListener(
            this, // The activity that builds the google api
            this, // The LocationObserver
            1500, // Normal update frequency of the location
            500, // Fastest update frequency of the location
            LocationRequest.PRIORITY_HIGH_ACCURACY // Accuracy of the location
            );

    // Create the context used by the google api (just stores the google key)
    context = new GeoApiContext().setApiKey(getString(R.string.google_maps_key_server));

    // Permissions
    // -----------------------------------------------------------------------------------------
    // Request permissions before doing anything else (after initializing the location listener!)
    requestPermissions();

    // Map Object
    // -----------------------------------------------------------------------------------------
    // Create the map object
    MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
    map = new Map(mapFragment);

    // Player objects
    // -----------------------------------------------------------------------------------------
    // Create the player
    map.addMapItem(
        new Player(GameSettings.getPlayerId(), GameSettings.getPlayerName(), new LatLng(0.0, 0.0)));

    // Networking
    // -----------------------------------------------------------------------------------------
    // Create the gameUpdateHandler object
    connection = new SocketIoConnection("testA1", String.valueOf(GameSettings.getGameId()));

    gameUpdateHandler = new GameUpdateHandler(this, connection, map, context);
    gameEventHandler = new GameEventHandler(connection, this);

    if (savedInstanceState != null)
      // When the game is corrupt, you are considered dead.
      gameUpdateHandler.sendDeathMessage("//", "A corrupted game");

    // Create the data server
    dataServer = new HttpConnector(getString(R.string.dataserver_url));

    // Setup the game ui
    if (!GameSettings.isOwner()) {
      Button startButton = (Button) findViewById(R.id.start_game_button);
      startButton.setVisibility(View.GONE);
    }

    Button wallBreaker = (Button) findViewById(R.id.breakWallButton);
    wallBreaker.setVisibility(View.GONE);

    LinearLayout travelledDistanceContainer =
        (LinearLayout) findViewById(R.id.travelledDistanceContainer);
    travelledDistanceContainer.setVisibility(View.GONE);

    TextView travelledDistance = (TextView) findViewById(R.id.travelledDistance);
    travelledDistance.setVisibility(View.GONE);

    TextView travelledDistanceHead = (TextView) findViewById(R.id.travelledDistanceHead);
    travelledDistanceHead.setVisibility(View.GONE);

    findViewById(R.id.eventContainer).setVisibility(View.GONE);
    findViewById(R.id.countdown).setVisibility(View.GONE);
  }