Skip to content
Snippets Groups Projects
Commit 71f63a88 authored by Drahomír Karchňák's avatar Drahomír Karchňák
Browse files

#93 Saving/Loading last map to db and cycling through maps even after restart...

parent 6fce705f
No related branches found
No related tags found
No related merge requests found
......@@ -28,6 +28,7 @@ import cz.nic.tablexia.game.games.pursuit.helper.GameRulesHelper;
import cz.nic.tablexia.game.games.pursuit.helper.TextureHelper;
import cz.nic.tablexia.game.games.pursuit.listener.VehicleDragListener;
import cz.nic.tablexia.game.games.pursuit.model.Grid;
import cz.nic.tablexia.game.games.pursuit.model.Maps;
import cz.nic.tablexia.game.games.pursuit.model.PuzzlePiece;
import cz.nic.tablexia.game.games.pursuit.model.Vehicle;
import cz.nic.tablexia.game.games.pursuit.model.VehicleType;
......@@ -103,10 +104,11 @@ public class PursuitGame extends AbstractTablexiaGame<int[][]> {
private static final String SUMMARY_TEXT_TIME = "victory_text_time";
private static final String SCORE_KEY_MOVE_COUNT = "move_count";
private static final String SCORE_KEY_LAST_MAP = "last_map_index";
private static final float OVERLAP_PIECE_SCALE = 1.2f;
private static final int MAPS_COUNT = 14;
private static final int MAPS_COUNT = Maps.values().length;
private Group contentGroup;
private Grid grid;
......@@ -115,7 +117,7 @@ public class PursuitGame extends AbstractTablexiaGame<int[][]> {
private TablexiaNoBlendingImage backgroundImage;
private DragAndRotateActorListener dragAndRotateActorListener;
private int movesCounter;
private int mapNumber = 0;//TODO automatically load last played map index +1
private int mapNumber = 0;
private Vehicle vehicle;
private Image finishFlag;
......@@ -154,7 +156,9 @@ public class PursuitGame extends AbstractTablexiaGame<int[][]> {
@Override
protected int[][] prepareGameData(Map<String, String> gameState) {
roadMap = getScreenTextureRegion(TextureHelper.getClickMapPath(mapNumber));
updateMapIndex();
roadMap = getScreenTextureRegion(TextureHelper.getClickMapPath(mapNumber));
if (!roadMap.getTexture().getTextureData().isPrepared()) {
roadMap.getTexture().getTextureData().prepare();
}
......@@ -262,6 +266,24 @@ public class PursuitGame extends AbstractTablexiaGame<int[][]> {
backgroundImage.addListener(dragAndRotateActorListener);
}
private void updateMapIndex() {
String lastMap = getGame().getLastGameScoreValueForGameAndKey(GameDefinition.PURSUIT.getGameNumber(), SCORE_KEY_LAST_MAP);
//couldn't find the last map
if(lastMap == null) {
mapNumber = 0;
}
else {
mapNumber = Integer.parseInt(lastMap);
}
//To loop over maps
mapNumber = (mapNumber + 1) % MAPS_COUNT;
//Update last map index in db
setGameScore(SCORE_KEY_LAST_MAP, mapNumber);
}
private void prepareBackground() {
backgroundImage = new TablexiaNoBlendingImage(getScreenTextureRegion(TextureHelper.getBgTexturePath()));
backgroundImage.setFillParent(true);
......@@ -288,7 +310,6 @@ public class PursuitGame extends AbstractTablexiaGame<int[][]> {
@Override
public void onVehicleDragComplete() {
showVictoryDialog();
mapNumber = (mapNumber + 1) % MAPS_COUNT;
//remove all actor listeners
vehicle.clear();
vehicle.remove();
......
......@@ -12,6 +12,7 @@ import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import cz.nic.tablexia.TablexiaSettings;
import cz.nic.tablexia.TablexiaStorage;
import cz.nic.tablexia.game.AbstractTablexiaGame;
import cz.nic.tablexia.game.GameDefinition;
......@@ -141,6 +142,10 @@ public class Game {
return selectGameForId(id);
}
public static String getLastGameScoreValueForGameAndKey(int gameNumber, String key) {
return selectLastGameScoreValue(TablexiaSettings.getInstance().getSelectedUser().getId(), gameNumber, key);
}
public static List<Game> getGamesForUserAndDefinition(Long userId, GameDefinition definition) {
return selectGamesForUserAndDefinition(userId, definition);
}
......@@ -226,7 +231,8 @@ public class Game {
public static final String GAME_SELECT_COUNT_FOR_GAME_AND_DIFFICULTY = "SELECT count(id) FROM game WHERE game_number = ? AND difficulty_number = ? AND user_id = ? AND end_time IS NOT NULL";
public static final String GAME_SELECT_FOR_USER_AND_DEFINITION = "SELECT id, user_id, difficulty_number, game_number, random_seed, start_time, end_time FROM game WHERE user_id = ? AND game_number = ? AND end_time IS NOT NULL";
public static final String GAME_SELECT_COUNT_FOR_GAME = "SELECT count(id) FROM game WHERE game_number = ? AND user_id = ?";
public static final String GAME_SELECT_LAST_SCORES_FOR_USER_AND_GAME = "SELECT value FROM game_score INNER JOIN game ON game_score.game_id=game.id AND game.user_id=? AND game.game_number=? WHERE game_score.key=? AND end_time IS NOT NULL ORDER BY game_id DESC LIMIT 1";
private static Long insertNewGame(User user, GameDifficulty difficulty, GameDefinition gameDefinition, TablexiaRandom random) {
try {
PreparedStatement insertStatement = TablexiaStorage.getInstance().prepareStatement(NEW_GAME_INSERT);
......@@ -244,6 +250,28 @@ public class Game {
return null;
}
private static String selectLastGameScoreValue(long userId, int gameNumber, String key) {
String result = null;
PreparedStatement statement = TablexiaStorage.getInstance().prepareStatement(GAME_SELECT_LAST_SCORES_FOR_USER_AND_GAME);
try {
//Set userId, gameNumber and key
statement.setLong(1, userId);
statement.setLong(2, gameNumber);
statement.setString(3, key);
ResultSet resultSet = statement.executeQuery();
while(resultSet.next()) {
resultSet.close();
result = resultSet.getString(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
private static Game selectGameForId(long id) {
Game selectedGame = null;
try {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment