소스 검색

Mer online game info uppdateringar

Axel Nordh 6 년 전
부모
커밋
62718cc5ef

+ 4 - 3
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.0000058487026}
+  m_AnchoredPosition: {x: -0.000045776367, y: -0.000022772734}
   m_SizeDelta: {x: 0, y: 0}
   m_Pivot: {x: 0.5, y: 1}
 --- !u!114 &321314050
@@ -6177,7 +6177,7 @@ MonoBehaviour:
   m_TargetGraphic: {fileID: 761483707}
   m_HandleRect: {fileID: 761483706}
   m_Direction: 2
-  m_Value: 0
+  m_Value: 1
   m_Size: 1
   m_NumberOfSteps: 0
   m_OnValueChanged:
@@ -9711,7 +9711,8 @@ MonoBehaviour:
   timeToAnswerInputField: {fileID: 1475087768}
   localGamePrefab: {fileID: 5023127887778350162, guid: e9fabf2145e478e41b3c41f4ed3306cc,
     type: 3}
-  onlineGamePrefab: {fileID: 0}
+  onlineGamePrefab: {fileID: 4443270113283690094, guid: 7f480eea4df9d6b4a8174b27a5f06df2,
+    type: 3}
   finishedGamePrefab: {fileID: 0}
 --- !u!1 &1698485521
 GameObject:

+ 4 - 3
Assets/Prefab/OnlineGame.prefab

@@ -227,7 +227,7 @@ MonoBehaviour:
   m_Script: {fileID: 11500000, guid: 539bd096d40d9934d916d02e161ee660, type: 3}
   m_Name: 
   m_EditorClassIdentifier: 
-  key: ONLINE_GAME_STATUS_TITLE_VALUE_PENDING
+  key: ONLINE_GAME_STATUS_VALUE_PENDING
 --- !u!114 &4443270113229645985
 MonoBehaviour:
   m_ObjectHideFlags: 0
@@ -299,9 +299,9 @@ RectTransform:
   m_RootOrder: 0
   m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
   m_AnchorMin: {x: 0, y: 0}
-  m_AnchorMax: {x: 0, y: 0}
+  m_AnchorMax: {x: 1, y: 0}
   m_AnchoredPosition: {x: 0, y: 0}
-  m_SizeDelta: {x: 0, y: 0}
+  m_SizeDelta: {x: -30, y: 0}
   m_Pivot: {x: 0.5, y: 0.5}
 --- !u!222 &4443270113283690090
 CanvasRenderer:
@@ -380,6 +380,7 @@ MonoBehaviour:
   m_EditorClassIdentifier: 
   gameStatusText: {fileID: 4443270112465168295}
   gameTitleText: {fileID: 4443270113439902654}
+  gameStatus: 
 --- !u!1 &4443270113439902640
 GameObject:
   m_ObjectHideFlags: 0

+ 55 - 6
Assets/Scripts/Database.cs

@@ -57,8 +57,10 @@ 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()));
+        Debug.Log(playerIds);
         int currentUser = Database.Instance.GetSignedInUser();
         playerIds.Add(currentUser);
+        Debug.Log(inviteUsers);
 
         var form = new WWWForm();
         form.AddField("currentUser", currentUser);
@@ -67,7 +69,7 @@ public class Database : MonoBehaviour {
         form.AddField("limitPerPlayer", limitPerPlayer);
         form.AddField("playerIds", String.Join(",", playerIds));
 
-        CallDatabase("NewOnlineGame.php", form);
+        Debug.Log(CallOnlineDatabaseWithResponse("NewOnlineGame.php", form));
     }
 
     [Serializable]
@@ -83,6 +85,17 @@ public class Database : MonoBehaviour {
         public List<Question> questionsList = new List<Question>();
     }
 
