Bläddra i källkod

Online games php start

Axel Nordh 6 år sedan
förälder
incheckning
004d91a780
3 ändrade filer med 80 tillägg och 4 borttagningar
  1. 1 1
      Assets/MainMenu.unity
  2. 49 3
      Assets/Scripts/Database.cs
  3. 30 0
      dbFiles/OnlineGames.php

+ 1 - 1
Assets/MainMenu.unity

@@ -2303,7 +2303,7 @@ RectTransform:
   m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
   m_AnchorMin: {x: 0, y: 1}
   m_AnchorMax: {x: 1, y: 1}
-  m_AnchoredPosition: {x: -0.000045776367, y: 0.0004983821}
+  m_AnchoredPosition: {x: -0.000045776367, y: -0.0000058487026}
   m_SizeDelta: {x: 0, y: 0}
   m_Pivot: {x: 0.5, y: 1}
 --- !u!114 &321314050

+ 49 - 3
Assets/Scripts/Database.cs

@@ -81,6 +81,24 @@ public class Database : MonoBehaviour {
         public List<Question> questionsList = new List<Question>();
     }
 
+    [Serializable]
+    public class OnlineGame {
+        public string id;
+        public string winNumber;
+        public string answerTimer;
+        public string roundTimeLimit;
+        public string currentPlayer;
+        public string round;
+        public string startDate;
+        public string LastPlayedDate;
+        public string finishedDate;
+    }
+
+    [Serializable]
+    public class OnlineGames {
+        public List<OnlineGame> onlineGamesList = new List<OnlineGame>();
+    }
+ 
     [Serializable]
     public class UserName {
         public string id;
@@ -92,8 +110,8 @@ public class Database : MonoBehaviour {
         public List<UserName> usernamesList = new List<UserName>();
     }
 
-    private void CallDatabase(string fileName, WWWForm formData) {
-        string postUrl = serverUrl + fileName;
+    private void CallDatabase(string filename, WWWForm formData) {
+        string postUrl = serverUrl + filename;
 
         UnityWebRequest www = UnityWebRequest.Post(postUrl, formData);
         www.SendWebRequest();
@@ -106,6 +124,22 @@ public class Database : MonoBehaviour {
         }
     }
 
+    private string CallOnlineDatabaseWithResponse(string filename, WWWForm formData) {
+        string postUrl = serverUrl + filename;
+
+        UnityWebRequest www = UnityWebRequest.Post(postUrl, formData);
+        www.SendWebRequest();
+
+        if (www.isNetworkError || www.isHttpError) {
+            Debug.Log(www.error);
+        } else {
+            while (!www.isDone) {
+            }
+        }
+
+        return www.downloadHandler.text;
+    }
+
     internal void SetLastPlayedDate(int gameId) {
         string sql = "UPDATE game SET lastPlayedDate = DATE() WHERE id = " + gameId;
         IDbCommand cmd = GetConnection();
@@ -224,7 +258,19 @@ public class Database : MonoBehaviour {
     }
 
     internal void GetOnlineGames(int userId) {
-        string sql = "SELECT * FROM games WHERE userId = " + userId;
+
+        WWWForm formData = new WWWForm();
+        formData.AddField("userId", userId);
+
+        string response = CallOnlineDatabaseWithResponse("OnlineGames.php", formData);
+
+        response = "{\"onlineGameList\" : [ " + response + " ]}";
+
+        OnlineGames og = new OnlineGames();
+        JsonUtility.FromJsonOverwrite(response, og);
+
+
+        //TODO Continue development here
     }
 
     internal List<LocalGameScript> GetLocalGames(GameObject prefab) {

+ 30 - 0
dbFiles/OnlineGames.php

@@ -0,0 +1,30 @@
+<?php
+/* Games.php */ 
+	$hostname = 'localhost';
+	$username = 'narKampen';
+	$pass = 'narKampenPassword';
+	$database = 'narKampen';
+	
+	$conn = new mysqli($hostname, $username, $pass, $database);
+	if (!$conn) {
+		die("Connection Failed. ". mysqli_connect_error());
+	}
+	mysqli_set_charset($conn,'utf8');
+	
+	$userId = $conn->real_escape_string($_POST['userId']);
+		
+	$sql = "SELECT * FROM game WHERE id IN (SELECT gameId FROM gamePlayers WHERE playerId = $userId)";
+	$result = $conn->query($sql);
+	if ($result->num_rows > 0) {
+		$returnArray = array();
+		$data = $result->fetch_assoc();
+	    foreach ($data as $key => $value) {
+			$returnArray[$key] = $value;
+        }
+		echo json_encode($returnArray);
+	} else {
+		echo "No games found for user";
+	}
+	$conn->close();
+	
+?>