251 lines
9.9 KiB
Java
251 lines
9.9 KiB
Java
package com.rubenvandeven.emotionhero;
|
|
|
|
import android.app.ProgressDialog;
|
|
import android.content.Intent;
|
|
import android.graphics.Typeface;
|
|
import android.os.Handler;
|
|
import android.support.v7.app.AppCompatActivity;
|
|
import android.os.Bundle;
|
|
import android.support.v7.widget.Toolbar;
|
|
import android.util.Log;
|
|
import android.view.MenuItem;
|
|
import android.view.View;
|
|
import android.view.WindowManager;
|
|
import android.widget.ProgressBar;
|
|
import android.widget.RelativeLayout;
|
|
import android.widget.TextView;
|
|
import android.widget.Toast;
|
|
|
|
import com.loopj.android.http.JsonHttpResponseHandler;
|
|
|
|
import org.json.JSONException;
|
|
import org.json.JSONObject;
|
|
import org.ocpsoft.prettytime.PrettyTime;
|
|
|
|
import cz.msebera.android.httpclient.Header;
|
|
|
|
public class ReviewActivity extends AppCompatActivity {
|
|
|
|
|
|
public final static String INTENT_EXTRA_GAME_ID = "com.rubenvandeven.emotionhero.GAME_ID";
|
|
public final static String INTENT_EXTRA_FROM_GAME = "com.rubenvandeven.emotionhero.FROM_GAME";
|
|
|
|
|
|
protected Player player;
|
|
|
|
protected Game game;
|
|
/**
|
|
* Whether this activity is loaded from the game (else from highscores)
|
|
*/
|
|
protected boolean fromGame;
|
|
|
|
TextView lvlNameText;
|
|
TextView dateText;
|
|
TextView scoreText;
|
|
TextView overallScorePercText;
|
|
TextView overallScoreText;
|
|
|
|
TextView improveArrow;
|
|
TextView hintText;
|
|
RelativeLayout retryLayout;
|
|
TextView retryArrow;
|
|
TextView retryText;
|
|
|
|
RelativeLayout nextLvlLayout;
|
|
TextView nextLvlArrow;
|
|
TextView nextLvlText;
|
|
|
|
ProgressBar scoreProgressBar;
|
|
|
|
protected Runnable loadRemoteInfo = new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
// retry until remoteId is set.
|
|
if(game.remoteId == null) {
|
|
final Handler handler = new Handler();
|
|
handler.postDelayed(loadRemoteInfo,500);
|
|
return;
|
|
} else {
|
|
player.api.get("/games/"+game.remoteId, null, new JsonHttpResponseHandler(){
|
|
@Override
|
|
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
|
|
try {
|
|
JSONObject position = response.getJSONObject("position");
|
|
// overallScorePercText.setText(String.format("%1$.0f%%", position.getDouble("percentage")));
|
|
overallScoreText.setText(String.format("You beat %1$.0f%%", position.getDouble("percentage")));
|
|
overallScorePercText.setText(Integer.toString(position.getInt("position")));
|
|
if(response.has("hint"))
|
|
{
|
|
hintText.setText(response.getString("hint"));
|
|
}
|
|
position.getInt("position");
|
|
|
|
scoreProgressBar.setVisibility(View.GONE);
|
|
} catch (JSONException e) {
|
|
e.printStackTrace();
|
|
Toast.makeText(getApplicationContext(), "Something went wrong when loading results", Toast.LENGTH_LONG).show();
|
|
}
|
|
}
|
|
@Override
|
|
public void onFailure(int statusCode, Header[] headers, String response, Throwable throwable) {
|
|
throwable.printStackTrace();
|
|
Log.e("API", response == null ? "NULL" : response);
|
|
Toast.makeText(getApplicationContext(), "Something went wrong when loading results", Toast.LENGTH_LONG).show();
|
|
scoreProgressBar.setVisibility(View.GONE);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
};
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
|
setContentView(R.layout.activity_review);
|
|
|
|
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
|
|
setSupportActionBar(toolbar);
|
|
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
|
getSupportActionBar().setTitle("Score");
|
|
|
|
|
|
player = Player.getInstance(getApplicationContext());
|
|
|
|
// get game from Intent
|
|
long gameId = getIntent().getLongExtra(INTENT_EXTRA_GAME_ID, 0);
|
|
fromGame = getIntent().getBooleanExtra(INTENT_EXTRA_FROM_GAME, false);
|
|
Log.d("Review", "gameid:" + gameId);
|
|
if(gameId != 0) {
|
|
game = player.getGameOpenHelper().getGameByid(gameId);
|
|
if(game == null) {
|
|
Log.e("Highscore", "CANNOT FIND GAME!! " + gameId);
|
|
throw new RuntimeException("Cannot find specified game");
|
|
} else {
|
|
Log.i("Highscore", "FOUND GAME" + game.id + " " + game.score);
|
|
Log.i("Highscore", "RANK " + player.getGameOpenHelper().getLocalRankOfGame(game));
|
|
player.api.syncGame(game); // sync game if that is not already done
|
|
}
|
|
} else {
|
|
// no game specified??? SHould not be possible so finish
|
|
finish();
|
|
}
|
|
|
|
lvlNameText = (TextView) findViewById(R.id.lvlNameText);
|
|
dateText = (TextView) findViewById(R.id.dateText);
|
|
scoreText= (TextView) findViewById(R.id.scoreText);
|
|
overallScorePercText= (TextView) findViewById(R.id.overallScorePercText);
|
|
overallScoreText= (TextView) findViewById(R.id.overallScoreText);
|
|
|
|
improveArrow = (TextView) findViewById(R.id.improveArrow);
|
|
hintText = (TextView) findViewById(R.id.hintText);
|
|
retryLayout = (RelativeLayout) findViewById(R.id.retryLayout);
|
|
retryArrow = (TextView) findViewById(R.id.retryArrow);
|
|
retryText = (TextView) findViewById(R.id.retryText);
|
|
|
|
nextLvlLayout = (RelativeLayout) findViewById(R.id.nextLvlLayout);
|
|
nextLvlArrow = (TextView) findViewById(R.id.nextLvlArrow);
|
|
nextLvlText = (TextView) findViewById(R.id.nextLvlText);
|
|
|
|
scoreProgressBar = (ProgressBar) findViewById(R.id.scoreProgressBar);
|
|
|
|
|
|
Typeface font = Typeface.createFromAsset(getAssets(), "unifont-9.0.02.ttf");
|
|
lvlNameText.setTypeface(font);
|
|
overallScorePercText.setTypeface(font);
|
|
retryArrow.setTypeface(font);
|
|
improveArrow.setTypeface(font);
|
|
nextLvlArrow.setTypeface(font);
|
|
|
|
lvlNameText.setText("\""+game.scenario.toString()+"\"");
|
|
PrettyTime p = new PrettyTime();
|
|
dateText.setText(p.format(game.time));
|
|
scoreText.setText(String.format("%1$.4f", game.score));
|
|
// overallScorePercText.setText(String.format("%1$.0f%%", 30f));
|
|
|
|
loadRemoteInfo.run();
|
|
|
|
retryLayout.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
Intent intent = new Intent(getApplicationContext(), GamingActivity.class);
|
|
intent.putExtra(GamingActivity.INTENT_EXTRA_SCENARIO, game.scenario.id);
|
|
ProgressDialog dialog = ProgressDialog.show(ReviewActivity.this, "",
|
|
getApplicationContext().getResources().getString(R.string.load_game_activity), true);
|
|
finish();
|
|
startActivity(intent);
|
|
}
|
|
});
|
|
|
|
if(player.getPlayerInfo().hasAccessToLevel(game.scenario.getNextLevelId())) {
|
|
nextLvlLayout.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
Intent intent = new Intent(getApplicationContext(), GamingActivity.class);
|
|
intent.putExtra(GamingActivity.INTENT_EXTRA_SCENARIO, game.scenario.getNextLevelId());
|
|
ProgressDialog dialog = ProgressDialog.show(ReviewActivity.this, "",
|
|
getApplicationContext().getResources().getString(R.string.load_game_activity), true);
|
|
finish();
|
|
startActivity(intent);
|
|
}
|
|
});
|
|
} else {
|
|
nextLvlText.setVisibility(View.GONE);
|
|
nextLvlLayout.setVisibility(View.GONE);
|
|
}
|
|
|
|
|
|
int rank = player.getGameOpenHelper().getLocalRankOfGame(game);
|
|
String personalScoreJudgement = rank < 7 ? "nice work!" : "you can do better.";
|
|
String retryString = "Your " + rank + intToPosition(rank) + " score, " + personalScoreJudgement + " Now, retry and improve your performance.";
|
|
retryText.setText(retryString);
|
|
}
|
|
|
|
/**
|
|
* Translate 1 => 1st, 2 => 2nd etc.
|
|
* Thanks to http://stackoverflow.com/a/23182005
|
|
* @param number
|
|
* @return
|
|
*/
|
|
public static String intToPosition(int number) {
|
|
String value = String.valueOf(number);
|
|
if(value.length() > 1) {
|
|
// Check for special case: 11 - 13 are all "th".
|
|
// So if the second to last digit is 1, it is "th".
|
|
char secondToLastDigit = value.charAt(value.length()-2);
|
|
if(secondToLastDigit == '1')
|
|
return "th";
|
|
}
|
|
char lastDigit = value.charAt(value.length()-1);
|
|
switch(lastDigit) {
|
|
case '1':
|
|
return "st";
|
|
case '2':
|
|
return "nd";
|
|
case '3':
|
|
return "rd";
|
|
default:
|
|
return "th";
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public boolean onOptionsItemSelected(MenuItem menuItem)
|
|
{
|
|
switch (menuItem.getItemId()) {
|
|
case android.R.id.home:
|
|
if(fromGame) {
|
|
Intent intent = new Intent(getApplicationContext(), HighscoreActivity.class);
|
|
intent.putExtra(HighscoreActivity.INTENT_EXTRA_GAME_ID, game.id);
|
|
finish();
|
|
startActivity(intent);
|
|
} else {
|
|
onBackPressed();
|
|
}
|
|
return true;
|
|
default:
|
|
return super.onOptionsItemSelected(menuItem);
|
|
}
|
|
}
|
|
}
|