+    [Serializable]
+    public class PlayerInfo {
+        public string username;
+        public string status;
+    }
+
+    [Serializable]
+    public class PlayerInfos {
+        public List<PlayerInfo> playerInfoList = new List<PlayerInfo>();
+    }
+
     [Serializable]
     public class OnlineGame {
         public string id;
@@ -267,7 +280,7 @@ public class Database : MonoBehaviour {
 
         string response = CallOnlineDatabaseWithResponse("OnlineGames.php", formData);
 
-        response = "{\"onlineGamesList\" : [ " + response + " ]}";
+        response = "{\"onlineGamesList\" :  " + response + " }";
 
         OnlineGames og = new OnlineGames();
         JsonUtility.FromJsonOverwrite(response, og);
@@ -278,13 +291,47 @@ public class Database : MonoBehaviour {
         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);
+            ogs.SetGameStatus(game.status);
+            if (game.status.Equals("PENDING")) {
+                Int32.TryParse(game.id, out int gameId);
+                List<KeyValuePair<string, string>> playerInfos = GetGameInfo(gameId);
+                string extraInfo = "";
+                foreach (KeyValuePair<string, string> s in playerInfos) {
+                    if (s.Value.Equals("WAITING")) {
+                        extraInfo += s.Key + ",";
+                    }
+                }
+                extraInfo = extraInfo.TrimEnd(',');
+                ogs.SetGameStatusText(extraInfo);
+            } else if (game.status.Equals("OTHERS_TURN")) {
+                ogs.SetGameStatusText(game.currentPlayer);
+            } else {
+                ogs.SetGameStatusText();
+            }
             games.Add(ogs);
         }
 
         return games;
-        //TODO Continue development here
+    }
+
+    internal List<KeyValuePair<string, string>> GetGameInfo(int gameId) {
+        List<KeyValuePair<string, string>> returnList = new List<KeyValuePair<string, string>>();
+
+        WWWForm form = new WWWForm();
+        form.AddField("gameId", gameId);
+
+        string response = CallOnlineDatabaseWithResponse("OnlineGameInfo.php", form);
+
+        response = "{\"playerInfoList\" : " + response + " }";
+        PlayerInfos pi = new PlayerInfos();
+        JsonUtility.FromJsonOverwrite(response, pi);
+
+        foreach (PlayerInfo p in pi.playerInfoList) {
+            KeyValuePair<string, string> player = new KeyValuePair<string, string>(p.username, p.status);
+            returnList.Add(player);
+        }
+
+        return returnList;
     }
 
     internal List<LocalGameScript> GetLocalGames(GameObject prefab) {
@@ -785,7 +832,9 @@ public class Database : MonoBehaviour {
             }
             // Show result
             string jsonData = www.downloadHandler.text;
-            jsonData = "{\"usernamesList\" : [ " + jsonData + " ]}";
+
+            jsonData = "{\"usernamesList\" :  " + jsonData + " }";
+
             JsonUtility.FromJsonOverwrite(jsonData, uNames);
         }
         // TODO handle empty

+ 1 - 0
Assets/Scripts/InvitePanelScript.cs

@@ -107,6 +107,7 @@ public class InvitePanelScript : MonoBehaviour {
                 GameObject question = Instantiate(inviteSearchResultPrefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
                 InviteSearchResult res = question.GetComponent<InviteSearchResult>();
                 res.SetName(un.username);
+                res.SetId(un.id);
                 res.transform.SetParent(searchResultPanel.transform, false);
             }
         }

+ 0 - 1
Assets/Scripts/Login/Register.cs

@@ -53,7 +53,6 @@ public class Register : MonoBehaviour {
         }
     }
 
