Created
January 26, 2012 02:21
-
-
Save derekbrameyer/1680546 to your computer and use it in GitHub Desktop.
IRLHVZ PVP Practice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.doomonafireball.irlhvz; | |
import java.net.URL; | |
import android.app.Activity; | |
import android.app.AlertDialog; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.DialogInterface; | |
import android.content.Intent; | |
import android.content.IntentFilter; | |
import android.os.Bundle; | |
import android.os.Handler; | |
import android.os.Message; | |
import android.os.Vibrator; | |
import android.util.Log; | |
import android.view.Gravity; | |
import android.view.MotionEvent; | |
import android.view.Window; | |
import android.view.WindowManager; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
public class PVPPracticeActivity extends Activity { | |
protected boolean _active = true; | |
protected int _battleTime = 10000; // time to display in ms | |
protected int _secTime = 1000; | |
protected int _endWaitTime = 3000; // time to countdown in ms | |
private int damage = 0; | |
private TextView damageTextView; | |
private TextView damageDoneTV; | |
private boolean touchme = false; | |
private Vibrator v; | |
public AppStrings appState; | |
Handler myViewUpdateHandler = new Handler() { | |
public void handleMessage(Message msg) { | |
switch (msg.what) { | |
case 6: | |
showEndADB(); | |
break; | |
case 4: | |
damageTextView.setText("ST\nOP"); | |
damageDoneTV.setText(""); | |
break; | |
case 3: | |
damageTextView.setText("3"); | |
damageDoneTV.setText("Get Ready..."); | |
break; | |
case 2: | |
damageTextView.setText("2"); | |
damageDoneTV.setText("Get Set..."); | |
break; | |
case 1: | |
damageTextView.setText("1"); | |
damageDoneTV.setText("GO!"); | |
break; | |
case 5: | |
damageTextView.setText("0"); | |
damageDoneTV.setText("damage done"); | |
break; | |
/*default: | |
damageTextView.setText(""); | |
break;*/ | |
} | |
super.handleMessage(msg); | |
} | |
}; | |
Thread timerThread = new Thread() { | |
@Override | |
public void run() { | |
try { | |
int waited = 0; | |
touchme = false; | |
Message m = new Message(); | |
m.what = 3; | |
PVPPracticeActivity.this.myViewUpdateHandler.sendMessage(m); | |
while (_active && (waited < _secTime)) { | |
sleep(100); | |
if (_active) { | |
waited += 100; | |
} | |
} | |
Message m2 = new Message(); | |
m2.what = 2; | |
PVPPracticeActivity.this.myViewUpdateHandler.sendMessage(m2); | |
waited = 0; | |
while (_active && (waited < _secTime)) { | |
sleep(100); | |
if (_active) { | |
waited += 100; | |
} | |
} | |
Message m3 = new Message(); | |
m3.what = 1; | |
PVPPracticeActivity.this.myViewUpdateHandler.sendMessage(m3); | |
waited = 0; | |
while (_active && (waited < _secTime)) { | |
sleep(100); | |
if (_active) { | |
waited += 100; | |
} | |
} | |
Message m4 = new Message(); | |
m4.what = 5; | |
PVPPracticeActivity.this.myViewUpdateHandler.sendMessage(m4); | |
waited = 0; | |
touchme = true; | |
while (_active && (waited < _battleTime)) { | |
sleep(100); | |
if (_active) { | |
waited += 100; | |
} | |
} | |
touchme = false; | |
Message m5 = new Message(); | |
m5.what = 4; | |
PVPPracticeActivity.this.myViewUpdateHandler.sendMessage(m5); | |
waited = 0; | |
while (_active && (waited < _endWaitTime)) { | |
sleep(100); | |
if (_active) { | |
waited += 100; | |
} | |
} | |
Message m6 = new Message(); | |
m6.what = 6; | |
PVPPracticeActivity.this.myViewUpdateHandler.sendMessage(m6); | |
} catch (InterruptedException e) { | |
// do nothing | |
} | |
} | |
}; | |
/** Called when the activity is first created. */ | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
// activity stuff | |
super.onCreate(savedInstanceState); | |
// Set to full screen mode to hide notification bar | |
requestWindowFeature(Window.FEATURE_NO_TITLE); | |
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, | |
WindowManager.LayoutParams.FLAG_FULLSCREEN); | |
touchme = false; | |
appState = ((AppStrings) getApplicationContext()); | |
setContentView(R.layout.pvpattack); | |
// Set up the Intent receiver for logging out | |
IntentFilter intentFilter = new IntentFilter(); | |
intentFilter.addAction("com.doomonafireball.ACTION_LOGOUT"); | |
registerReceiver(new BroadcastReceiver() { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
Log.d("onReceive", "Logout in progress"); | |
finish(); | |
} | |
}, intentFilter); | |
v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); | |
damageTextView = (TextView) findViewById(R.id.textViewAttackNumber); | |
damageDoneTV = (TextView) findViewById(R.id.textViewDamageDoneTitle); | |
AlertDialog.Builder startADB = new AlertDialog.Builder( | |
PVPPracticeActivity.this); | |
startADB.setTitle("PVP Practice"); | |
startADB.setMessage("Try to get the highest number of taps in 10 seconds! The screen will count down from 3 (with tapping disabled), then you're free to start tapping."); | |
startADB.setPositiveButton("Start!", startListener); | |
startADB.show(); | |
} | |
private DialogInterface.OnClickListener startListener = new DialogInterface.OnClickListener() { | |
public void onClick(DialogInterface dialog, int which) { | |
// Start the game | |
timerThread.start(); | |
} | |
}; | |
private DialogInterface.OnClickListener restartListener = new DialogInterface.OnClickListener() { | |
public void onClick(DialogInterface dialog, int which) { | |
// Restart the game | |
finish(); | |
startActivity(new Intent( | |
"com.doomonafireball.irlhvz.PVPPracticeActivity")); | |
} | |
}; | |
private DialogInterface.OnClickListener leaderboardListener = new DialogInterface.OnClickListener() { | |
public void onClick(DialogInterface dialog, int which) { | |
// Go to the leaderboard | |
finish(); | |
startActivity(new Intent( | |
"com.doomonafireball.irlhvz.PVPLeaderboardActivity")); | |
} | |
}; | |
private DialogInterface.OnClickListener endListener = new DialogInterface.OnClickListener() { | |
public void onClick(DialogInterface dialog, int which) { | |
// Back to the Main Lobby | |
finish(); | |
startActivity(new Intent( | |
"com.doomonafireball.irlhvz.LobbyTabActivity")); | |
} | |
}; | |
private void showEndADB() { | |
// Send the score to the leaderboard. | |
sendScoreToLeaderboard(); | |
AlertDialog.Builder endADB = new AlertDialog.Builder( | |
PVPPracticeActivity.this); | |
endADB.setTitle("Congratulations!"); | |
endADB.setMessage("You tapped the screen " + damage | |
+ " times. Your score has been submitted to the leaderboard."); | |
endADB.setPositiveButton("Restart Practice", restartListener); | |
endADB.setNeutralButton("View Top 50", leaderboardListener); | |
endADB.setNegativeButton("Back to Lobby", endListener); | |
endADB.show(); | |
} | |
private void sendScoreToLeaderboard() { | |
// Contact the web service to submit this practice's score. | |
try { | |
String username = appState.getUsername(); | |
String score = Integer.toString(damage); | |
// queries the server and gets the response stream | |
URL query = new URL( | |
"http://plato.cs.virginia.edu/~dkb4j/webservices/check_with_leaderboard.php?user=" | |
+ username + "&score=" + score); | |
// open the stream | |
query.openStream(); | |
} catch (Exception e) { | |
// Toast for failure to connect to server | |
Toast errorToast = Toast.makeText(getApplicationContext(), | |
"Failed to connect to server to submit your score.", 3); | |
errorToast.setGravity(Gravity.CENTER_VERTICAL | |
| Gravity.CENTER_HORIZONTAL, 0, 0); | |
errorToast.show(); | |
} | |
} | |
@Override | |
public boolean onTouchEvent(MotionEvent event) { | |
if (touchme) { | |
// BAM! Multitouch! | |
int action = event.getAction(); | |
int actionCode = action & MotionEvent.ACTION_MASK; | |
if (action == MotionEvent.ACTION_DOWN) { | |
damage++; | |
v.vibrate(50); | |
damageTextView.setText(Integer.toString(damage)); | |
} else if (actionCode == MotionEvent.ACTION_POINTER_DOWN) { | |
damage++; | |
v.vibrate(50); | |
damageTextView.setText(Integer.toString(damage)); | |
} | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment