From 7cb950072c952bb45c2cc1f82c8a9719ac17afb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Tue, 16 Feb 2016 16:28:54 +0100 Subject: DB: replace firstname,lastname with name in playres table, fix player store/load --- .../asynk/rustanddust/android/AndroidBackend.java | 4 +-- core/src/ch/asynk/rustanddust/util/DB.java | 34 ++++++++++++---------- .../asynk/rustanddust/desktop/DesktopBackend.java | 4 +-- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/android/src/ch/asynk/rustanddust/android/AndroidBackend.java b/android/src/ch/asynk/rustanddust/android/AndroidBackend.java index f0b1a6d..f61a1b5 100644 --- a/android/src/ch/asynk/rustanddust/android/AndroidBackend.java +++ b/android/src/ch/asynk/rustanddust/android/AndroidBackend.java @@ -21,7 +21,7 @@ public class AndroidBackend implements Backend public void init(RustAndDust game) { - me = game.db.storePlayerGetId("me", "myself", "I"); - opponent = game.db.storePlayerGetId("opponent", "other", "you"); + me = game.db.storePlayerGetId("me", "myself"); + opponent = game.db.storePlayerGetId("opponent", "other"); } } diff --git a/core/src/ch/asynk/rustanddust/util/DB.java b/core/src/ch/asynk/rustanddust/util/DB.java index ba03100..4bfa046 100644 --- a/core/src/ch/asynk/rustanddust/util/DB.java +++ b/core/src/ch/asynk/rustanddust/util/DB.java @@ -26,7 +26,7 @@ public class DB private static final String TBL_PLAYERS_CRT = "create table if not exists" + " players ( _id integer primary key autoincrement," - + " hash text unique not null, gmail text unique not null, firstname text, lastname text" + + " hash text unique not null, gmail text unique not null, name text not null" + ");"; private static final String TBL_BATTLES_CRT = "create table if not exists" @@ -58,8 +58,9 @@ public class DB private static final String INSERT_CONFIG = "insert or replace into config(key, value) values ('options','%s');"; private static final String GET_CONFIG = "select value from config where key='options';"; - private static final String INSERT_PLAYER = "insert or ignore into players(hash,gmail,firstname,lastname) values ('%s','%s','%s','%s');"; - private static final String GET_PLAYER_ID = "select _id from players where hash='%s';"; + private static final String INSERT_PLAYER = "insert or ignore into players(hash,gmail,name) values ('%s','%s','%s');"; + private static final String GET_PLAYER_ID_FROM_HASH = "select _id from players where hash='%s';"; + private static final String GET_PLAYER_ID_FROM_GMAIL = "select _id from players where gmail='%s';"; private static final String UPDATE_BATTLE = "insert or replace into battles values (%d,'%s');"; private static final String INSERT_GAME = "insert or ignore into games(_p1,_p2,_b,m) values (%d,%d,%d,%d);"; private static final String GET_GAME_ID = "select _id from games where _p1=%d and _p2=%d and _b=%d;"; @@ -119,24 +120,28 @@ public class DB return ret; } - public String storePlayer(String gmail, String firstname, String lastname) + public void storePlayer(String gmail, String name) + { + String hash = getDigest(String.format("#%s@%s*", gmail, df.format(new Date()))); + if (hash != null) + storePlayer(gmail, name, hash); + } + + public void storePlayer(String gmail, String name, String hash) { - String hash = getDigest(gmail + firstname + lastname); - if (hash == null) return null; try { - db.execSQL(String.format(INSERT_PLAYER, hash, gmail, firstname, lastname)); + db.execSQL(String.format(INSERT_PLAYER, hash, gmail, name)); } catch (SQLiteGdxException e) { RustAndDust.error("storePlayer"); - return null; } - return hash; } - public int getPlayerId(String hash) + public int getPlayerId(boolean hash, String s) { int ret = NO_RECORDS; + String sql = (hash ? GET_PLAYER_ID_FROM_HASH : GET_PLAYER_ID_FROM_GMAIL); try { - DatabaseCursor cursor = db.rawQuery(String.format(GET_PLAYER_ID, hash)); + DatabaseCursor cursor = db.rawQuery(String.format(sql, s)); if (cursor.getCount() > 0) { cursor.next(); ret = cursor.getInt(0); @@ -145,11 +150,10 @@ public class DB return ret; } - public int storePlayerGetId(String gmail, String firstname, String lastname) + public int storePlayerGetId(String gmail, String name) { - String hash = storePlayer(gmail, firstname, lastname); - if (hash == null) return NO_RECORDS; - return getPlayerId(hash); + storePlayer(gmail, name); + return getPlayerId(false, gmail); } public void storeBattle(int id, String name) diff --git a/desktop/src/ch/asynk/rustanddust/desktop/DesktopBackend.java b/desktop/src/ch/asynk/rustanddust/desktop/DesktopBackend.java index 48dd97b..22cca60 100644 --- a/desktop/src/ch/asynk/rustanddust/desktop/DesktopBackend.java +++ b/desktop/src/ch/asynk/rustanddust/desktop/DesktopBackend.java @@ -17,7 +17,7 @@ public class DesktopBackend implements Backend public void init(RustAndDust game) { - me = game.db.storePlayerGetId("me", "myself", "I"); - opponent = game.db.storePlayerGetId("opponent", "other", "you"); + me = game.db.storePlayerGetId("me", "myself"); + opponent = game.db.storePlayerGetId("opponent", "other"); } } -- cgit v1.1-2-g2b99