-    // TODO Localizaion!
     void RegisterAction() {
         string errorMessage = "";
         if (Username == "" || Username.Length < 3) {

+ 5 - 3
Assets/Scripts/MainMenu.cs

@@ -27,7 +27,6 @@ public class MainMenu : MonoBehaviour {
     private readonly string gamesPostUrl = "nordh.xyz/narKampen/dbFiles/Games.php?";
 
     private void Start() {
-        // GetGames();
         startNewGameButton.onClick.AddListener(StartNewGameAction);
         backButton.onClick.AddListener(BackButtonAction);
 
@@ -36,9 +35,12 @@ public class MainMenu : MonoBehaviour {
         localPlayers.onValueChanged.AddListener(LocalPlayersAction);
         startLocalGameButton.onClick.AddListener(StartLocalGame);
 
-        GetGames();
     }
 
+
+    private void Awake() {
+        GetGames();
+    }
     private void GetGames() {
         loggedInUserId = Database.Instance.GetSignedInUser();
         List<LocalGameScript> localGames = Database.Instance.GetLocalGames(localGamePrefab);
@@ -63,7 +65,7 @@ public class MainMenu : MonoBehaviour {
         if (checkInternetConnection()) {
             List<OnlineGameScript> onlineGames = Database.Instance.GetOnlineGames(loggedInUserId, onlineGamePrefab);
             foreach (OnlineGameScript ogs in onlineGames) {
-                ogs.transform.SetParent(onlineGamesList);
+                ogs.transform.SetParent(onlineGamesList, false);
                 ogs.transform.SetSiblingIndex(onlineIndex++);
             }
         } else {

+ 51 - 20
Assets/Scripts/OnlineGameScript.cs

@@ -1,5 +1,4 @@
 using System;
-using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
@@ -7,7 +6,7 @@ using UnityEngine.UI;
 public class OnlineGameScript : MonoBehaviour {
 
     // Status color Pending, not your turn, your turn, declined
-    List<Color32> statusColors = new List<Color32>() { new Color32(234, 164, 17, 100), new Color32(255, 255, 24, 175), new Color32(0, 255, 0, 100), new Color32(75, 50, 75, 100) };
+    List<Color32> statusColors = new List<Color32>() { new Color32(234, 164, 17, 100), new Color32(255, 255, 24, 175), new Color32(0, 255, 0, 100), new Color32(219, 12, 65, 100) };
     public Text gameStatusText;
     public Text gameTitleText;
 
@@ -30,17 +29,27 @@ public class OnlineGameScript : MonoBehaviour {
     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,
-        YOUR_TURN,
-        DECLINED
+    public string GameStatus {
+        get { return gameStatus; }
+        set {
+            gameStatus = value;
+            SetColor(gameStatus);
+        }
     }
 
+    private string PENDING = "ONLINE_GAME_STATUS_VALUE_PENDING";
+    private string OTHERS_TURN = "ONLINE_GAME_STATUS_VALUE_OTHERS_TURN";
+    private string DECLINED = "ONLINE_GAME_STATUS_VALUE_DECLINED";
+    private string YOUR_TURN = "ONLINE_GAME_STATUS_VALUE_YOUR_TURN";
+
+    // behövs nog en partialy declined status också, om en av fyra tackar nej kanske man viss spela ändå.
+    // behövs också en med dina inbjudningar.
+
+    public string gameStatus;
+
     // Start is called before the first frame update
     void Start() {
-
+        
     }
 
     // Update is called once per frame
@@ -48,15 +57,19 @@ public class OnlineGameScript : MonoBehaviour {
 
     }
 
-    public void SetStatusColor(Status status) {
-        if (status.Equals(Status.PENDING)) {
-            this.GetComponent<Image>().color = statusColors[0];
-        } else if (status.Equals(Status.OTHERS_TURN)) {
-            this.GetComponent<Image>().color = statusColors[1];
-        } else if (status.Equals(Status.YOUR_TURN)) {
-            this.GetComponent<Image>().color = statusColors[2];
-        } else if (status.Equals(Status.DECLINED)) {
-            this.GetComponent<Image>().color = statusColors[3];
+    private void SetColor(string status) {
+        Image image = this.GetComponent<Image>();
+        Debug.Log(status);
+        if ("PENDING".Equals(status)) {
+            image.color = statusColors[0];
+        } else if ("YOUR_TURN".Equals(status)) {
+            image.color = statusColors[1];
+        } else if ("OTHERS_TURN".Equals(status)) {
+            image.color = statusColors[2];
+        } else if ("DECLINED".Equals(status)) {
+            image.color = statusColors[3];
+        } else {
+            image.color = new Color32(100, 100, 100, 100);
         }
     }
 
@@ -64,8 +77,22 @@ public class OnlineGameScript : MonoBehaviour {
         gameTitleText.text = text;
     }
 
-    public void SetGameStatusText(string text) {
-        gameStatusText.text = text;
+    public void SetGameStatusText() {
+        SetGameStatusText("");
+    }
+    public void SetGameStatusText(string extraInfo) {
+        if ("PENDING".Equals(GameStatus)) {
+            gameStatusText.text = LocalizationManager.Instance.GetText(PENDING);
+        } else if ("YOUR_TURN".Equals(GameStatus)) {
+            gameStatusText.text = LocalizationManager.Instance.GetText(YOUR_TURN);
+        } else if ("OTHERS_TURN".Equals(GameStatus)) {
+            string message = LocalizationManager.Instance.GetText(OTHERS_TURN);
+            gameStatusText.text = String.Format(message, extraInfo);
+        } else if ("DECLINED".Equals(GameStatus)) {
+            gameStatusText.text = LocalizationManager.Instance.GetText(DECLINED);
+        } else {
+            gameStatusText.text = "SOMETHING WRONG WITH STATUS TITLE FROM TEXT " + GameStatus;
+        }
     }
 
     public void SetId(string id) {
@@ -92,4 +119,8 @@ public class OnlineGameScript : MonoBehaviour {
         Int32.TryParse(round, out int intRound);
         Round = intRound;
     }
+
+    public void SetGameStatus(string status) {
+        GameStatus = status;
+    }
 }

+ 1 - 1
Assets/Translations/ENGLISH.xml

@@ -75,7 +75,7 @@
   <text key="KEEP_SIGNED_IN_TEXT">Keep me signed in</text>
   <text key="ONLINE_GAME_STATUS_TITLE_VALUE_PENDING">Pending answers</text>
   <text key="ONLINE_GAME_STATUS_TITLE">Status</text>
-  <text key="ONLINE_GAME_STATUS_PENDING">Awaiting player actions</text>
+  <text key="ONLINE_GAME_STATUS_PENDING">Waiting for player {0}</text>
   <text key="ONLINE_GAME_STATUS_YOUR_TURN">Your turn</text>
   <text key="ONLINE_GAME_STATUS_ANOTHER_PLAYER">{0}s turn</text>
   <text key="ONLINE_GAME_STATUS_DECLINED">Game invite declined</text>

+ 1 - 1
Assets/Translations/SWEDISH.xml

@@ -76,7 +76,7 @@
   <text key="KEEP_SIGNED_IN_TEXT">Håll mig inloggad</text>
   <text key="ONLINE_GAME_STATUS_VALUE_PENDING">Väntar svar</text>
   <text key="ONLINE_GAME_STATUS_TITLE">Status</text>
-  <text key="ONLINE_GAME_STATUS_PENDING">Väntar på spelare</text>
+  <text key="ONLINE_GAME_STATUS_PENDING">Väntar på spelare {0}</text>
   <text key="ONLINE_GAME_STATUS_YOUR_TURN">Din tur</text>
   <text key="ONLINE_GAME_STATUS_ANOTHER_PLAYER">{0}s tur</text>
   <text key="ONLINE_GAME_STATUS_DECLINED">Spelet nekat</text>

BIN
Assets/narKampenLocal.db


+ 36 - 0
dbFiles/OnlineGameInfo.php

@@ -0,0 +1,36 @@
+<?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');
+	
+	$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);
+	
+	
+	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";
+	}
+	$conn->close();
+	
+?>

+ 10 - 4
dbFiles/OnlineGames.php

@@ -15,13 +15,19 @@
 		
 	$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;
-        }
+		$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";
 	}

+ 1 - 1
dbFiles/PlayerSearch.php

@@ -22,7 +22,7 @@
 	
 	if (mysqli_num_rows($result) > 0) {
 		while ($row = mysqli_fetch_assoc($result)) {
-			$json += array('id' => $row['id'],
+			$json[] = array('id' => $row['id'],
 				'username' => $row['username']);
 		}
 		$jsonString = json_encode($json);