timer now always counts in stats
This commit is contained in:
parent
01b3e37ce1
commit
8b98f2531d
5 changed files with 97 additions and 12 deletions
|
@ -16,17 +16,18 @@ import tu_darmstadt.sudoku.controller.helper.GameInfoContainer;
|
||||||
import tu_darmstadt.sudoku.controller.helper.HighscoreInfoContainer;
|
import tu_darmstadt.sudoku.controller.helper.HighscoreInfoContainer;
|
||||||
import tu_darmstadt.sudoku.game.GameDifficulty;
|
import tu_darmstadt.sudoku.game.GameDifficulty;
|
||||||
import tu_darmstadt.sudoku.game.GameType;
|
import tu_darmstadt.sudoku.game.GameType;
|
||||||
|
import tu_darmstadt.sudoku.game.listener.ITimerListener;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by TMZ_LToP on 19.11.2015.
|
* 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 FILE_EXTENSION = ".txt";
|
||||||
private static String SAVE_PREFIX = "stat";
|
private static String SAVE_PREFIX = "stat";
|
||||||
private static String SAVES_DIR = "stats";
|
private static String SAVES_DIR = "stats";
|
||||||
|
private GameController gc;
|
||||||
Context context;
|
Context context;
|
||||||
private int numberOfArguents = 2;
|
private int numberOfArguents = 2;
|
||||||
|
|
||||||
|
@ -34,6 +35,39 @@ public class SaveLoadStatistics {
|
||||||
public SaveLoadStatistics(Context context){
|
public SaveLoadStatistics(Context context){
|
||||||
this.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) {
|
public List<HighscoreInfoContainer> loadStats(GameType t) {
|
||||||
File dir = context.getDir(SAVES_DIR, 0);
|
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();
|
HighscoreInfoContainer infoContainer = new HighscoreInfoContainer();
|
||||||
|
|
||||||
// Read existing stats
|
// Read existing stats
|
||||||
File dir = context.getDir(SAVES_DIR, 0);
|
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()){
|
if (file.isFile()){
|
||||||
|
@ -122,7 +189,7 @@ public class SaveLoadStatistics {
|
||||||
}
|
}
|
||||||
|
|
||||||
//add stats of current game stats or create init stats
|
//add stats of current game stats or create init stats
|
||||||
infoContainer.add(gameController);
|
infoContainer.add(gc);
|
||||||
|
|
||||||
String stats = infoContainer.getActualStats();
|
String stats = infoContainer.getActualStats();
|
||||||
try {
|
try {
|
||||||
|
@ -136,4 +203,10 @@ public class SaveLoadStatistics {
|
||||||
Log.e("File Manager", "Could not save game. IOException occured.");
|
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
|
//add all wanted Game Stats
|
||||||
difficulty = (difficulty== null) ? gc.getDifficulty() : difficulty;
|
difficulty = (difficulty== null) ? gc.getDifficulty() : difficulty;
|
||||||
type = (type == null) ? gc.getGameType() : type;
|
type = (type == null) ? gc.getGameType() : type;
|
||||||
time += gc.getTime();
|
//time += gc.getTime();
|
||||||
numberOfHintsUsed += gc.getUsedHints();
|
//numberOfHintsUsed += gc.getUsedHints();
|
||||||
numberOfGames++;
|
numberOfGames++;
|
||||||
// min time is only minTime of games without hints used
|
// min time is only minTime of games without hints used
|
||||||
minTime = (gc.getUsedHints() == 0 && gc.getTime()< minTime) ? gc.getTime() : minTime;
|
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;
|
timeNoHints = (gc.getUsedHints() == 0) ? timeNoHints + gc.getTime() : timeNoHints;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
public void incHints(){
|
||||||
|
numberOfHintsUsed++;
|
||||||
|
}
|
||||||
|
public void incTime() {
|
||||||
|
time++;
|
||||||
|
}
|
||||||
|
|
||||||
public void setInfosFromFile(String s){
|
public void setInfosFromFile(String s){
|
||||||
if(s.isEmpty()) return;
|
if(s.isEmpty()) return;
|
||||||
|
|
|
@ -53,6 +53,7 @@ public class GameActivity extends AppCompatActivity implements NavigationView.On
|
||||||
TextView viewName ;
|
TextView viewName ;
|
||||||
RatingBar ratingBar;
|
RatingBar ratingBar;
|
||||||
private boolean gameSolved = false;
|
private boolean gameSolved = false;
|
||||||
|
SaveLoadStatistics statistics = new SaveLoadStatistics(this);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
@ -88,7 +89,7 @@ public class GameActivity extends AppCompatActivity implements NavigationView.On
|
||||||
gameController = new GameController(sharedPref, getApplicationContext());
|
gameController = new GameController(sharedPref, getApplicationContext());
|
||||||
gameController.registerGameSolvedListener(this);
|
gameController.registerGameSolvedListener(this);
|
||||||
gameController.registerTimerListener(this);
|
gameController.registerTimerListener(this);
|
||||||
|
statistics.setGameController(gameController);
|
||||||
List<GameInfoContainer> loadableGames = GameStateManager.getLoadableGameList();
|
List<GameInfoContainer> loadableGames = GameStateManager.getLoadableGameList();
|
||||||
|
|
||||||
if(loadLevel && loadableGames.size() > loadLevelID) {
|
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);
|
Toast t = Toast.makeText(this,"Congratulations you have solved the puzzle!", Toast.LENGTH_SHORT);
|
||||||
t.show();
|
t.show();
|
||||||
|
|
||||||
SaveLoadStatistics s = new SaveLoadStatistics(this);
|
statistics.saveGameStats();
|
||||||
s.saveGameStats(gameController);
|
|
||||||
|
|
||||||
FragmentManager fr = getSupportFragmentManager();
|
FragmentManager fr = getSupportFragmentManager();
|
||||||
DialogWinScreen win = new DialogWinScreen();
|
DialogWinScreen win = new DialogWinScreen();
|
||||||
|
|
6
app/src/main/res/drawable/standalone_button.xml
Normal file
6
app/src/main/res/drawable/standalone_button.xml
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"
|
||||||
|
>
|
||||||
|
<gradient android:endColor="@color/colorPrimary" android:startColor="@color/colorPrimary" />
|
||||||
|
<corners android:radius="6dp" />
|
||||||
|
</shape>
|
|
@ -114,7 +114,7 @@
|
||||||
android:capitalize="none"
|
android:capitalize="none"
|
||||||
android:clickable="false"
|
android:clickable="false"
|
||||||
android:elevation="10dp"
|
android:elevation="10dp"
|
||||||
android:background="@drawable/mnenomic_numpad_button"/>
|
android:background="@drawable/standalone_button"/>
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
android:textColor="@color/white"
|
android:textColor="@color/white"
|
||||||
|
@ -131,7 +131,7 @@
|
||||||
android:onClick="onClick"
|
android:onClick="onClick"
|
||||||
android:capitalize="none"
|
android:capitalize="none"
|
||||||
android:clickable="false"
|
android:clickable="false"
|
||||||
android:background="@drawable/mnenomic_numpad_button"/>
|
android:background="@drawable/standalone_button"/>
|
||||||
<Button
|
<Button
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
|
|
Loading…
Reference in a new issue