Axel Nordh 6 лет назад
Родитель
Сommit
ee4c333dc3
3 измененных файлов с 81 добавлено и 6 удалено
  1. 13 3
      Assets/Scripts/Database.cs
  2. 47 1
      Assets/Scripts/OnlineGameScript.cs
  3. 21 2
      dbFiles/NewOnlineGame.php

+ 13 - 3
Assets/Scripts/Database.cs

@@ -57,9 +57,11 @@ public class Database : MonoBehaviour {
     internal void SetupNewOnlineGame(int limitPerQuestion, int limitPerPlayer, int toWin, List<InviteSearchResult> inviteUsers) {
         List<int> playerIds = new List<int>();
         inviteUsers.ForEach(i => playerIds.Add(i.GetId()));
-        playerIds.Add(Database.Instance.GetSignedInUser());
+        int currentUser = Database.Instance.GetSignedInUser();
+        playerIds.Add(currentUser);
 
         var form = new WWWForm();
+        form.AddField("currentUser", currentUser);
         form.AddField("winNumber", toWin);
         form.AddField("limitPerQuestion", limitPerQuestion);
         form.AddField("limitPerPlayer", limitPerPlayer);
@@ -92,6 +94,7 @@ public class Database : MonoBehaviour {
         public string startDate;
         public string LastPlayedDate;
         public string finishedDate;
+        public string status;
     }
 
     [Serializable]
@@ -257,19 +260,26 @@ public class Database : MonoBehaviour {
         return this.questionTimer;
     }
 
-    internal void GetOnlineGames(int userId) {
+    internal void GetOnlineGames(int userId, GameObject prefab) {
 
         WWWForm formData = new WWWForm();
         formData.AddField("userId", userId);
 
         string response = CallOnlineDatabaseWithResponse("OnlineGames.php", formData);
 
-        response = "{\"onlineGameList\" : [ " + response + " ]}";
+        response = "{\"onlineGamesList\" : [ " + response + " ]}";
 
         OnlineGames og = new OnlineGames();
         JsonUtility.FromJsonOverwrite(response, og);
 
+        GameObject onlineGameObject;
+        OnlineGameScript ogs = null;
+        foreach (OnlineGame game in og.onlineGamesList) {
+            onlineGameObject = Instantiate(prefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
+            ogs = onlineGameObject.GetComponent<OnlineGameScript>();
 
+            ogs.SetGameStatusText(game.status);
+        }
         //TODO Continue development here
     }
 

+ 47 - 1
Assets/Scripts/OnlineGameScript.cs

@@ -1,4 +1,5 @@
-using System.Collections;
+using System;
+using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
@@ -10,6 +11,26 @@ public class OnlineGameScript : MonoBehaviour {
     public Text gameStatusText;
     public Text gameTitleText;
 
+    private int id;
+    private int winNumber;
+    private int answerTimer;
+    private int roundTimeLimit;
+    private string currentPlayer;
+    private int round;
+    private string startDate;
+    private string LastPlayedDate;
+    private string finishedDate;
+
+    public int Id { get => id; set => id = value; }
+    public int WinNumber { get => winNumber; set => winNumber = value; }
+    public int AnswerTimer { get => answerTimer; set => answerTimer = value; }
+    public int RoundTimeLimit { get => roundTimeLimit; set => roundTimeLimit = value; }
+    public string CurrentPlayer { get => currentPlayer; set => currentPlayer = value; }
+    public int Round { get => round; set => round = value; }
+    public string StartDate { get => startDate; set => startDate = value; }
+    public string LastPlayedDate1 { get => LastPlayedDate; set => LastPlayedDate = value; }
+    public string FinishedDate { get => finishedDate; set => finishedDate = value; }
+
     public enum Status {
         PENDING,
         OTHERS_TURN,
@@ -46,4 +67,29 @@ public class OnlineGameScript : MonoBehaviour {
     public void SetGameStatusText(string text) {
         gameStatusText.text = text;
     }
+
+    public void SetId(string id) {
+        Int32.TryParse(id, out int intId);
+        Id = intId;
+    }
+
+    public void SetWinNumber(string winNumber) {
+        Int32.TryParse(winNumber, out int intWinNumber);
+        WinNumber = intWinNumber;
+    }
+
+    public void SetAnswerTimer(string answerTimer) {
+        Int32.TryParse(answerTimer, out int intAnswerTimer);
+        AnswerTimer = intAnswerTimer;
+    }
+
+    public void SetRoundTimeLimit(string roundTimeLimit) {
+        Int32.TryParse(roundTimeLimit, out int intRoundTimelimit);
+        RoundTimeLimit = intRoundTimelimit;
+    }
+
+    public void SetRound(string round) {
+        Int32.TryParse(round, out int intRound);
+        Round = intRound;
+    }
 }

+ 21 - 2
dbFiles/NewOnlineGame.php

@@ -16,11 +16,18 @@
 	$limitPerQuestion = $_POST['limitPerQuestion'];
 	$limitPerPlayer = $_POST['limitPerPlayer'];
 	$playerIds = $_POST['playerIds'];
+	$currentUser = $_POST['currentUser'];
 	
 	$playerIdsArray = explode(",",$playerIds);
-	
+	$playerCount = count($playerIdsArray);
+	if ($playerCount == 1) {
+		$status = "STARTED";
+	} else {
+		$status = "PENDING";
+	}
 	$sql = "INSERT INTO game(" .
             "gameMode, " .
+			"status, " .
             "winNumber, " .
             "answerTimer, " .
             "roundTimeLimit, " .
@@ -30,10 +37,11 @@
             "startedDate) " .
             "VALUES (" .
             "'Online', " .
+			"'$status', " .
             "$winNumber, " .
             "$limitPerQuestion, " .
             "$limitPerPlayer, " .
-            count($playerIdsArray) . ", " .
+            "$playerCount, " .
             "(SELECT username FROM users WHERE id IN ($playerIds) ORDER BY RAND() LIMIT 1), " .
             "1, " .
             "NOW())";
@@ -43,5 +51,16 @@
 	if ($error === "") {
 		echo $error;
 	}
+	$gameId = mysqli_insert_id($conn);
+	$playerPartInsertSql = "";
+	foreach ($playerIdsArray AS $playerId) {
+		if ($currentUser == $playerId) {
+			$playerPartInsertSql .= "($gameId, $playerId, 1, 1, 'ACCEPTED'),";
+		} else {
+			$playerPartInsertSql .= "($gameId, $playerId, 1, 1, 'WAITING'),";
+		}
+	}
+	$playerPartInsertSql = rtrim($playerPartInsertSql,",");
+	$playerSql = "INSERT INTO gamePlayers (gameId, playerId, userLockedQuestions, questionsLost, status) VALUES $playerPartInsertSql"
 	
 ?>