浏览代码

Hämta frågor online

Axel Nordh 6 年之前
父节点
当前提交
55fb2c4d78
共有 5 个文件被更改,包括 61 次插入38 次删除
  1. 4 4
      Assets/MainMenu.unity
  2. 25 23
      Assets/Scripts/Database/OnlineDatabase.cs
  3. 30 9
      dbFiles/OnlineGameInfo.php
  4. 1 1
      dbFiles/OnlineGames.php
  5. 1 1
      dbFiles/Question.php

+ 4 - 4
Assets/MainMenu.unity

@@ -2658,7 +2658,7 @@ RectTransform:
   m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
   m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
   m_AnchorMin: {x: 0, y: 1}
   m_AnchorMin: {x: 0, y: 1}
   m_AnchorMax: {x: 1, y: 1}
   m_AnchorMax: {x: 1, y: 1}
-  m_AnchoredPosition: {x: -0.000045776367, y: 8.324223}
+  m_AnchoredPosition: {x: -0.000045776367, y: 8.757653}
   m_SizeDelta: {x: 0, y: 0}
   m_SizeDelta: {x: 0, y: 0}
   m_Pivot: {x: 0.5, y: 1}
   m_Pivot: {x: 0.5, y: 1}
 --- !u!114 &321314050
 --- !u!114 &321314050
@@ -5679,7 +5679,7 @@ RectTransform:
   m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
   m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
   m_AnchorMin: {x: 0, y: 1}
   m_AnchorMin: {x: 0, y: 1}
   m_AnchorMax: {x: 1, y: 1}
   m_AnchorMax: {x: 1, y: 1}
-  m_AnchoredPosition: {x: -0.000015258789, y: 8.324223}
+  m_AnchoredPosition: {x: -0.000015258789, y: 8.757653}
   m_SizeDelta: {x: 0, y: 0}
   m_SizeDelta: {x: 0, y: 0}
   m_Pivot: {x: 0.5, y: 1}
   m_Pivot: {x: 0.5, y: 1}
 --- !u!114 &777178507
 --- !u!114 &777178507
@@ -7378,7 +7378,7 @@ MonoBehaviour:
   m_HandleRect: {fileID: 761483706}
   m_HandleRect: {fileID: 761483706}
   m_Direction: 2
   m_Direction: 2
   m_Value: 0
   m_Value: 0
-  m_Size: 0.9450287
+  m_Size: 0.94679534
   m_NumberOfSteps: 0
   m_NumberOfSteps: 0
   m_OnValueChanged:
   m_OnValueChanged:
     m_PersistentCalls:
     m_PersistentCalls:
@@ -10953,7 +10953,7 @@ MonoBehaviour:
   m_HandleRect: {fileID: 359479519}
   m_HandleRect: {fileID: 359479519}
   m_Direction: 2
   m_Direction: 2
   m_Value: 0
   m_Value: 0
-  m_Size: 0.9450287
+  m_Size: 0.94679534
   m_NumberOfSteps: 0
   m_NumberOfSteps: 0
   m_OnValueChanged:
   m_OnValueChanged:
     m_PersistentCalls:
     m_PersistentCalls:

+ 25 - 23
Assets/Scripts/Database/OnlineDatabase.cs

@@ -68,6 +68,10 @@ public class OnlineDatabase : MonoBehaviour {
         public string answer;
         public string answer;
         public string id;
         public string id;
         public string category;
         public string category;
+        public int r;
+        public int g;
+        public int b;
+        public int a;
     }
     }
 
 
     [Serializable]
     [Serializable]
@@ -290,6 +294,7 @@ public class OnlineDatabase : MonoBehaviour {
         List<KeyValuePair<string, string>> returnList = new List<KeyValuePair<string, string>>();
         List<KeyValuePair<string, string>> returnList = new List<KeyValuePair<string, string>>();
 
 
         WWWForm form = new WWWForm();
         WWWForm form = new WWWForm();
+        form.AddField("f", "GetGameInfo");
         form.AddField("gameId", gameId);
         form.AddField("gameId", gameId);
 
 
         string response = CallOnlineDatabaseWithResponse("OnlineGameInfo.php", form);
         string response = CallOnlineDatabaseWithResponse("OnlineGameInfo.php", form);
@@ -579,36 +584,33 @@ public class OnlineDatabase : MonoBehaviour {
         }
         }
     }
     }
 
 
-    public List<QuestionCard> GetPlayerQuestions(int gameId, string playerNameValue) {
+    public List<QuestionCard> GetPlayerQuestions(int gameId, string playerName) {
         List<QuestionCard> questions = new List<QuestionCard>();
         List<QuestionCard> questions = new List<QuestionCard>();
+        WWWForm form = new WWWForm();
+        form.AddField("f", "PlayerQuestions");
+        form.AddField("gameId", gameId);
+        form.AddField("userName", playerName);
 
 
-        /*
-        if (connectionType.Equals("Local")) {
-            string sql = "SELECT * FROM questions inner join questionToCategory on questions.id = questionToCategory.questionId INNER JOIN category ON category.id = questionToCategory.categoryId WHERE questions.id IN ( SELECT questionId FROM usersLockedQuestions WHERE gameId = " + gameId + " AND playerName = '" + playerNameValue + "') ORDER BY answer ASC";
-            IDbConnection conn = new SqliteConnection(databaseUrl);
-            conn.Open();
-            IDbCommand cmd = conn.CreateCommand();
-            cmd.CommandText = sql;
+        string response = CallOnlineDatabaseWithResponse("OnlineGameInfo.php", form);
+        response = "{\"questionsList\" : [ " + response + " ]}";
 
 
-            IDataReader reader = cmd.ExecuteReader();
-            while (reader.Read()) {
-                GameObject question = Instantiate(questionCardPrefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
-                QuestionCard q = question.GetComponent<QuestionCard>();
+        Questions ql = new Questions();
 
 
-                q.SetAnswerText(reader.GetInt32(2).ToString());
-                q.SetQuestionText(reader.GetString(1));
-                q.idString = reader.GetInt32(0).ToString();
-                Color32 questionCategoryColor = new Color32((byte)reader.GetInt32(7), (byte)reader.GetInt32(8), (byte)reader.GetInt32(9), (byte)reader.GetInt32(10));
+        JsonUtility.FromJsonOverwrite(response, ql);
 
 
-                q.SetQuestionCategoryColor(questionCategoryColor);
+        foreach (Question q in ql.questionsList) {
+            GameObject question = Instantiate(questionCardPrefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
+            QuestionCard qc = question.GetComponent<QuestionCard>();
 
 
-                questions.Add(q);
-            }
-            cmd.Dispose();
-            conn.Close();
+            qc.SetAnswerText(q.answer);
+            qc.SetQuestionText(q.question);
+            qc.idString = q.category;
+
+            Color32 questionCategoryColor = new Color32((byte)q.r, (byte)q.g, (byte)q.b, (byte)q.a);
+            qc.SetQuestionCategoryColor(questionCategoryColor);
+
+            questions.Add(qc);
         }
         }
-        */
-        // TODO Fix online call to question
         return questions;
         return questions;
     }
     }
 
 

