Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
cd1b9ece7b
3 changed files with 88 additions and 10 deletions
|
@ -17,13 +17,13 @@ import org.secuso.privacyfriendlysudoku.game.GameType;
|
|||
/**
|
||||
* Created by TMZ_LToP on 19.11.2015.
|
||||
*/
|
||||
public class SaveLoadStatistics {
|
||||
public class SaveLoadStatistics implements ITimerListener {
|
||||
|
||||
|
||||
private static String FILE_EXTENSION = ".txt";
|
||||
private static String SAVE_PREFIX = "stat";
|
||||
private static String SAVES_DIR = "stats";
|
||||
|
||||
private GameController gc;
|
||||
Context context;
|
||||
private int numberOfArguents = 2;
|
||||
|
||||
|
@ -31,6 +31,39 @@ public class SaveLoadStatistics {
|
|||
public SaveLoadStatistics(Context context){
|
||||
this.context = context;
|
||||
}
|
||||
public void setGameController(GameController gc) {
|
||||
this.gc = gc;
|
||||
gc.registerTimerListener(this);
|
||||
}
|
||||
|
||||
public HighscoreInfoContainer loadStats(GameType t, GameDifficulty gd){
|
||||
File dir = context.getDir(SAVES_DIR, 0);
|
||||
HighscoreInfoContainer infos;
|
||||
byte[] bytes;
|
||||
FileInputStream inputStream;
|
||||
File file;
|
||||
file = new File(dir,SAVE_PREFIX+t.name()+"_"+gd.name()+FILE_EXTENSION);
|
||||
bytes = new byte[(int)file.length()];
|
||||
|
||||
try {
|
||||
inputStream = new FileInputStream(file);
|
||||
try {
|
||||
inputStream.read(bytes);
|
||||
}finally {
|
||||
inputStream.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
Log.e("Failed to read file","File could not be read");
|
||||
}
|
||||
infos = new HighscoreInfoContainer(t,gd);
|
||||
try {
|
||||
infos.setInfosFromFile(new String(bytes));
|
||||
} catch (IllegalArgumentException e){
|
||||
file.delete();
|
||||
}
|
||||
return infos;
|
||||
|
||||
}
|
||||
|
||||
public List<HighscoreInfoContainer> loadStats(GameType t) {
|
||||
File dir = context.getDir(SAVES_DIR, 0);
|
||||
|
@ -81,16 +114,49 @@ public class SaveLoadStatistics {
|
|||
|
||||
}
|
||||
|
||||
public void saveTime(GameDifficulty gd, GameType gameType) {
|
||||
//TODO: Increse time every second
|
||||
HighscoreInfoContainer infos = loadStats(gameType, gd);
|
||||
infos.incTime();
|
||||
saveContainer(infos,gd,gameType);
|
||||
|
||||
public void saveGameStats(GameController gameController) {
|
||||
|
||||
}
|
||||
public void saveHints(GameDifficulty gd, GameType gameType){
|
||||
HighscoreInfoContainer infos = loadStats(gameType,gd);
|
||||
infos.incHints();
|
||||
saveContainer(infos,gd,gameType);
|
||||
}
|
||||
|
||||
public void saveContainer (HighscoreInfoContainer infos, GameDifficulty gd, GameType t){
|
||||
|
||||
File dir = context.getDir(SAVES_DIR, 0);
|
||||
File file = new File(dir, SAVE_PREFIX+t.name()+"_"+gd.name()+FILE_EXTENSION);
|
||||
|
||||
|
||||
String stats = infos.getActualStats();
|
||||
try {
|
||||
FileOutputStream stream = new FileOutputStream(file);
|
||||
try {
|
||||
stream.write(stats.getBytes());
|
||||
} finally {
|
||||
stream.close();
|
||||
}
|
||||
} catch(IOException e) {
|
||||
Log.e("File Manager", "Could not save game. IOException occured.");
|
||||
}
|
||||
}
|
||||
|
||||
public void saveGameStats() {
|
||||
|
||||
if (gc == null) return;
|
||||
|
||||
HighscoreInfoContainer infoContainer = new HighscoreInfoContainer();
|
||||
|
||||
// Read existing stats
|
||||
File dir = context.getDir(SAVES_DIR, 0);
|
||||
|
||||
File file = new File(dir, SAVE_PREFIX+gameController.getGameType().name()+"_"+gameController.getDifficulty().name()+FILE_EXTENSION);
|
||||
File file = new File(dir, SAVE_PREFIX+gc.getGameType().name()+"_"+gc.getDifficulty().name()+FILE_EXTENSION);
|
||||
|
||||
|
||||
if (file.isFile()){
|
||||
|
@ -119,7 +185,7 @@ public class SaveLoadStatistics {
|
|||
}
|
||||
|
||||
//add stats of current game stats or create init stats
|
||||
infoContainer.add(gameController);
|
||||
infoContainer.add(gc);
|
||||
|
||||
String stats = infoContainer.getActualStats();
|
||||
try {
|
||||
|
@ -133,4 +199,10 @@ public class SaveLoadStatistics {
|
|||
Log.e("File Manager", "Could not save game. IOException occured.");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTick(int time) {
|
||||
saveTime(gc.getDifficulty(),gc.getGameType());
|
||||
//gc.getUsedHints();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,8 +33,8 @@ public class HighscoreInfoContainer {
|
|||
//add all wanted Game Stats
|
||||
difficulty = (difficulty== null) ? gc.getDifficulty() : difficulty;
|
||||
type = (type == null) ? gc.getGameType() : type;
|
||||
time += gc.getTime();
|
||||
numberOfHintsUsed += gc.getUsedHints();
|
||||
//time += gc.getTime();
|
||||
//numberOfHintsUsed += gc.getUsedHints();
|
||||
numberOfGames++;
|
||||
// min time is only minTime of games without hints used
|
||||
minTime = (gc.getUsedHints() == 0 && gc.getTime()< minTime) ? gc.getTime() : minTime;
|
||||
|
@ -42,6 +42,12 @@ public class HighscoreInfoContainer {
|
|||
timeNoHints = (gc.getUsedHints() == 0) ? timeNoHints + gc.getTime() : timeNoHints;
|
||||
|
||||
}
|
||||
public void incHints(){
|
||||
numberOfHintsUsed++;
|
||||
}
|
||||
public void incTime() {
|
||||
time++;
|
||||
}
|
||||
|
||||
public void setInfosFromFile(String s){
|
||||
if(s.isEmpty()) return;
|
||||
|
|
|
@ -53,6 +53,7 @@ public class GameActivity extends AppCompatActivity implements NavigationView.On
|
|||
TextView viewName ;
|
||||
RatingBar ratingBar;
|
||||
private boolean gameSolved = false;
|
||||
SaveLoadStatistics statistics = new SaveLoadStatistics(this);
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
|
@ -88,7 +89,7 @@ public class GameActivity extends AppCompatActivity implements NavigationView.On
|
|||
gameController = new GameController(sharedPref, getApplicationContext());
|
||||
gameController.registerGameSolvedListener(this);
|
||||
gameController.registerTimerListener(this);
|
||||
|
||||
statistics.setGameController(gameController);
|
||||
List<GameInfoContainer> loadableGames = GameStateManager.getLoadableGameList();
|
||||
|
||||
if(loadLevel && loadableGames.size() > loadLevelID) {
|
||||
|
@ -254,8 +255,7 @@ public class GameActivity extends AppCompatActivity implements NavigationView.On
|
|||
Toast t = Toast.makeText(this, R.string.win_text, Toast.LENGTH_SHORT);
|
||||
t.show();
|
||||
|
||||
SaveLoadStatistics s = new SaveLoadStatistics(this);
|
||||
s.saveGameStats(gameController);
|
||||
statistics.saveGameStats();
|
||||
|
||||
FragmentManager fr = getSupportFragmentManager();
|
||||
DialogWinScreen win = new DialogWinScreen();
|
||||
|
|
Loading…
Reference in a new issue