diff --git a/app/src/main/java/tu_darmstadt/sudoku/controller/SaveLoadStatistics.java b/app/src/main/java/tu_darmstadt/sudoku/controller/SaveLoadStatistics.java index 897f704..37709a4 100644 --- a/app/src/main/java/tu_darmstadt/sudoku/controller/SaveLoadStatistics.java +++ b/app/src/main/java/tu_darmstadt/sudoku/controller/SaveLoadStatistics.java @@ -16,17 +16,18 @@ import tu_darmstadt.sudoku.controller.helper.GameInfoContainer; import tu_darmstadt.sudoku.controller.helper.HighscoreInfoContainer; import tu_darmstadt.sudoku.game.GameDifficulty; import tu_darmstadt.sudoku.game.GameType; +import tu_darmstadt.sudoku.game.listener.ITimerListener; /** * 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; @@ -34,6 +35,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 loadStats(GameType t) { File dir = context.getDir(SAVES_DIR, 0); @@ -84,16 +118,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()){ @@ -122,7 +189,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 { @@ -136,4 +203,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(); + } } diff --git a/app/src/main/java/tu_darmstadt/sudoku/controller/helper/HighscoreInfoContainer.java b/app/src/main/java/tu_darmstadt/sudoku/controller/helper/HighscoreInfoContainer.java index e18d468..f8e53fb 100644 --- a/app/src/main/java/tu_darmstadt/sudoku/controller/helper/HighscoreInfoContainer.java +++ b/app/src/main/java/tu_darmstadt/sudoku/controller/helper/HighscoreInfoContainer.java @@ -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; diff --git a/app/src/main/java/tu_darmstadt/sudoku/ui/GameActivity.java b/app/src/main/java/tu_darmstadt/sudoku/ui/GameActivity.java index e3368aa..4bde355 100644 --- a/app/src/main/java/tu_darmstadt/sudoku/ui/GameActivity.java +++ b/app/src/main/java/tu_darmstadt/sudoku/ui/GameActivity.java @@ -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 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,"Congratulations you have solved the puzzle!", Toast.LENGTH_SHORT); t.show(); - SaveLoadStatistics s = new SaveLoadStatistics(this); - s.saveGameStats(gameController); + statistics.saveGameStats(); FragmentManager fr = getSupportFragmentManager(); DialogWinScreen win = new DialogWinScreen(); diff --git a/app/src/main/res/drawable/standalone_button.xml b/app/src/main/res/drawable/standalone_button.xml new file mode 100644 index 0000000..ba58220 --- /dev/null +++ b/app/src/main/res/drawable/standalone_button.xml @@ -0,0 +1,6 @@ + + + + + diff --git a/app/src/main/res/layout/activity_main_menu.xml b/app/src/main/res/layout/activity_main_menu.xml index 6e98d1d..be7c7e9 100644 --- a/app/src/main/res/layout/activity_main_menu.xml +++ b/app/src/main/res/layout/activity_main_menu.xml @@ -114,7 +114,7 @@ android:capitalize="none" android:clickable="false" android:elevation="10dp" - android:background="@drawable/mnenomic_numpad_button"/> + android:background="@drawable/standalone_button"/>