+ 30 - 9
dbFiles/OnlineGameInfo.php

@@ -12,14 +12,38 @@
 	mysqli_set_charset($conn,'utf8');
 	mysqli_set_charset($conn,'utf8');
 	
 	
 	$gameId = $conn->real_escape_string($_POST['gameId']);
 	$gameId = $conn->real_escape_string($_POST['gameId']);
-		
-	$sql = "SELECT username, status FROM gamePlayers INNER JOIN users ON users.id = playerId WHERE gameId = $gameId";
-	$result = $conn->query($sql);
+	$callFunction = $conn->real_escape_string($_POST['f']);
 	
 	
-	
-	if ($result->num_rows > 0) {
+	if ($callFunction === "GetGameInfo") {
+		$sql = "SELECT username, status FROM gamePlayers INNER JOIN users ON users.id = playerId WHERE gameId = $gameId";
+		$result = $conn->query($sql);
+
+
+		if ($result->num_rows > 0) {
+			$returnArray = array();
+			$i = 0;
+			while ($data = $result->fetch_assoc()) {
+				foreach ($data as $key => $value) {
+					$returnArray[$i][$key] = $value;
+				}
+				$i++;
+			}
+			echo json_encode($returnArray);
+			
+		} else {
+			echo "No games found for user";
+		}
+	} else if ($callFunction === "PlayerQuestions") {
+		$userName = $conn->real_escape_string($_POST['userName']);
+		$sql = "SELECT question, answer, categoryId as category, r,g,b,a FROM questions 
+					INNER JOIN questionToCategory on questions.id = questionToCategory.questionId 
+					INNER JOIN category ON category.id = questionToCategory.categoryId 
+					WHERE questions.id IN 
+						(SELECT questionId FROM usersLockedQuestions 
+							WHERE 
+						gameId = $gameId AND playerName = '$userName') ORDER BY answer ASC";
+		$result = $conn->query($sql);
 		$returnArray = array();
 		$returnArray = array();
-		$i = 0;
 		while ($data = $result->fetch_assoc()) {
 		while ($data = $result->fetch_assoc()) {
 			foreach ($data as $key => $value) {
 			foreach ($data as $key => $value) {
 				$returnArray[$i][$key] = $value;
 				$returnArray[$i][$key] = $value;
@@ -27,9 +51,6 @@
 			$i++;
 			$i++;
 		}
 		}
 		echo json_encode($returnArray);
 		echo json_encode($returnArray);
-		
-	} else {
-		echo "No games found for user";
 	}
 	}
 	$conn->close();
 	$conn->close();
 	
 	

+ 1 - 1
dbFiles/OnlineGames.php

@@ -162,7 +162,7 @@
 		}
 		}
 		echo json_encode($returnArray);
 		echo json_encode($returnArray);
 	} else if ($callFunction === "GetQuestionsLost") {
 	} else if ($callFunction === "GetQuestionsLost") {
-		$userName = $_POST['userName'];
+		$userName = $conn->real_escape_string($_POST['userName']);
 		$sql = "SELECT questionsLost FROM gamePlayers WHERE gameId = $gameId AND playerId = (SELECT id from users WHERE username = '$userName')";
 		$sql = "SELECT questionsLost FROM gamePlayers WHERE gameId = $gameId AND playerId = (SELECT id from users WHERE username = '$userName')";
 		$result = $conn->query($sql);
 		$result = $conn->query($sql);
 		$data = $result->fetch_assoc();
 		$data = $result->fetch_assoc();

+ 1 - 1
dbFiles/Question.php

@@ -12,7 +12,7 @@
 	}
 	}
 	mysqli_set_charset($conn,'utf8');
 	mysqli_set_charset($conn,'utf8');
 	
 	
-	$sql = 'SELECT questions.id, question, answer, categoryId as category, name FROM questions INNER JOIN questionToCategory ON questions.id = questionToCategory.questionId INNER JOIN category on category.id = questionToCategory.categoryId ORDER BY RAND() limit 1';
+	$sql = 'SELECT questions.id, question, answer, categoryId as category, name, r,g,b,a FROM questions INNER JOIN questionToCategory ON questions.id = questionToCategory.questionId INNER JOIN category on category.id = questionToCategory.categoryId ORDER BY RAND() limit 1';
 	
 	
 	$result = mysqli_query($conn, $sql);
 	$result = mysqli_query($conn, $sql);