|
|
@@ -5,6 +5,7 @@ using Mono.Data.Sqlite;
|
|
|
using System.Data;
|
|
|
using System;
|
|
|
using UnityEngine.Networking;
|
|
|
+using System.IO;
|
|
|
|
|
|
public class Database : MonoBehaviour {
|
|
|
string connectionType;
|
|
|
@@ -27,11 +28,23 @@ public class Database : MonoBehaviour {
|
|
|
public List<Question> questionsList = new List<Question>();
|
|
|
}
|
|
|
|
|
|
+ internal void SetLastPlayedDate(int gameId) {
|
|
|
+ string sql = "UPDATE game SET lastPlayedDate = DATE() WHERE id = " + gameId;
|
|
|
+ IDbConnection conn = new SqliteConnection(databaseUrl);
|
|
|
+ conn.Open();
|
|
|
+ IDbCommand cmd = conn.CreateCommand();
|
|
|
+ cmd.CommandText = sql;
|
|
|
+ cmd.ExecuteNonQuery();
|
|
|
+
|
|
|
+ cmd.Dispose();
|
|
|
+ conn.Close();
|
|
|
+ }
|
|
|
+
|
|
|
string questionString = "";
|
|
|
string answerString = "";
|
|
|
string idString = "";
|
|
|
string categoryString = "";
|
|
|
- private int round;
|
|
|
+ private int round = -1;
|
|
|
|
|
|
public string QuestionString { get => questionString; set => questionString = value; }
|
|
|
|
|
|
@@ -70,37 +83,43 @@ public class Database : MonoBehaviour {
|
|
|
|
|
|
internal List<LocalGameScript> GetLocalGames(GameObject prefab) {
|
|
|
List<LocalGameScript> games = new List<LocalGameScript>();
|
|
|
- string sql = "SELECT game.*, name,count(name) as points, localUsers.id FROM game INNER JOIN localGamePlayers on game.id = localGamePlayers.gameId INNER JOIN localUsers on playerId = localUsers.id INNER JOIN usersLockedQuestions ON name = usersLockedQuestions.playerName GROUP BY name order by points ASC;";
|
|
|
+ string sql = "SELECT game.*, name, localUsers.id as userId FROM game INNER JOIN localGamePlayers on game.id = localGamePlayers.gameId INNER JOIN localUsers on playerId = localUsers.id order by game.id ASC, userId ASC";
|
|
|
IDbConnection conn = new SqliteConnection(databaseUrl);
|
|
|
conn.Open();
|
|
|
IDbCommand cmd = conn.CreateCommand();
|
|
|
cmd.CommandText = sql;
|
|
|
IDataReader reader = cmd.ExecuteReader();
|
|
|
int currentGame = -1;
|
|
|
- GameObject localGame = Instantiate(prefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
|
|
|
- LocalGameScript lgs = localGame.GetComponent<LocalGameScript>();
|
|
|
+ GameObject localGame;// = Instantiate(prefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
|
|
|
+ LocalGameScript lgs = null;//= localGame.GetComponent<LocalGameScript>();
|
|
|
|
|
|
+ string currentPlayerName = "";
|
|
|
+ int currentPoints = 0;
|
|
|
while (reader.Read()) {
|
|
|
|
|
|
|
|
|
- if (currentGame == reader.GetInt32(0)) {
|
|
|
- lgs.AddPlayer(reader.GetString(10), reader.GetInt32(11));
|
|
|
- } else {
|
|
|
+ if (currentGame != reader.GetInt32(0)) {
|
|
|
localGame = Instantiate(prefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
|
|
|
lgs = localGame.GetComponent<LocalGameScript>();
|
|
|
|
|
|
lgs.GameId = reader.GetInt32(0);
|
|
|
lgs.GameMode = reader.GetString(1);
|
|
|
- lgs.QuestionsNeededToWin = reader.GetInt32(2);
|
|
|
+ lgs.QuestionsNeededToWin = reader.GetInt32(2).ToString();
|
|
|
lgs.AnswerTimer = reader.GetInt32(3);
|
|
|
- lgs.NumberOfPlayers = reader.GetInt32(4);
|
|
|
+ lgs.NumberOfPlayers = reader.GetInt32(4).ToString();
|
|
|
lgs.CurrentPlayer = reader.GetString(5);
|
|
|
- lgs.Round = reader.GetInt32(6);
|
|
|
+ lgs.Round = reader.GetInt32(6).ToString();
|
|
|
lgs.StartDate = reader.GetValue(7) != DBNull.Value ? reader.GetString(7) : "";
|
|
|
lgs.LastPlayedDate = reader.GetValue(8) != DBNull.Value ? reader.GetString(8) : "";
|
|
|
lgs.FinishedDate = reader.GetValue(9) != DBNull.Value ? reader.GetString(9) : "";
|
|
|
- lgs.AddPlayer(reader.GetString(10), reader.GetInt32(11));
|
|
|
+ currentPlayerName = reader.GetString(10);
|
|
|
+ currentPoints = GetPlayerPoints(reader.GetInt32(0), currentPlayerName);
|
|
|
+ lgs.AddPlayer(currentPlayerName, currentPoints);
|
|
|
games.Add(lgs);
|
|
|
+ } else if (currentGame == reader.GetInt32(0)) {
|
|
|
+ currentPlayerName = reader.GetString(10);
|
|
|
+ currentPoints = GetPlayerPoints(currentGame, currentPlayerName);
|
|
|
+ lgs.AddPlayer(currentPlayerName, currentPoints);
|
|
|
}
|
|
|
|
|
|
currentGame = reader.GetInt32(0);
|
|
|
@@ -143,6 +162,25 @@ public class Database : MonoBehaviour {
|
|
|
conn.Close();
|
|
|
}
|
|
|
|
|
|
+ internal int GetPlayerPoints(int gameId, string playerName) {
|
|
|
+ string sql = "SELECT count(*) FROM usersLockedQuestions WHERE gameId = " + gameId + " AND playerName = '" +playerName + "'";
|
|
|
+ IDbConnection conn = new SqliteConnection(databaseUrl);
|
|
|
+ conn.Open();
|
|
|
+ IDbCommand cmd = conn.CreateCommand();
|
|
|
+ cmd.CommandText = sql;
|
|
|
+ IDataReader reader = cmd.ExecuteReader();
|
|
|
+
|
|
|
+ int points = 0;
|
|
|
+ while (reader.Read()) {
|
|
|
+ points = reader.GetInt32(0);
|
|
|
+ }
|
|
|
+ reader.Close();
|
|
|
+ cmd.Dispose();
|
|
|
+ conn.Close();
|
|
|
+
|
|
|
+ return points;
|
|
|
+ }
|
|
|
+
|
|
|
internal void SetFinishedDate(int gameId, string finishedDate) {
|
|
|
string sql = "UPDATE game SET finishedDate = '" + finishedDate + "' WHERE id = " + gameId;
|
|
|
IDbConnection conn = new SqliteConnection(databaseUrl);
|
|
|
@@ -168,7 +206,7 @@ public class Database : MonoBehaviour {
|
|
|
}
|
|
|
|
|
|
internal int GetRoundValue(int gameId) {
|
|
|
- if (this.round == null) {
|
|
|
+ if (this.round == -1) {
|
|
|
string sql = "SELECT round FROM game WHERE id = " + gameId;
|
|
|
IDbConnection conn = new SqliteConnection(databaseUrl);
|
|
|
conn.Open();
|
|
|
@@ -176,7 +214,7 @@ public class Database : MonoBehaviour {
|
|
|
cmd.CommandText = sql;
|
|
|
IDataReader reader = cmd.ExecuteReader();
|
|
|
while (reader.Read()) {
|
|
|
- this.gameMode = reader.GetString(0);
|
|
|
+ this.round = reader.GetInt32(0);
|
|
|
}
|
|
|
reader.Close();
|
|
|
cmd.Dispose();
|
|
|
@@ -186,7 +224,7 @@ public class Database : MonoBehaviour {
|
|
|
}
|
|
|
|
|
|
internal List<KeyValuePair<string, int>> GetPlayersForGame(int gameId) {
|
|
|
- string sql = "SELECT name, (SELECT count(*) FROM usersLockedQuestions WHERE playerName = localUsers.name) as numAnswers FROM localGamePlayers " +
|
|
|
+ string sql = "SELECT name, (SELECT count(*) FROM usersLockedQuestions WHERE playerName = localUsers.name AND gameId = " + gameId + ") as numAnswers FROM localGamePlayers " +
|
|
|
"LEFT JOIN localUsers ON localGamePlayers.playerId = localUsers.id " +
|
|
|
"WHERE gameId = " + gameId;
|
|
|
IDbConnection conn = new SqliteConnection(databaseUrl);
|
|
|
@@ -211,8 +249,24 @@ public class Database : MonoBehaviour {
|
|
|
|
|
|
public void SetLocalOrOnline(string type) {
|
|
|
gameMode = type;
|
|
|
+ string databaseName = "narKampenLocal.db";
|
|
|
if (type.Equals("Local")) {
|
|
|
- databaseUrl = "URI=file:" + Application.dataPath + "/narKampenLocal.db";
|
|
|
+ if (Application.platform == RuntimePlatform.Android) {
|
|
|
+ databaseUrl = Application.persistentDataPath + "/" + databaseName;
|
|
|
+
|
|
|
+ if (!File.Exists(databaseUrl)) {
|
|
|
+ UnityWebRequest load = UnityWebRequest.Get("jar:file://" + Application.dataPath + "!/assets/" + databaseName);
|
|
|
+ // WWW load = new WWW("jar:file://" + Application.dataPath + "!/assets/" + databaseName);
|
|
|
+ //while (!load.isDone) { }
|
|
|
+ load.SendWebRequest();
|
|
|
+
|
|
|
+ File.WriteAllBytes(databaseUrl, load.downloadHandler.data);
|
|
|
+ //File.WriteAllBytes(databaseUrl, load.bytes);
|
|
|
+ }
|
|
|
+ databaseUrl = "URI=file:" + databaseUrl;
|
|
|
+ } else {
|
|
|
+ databaseUrl = "URI=file:" + Application.dataPath + "/narKampenLocal.db";
|
|
|
+ }
|
|
|
} else {
|
|
|
databaseUrl = "nordh.xyz/narKampen/dbFiles/dbAccess.php";
|
|
|
}
|
|
|
@@ -238,18 +292,25 @@ public class Database : MonoBehaviour {
|
|
|
}
|
|
|
|
|
|
public void LinkPlayersToLocalGame(List<string> playerNames, int gameId) {
|
|
|
+
|
|
|
+ IDbConnection conn = new SqliteConnection(databaseUrl);
|
|
|
+ conn.Open();
|
|
|
+ string questionSql = "SELECT id FROM questions order by random() limit 1";
|
|
|
+ IDbCommand cmd = conn.CreateCommand();
|
|
|
+ cmd.CommandText = questionSql;
|
|
|
+ IDataReader reader = cmd.ExecuteReader();
|
|
|
+ int questionId = -1;
|
|
|
+ while (reader.Read()) {
|
|
|
+ questionId = reader.GetInt32(0);
|
|
|
+ }
|
|
|
foreach (string player in playerNames) {
|
|
|
string sql = "SELECT id FROM localUsers WHERE name = '" + player + "'";
|
|
|
- IDbConnection conn = new SqliteConnection(databaseUrl);
|
|
|
- conn.Open();
|
|
|
- IDbCommand cmd = conn.CreateCommand();
|
|
|
+ int playerId;
|
|
|
+ cmd = conn.CreateCommand();
|
|
|
cmd.CommandText = sql;
|
|
|
- IDataReader reader = cmd.ExecuteReader();
|
|
|
+ reader = cmd.ExecuteReader();
|
|
|
if (reader.Read()) {
|
|
|
- int playerId = reader.GetInt32(0);
|
|
|
- conn.Close();
|
|
|
- LinkPlayerToGame(playerId, gameId);
|
|
|
- continue;
|
|
|
+ playerId = reader.GetInt32(0);
|
|
|
} else {
|
|
|
reader.Close();
|
|
|
sql = "INSERT INTO localUsers (name) VALUES ('" + player + "')";
|
|
|
@@ -258,14 +319,20 @@ public class Database : MonoBehaviour {
|
|
|
|
|
|
cmd.CommandText = "SELECT last_insert_rowid()";
|
|
|
Int64 lastInsert64 = (Int64)cmd.ExecuteScalar();
|
|
|
-
|
|
|
- LinkPlayerToGame((int)lastInsert64, gameId);
|
|
|
+ playerId = (int)lastInsert64;
|
|
|
}
|
|
|
- conn.Close();
|
|
|
cmd.Dispose();
|
|
|
reader.Close();
|
|
|
- }
|
|
|
|
|
|
+ LinkPlayerToGame(playerId, gameId);
|
|
|
+ SavePlayersQuestion(questionId.ToString(), player, gameId);
|
|
|
+
|
|
|
+ }
|
|
|
+ IDbCommand cmd2 = conn.CreateCommand();
|
|
|
+ cmd2.CommandText = "UPDATE game SET currentPlayer = '" + playerNames[0] + "' WHERE id = " + gameId;
|
|
|
+ cmd2.ExecuteNonQuery();
|
|
|
+ cmd2.Dispose();
|
|
|
+ conn.Close();
|
|
|
}
|
|
|
|
|
|
private void LinkPlayerToGame(int playerId, int gameId) {
|
|
|
@@ -274,7 +341,7 @@ public class Database : MonoBehaviour {
|
|
|
conn2.Open();
|
|
|
IDbCommand cmd = conn2.CreateCommand();
|
|
|
cmd.CommandText = sql;
|
|
|
- cmd.ExecuteScalar();
|
|
|
+ cmd.ExecuteNonQuery();
|
|
|
cmd.Dispose();
|
|
|
conn2.Close();
|
|
|
}
|
|
|
@@ -298,7 +365,7 @@ public class Database : MonoBehaviour {
|
|
|
}
|
|
|
|
|
|
public int SetupNewLocalGame(int winNumber, int numberOfPlayers, int questionTimer) {
|
|
|
- string sql = "INSERT INTO game (winNumber, numberOfPlayers, answerTimer, gameMode) VALUES (" + winNumber + "," + numberOfPlayers + "," + questionTimer + ", 'Local')";
|
|
|
+ string sql = "INSERT INTO game (winNumber, numberOfPlayers, answerTimer, gameMode, startedDate) VALUES (" + winNumber + "," + numberOfPlayers + "," + questionTimer + ", 'Local', DATE())";
|
|
|
IDbConnection conn = new SqliteConnection(databaseUrl);
|
|
|
conn.Open();
|
|
|
IDbCommand cmd = conn.CreateCommand();
|