diff --git a/app/src/main/java/com/rubenvandeven/emotionhero/GamingActivity.java b/app/src/main/java/com/rubenvandeven/emotionhero/GamingActivity.java index f504aba..b04fd44 100644 --- a/app/src/main/java/com/rubenvandeven/emotionhero/GamingActivity.java +++ b/app/src/main/java/com/rubenvandeven/emotionhero/GamingActivity.java @@ -2,6 +2,7 @@ package com.rubenvandeven.emotionhero; import android.Manifest; import android.annotation.TargetApi; +import android.content.Context; import android.content.pm.PackageManager; import android.media.AudioAttributes; import android.media.AudioManager; @@ -25,6 +26,11 @@ import com.affectiva.android.affdex.sdk.detector.CameraDetector; import com.affectiva.android.affdex.sdk.detector.Detector; import com.affectiva.android.affdex.sdk.detector.Face; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -35,6 +41,7 @@ import java.util.List; public class GamingActivity extends AppCompatActivity implements Detector.ImageListener, CameraDetector.CameraEventListener, Detector.FaceListener { final static String LOG_TAG = "EmotionHero"; + final static String HIGHSCORE_FILENAME = "highscores.json"; final int PERMISSIONS_REQUEST_CAMERA = 1; @@ -205,9 +212,7 @@ public class GamingActivity extends AppCompatActivity implements Detector.ImageL // if(!currentScenario.isWithinTime(timestamp)) if(currentScenario.isFinished()) { - setText("LEVEL ENDED"); - stopDetector(); - restartButton.setVisibility(View.VISIBLE); + finishLevel(); return; } if (list == null) @@ -318,4 +323,60 @@ public class GamingActivity extends AppCompatActivity implements Detector.ImageL protected void createOldSoundPool(){ sound = new SoundPool(5, AudioManager.STREAM_MUSIC,0); } + + public Highscores getHighscores() { + try{ + FileInputStream fis = openFileInput(HIGHSCORE_FILENAME); + StringBuilder builder = new StringBuilder(); + int ch; + while((ch = fis.read()) != -1){ + builder.append((char)ch); + } + return Highscores.fromJson(builder.toString()); + } catch (IOException e) { + return new Highscores(); + } + } + + public void addHighscore(Highscore score) { + Highscores scores = getHighscores(); + scores.add(score); + saveHighscores(scores); + } + + public void saveHighscores(Highscores scores) { + try { + FileOutputStream fos = openFileOutput(HIGHSCORE_FILENAME, Context.MODE_PRIVATE); + fos.write(scores.toJson().getBytes()); + fos.close(); + } catch(IOException e) { + // for now skip error + Log.e(LOG_TAG, "Could not save highscore!"); + } + } + + public void finishLevel() { + setText("LEVEL ENDED"); + stopDetector(); + restartButton.setVisibility(View.VISIBLE); + Highscores scores = getHighscores(); + Highscore score = currentScenario.getHighscore(); + + if(score.score == currentScenario.getMaxScore()) { + // Maximum SCORE!! + // You nailed it! + } if(scores.isEmpty()) { + // First play! Nice :-) + } + else if(scores.isHighest(score)) { + // HIGHSCORE!!!!! + } else { + // Better luck next time + } + + // show the highscores (top 3?)... + + scores.add(score); + saveHighscores(scores); + } } diff --git a/app/src/main/java/com/rubenvandeven/emotionhero/Highscores.java b/app/src/main/java/com/rubenvandeven/emotionhero/Highscores.java index 3571de5..5e2565c 100644 --- a/app/src/main/java/com/rubenvandeven/emotionhero/Highscores.java +++ b/app/src/main/java/com/rubenvandeven/emotionhero/Highscores.java @@ -26,23 +26,66 @@ public class Highscores extends ArrayList{ return gson.toJson(this); } + /** + * Check if given highscore is highest score in this set. + * @param score + * @return + */ + public boolean isHighest(Highscore score) { + for(Highscore s: this) { + if(s == score) // to allow comparison of items that are already in the set + continue; // skip if it wants to compare with self. + + if(s.score > score.score) { + return false; + } + } + return true; + } + /** * Get the n highest scores */ - public ArrayList getTopN(int n) { + public Highscores getTopN(int n) { Collections.sort(this, new Comparator() { @Override public int compare(Highscore score1, Highscore score2) { - return score1.score < score2.score ? -1 : score1.score == score2.score ? 0 : 1; + // return highest first + return score1.score > score2.score ? -1 : score1.score == score2.score ? 0 : 1; } }); - return this; + + Highscores scores = new Highscores(); + for (int i = 0; i < n; i++) { + scores.add(this.get(i)); + } + return scores; } -// public ArrayList getLastN(int n) { -// -// } + /** + * Get the n highest scores + */ + public Highscores getLast(Integer n) { + Collections.sort(this, new Comparator() { + @Override + public int compare(Highscore score1, Highscore score2) + { + // return newest first + return score1.time.before(score2.time) ? -1 : score1.time == score2.time ? 0 : 1; + } + }); + + if(n == null) { // no limit + return this; + } + + Highscores scores = new Highscores(); + for (int i = 0; i < n; i++) { + scores.add(this.get(i)); + } + return scores; + } } diff --git a/app/src/main/java/com/rubenvandeven/emotionhero/Scenario.java b/app/src/main/java/com/rubenvandeven/emotionhero/Scenario.java index e94519d..8e4a461 100644 --- a/app/src/main/java/com/rubenvandeven/emotionhero/Scenario.java +++ b/app/src/main/java/com/rubenvandeven/emotionhero/Scenario.java @@ -261,6 +261,18 @@ abstract public class Scenario { return runningTime > duration; } + public Highscore getHighscore() { + if(!isFinished()) { + return null; + } + + return new Highscore("Levelname!", getTotalScore()); + } + + public int getMaxScore() { + return targets.size() * 100; + } + // TODO: create a 'tick' that checks all current values with requirements and increases the timer etc // TODO: ... if scenario is running. This internal times makes it easier to pause etc. }