Search in sources :

Example 6 with Game

use of com.nolanlawson.keepscore.db.Game in project KeepScore by nolanlawson.

the class GamesBackupSerializer method serializeAsRawXml.

private static String serializeAsRawXml(GamesBackup gamesBackup) {
    XmlSerializer serializer = Xml.newSerializer();
    StringWriter writer = new StringWriter();
    try {
        serializer.setOutput(writer);
        serializer.startDocument("UTF-8", true);
        serializer.startTag("", Tag.GamesBackup.name());
        addTag(serializer, Tag.gameCount, gamesBackup.getGameCount());
        addTag(serializer, Tag.version, gamesBackup.getVersion());
        addTag(serializer, Tag.automatic, gamesBackup.isAutomatic());
        addTag(serializer, Tag.backupFilename, gamesBackup.getFilename());
        addTag(serializer, Tag.dateBackupSaved, gamesBackup.getDateSaved());
        serializer.startTag("", Tag.Games.name());
        for (Game game : gamesBackup.getGames()) {
            serializer.startTag("", Tag.Game.name());
            addTag(serializer, Tag.dateGameSaved, game.getDateSaved());
            addTag(serializer, Tag.dateGameStarted, game.getDateStarted());
            addTag(serializer, Tag.gameName, game.getName());
            serializer.startTag("", Tag.PlayerScores.name());
            for (PlayerScore playerScore : game.getPlayerScores()) {
                serializer.startTag("", Tag.PlayerScore.name());
                addTag(serializer, Tag.playerName, playerScore.getName());
                addTag(serializer, Tag.score, playerScore.getScore());
                addTag(serializer, Tag.playerNumber, playerScore.getPlayerNumber());
                Pair<String, String> historyAsStrings = Delta.toJoinedStrings(playerScore.getHistory());
                addTag(serializer, Tag.history, historyAsStrings.getFirst());
                addTag(serializer, Tag.historyTimestamps, historyAsStrings.getSecond());
                addTag(serializer, Tag.lastUpdate, Long.toString(playerScore.getLastUpdate()));
                addTag(serializer, Tag.color, PlayerColor.serialize(playerScore.getPlayerColor()));
                serializer.endTag("", Tag.PlayerScore.name());
            }
            serializer.endTag("", Tag.PlayerScores.name());
            serializer.endTag("", Tag.Game.name());
        }
        serializer.endTag("", Tag.Games.name());
        serializer.endTag("", Tag.GamesBackup.name());
        serializer.endDocument();
        return writer.toString();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Game(com.nolanlawson.keepscore.db.Game) StringWriter(java.io.StringWriter) IOException(java.io.IOException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) XmlSerializer(org.xmlpull.v1.XmlSerializer) PlayerScore(com.nolanlawson.keepscore.db.PlayerScore)

Example 7 with Game

use of com.nolanlawson.keepscore.db.Game in project KeepScore by nolanlawson.

the class SerializationTest method createRandomGamesBackup.

private GamesBackup createRandomGamesBackup() {
    GamesBackup gamesBackup = new GamesBackup();
    gamesBackup.setGames(new ArrayList<Game>());
    gamesBackup.setDateSaved(2437934297L);
    gamesBackup.setVersion(GamesBackupSerializer.CURRENT_VERSION);
    int numGames = random.nextInt(10) + 2;
    for (int i = 0; i < numGames; i++) {
        Game game = new Game();
        game.setDateSaved(random.nextLong());
        game.setDateStarted(random.nextLong());
        game.setName(Long.toString(random.nextLong(), 16));
        game.setPlayerScores(createRandomPlayerScores());
        gamesBackup.getGames().add(game);
    }
    gamesBackup.setGameCount(gamesBackup.getGames().size());
    return gamesBackup;
}
Also used : Game(com.nolanlawson.keepscore.db.Game) GamesBackup(com.nolanlawson.keepscore.serialization.GamesBackup)

Example 8 with Game

use of com.nolanlawson.keepscore.db.Game in project KeepScore by nolanlawson.

the class MainActivity method copyGame.

private void copyGame(Game game, final boolean resetScores) {
    final Game newGame = game.makeCleanCopy();
    if (resetScores) {
        for (PlayerScore playerScore : newGame.getPlayerScores()) {
            playerScore.setScore(PreferenceHelper.getIntPreference(R.string.CONSTANT_pref_initial_score, R.string.CONSTANT_pref_initial_score_default, this));
            playerScore.setHistory(new ArrayList<Delta>());
        }
    }
    new AsyncTask<Void, Void, Void>() {

        @Override
        protected Void doInBackground(Void... params) {
            GameDBHelper dbHelper = null;
            try {
                dbHelper = new GameDBHelper(MainActivity.this);
                dbHelper.saveGame(newGame);
            } finally {
                if (dbHelper != null) {
                    dbHelper.close();
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            onNewGameCreated(newGame);
            ToastHelper.showShort(MainActivity.this, resetScores ? R.string.toast_rematch_created : R.string.toast_game_copied);
        }
    }.execute((Void) null);
}
Also used : Game(com.nolanlawson.keepscore.db.Game) Delta(com.nolanlawson.keepscore.db.Delta) GameDBHelper(com.nolanlawson.keepscore.db.GameDBHelper) PlayerScore(com.nolanlawson.keepscore.db.PlayerScore)

Example 9 with Game

use of com.nolanlawson.keepscore.db.Game in project KeepScore by nolanlawson.

the class MainActivity method exportToSpreadsheet.

private void exportToSpreadsheet(final List<Integer> gameIds) {
    final ProgressDialog progressDialog = showProgressDialog(R.string.text_loading_generic, // plus one for saving the spreadsheet itself
    gameIds.size() + 1);
    new AsyncTask<Void, Void, String>() {

        @Override
        protected String doInBackground(Void... params) {
            List<Game> games = new ArrayList<Game>();
            GameDBHelper dbHelper = null;
            try {
                dbHelper = new GameDBHelper(MainActivity.this);
                for (Integer gameId : gameIds) {
                    games.add(dbHelper.findGameById(gameId));
                    publishProgress((Void) null);
                }
            } finally {
                if (dbHelper != null) {
                    dbHelper.close();
                }
            }
            String filename = SdcardHelper.createSpreadsheetFilename();
            SdcardHelper.saveSpreadsheet(filename, games, MainActivity.this);
            publishProgress((Void) null);
            return filename;
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
            progressDialog.incrementProgressBy(1);
        }

        @Override
        protected void onPostExecute(String filename) {
            super.onPostExecute(filename);
            progressDialog.dismiss();
            sendSpreadsheetAsAttachment(filename, gameIds.size());
        }
    }.execute((Void) null);
}
Also used : Game(com.nolanlawson.keepscore.db.Game) GameDBHelper(com.nolanlawson.keepscore.db.GameDBHelper) List(java.util.List) ArrayList(java.util.ArrayList) ProgressDialog(android.app.ProgressDialog)

Example 10 with Game

use of com.nolanlawson.keepscore.db.Game in project KeepScore by nolanlawson.

the class PeriodicAutomaticBackupService method onHandleIntent.

@Override
protected void onHandleIntent(Intent intent) {
    String filename = SdcardHelper.createBackupFilename(Format.GZIP);
    GameDBHelper dbHelper = null;
    try {
        dbHelper = new GameDBHelper(this);
        List<Game> games = dbHelper.findAllGames();
        log.i("Beginning periodic automatic backup of %d saved KeepScore games...", games.size());
        GamesBackup gamesBackup = new GamesBackup();
        gamesBackup.setVersion(GamesBackupSerializer.CURRENT_VERSION);
        gamesBackup.setDateSaved(System.currentTimeMillis());
        gamesBackup.setGameCount(games.size());
        gamesBackup.setAutomatic(true);
        gamesBackup.setFilename(filename);
        gamesBackup.setGames(games);
        String xmlData = GamesBackupSerializer.serialize(gamesBackup);
        SdcardHelper.save(filename, Format.GZIP, Location.Backups, xmlData);
        log.i("KeepScore backed up %d games to \"%s\".", games.size(), filename);
    } finally {
        if (dbHelper != null) {
            dbHelper.close();
        }
    }
}
Also used : Game(com.nolanlawson.keepscore.db.Game) GameDBHelper(com.nolanlawson.keepscore.db.GameDBHelper) GamesBackup(com.nolanlawson.keepscore.serialization.GamesBackup)

Aggregations

Game (com.nolanlawson.keepscore.db.Game)11 GameDBHelper (com.nolanlawson.keepscore.db.GameDBHelper)5 PlayerScore (com.nolanlawson.keepscore.db.PlayerScore)5 GamesBackup (com.nolanlawson.keepscore.serialization.GamesBackup)4 ArrayList (java.util.ArrayList)4 IOException (java.io.IOException)3 ProgressDialog (android.app.ProgressDialog)2 Delta (com.nolanlawson.keepscore.db.Delta)2 List (java.util.List)2 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)2 AlertDialog (android.app.AlertDialog)1 LoadGamesBackupResult (com.nolanlawson.keepscore.data.LoadGamesBackupResult)1 Function (com.nolanlawson.keepscore.util.CollectionUtil.Function)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 StringWriter (java.io.StringWriter)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1