private void joinGame(LobbyListItem item) { if (!checkBoxView.isChecked() && item.getPlayerCount() >= item.getMaxPlayersAsInteger()) { Toast.makeText(this, getString(R.string.too_many_players), Toast.LENGTH_SHORT).show(); return; } String gameName = item.getGamename(); GameSettings.setGameName(gameName); GameSettings.setGameId(roomIds.get(gameName)); GameSettings.setOwnerId(item.getHostId()); GameSettings.setCanBreakWall(item.getCanBreakWall()); GameSettings.setTimeLimit(item.getTimeLimit()); GameSettings.setMaxDistance(item.getMaxDist()); GameSettings.setMaxPlayers(item.getMaxPlayersAsInteger()); GameSettings.setSpectate(checkBoxView.isChecked()); showToast(R.string.joinging_message, gameName); if (!GameSettings.getSpectate()) { Map<String, String> query = ImmutableMap.of( "gameId", String.valueOf(roomIds.get(gameName)), "id", String.valueOf(GameSettings.getPlayerId()), "token", GameSettings.getPlayerToken()); dataServer.sendRequest( ServerCommand.JOIN_GAME, query, new HttpConnector.Callback() { @Override public void handleResult(String data) { try { JSONObject result = new JSONObject(data); // TODO check for errors showRoomActivity(); } catch (JSONException e) { e.printStackTrace(); } } }); } else { showRoomActivity(); } }
// 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); }