Bläddra i källkod

refactor OnlineGameList

Axel Nordh 5 år sedan
förälder
incheckning
4b7bcf7706

+ 28 - 12
Assets/AnswerLineInfoScript.cs

@@ -20,6 +20,7 @@ public class AnswerLineInfoScript : MonoBehaviour
     private List<KeyValuePair<string, int>> players;
     private string currentPlayerShown;
     private String signedInName;
+    private GameManagerScript gameManagerScript;
     
     private void Start() {
         nextPlayerButton.onClick.AddListener(ShowNextPlayerAnswerLine);
@@ -27,6 +28,7 @@ public class AnswerLineInfoScript : MonoBehaviour
         currentPlayerShown = GameManagerScript.GetCurrentPlayer();
         signedInName = Database.Instance.GetSignedInUser().Value;
         lockedQuestionsText.resizeTextMaxSize = unlockedQuestionsText.resizeTextMaxSize;
+        gameManagerScript = gameManager.GetComponent<GameManagerScript>();
     }
 
     private void Update() {
@@ -37,16 +39,23 @@ public class AnswerLineInfoScript : MonoBehaviour
     }
 
     private void SetQuestionClickable() {
-        if (GameManagerScript.GetCurrentPlayer().Equals(currentPlayerShown, StringComparison.InvariantCultureIgnoreCase)) {
-            newQuestion.GetComponent<NewQuestionCardController>().BackClickable = true;
+        List<KeyValuePair<string, int>> players = gameManagerScript.GetPlayers();
+        if (gameManagerScript.GetGameMode().Equals("ONLINE")) {
+            if (players.Count > 1 && currentPlayerShown != null) {
+                if (GameManagerScript.GetCurrentPlayer().Equals(currentPlayerShown, StringComparison.InvariantCultureIgnoreCase)) {
+                    newQuestion.GetComponent<NewQuestionCardController>().BackClickable = true;
+                } else {
+                    newQuestion.GetComponent<NewQuestionCardController>().BackClickable = false;
+                }
+            }
         } else {
-            newQuestion.GetComponent<NewQuestionCardController>().BackClickable = false;
+                newQuestion.GetComponent<NewQuestionCardController>().BackClickable = true;
         }
     }
 
     public void UpdateButtonsText() {
         if (players == null) {
-            players = gameManager.GetComponent<GameManagerScript>().GetPlayers();
+            players = gameManagerScript.GetPlayers();
         }
 
         if (players.Count <= 1) {
@@ -56,8 +65,10 @@ public class AnswerLineInfoScript : MonoBehaviour
             nextPlayerButton.gameObject.SetActive(true);
             prevPlayerButton.gameObject.SetActive(true);
 
-            if (currentPlayerShown == null) {
+            if (gameManagerScript.GetGameMode().Equals("Online") && currentPlayerShown == null) { // fungerar enbart vid online
                 currentPlayerShown = signedInName;
+            } else if (gameManagerScript.GetGameMode().Equals("Local") && currentPlayerShown == null) {
+                currentPlayerShown = Database.Instance.GetCurrentPlayer(gameManagerScript.GameId,gameManagerScript.GameMode);
             }
 
             string playerBaseText = LocalizationManager.Instance.GetText("ANSWERLINE_OTHER_PLAYER");
@@ -140,7 +151,6 @@ public class AnswerLineInfoScript : MonoBehaviour
 
     private void ShowNextPlayerAnswerLine()
     {
-        GameManagerScript gameManagerScript = gameManager.GetComponent<GameManagerScript>();
         if (nextPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName.Equals(LocalizationManager.Instance.GetText("YOUR"))) {
             currentPlayerShown = signedInName;
         } else {
@@ -155,7 +165,6 @@ public class AnswerLineInfoScript : MonoBehaviour
 
     private void ShowPrevPlayerAnswerLine()
     {       
-        GameManagerScript gameManagerScript = gameManager.GetComponent<GameManagerScript>();
         if (prevPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName.Equals(LocalizationManager.Instance.GetText("YOUR"))) {
             currentPlayerShown = signedInName;
         } else {
@@ -173,11 +182,18 @@ public class AnswerLineInfoScript : MonoBehaviour
         ScrollViewScript scrollViewScript = scrollView.GetComponent<ScrollViewScript>();
         scrollViewScript.RemoveEverythingFromAnswerline();
         scrollViewScript.SetQuestionsInAnswerLine(questions);
-        //if (GameManagerScript.GetCurrentPlayer().Equals(currentPlayerShown)) {
-        if (Database.Instance.GetSignedInUser().Value.Equals(currentPlayerShown, StringComparison.InvariantCultureIgnoreCase)) {
-            scrollViewScript.SetQuestionsFrosted(false);
-        } else {
-            scrollViewScript.SetQuestionsFrosted(true);
+        if (gameManagerScript.GetGameMode().Equals("Online")) {
+            if (Database.Instance.GetSignedInUser().Value.Equals(currentPlayerShown, StringComparison.InvariantCultureIgnoreCase)) {
+                scrollViewScript.SetQuestionsFrosted(false);
+            } else {
+                scrollViewScript.SetQuestionsFrosted(true);
+            }
+        } else  {
+            if (currentPlayerShown.Equals(Database.Instance.GetCurrentPlayer(gameManagerScript.GameId, gameManagerScript.GameMode))) {
+                scrollViewScript.SetQuestionsFrosted(false);
+            } else {
+                scrollViewScript.SetQuestionsFrosted(true);
+            }
         }
     }
 }

+ 26 - 49
Assets/Scripts/Database/OnlineDatabase.cs

@@ -151,6 +151,7 @@ public class OnlineDatabase : MonoBehaviour {
         public string userId;
         public string username;
         public string userLockedQuestions;
+        public string playerStatus;
     }
 
     [Serializable]
@@ -300,78 +301,54 @@ public class OnlineDatabase : MonoBehaviour {
         OnlineGameScript ogs = null;
         List<OnlineGameScript> games = new List<OnlineGameScript>();
         int gameId = -1;
-        string playerToAct = "";
 
         foreach (OnlineGame game in og.onlineGamesList) {
             Int32.TryParse(game.id, out int currentGameId);
-            if (gameId != currentGameId) {
-                if (ogs != null) {
+            if (gameId != currentGameId) { // Spel ej i listan
+                if (ogs != null) { // lägg till spel i listan, inte första gången
+                    SetGlobalGameInfo(userName, ogs, gameId, game);
                     games.Add(ogs);
                 }
                 onlineGameObject = Instantiate(prefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
                 ogs = onlineGameObject.GetComponent<OnlineGameScript>();
                 gameId = currentGameId;
 
-                if (game.currentPlayer.Equals(userId.ToString())) {
-                    playerToAct = game.username;
+                /*    if (game.currentPlayer.Equals(userId.ToString())) { // Inloggad spelares tur att agera
+                        ogs.CurrentPlayer = game.username;
+                        ogs.addPlayer(game.username);
+                    } else */
+                if (game.currentPlayer.Equals(game.userId)) { // om första raden i spelinfo är annan spelare och det är dennes tur att agera
                     ogs.CurrentPlayer = game.username;
                     ogs.addPlayer(game.username);
-                    ogs.SetGameStatus("YOUR_TURN");
-                } else if (game.currentPlayer.Equals(game.userId)) {
-                    ogs.CurrentPlayer = game.username;
+                    ogs.PlayerInfos.Add(new KeyValuePair<string, string>(game.username, game.playerStatus));
                 }
-            } else {
+            } else { // Spel redan i listan, fyll på med info
                 ogs.addPlayer(game.username);
-                if (game.currentPlayer.Equals(userId.ToString())) {
-                    ogs.SetGameStatus("YOUR_TURN");
-                    playerToAct = game.username;
-                    ogs.CurrentPlayer = game.username;
-                    ogs.addPlayer(game.username);
-                } else if (game.currentPlayer.Equals(game.userId)) {
+                ogs.PlayerInfos.Add(new KeyValuePair<string, string>(game.username, game.playerStatus));
+                if (game.currentPlayer.Equals(game.userId)) {
                     ogs.CurrentPlayer = game.username;
                 }
             }
-            
-            ogs.SetGameStatus(game.status);
-            //ogs.CurrentPlayer = game.currentPlayer;
-            List<KeyValuePair<string, string>> playerInfos = GetGameInfo(gameId); // TODO, This is not good, too many db calls!
-            if (game.status.Equals("PENDING")) {
-                string extraInfo = "";
-                foreach (KeyValuePair<string, string> s in playerInfos) {
-                    if (s.Value.Equals("WAITING") && s.Key.Equals(userName, StringComparison.InvariantCultureIgnoreCase)) {
-                        ogs.SetGameStatus("INVITED");
-                        extraInfo += s.Key + ",";
-                    } else if (s.Value.EndsWith("WAITING")) {
-                        extraInfo += s.Key + ",";
-                    }
-                }
-                extraInfo = extraInfo.TrimEnd(',');
-                ogs.SetGameStatusText(extraInfo);
-
-            } else if (game.status.Equals("OTHERS_TURN")) {
-                ogs.SetGameStatusText(ogs.CurrentPlayer);
-            } else if (game.status.Equals("ACTIVE")) {
-                ogs.SetGameStatusText(ogs.CurrentPlayer);
-            } else {
-                ogs.SetGameStatusText();
-            }
 
-            ogs.SetId(game.id);
-            ogs.SetWinNumber(game.winNumber);
-            ogs.SetAnswerTimer(game.answerTimer);
-            ogs.SetRoundTimeLimit(game.roundTimeLimit);
-            ogs.SetRound(game.round);
-            ogs.StartDate = game.startDate;
-            ogs.LastPlayedDate = game.lastPlayedDate;
-
-            ogs.PlayerInfos = playerInfos;
+  
         }
-
+        SetGlobalGameInfo(userName, ogs, gameId, og.onlineGamesList[og.onlineGamesList.Count]);
         games.Add(ogs);
         
         return games;
     }
 
+    private void SetGlobalGameInfo(string userName, OnlineGameScript ogs, int gameId, OnlineGame game) {
+        ogs.SetGameStatus(game.status);
+        ogs.SetId(game.id);
+        ogs.SetWinNumber(game.winNumber);
+        ogs.SetAnswerTimer(game.answerTimer);
+        ogs.SetRoundTimeLimit(game.roundTimeLimit);
+        ogs.SetRound(game.round);
+        ogs.StartDate = game.startDate;
+        ogs.LastPlayedDate = game.lastPlayedDate;
+    }
+
     internal List<KeyValuePair<string, string>> GetGameInfo(int gameId) {
         List<KeyValuePair<string, string>> returnList = new List<KeyValuePair<string, string>>();
 

+ 7 - 2
Assets/Scripts/GameManagerScript.cs

@@ -7,7 +7,6 @@ using UnityEngine.SceneManagement;
 
 public class GameManagerScript : MonoBehaviour {
 
-    private int playerCount;
     private int questionTimer;
     private string gameMode;
 
@@ -263,7 +262,13 @@ public class GameManagerScript : MonoBehaviour {
         dialog.SetMessage(String.Format(message, currentPlayer));
         dialog.SetOnAccept(LocalizationManager.Instance.GetText("OK"), () => {
             scrollViewScript.RemoveEverythingFromAnswerline();
-            List<NewQuestionData> questions = Database.Instance.GetPlayerQuestions(GameId, Database.Instance.GetSignedInUser().Value, GetGameMode());
+            string gameMode = GetGameMode();
+            List<NewQuestionData> questions = new List<NewQuestionData>();
+            if (gameMode.Equals("Online")) {
+                questions = Database.Instance.GetPlayerQuestions(GameId, Database.Instance.GetSignedInUser().Value, gameMode);
+            } else {
+                questions = Database.Instance.GetPlayerQuestions(GameId, currentPlayer, gameMode);
+            }
             scrollViewScript.SetQuestionsInAnswerLine(questions);
 
             statsScript.SetQuestionsLost(Database.Instance.GetQuestionsLost(GameId, currentPlayer, GetGameMode()));

+ 3 - 6
Assets/Scripts/NewGameScene/OnlineGameScript.cs

@@ -8,6 +8,7 @@ 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(219, 12, 65, 100), new Color32(12,12,219,150) };
+
     public Text gameStatusText;
     public Text gameTitleText;
 
@@ -46,8 +47,7 @@ public class OnlineGameScript : MonoBehaviour {
         set {
             gameStatus = value;
             SetColor(gameStatus);
-
-            //SetGameStatusText();
+            SetGameStatusText();
         }
     }
 
@@ -123,10 +123,7 @@ public class OnlineGameScript : MonoBehaviour {
     }
 
     public void SetGameStatusText() {
-        SetGameStatusText("");
-    }
-
-    public void SetGameStatusText(string extraInfo) {
+        String extraInfo = ""; // TODO
         if ("PENDING".Equals(GameStatus)) {
             gameStatusText.gameObject.GetComponent<TextLocalization>().key = PENDING;
             gameStatusText.text = LocalizationManager.Instance.GetText(PENDING);

BIN
Assets/narKampenLocal.db


+ 1 - 1
dbFiles/OnlineGames.php

@@ -26,7 +26,7 @@
 				"ORDER BY FIELD(PlayerToAct, '$userName') DESC, lastPlayedDate DESC";
 */
 
-		$sql = "SELECT game.*, users.id as userId, users.username as username, gamePlayers.userLockedQuestions FROM game " . 
+		$sql = "SELECT game.*, users.id as userId, users.username as username, gamePlayers.userLockedQuestions, gamePlayers.status playerStatus FROM game " . 
 			"INNER JOIN gamePlayers ON gamePlayers.gameId = game.id " . 
 			"INNER JOIN users ON users.id = playerId " .
 			"WHERE game.id IN (SELECT gameId FROM gamePlayers WHERE playerId = $userId) " .