From a2c52f0891bc1b4d4fd8e9a9647567116a5c7793 Mon Sep 17 00:00:00 2001 From: uykek Date: Mon, 25 May 2020 16:53:34 +0200 Subject: [PATCH] Reserve maximum id to identify daily sudoku within app, add method which saves a sudoku to the daily sudoku data base using the current date as its id --- .../controller/GameController.java | 26 ++++++++++++++++++- .../ui/GameActivity.java | 8 +++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/GameController.java b/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/GameController.java index b400d60..f2bb7f0 100644 --- a/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/GameController.java +++ b/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/GameController.java @@ -6,6 +6,8 @@ import android.os.Handler; import android.os.Parcel; import android.os.Parcelable; +import org.secuso.privacyfriendlysudoku.controller.database.DatabaseHelper; +import org.secuso.privacyfriendlysudoku.controller.database.model.DailySudoku; import org.secuso.privacyfriendlysudoku.controller.helper.GameInfoContainer; import org.secuso.privacyfriendlysudoku.game.CellConflict; import org.secuso.privacyfriendlysudoku.game.CellConflictList; @@ -19,7 +21,10 @@ import org.secuso.privacyfriendlysudoku.game.listener.IHighlightChangedListener; import org.secuso.privacyfriendlysudoku.game.listener.IHintListener; import org.secuso.privacyfriendlysudoku.game.listener.IModelChangedListener; import org.secuso.privacyfriendlysudoku.game.listener.ITimerListener; +import org.secuso.privacyfriendlysudoku.ui.GameActivity; +import java.util.Calendar; +import java.util.Date; import java.util.LinkedList; import java.util.List; import java.util.Timer; @@ -32,6 +37,7 @@ import java.util.concurrent.atomic.AtomicBoolean; public class GameController implements IModelChangedListener, Parcelable { // General + public static final int DAILY_SUDOKU_ID = Integer.MAX_VALUE - 1; private SharedPreferences settings; // View @@ -305,7 +311,7 @@ public class GameController implements IModelChangedListener, Parcelable { SharedPreferences.Editor editor = settings.edit(); // is anyone ever gonna play so many levels? :) - if(gameID == Integer.MAX_VALUE-1) { + if(gameID == DAILY_SUDOKU_ID - 1) { editor.putInt("lastGameID", 1); } else { editor.putInt("lastGameID", gameID); @@ -318,6 +324,24 @@ public class GameController implements IModelChangedListener, Parcelable { fm.saveGameState(this); } + public void saveDailySudoku(Context context) { + int amountOfCells = size * size; + int[] encodedBoard = new int[amountOfCells]; + for (int i = 0; i < size; i++) { + for (int j = 0; j < size; j++) { + encodedBoard[i * size + j] = gameBoard.getCell(i, j).getValue(); + } + } + + Calendar currentDate = Calendar.getInstance(); + int id = currentDate.get(Calendar.DAY_OF_MONTH) * 1000000 + + (currentDate.get(Calendar.MONTH) + 1) * 10000 + currentDate.get(Calendar.YEAR); + + DatabaseHelper db = new DatabaseHelper(context); + DailySudoku dailySudoku = new DailySudoku(id, difficulty, gameType, encodedBoard, usedHints, GameActivity.timeToString(time)); + db.addDailySudoku(dailySudoku); + } + public void deleteGame(Context context) { if(gameID == 0) { throw new IllegalArgumentException("GameID may not be 0."); diff --git a/app/src/main/java/org/secuso/privacyfriendlysudoku/ui/GameActivity.java b/app/src/main/java/org/secuso/privacyfriendlysudoku/ui/GameActivity.java index 8b2bd2d..7481a83 100644 --- a/app/src/main/java/org/secuso/privacyfriendlysudoku/ui/GameActivity.java +++ b/app/src/main/java/org/secuso/privacyfriendlysudoku/ui/GameActivity.java @@ -369,6 +369,7 @@ public class GameActivity extends BaseActivity implements NavigationView.OnNavig } }); shareDialog.show(getFragmentManager(), "ShareDialogFragment"); + break; case R.id.nav_newgame: @@ -435,6 +436,11 @@ public class GameActivity extends BaseActivity implements NavigationView.OnNavig gameController.deleteGame(this); disableReset(); + //Save solved sudoku, if it happens to be a daily sudoku, to daily sudoku database + if(gameController.getGameID() == GameController.DAILY_SUDOKU_ID) { + gameController.saveDailySudoku(GameActivity.this); + } + //Show time hints new plus old best time statistics.saveGameStats(); @@ -480,7 +486,7 @@ public class GameActivity extends BaseActivity implements NavigationView.OnNavig specialButtonLayout.setButtonsEnabled(false); } - public String timeToString(int time) { + public static String timeToString(int time) { int seconds = time % 60; int minutes = ((time - seconds) / 60) % 60; int hours = (time - minutes - seconds) / (3600);