public void loadPreferences() { // Get the app's shared preferences // prefsPrivate = PreferenceManager.getDefaultSharedPreferences(this); prefsPrivate = getSharedPreferences("preferences", Context.MODE_PRIVATE); int pcounter = prefsPrivate.getInt("pcounter", 0); // A program counter prefsEditor = prefsPrivate.edit(); prefsEditor.putInt("pcounter", ++pcounter); prefsEditor.commit(); // Very important // Facebook access String access_token = prefsPrivate.getString("access_token", null); Long access_expires = prefsPrivate.getLong("access_expires", 0); if (access_token != null) { mFacebook.setAccessToken(access_token); } if (access_expires != 0) { mFacebook.setAccessExpires(access_expires); } // App's private prefs game.setPlayer( new Player( prefsPrivate.getInt("userid", 0), prefsPrivate.getString("user", "Anonomous"), prefsPrivate.getString("pass", ""), prefsPrivate.getString("email", ""))); game.setCash(prefsPrivate.getInt("cash", GameMechanics.DEFAULT_CASH)); // Start a RegisterActivity /*if(pcounter <= 1 || game.getPlayer().getUserId() == 0) showRegisterActivity(); return;*/ }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.v(TAG, "RUNOPOLY STARTED"); game = new GameMechanics(); dbc = new DataBaseComm(); // Fancy new login mFacebook = new Facebook(FB_APP_ID); mAsyncRunner = new AsyncFacebookRunner(mFacebook); loadPreferences(); int storedcash = dbc.loadCash(game.getPlayer().getUserId()); if (storedcash > game.getCash()) game.setCash(storedcash); Log.d(TAG, "Cash loaded: " + Integer.toString(storedcash)); if (!mFacebook.isSessionValid()) { mFacebook.authorize( this, new String[] {"email", "publish_stream"}, new DialogListener() { @Override public void onComplete(Bundle values) { prefsEditor.putString("access_token", mFacebook.getAccessToken()); prefsEditor.putLong("access_expires", mFacebook.getAccessExpires()); prefsEditor.commit(); mAsyncRunner.request("me", new IDRequestListener()); } @Override public void onFacebookError(FacebookError error) {} @Override public void onError(DialogError e) {} @Override public void onCancel() {} }); } pm = (PowerManager) getSystemService(Context.POWER_SERVICE); wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "RUNOPOLIS"); wl.acquire(); lm = (LocationManager) getSystemService(LOCATION_SERVICE); vib = (Vibrator) getSystemService(VIBRATOR_SERVICE); setContentView(R.layout.game); // game.setCash(1000); // game.setCurrentCard(new Card("1 V 3111 41111", "Norrmalmstorg", "Stockholm", 3, "Me", 1, 2)); loadMainContent(); updateMainContent(game.getCurrentCard()); }
protected void syncAllCash(boolean online) { prefsEditor = prefsPrivate.edit(); prefsEditor.putInt("cash", game.getCash()); prefsEditor.commit(); // Very important if (online) { dbc.syncCash(game.getPlayer().getUserId(), game.getCash()); int income = dbc.retrieveTransfer(game.getPlayer().getUserId()); Log.v(TAG, "Tax Income: " + Integer.toString(income)); if (income > 0) game.addCash(income); } }
@Override public boolean onOptionsItemSelected(MenuItem item) { // Handle item selection switch (item.getItemId()) { case R.id.about: Intent mainIntent = new Intent(MainActivity.this, AboutActivity.class); MainActivity.this.startActivityForResult(mainIntent, -1); return true; case R.id.listcards: Intent mainIntent2 = new Intent(MainActivity.this, ListCardsActivity.class); mainIntent2.putExtra("USERID", game.getPlayer().getUserId()); MainActivity.this.startActivityForResult(mainIntent2, -1); return true; case R.id.statistics: Intent mainIntent3 = new Intent(MainActivity.this, StatisticsActivity.class); MainActivity.this.startActivityForResult(mainIntent3, -1); return true; case R.id.refresh: // Refreshing stuff! // New owner? Card lc = dbc.loadCard(game.getCurrentCard().getId()); // Load the new card from db if (lc != null) { Log.d(TAG, Integer.toString(lc.getOwnerId())); game.setCurrentCard(lc); game.getCurrentCard().setOwner(dbc.loadUserName(lc.getOwnerId())); game.getCurrentCard() .setLocationData(this.getBaseContext(), game.getCurrentCard().getPosition()); } // Cash syncAllCash(true); updateMainContent(game.getCurrentCard()); Toast.makeText(this, "Refresh Complete!", Toast.LENGTH_SHORT).show(); return true; case R.id.logout: // Log out Facebook Log.d(TAG, "Logout Facebook"); prefsEditor = prefsPrivate.edit(); prefsEditor.putString("access_token", null); prefsEditor.putLong("access_expires", 0); prefsEditor.putString("user", null); prefsEditor.putString("pass", null); prefsEditor.putString("email", null); prefsEditor.putInt("userid", 0); prefsEditor.putInt("cash", 0); prefsEditor.commit(); String method = "DELETE"; Bundle params = new Bundle(); /* * this will revoke 'publish_stream' permission * Note: If you don't specify a permission then this will de-authorize the application completely. */ params.putString("permission", ""); mAsyncRunner.request( "/me/permissions", params, method, new RequestListener() { @Override public void onComplete(String response, Object state) {} @Override public void onIOException(IOException e, Object state) {} @Override public void onFileNotFoundException(FileNotFoundException e, Object state) {} @Override public void onMalformedURLException(MalformedURLException e, Object state) {} @Override public void onFacebookError(FacebookError e, Object state) {} }, null); mAsyncRunner.logout( this.getBaseContext(), new RequestListener() { @Override public void onMalformedURLException(MalformedURLException e, Object state) {} @Override public void onIOException(IOException e, Object state) {} @Override public void onFileNotFoundException(FileNotFoundException e, Object state) {} @Override public void onFacebookError(FacebookError e, Object state) {} @Override public void onComplete(String response, Object state) {} }); return true; default: return super.onOptionsItemSelected(item); } }
public void updateMainContent(Card cc) { TextView cash = (TextView) findViewById(R.id.menu_text); if (game.getAddedCash() > 0) cash.setText("Cash: $" + game.getCash() + " (+$" + game.getAddedCash() + ")"); else if (game.getAddedCash() < 0) cash.setText("Cash: $" + game.getCash() + " ($" + game.getAddedCash() + ")"); else cash.setText("Cash: $" + game.getCash()); LinearLayout ll = (LinearLayout) findViewById(R.id.linearLayout3); new Color(); ll.setBackgroundColor( Color.rgb( game.getCurrentCard().getColor()[0], game.getCurrentCard().getColor()[1], game.getCurrentCard().getColor()[2])); TextView h1 = (TextView) findViewById(R.id.textView1); h1.setText(cc.getStreet().toUpperCase()); TextView h2 = (TextView) findViewById(R.id.textView2); h2.setText(cc.getArea()); TextView own = (TextView) findViewById(R.id.textView3); own.setText("" + cc.getOwner()); TextView val = (TextView) findViewById(R.id.textView4); val.setText("$" + Integer.toString(cc.getValue())); TextView tax = (TextView) findViewById(R.id.textView5); tax.setText("$" + Integer.toString(cc.getTax())); TextView hou = (TextView) findViewById(R.id.textView6); String pri = new String(); if (cc.getHousePrise() <= 0) pri = " --"; else pri = Integer.toString(cc.getHousePrise()); hou.setText("$" + pri); // The buttons buyStreet.setEnabled( game.getCash() >= game.getCurrentCard().getValue() && game.getCurrentCard().getStreet() != GameMechanics.DEFAULT_STREET && game.getPlayer().getUserId() != game.getCurrentCard().getOwnerId()); buyHouse.setEnabled( game.getCash() >= game.getCurrentCard().getHousePrise() && game.getCurrentCard().getHouses() < 5 && game.getCurrentCard().getStreet() != GameMechanics.DEFAULT_STREET && game.getPlayer().getUserId() == game.getCurrentCard().getOwnerId()); // Do the houses for (int i = 1; i <= 5; i++) { ImageView houses = new ImageView(this); houses = (ImageView) findViewById(ivid[i]); if (cc.getHouses() >= i) houses.setImageResource(hoid[i]); else houses.setImageResource(hoid[0]); houses.invalidate(); } return; }
@Override public void onLocationChanged(Location location) { Log.v(TAG, "Location Changed"); nofixes++; StringBuilder sb = new StringBuilder(); // Get some cash on each fix and make sound game.foundCash(); mp.get(0).start(); // vib.vibrate(100); try { sb.append( game.getPlayer().getUserId() + ", " + game.getPlayer().getUser() + ", " + game.getPlayer().getEmail() + "\n"); sb.append(nofixes + ", "); // Decode point address List<Address> listan = new LinkedList<Address>(); Geocoder adam = new Geocoder(this.getBaseContext()); listan.addAll(adam.getFromLocation(location.getLatitude(), location.getLongitude(), 9)); Log.v(TAG, Integer.toString(nofixes)); sb.append(location.getProvider()); // Conversion to UTM CoordinateConversion bertil = new CoordinateConversion(); String cesar = bertil.latLon2UTM(location.getLatitude(), location.getLongitude()); String[] david = cesar.split(" "); // if new square - update! String newid = david[0] + Character.getNumericValue(david[1].toCharArray()[0]) + david[2].substring(0, 3) + david[3].substring(0, 4); if (!newid.matches(game.getCurrentCard().getId()) && game.getPlayer().getUserId() != 0) { Log.v(TAG, "NEW AREA!!!"); if (!dbc.existsCard(newid)) { Log.v(TAG, "NEW CARD! (Insert card)"); game.setCurrentCard(new Card(cesar, "", "", 0, GameMechanics.DEFAULT_OWNER, 0, 0)); game.getCurrentCard().setLocationData(this.getBaseContext(), cesar); dbc.insertCard(game.getCurrentCard()); } else { Log.v(TAG, "CARD EXISTS! (Load card)"); Card lc = dbc.loadCard(newid); // Load the new card from db if (lc != null) { Log.d(TAG, Integer.toString(lc.getOwnerId())); game.setCurrentCard(lc); game.getCurrentCard().setOwner(dbc.loadUserName(lc.getOwnerId())); game.getCurrentCard().setLocationData(this.getBaseContext(), cesar); } // potential owner / disable button issue? or will it be fixed in the update views?.... // yes probably // pay taxes! if (game.getPlayer().getUserId() != lc.getOwnerId() && lc.getOwnerId() != 0) dbc.insertTransfer( game.getPlayer().getUserId(), lc.getOwnerId(), lc.getId(), lc.getTax(), 0); // 0 = tax } syncAllCash(true); // make a new sound mp.get(2).start(); } else { Log.v(TAG, "SAME OLD AREA!"); game.getCurrentCard().setLocationData(this.getBaseContext(), cesar); syncAllCash(false); } // Update current card and funds updateMainContent(game.getCurrentCard()); sb.append(cesar + ", "); sb.append(game.getCurrentCard().getId() + "\n"); sb.append("\n"); sb.append(location.getProvider() + ", "); sb.append(location.getAccuracy() + "\n"); /*for(int i = 0; i < listan.size(); i++){ sb.append(listan.get(i).getThoroughfare() + ", " + listan.get(i).getLocality() + "\n"); }*/ // Debug text if (DEBUG) tw.setText(sb.toString()); } catch (Exception e) { e.printStackTrace(); } if (game.getAddedCash() > 100) mp.get(3).start(); }