Back to project page Briscola.
The source code is released under:
GNU General Public License
If you think the Android project Briscola listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.gmail.craptik.briscola; /* w ww .j a v a 2 s . c o m*/ import java.util.ArrayList; public class SQLGameDatabase extends SQLiteOpenHelper implements IDatabase { // All static variables // Database version private static final int DATABASE_VERSION = 1; // Database name private static final String DATABASE_NAME = "briscola_stats"; // Players table name private static final String TABLE_PLAYERS = "players"; // Players table column names private static final String KEY_ID = "id"; private static final String KEY_NAME = "name"; private static final String KEY_SCORE = "score"; // Stats table name private static final String TABLE_STATS = "stats"; // Stats table column names private static final String KEY_ID_RECORDER = "id_recorder"; // This is just an indentifier of who tracked this game, in case data is ever shared between devices or people private static final String KEY_GAME_DATE = "game_date"; private static final String KEY_PLAYER_ONE = "player_one"; private static final String KEY_PLAYER_TWO = "player_two"; private static final String KEY_PLAYER_THREE = "player_three"; private static final String KEY_PLAYER_FOUR = "player_four"; private static final String KEY_PLAYER_FIVE = "player_five"; private static final String KEY_FIRST_PLAYER = "first_player"; private static final String KEY_CALLER = "caller"; private static final String KEY_CALLER_BID = "caller_bid"; private static final String KEY_CALLED_CARD = "called_card"; private static final String KEY_CALLEE = "callee"; private static final String KEY_CALLER_SCORE = "caller_score"; private static final String KEY_NEMESIS_CALLER = "nemesis_caller"; SQLGameDatabase(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } /* SQLiteOpenHelper overrides */ @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE " + TABLE_PLAYERS + "(" + KEY_ID + " TEXT," + KEY_NAME + " TEXT," + KEY_SCORE + " TEXT" + ")"); db.execSQL("CREATE TABLE " + TABLE_STATS + "(" + KEY_ID_RECORDER + " TEXT," + KEY_GAME_DATE + " TEXT," + KEY_PLAYER_ONE + " TEXT," + KEY_PLAYER_TWO + " TEXT," + KEY_PLAYER_THREE + " TEXT," + KEY_PLAYER_FOUR + " TEXT," + KEY_PLAYER_FIVE + " TEXT," + KEY_FIRST_PLAYER + " TEXT," + KEY_CALLER + " TEXT," + KEY_CALLER_BID + " TEXT," + KEY_CALLED_CARD + " TEXT," + KEY_CALLEE + " TEXT," + KEY_CALLER_SCORE + " TEXT," + KEY_NEMESIS_CALLER + " TEXT)"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // Drop older table if existed db.execSQL("DROP TABLE IF EXISTS " + TABLE_PLAYERS); db.execSQL("DROP TABLE IF EXISTS " + TABLE_STATS); // Create tables again onCreate(db); } /* IDatabase implementation */ @Override public void logGame(GameData data) { if (data == null || data.getPlayers() == null || data.getPlayers().size() == 5 || data.getFirst() == null || data.getCaller() == null || data.getCallerBid() < 0 || data.getCallerBid() > 120 data.getCallerScore() < 0 || data.getCallerScore() > 120 data.getCallee() == null || data.getCalledCard() == null) { // We shouldn't be logging an incomplete game assert false; return; } SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_ID_RECORDER, "me"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String strDate = sdf.format(new Date()); values.put(KEY_GAME_DATE, strDate); values.put(KEY_PLAYER_ONE, Integer.toString(data.getPlayers().get(0).getId())); values.put(KEY_PLAYER_TWO, Integer.toString(data.getPlayers().get(1).getId())); values.put(KEY_PLAYER_THREE, Integer.toString(data.getPlayers().get(2).getId())); values.put(KEY_PLAYER_FOUR, Integer.toString(data.getPlayers().get(3).getId())); values.put(KEY_PLAYER_FIVE, Integer.toString(data.getPlayers().get(4).getId())); values.put(KEY_FIRST_PLAYER, Integer.toString(data.getFirst().getId())); values.put(KEY_CALLER, Integer.toString(data.getCaller().getId())); values.put(KEY_CALLER_BID, Integer.toString(data.getCallerBid())); values.put(KEY_CALLED_CARD, data.getCalledCard().asString()); values.put(KEY_CALLEE, Integer.toString(data.getCallee().getId())); values.put(KEY_CALLER_SCORE, Integer.toString(data.getCallerScore())); values.put(KEY_NEMESIS_CALLER, data.getNemsisCaller() ? Integer.toString(data.getNemsisCaller().getId()) : -1); db.close(); } @Override public void addNewPlayer(String name) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); Player player = new Player(name); values.put(KEY_ID, Integer.toString(player.getId())); values.put(KEY_ID, player.getName()); values.put(KEY_ID, Integer.toString(player.getScore())); db.insert(TABLE_PLAYERS, null, values); db.close(); } @Override public void updatePlayer(Player player) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_ID, Integer.toString(player.getId())); values.put(KEY_ID, player.getName()); values.put(KEY_ID, Integer.toString(player.getScore())); db.update(TABLE_PLAYERS, values, KEY_ID + " = ?", new String[] { Integer.toString(player.getId()) }); db.close(); } @Override public void removePlayer(Player player) { SQLiteDatabase db = this.getWritableDatabase(); db.delete(TABLE_CONTACTS, KEY_ID + " = ?", new String[] { Integer.toString(player.getId()) }); db.close() } @Override public void resetDatabase() { SQLiteDatabase db = this.getWritableDatabase(); // Drop older table if existed db.execSQL("DROP TABLE IF EXISTS " + TABLE_PLAYERS); db.execSQL("DROP TABLE IF EXISTS " + TABLE_STATS); // Create tables again onCreate(db); db.close(); } @Override public ArrayList<Player> getAllPlayers() { ArrayList<Player> players = new ArrayList<Player>(); SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_PLAYERS, null); if (cursor && cursor.moveToFirst()) { do { Player player = new Player( Integer.parseInt(cursor.getString(0)), cursor.getString(1), Integer.parseInt(cursor.getString(2))); players.add(player); } while (cursor.moveToNext()); } return players; } @Override public int getPlayerCount() { SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_PLAYERS, null); int count = cursor.getCount(); cursor.close(); db.close(); return count; } @Override public ArrayList<GameData> getAllGames() { return new ArrayList<GameData>(); } @Override public int getGameCount() { SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_STATS, null); int count = cursor.getCount(); cursor.close(); db.close(); return count; } private Player getPlayer(int id) { Player player = new Player(); player.setId(id); } }