348 lines
14 KiB
Java
348 lines
14 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 achievementTitle;
|
|
RelativeLayout achievementLayout;
|
|
TextView achievementArrow;
|
|
TextView achievementText;
|
|
TextView improveArrow;
|
|
RelativeLayout improveLayout;
|
|
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);
|
|
// TODO: pass the game to checkAchievements() to avoid crash on null error
|
|
// means we have to load Player.getInstance form here and load the game
|
|
// from given id to be save.
|
|
// checkAchievements();
|
|
} 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);
|
|
}
|
|
});
|
|
// try submitting images if not yet done.
|
|
player.api.sendHitImages(game.hits.values());
|
|
}
|
|
}
|
|
};
|
|
|
|
@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
|
|
// This happens when comming back.
|
|
// apparently back button does not acknowledge intent??
|
|
// finish(); // doesn't work?
|
|
}
|
|
|
|
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);
|
|
|
|
achievementTitle = (TextView) findViewById(R.id.achievementTitle);
|
|
achievementLayout = (RelativeLayout) findViewById(R.id.achievementLayout);
|
|
achievementArrow = (TextView) findViewById(R.id.achievementArrow);
|
|
achievementText = (TextView) findViewById(R.id.achievementText);
|
|
|
|
// improveLayout = (RelativeLayout) findViewById(R.id.improveLayout);
|
|
// 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);
|
|
|
|
nextLvlText.setVisibility(View.GONE);
|
|
nextLvlLayout.setVisibility(View.GONE);
|
|
|
|
checkAchievements();
|
|
|
|
Typeface font = Typeface.createFromAsset(getAssets(), "unifont-9.0.02.ttf");
|
|
lvlNameText.setTypeface(font);
|
|
overallScorePercText.setTypeface(font);
|
|
achievementArrow.setTypeface(font);
|
|
achievementText.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+game.bonus));
|
|
// 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);
|
|
}
|
|
});
|
|
|
|
|
|
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);
|
|
}
|
|
|
|
public void setNextLvlMessage(boolean hasAccess) {
|
|
nextLvlText.setVisibility(View.VISIBLE);
|
|
nextLvlLayout.setVisibility(View.VISIBLE);
|
|
if(hasAccess) {
|
|
if(game.scenario.isFinalLevel()) {
|
|
nextLvlText.setText("This was the final level! Let's see what the ending brings...");
|
|
nextLvlLayout.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
Intent intent = new Intent(getApplicationContext(), EndingActivity.class);
|
|
finish();
|
|
startActivity(intent);
|
|
}
|
|
});
|
|
} else {
|
|
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.setText(String.format("You need at least %1$.0f points, or unlock %2$d achievements to get to the next level.", game.scenario.minimumScore, game.scenario.minimumAchievements));
|
|
nextLvlArrow.setVisibility(View.GONE);
|
|
}
|
|
}
|
|
|
|
public void checkAchievements() {
|
|
if(game.achievements != null && game.achievements.size() > 0) {
|
|
achievementTitle.setVisibility(View.VISIBLE);
|
|
achievementLayout.setVisibility(View.VISIBLE);
|
|
|
|
String achievementString = game.achievements.size() > 1 ? "You've obtained special achievements!\n - " : "You've obtained the special achievement ";
|
|
boolean firstAchievement = true;
|
|
for(Achievement achievement: game.achievements) {
|
|
if(firstAchievement ) {
|
|
firstAchievement = false;
|
|
} else {
|
|
achievementString += "\n - ";
|
|
}
|
|
achievementString += String.format("%1$s", achievement.title);
|
|
}
|
|
achievementText.setText(achievementString);
|
|
achievementLayout.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
Intent intent = new Intent(getApplicationContext(), ReviewAchievementsActivity.class);
|
|
intent.putExtra(ReviewAchievementsActivity.INTENT_EXTRA_GAME_ID, game.id);
|
|
startActivity(intent);
|
|
}
|
|
});
|
|
} else {
|
|
achievementTitle.setVisibility(View.GONE);
|
|
achievementLayout.setVisibility(View.GONE);
|
|
}
|
|
|
|
// only check after sync has been done (so achievements is [] rather than null))
|
|
if(game.achievements != null) {
|
|
// check next level access, based on achievements:
|
|
int nextLevelId = game.scenario.getNextLevelId();
|
|
PlayerInfo playerInfo = player.getPlayerInfo();
|
|
// if s/he has access, there is nothing to check.. if not.. go ahead
|
|
if(!playerInfo.hasAccessToLevel(nextLevelId)) {
|
|
int lvlAchievementCount = player.getGameOpenHelper().countAchievementsForLevel(game.scenario.id);
|
|
if(lvlAchievementCount >= game.scenario.minimumAchievements) {
|
|
playerInfo.reachedLevelId = nextLevelId;
|
|
player.savePlayerInfo(playerInfo);
|
|
} else {
|
|
// only reveal message if there is results
|
|
setNextLvlMessage(false);
|
|
return;
|
|
}
|
|
} else {
|
|
if(game.scenario.isFinalLevel() && !playerInfo.completedAll) {
|
|
setNextLvlMessage(false);
|
|
} else{
|
|
setNextLvlMessage(true);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
}
|
|
}
|
|
}
|