Procházet zdrojové kódy

Spara låsta frågor online

Axel Nordh před 6 roky
rodič
revize
5adc802d3f

+ 15 - 11
Assets/Scripts/Database/Database.cs

@@ -544,6 +544,8 @@ public class Database : MonoBehaviour {
         while (reader.Read()) {
             questionId = reader.GetInt32(0);
         }
+        List<int> l = new List<int>();
+        l.Add(questionId);
         foreach (string player in playerNames) {
             string sql = "SELECT id FROM localUsers WHERE name = '" + player + "'";
             int playerId;
@@ -566,7 +568,7 @@ public class Database : MonoBehaviour {
             reader.Close();
 
             LinkPlayerToGame(playerId, gameId);
-            SavePlayersQuestion(questionId.ToString(), player, gameId);
+            SavePlayersQuestion(l, player, gameId, "Local");
 
         }
         IDbCommand cmd2 = conn.CreateCommand();
@@ -684,14 +686,18 @@ public class Database : MonoBehaviour {
         return q;
     }
 
-    public void SavePlayersQuestion(string questionId, string playerNameValue, int gameId) {
-        string gameMode = PlayerPrefs.GetString("GameMode");
-        SetLocalOrOnline(gameMode);
-
-        Int32.TryParse(questionId, out int qId);
-
-        if (gameMode.Equals("Local")) {
-            string sql = "INSERT OR IGNORE INTO usersLockedQuestions (playerName, questionId, gameId) VALUES ('" + playerNameValue + "'," + qId + "," + gameId + ")";
+    public void SavePlayersQuestion(List<int> questionsToSave, string playerNameValue, int gameId, string gameMode) {
+        if (gameMode.Equals("Online")) {
+            OnlineDatabase.Instance.SavePlayersQuestion(questionsToSave, playerNameValue, gameId);
+        } else { 
+            SetLocalOrOnline(gameMode);
+            
+            string values = "";
+            foreach (int questionId in questionsToSave) {
+                values += "(" + playerNameValue + "," + questionId + "," + gameId + "),";
+            }
+            values = values.Substring(0, values.Length - 1);
+            string sql = "INSERT OR IGNORE INTO usersLockedQuestions (playerName, questionId, gameId) VALUES " + values;
             IDbConnection conn = new SqliteConnection(databaseUrl);
             conn.Open();
             IDbCommand cmd = conn.CreateCommand();
@@ -701,8 +707,6 @@ public class Database : MonoBehaviour {
 
             cmd.Dispose();
             conn.Close();
-        } else {
-            // TODO Save question to database online;
         }
     }
 

+ 11 - 72
Assets/Scripts/Database/OnlineDatabase.cs

@@ -475,69 +475,15 @@ public class OnlineDatabase : MonoBehaviour {
         return this.gameMode;
     }
 
-    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 + "'";
-            int playerId;
-            cmd = conn.CreateCommand();
-            cmd.CommandText = sql;
-            reader = cmd.ExecuteReader();
-            if (reader.Read()) {
-                playerId = reader.GetInt32(0);
-            } else {
-                reader.Close();
-                sql = "INSERT INTO localUsers (name) VALUES ('" + player + "')";
-                cmd.CommandText = sql;
-                cmd.ExecuteNonQuery();
-
-                cmd.CommandText = "SELECT last_insert_rowid()";
-                Int64 lastInsert64 = (Int64)cmd.ExecuteScalar();
-                playerId = (int)lastInsert64;
-            }
-            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) {
-        string sql = "INSERT INTO localGamePlayers (gameId, playerId) VALUES (" + gameId + ", " + playerId + ")";
-        SqliteConnection conn2 = new SqliteConnection(databaseUrl);
-        conn2.Open();
-        IDbCommand cmd = conn2.CreateCommand();
-        cmd.CommandText = sql;
-        cmd.ExecuteNonQuery();
-        cmd.Dispose();
-        conn2.Close();
-    }
-
     internal int GetWinCondition(int gameId) {
         if (winAmount == -1) {
             WWWForm form = new WWWForm();
             form.AddField("gameId", gameId);
+            form.AddField("f", "GetWinCondition");
 
             string response = CallOnlineDatabaseWithResponse("OnlineGames.php", form);
 
-            Int32.TryParse(response, out int winAmount);
+            Int32.TryParse(response, out this.winAmount);
 
         }
         return this.winAmount;
@@ -558,24 +504,17 @@ public class OnlineDatabase : MonoBehaviour {
         return nq;
     }
 
-    public void SavePlayersQuestion(string questionId, string playerNameValue, int gameId) {
-        string gameMode = PlayerPrefs.GetString("GameMode");
-
-        Int32.TryParse(questionId, out int qId);
-
-        if (gameMode.Equals("Local")) {
-            string sql = "INSERT OR IGNORE INTO usersLockedQuestions (playerName, questionId, gameId) VALUES ('" + playerNameValue + "'," + qId + "," + gameId + ")";
-            IDbConnection conn = new SqliteConnection(databaseUrl);
-            conn.Open();
-            IDbCommand cmd = conn.CreateCommand();
-            cmd.CommandText = sql;
+    public void SavePlayersQuestion(List<int> questionsToSave, string playerNameValue, int gameId) {
+        WWWForm form = new WWWForm();
+        form.AddField("f", "SavePlayerQuestions");
+        form.AddField("gameId", gameId);
+        form.AddField("questionsToSave", questionsToSave.ToString());
+        form.AddField("userName", playerNameValue);
 
-            cmd.ExecuteReader();
+        string response = CallOnlineDatabaseWithResponse("OnlineGames.php", form);
 
-            cmd.Dispose();
-            conn.Close();
-        } else {
-            // TODO Save question to database online;
+        if (!response.Equals("")) {
+            Debug.Log(response);
         }
     }
 

+ 6 - 3
Assets/Scripts/ScrollViewScript.cs

@@ -105,15 +105,18 @@ public class ScrollViewScript : MonoBehaviour, IDropHandler {
     }
 
     public void SetAllQuestionsLocked(bool needsSave) {
+        List<int> saveQuestions = new List<int>();
         for (int i = 0; i < contentPanel.childCount; i++) {
             QuestionCard q = contentPanel.GetChild(i).GetComponent<QuestionCard>();
             q.SetQuestionSafe();
-
             if (needsSave) {
-                Database db = GameObject.Find("GameManager").GetComponent<Database>();
-                db.SavePlayersQuestion(q.idString, currentPlayer, gameId);
+                Int32.TryParse(q.questionString, out int qId);
+                saveQuestions.Add(qId);
             }
         }
+        if (saveQuestions.Count > 0) {
+            Database.Instance.SavePlayersQuestion(saveQuestions, currentPlayer, gameId, GetGameMode());
+        }
     }
 
     public void SetNewQuestion(NewQuestion q) {

+ 15 - 0
dbFiles/OnlineGames.php

@@ -174,6 +174,21 @@
 		$data = $result->fetch_assoc();
 		 
 		echo $data['winNumber'];
+	} else if ($callFunction === "SavePlayerQuestions") {
+		$userName = $conn->real_escape_string($_POST['userName']);
+		
+		$questionIds = $conn->real_escape_string($_POST['questionsToSave');
+		$questionIdArray = explode(",",$questionIds);
+		$values = "";
+		foreach($questionIdArray as $qId) {
+			$values .= "($userName, $qId, $gameId),";
+		}
+		$values = rtrim($values, ',');
+		$sql = "INSERT INTO usersLockedQuestions (playerName, questionId, gameId) VALUES $values ON DUPLICATE KEY UPDATE playerName = $userName, gameId = $gameId";
+		$result = $conn->query($sql);
+		if (!$result) {
+			echo "ERROR while svaing player questions for game $gameId and player $userName with error " . $conn->error;
+		}
 	}