93 lines
2.3 KiB
Java
93 lines
2.3 KiB
Java
package com.rubenvandeven.emotionhero;
|
|
|
|
import android.util.Log;
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.UUID;
|
|
|
|
/**
|
|
* Created by ruben on 20/08/16.
|
|
*/
|
|
|
|
public class PlayerInfo {
|
|
public Map<Integer, ScoreList> levelScores = new HashMap<>();
|
|
public int reachedLevelId = -1;
|
|
public boolean completedAll = false;
|
|
|
|
public ScoreList getScoresForLevel(int lvl_id) {
|
|
ScoreList scoreList;
|
|
if(!levelScores.containsKey(lvl_id)) {
|
|
scoreList = new ScoreList();
|
|
levelScores.put(lvl_id, scoreList);
|
|
} else {
|
|
scoreList = levelScores.get(lvl_id);
|
|
}
|
|
return scoreList;
|
|
}
|
|
|
|
/**
|
|
* The individual score object contains the level id, so we can fetch the corresponding
|
|
* scorelist to add this score
|
|
* @param score
|
|
*/
|
|
public void addScore(Score score) {
|
|
ScoreList scoreList = getScoresForLevel(score.lvl_id);
|
|
scoreList.add(score);
|
|
}
|
|
|
|
public static PlayerInfo fromJson(String json) {
|
|
Gson gson = new Gson();
|
|
return gson.fromJson(json, PlayerInfo.class);
|
|
}
|
|
|
|
public String toJson() {
|
|
Gson gson = new Gson();
|
|
String json = gson.toJson(this);
|
|
Log.i("PlayerInfo", "Generated: "+json);
|
|
return json;
|
|
}
|
|
|
|
/**
|
|
* Whether the reachedLevelId is higher than the first level
|
|
* used to show 'continue' button in menu
|
|
* @return
|
|
*/
|
|
public boolean canContinueLevel() {
|
|
if(reachedLevelId < 0)
|
|
return false;
|
|
|
|
if(Scenario.SCENARIOS.indexOf(reachedLevelId) > 0) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public boolean hasAccessToLevel(int lvl_id) {
|
|
if(lvl_id == reachedLevelId) {
|
|
return true;
|
|
}
|
|
// always access to first level (where would one otherwise begin?)
|
|
if(Scenario.SCENARIOS.get(0) == lvl_id) {
|
|
return true;
|
|
}
|
|
|
|
return Scenario.isHigherLevel(reachedLevelId, lvl_id);
|
|
}
|
|
|
|
public Score getScore(UUID score_id) {
|
|
for(ScoreList scores: levelScores.values()) {
|
|
for(Score score: scores) {
|
|
if(score.id != null && score.id.equals(score_id)) {
|
|
return score;
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
}
|