2016-09-04 01:03:22 +02:00
package com.rubenvandeven.emotionhero ;
2016-09-06 15:35:40 +02:00
import android.app.ProgressDialog ;
2016-09-04 01:03:22 +02:00
import android.content.Intent ;
import android.graphics.Typeface ;
2016-09-06 15:35:40 +02:00
import android.os.Handler ;
2016-09-04 01:03:22 +02:00
import android.support.v7.app.AppCompatActivity ;
import android.os.Bundle ;
import android.support.v7.widget.Toolbar ;
import android.util.Log ;
import android.view.MenuItem ;
2016-09-06 15:35:40 +02:00
import android.view.View ;
2016-09-04 01:03:22 +02:00
import android.view.WindowManager ;
2016-09-06 15:35:40 +02:00
import android.widget.ProgressBar ;
import android.widget.RelativeLayout ;
2016-09-04 01:03:22 +02:00
import android.widget.TextView ;
2016-09-06 15:35:40 +02:00
import android.widget.Toast ;
2016-09-04 01:03:22 +02:00
2016-09-06 15:35:40 +02:00
import com.loopj.android.http.JsonHttpResponseHandler ;
import org.json.JSONException ;
import org.json.JSONObject ;
2016-09-04 01:03:22 +02:00
import org.ocpsoft.prettytime.PrettyTime ;
2016-09-06 15:35:40 +02:00
import cz.msebera.android.httpclient.Header ;
2016-09-04 01:03:22 +02:00
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 ;
2016-09-06 15:35:40 +02:00
TextView lvlNameText ;
TextView dateText ;
TextView scoreText ;
TextView overallScorePercText ;
TextView overallScoreText ;
2016-09-10 16:51:34 +02:00
TextView achievementTitle ;
RelativeLayout achievementLayout ;
TextView achievementArrow ;
TextView achievementText ;
2016-09-06 15:35:40 +02:00
TextView improveArrow ;
2016-09-16 19:04:01 +02:00
RelativeLayout improveLayout ;
2016-09-06 15:35:40 +02:00
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 ) ;
2016-09-12 01:08:37 +02:00
// 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.
2016-09-16 19:04:01 +02:00
// checkAchievements();
2016-09-06 15:35:40 +02:00
} 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 ) ;
}
} ) ;
2016-09-15 16:23:38 +02:00
// try submitting images if not yet done.
player . api . sendHitImages ( game . hits . values ( ) ) ;
2016-09-06 15:35:40 +02:00
}
}
} ;
2016-09-04 01:03:22 +02:00
@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
2016-09-16 19:04:01 +02:00
// This happens when comming back.
// apparently back button does not acknowledge intent??
// finish(); // doesn't work?
2016-09-04 01:03:22 +02:00
}
2016-09-06 15:35:40 +02:00
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 ) ;
2016-09-04 01:03:22 +02:00
2016-09-10 16:51:34 +02:00
achievementTitle = ( TextView ) findViewById ( R . id . achievementTitle ) ;
achievementLayout = ( RelativeLayout ) findViewById ( R . id . achievementLayout ) ;
achievementArrow = ( TextView ) findViewById ( R . id . achievementArrow ) ;
achievementText = ( TextView ) findViewById ( R . id . achievementText ) ;
2016-09-16 19:04:01 +02:00
// improveLayout = (RelativeLayout) findViewById(R.id.improveLayout);
// improveArrow = (TextView) findViewById(R.id.improveArrow);
2016-09-06 15:35:40 +02:00
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 ) ;
2016-09-04 01:03:22 +02:00
2016-09-16 19:04:01 +02:00
nextLvlText . setVisibility ( View . GONE ) ;
nextLvlLayout . setVisibility ( View . GONE ) ;
2016-09-10 16:51:34 +02:00
checkAchievements ( ) ;
2016-09-04 01:03:22 +02:00
Typeface font = Typeface . createFromAsset ( getAssets ( ) , " unifont-9.0.02.ttf " ) ;
lvlNameText . setTypeface ( font ) ;
overallScorePercText . setTypeface ( font ) ;
2016-09-10 16:51:34 +02:00
achievementArrow . setTypeface ( font ) ;
2016-09-11 23:35:38 +02:00
achievementText . setTypeface ( font ) ;
2016-09-04 01:03:22 +02:00
retryArrow . setTypeface ( font ) ;
2016-09-16 19:04:01 +02:00
// improveArrow.setTypeface(font);
2016-09-06 15:35:40 +02:00
nextLvlArrow . setTypeface ( font ) ;
2016-09-04 01:03:22 +02:00
lvlNameText . setText ( " \" " + game . scenario . toString ( ) + " \" " ) ;
PrettyTime p = new PrettyTime ( ) ;
dateText . setText ( p . format ( game . time ) ) ;
2016-09-10 01:03:10 +02:00
scoreText . setText ( String . format ( " %1$.4f " , game . score + game . bonus ) ) ;
2016-09-06 15:35:40 +02:00
// 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 ) ;
}
} ) ;
2016-09-04 01:03:22 +02:00
2016-09-06 15:35:40 +02:00
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 ) ;
}
2016-09-16 19:04:01 +02:00
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 ) ;
}
}
2016-09-10 16:51:34 +02:00
public void checkAchievements ( ) {
2016-09-11 23:35:38 +02:00
if ( game . achievements ! = null & & game . achievements . size ( ) > 0 ) {
2016-09-10 16:51:34 +02:00
achievementTitle . setVisibility ( View . VISIBLE ) ;
achievementLayout . setVisibility ( View . VISIBLE ) ;
2016-09-16 19:04:01 +02:00
String achievementString = game . achievements . size ( ) > 1 ? " You've obtained special achievements! \ n - " : " You've obtained the special achievement " ;
2016-09-12 01:00:26 +02:00
boolean firstAchievement = true ;
2016-09-10 16:51:34 +02:00
for ( Achievement achievement : game . achievements ) {
2016-09-12 01:00:26 +02:00
if ( firstAchievement ) {
firstAchievement = false ;
} else {
2016-09-16 19:04:01 +02:00
achievementString + = " \ n - " ;
2016-09-10 16:51:34 +02:00
}
2016-09-11 23:35:38 +02:00
achievementString + = String . format ( " %1$s " , achievement . title ) ;
2016-09-10 16:51:34 +02:00
}
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 ) ;
}
2016-09-16 19:04:01 +02:00
// 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 ) ;
}
}
}
2016-09-10 16:51:34 +02:00
}
2016-09-06 15:35:40 +02:00
/ * *
* 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 " ;
}
2016-09-04 01:03:22 +02:00
}
@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 ) ;
}
}
}