From 148c378387cc5c7fa2eb9d015320fa74978ce0bd Mon Sep 17 00:00:00 2001 From: KeyKoder Date: Thu, 15 Jul 2021 11:31:18 +0200 Subject: [PATCH] Close receiver thread when disconnecting --- unity/Assets/Prefabs/CardCanvas.prefab | 1 + unity/Assets/Scenes/SampleScene.unity | 2 +- unity/Assets/Scripts/Client.cs | 10 ++++--- unity/Assets/Scripts/GameLoader.cs | 33 +++++++++++----------- unity/Assets/Scripts/MainMenuController.cs | 5 +++- unity/Assets/Scripts/TcpConnection.cs | 5 ++++ 6 files changed, 34 insertions(+), 22 deletions(-) diff --git a/unity/Assets/Prefabs/CardCanvas.prefab b/unity/Assets/Prefabs/CardCanvas.prefab index d89e9e3..e5cff3e 100644 --- a/unity/Assets/Prefabs/CardCanvas.prefab +++ b/unity/Assets/Prefabs/CardCanvas.prefab @@ -81,6 +81,7 @@ MonoBehaviour: m_FallbackScreenDPI: 96 m_DefaultSpriteDPI: 96 m_DynamicPixelsPerUnit: 1 + m_PresetInfoIsWorld: 0 --- !u!114 &5463950459347836261 MonoBehaviour: m_ObjectHideFlags: 0 diff --git a/unity/Assets/Scenes/SampleScene.unity b/unity/Assets/Scenes/SampleScene.unity index 289e1c8..1d5f722 100644 --- a/unity/Assets/Scenes/SampleScene.unity +++ b/unity/Assets/Scenes/SampleScene.unity @@ -135,7 +135,7 @@ GameObject: - component: {fileID: 40103895} - component: {fileID: 40103897} m_Layer: 0 - m_Name: DeckGen + m_Name: Game Loader m_TagString: Untagged m_Icon: {fileID: 0} m_NavMeshLayer: 0 diff --git a/unity/Assets/Scripts/Client.cs b/unity/Assets/Scripts/Client.cs index e6c7749..cc13dd4 100644 --- a/unity/Assets/Scripts/Client.cs +++ b/unity/Assets/Scripts/Client.cs @@ -40,10 +40,6 @@ public class Client : MonoBehaviour { } } - void OnApplicationQuit() { - CloseConnection(); - } - public class NetEventManager { private Dictionary> returnNameHandlers = new Dictionary>(); private Dictionary> returnConnectHandlers = new Dictionary>(); @@ -118,6 +114,7 @@ public class Client : MonoBehaviour { } break; case Protocol.ServerClientPacket.DataOneofCase.ReturnCardImage: { + Debug.Log("got: "+p); var v = new Action[this.returnCardImageHandlers.Count]; this.returnCardImageHandlers.Values.CopyTo(v, 0); foreach (var n in v) { @@ -273,6 +270,10 @@ public class Client : MonoBehaviour { conn.SendMessage(new Protocol.ClientServerPacket() { QueryUsers = new Empty() }); } + public void GetGameStatus() { + conn.SendMessage(new Protocol.ClientServerPacket() { QueryGameStatus = new Empty() }); + } + public List GetLobbyVotes(uint game) { var gameVotes = new List(); if (lobby_status.Get() != null){ @@ -373,6 +374,7 @@ public class Client : MonoBehaviour { public void Close() { conn.SendMessage(new Protocol.ClientServerPacket() {Disconnect = new Empty()}); + conn.Close(); } } diff --git a/unity/Assets/Scripts/GameLoader.cs b/unity/Assets/Scripts/GameLoader.cs index 299078c..4a98732 100644 --- a/unity/Assets/Scripts/GameLoader.cs +++ b/unity/Assets/Scripts/GameLoader.cs @@ -1,7 +1,9 @@ -using System.Collections; +using System; +using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; +using Random = UnityEngine.Random; public class GameLoader : MonoBehaviour { public GameObject table; @@ -14,12 +16,15 @@ public class GameLoader : MonoBehaviour { public GameObject deck; public GameObject thrownCards; public static MainMenuController mmc; - public static System.Action onReloadPiles; + public static GameLoader myself; - private int tmpCounter = 0; + void Awake() { + myself = this; + } void Start() { mmc = FindObjectOfType(); mmc.gameObject.SetActive(false); + var angleDelta = (270) / playerCount; var dst = 15; var angleOffset = 45; @@ -32,15 +37,13 @@ public class GameLoader : MonoBehaviour { var player = Instantiate(playerCube, pos, Quaternion.AngleAxis(-angle, Vector3.up)); player.GetComponent().material.color = playerColors[i]; } - SpawnClientCards(); - SpawnCommonCards(); - onReloadPiles += SpawnCommonCards; - onReloadPiles += SpawnClientCards; + ReloadPiles(); } void SpawnCard(Client.ConnectionImpl conn, string cardKind, GameObject parent, System.Action callback) { - var card = Instantiate(cardPrefab, Vector3.zero, Quaternion.identity, parent.transform); - conn.eventManager.AddHandler("game_card_image", (Game.Image image) => { + string uuid = Guid.NewGuid().ToString(); + conn.eventManager.AddHandler("game_card_image_"+uuid, (Game.Image image) => { + var card = Instantiate(cardPrefab, Vector3.zero, Quaternion.identity, parent.transform); var front = new Texture2D(1, 1); front.LoadImage(image.Face.Span.ToArray()); var back = new Texture2D(1, 1); @@ -49,7 +52,7 @@ public class GameLoader : MonoBehaviour { card.transform.Find("Images").GetComponentsInChildren()[1].texture = back; card.transform.localScale = new Vector3(0.15f, 0.15f, 1f); callback.Invoke(card); - Client.GetConnection().eventManager.RemoveHandler(Protocol.ServerClientPacket.DataOneofCase.ReturnCardImage, "game_card_image"); + conn.eventManager.RemoveHandler(Protocol.ServerClientPacket.DataOneofCase.ReturnCardImage, "game_card_image_"+uuid); }); conn.GetCardImage(cardKind); } @@ -57,9 +60,7 @@ public class GameLoader : MonoBehaviour { void SpawnClientCards() { var conn = Client.GetConnection(); if (conn != null) { - Debug.Log("Spawning player cards."); var clientPiles = conn.GetPlayerPiles(mmc.currentUsername); - Debug.Log("[player] clientPiles = "+clientPiles); if (clientPiles == null) return; foreach (string key in clientPiles.Keys) { var pile = clientPiles[key]; @@ -76,15 +77,14 @@ public class GameLoader : MonoBehaviour { void SpawnCommonCards() { var conn = Client.GetConnection(); if (conn != null) { - Debug.Log("Spawning common cards."); var clientPiles = conn.GetCommonPiles(); - Debug.Log("[common] clientPiles = " + clientPiles); if (clientPiles == null) return; foreach (string key in clientPiles.Keys) { var pile = clientPiles[key]; int idx = 0; foreach (Game.GameStatus.Types.Card card in pile.Cards) { SpawnCard(conn, card.Kind.Kind, key == "placed" ? thrownCards : deck, (GameObject card) => { + card.GetComponent().ThrowCard(key == "placed" ? thrownCards : deck); card.GetComponent().InitData(key, true, idx); }); idx++; @@ -92,12 +92,13 @@ public class GameLoader : MonoBehaviour { } } } - public static void ReloadPiles() { + if (myself == null) return; var cards = GameObject.FindGameObjectsWithTag("Card"); foreach (GameObject card in cards) { Destroy(card); } - onReloadPiles(); + myself.SpawnCommonCards(); + myself.SpawnClientCards(); } } diff --git a/unity/Assets/Scripts/MainMenuController.cs b/unity/Assets/Scripts/MainMenuController.cs index 6dc894d..b092f7d 100644 --- a/unity/Assets/Scripts/MainMenuController.cs +++ b/unity/Assets/Scripts/MainMenuController.cs @@ -74,7 +74,10 @@ public class MainMenuController : MonoBehaviour { void Update() { var conn = Client.GetConnection(); if(conn != null) { - if (conn.IsStarting()) SceneManager.LoadScene(1); + if (conn.IsStarting()) { + SceneManager.LoadScene(1); + conn.GetGameStatus(); + } } } diff --git a/unity/Assets/Scripts/TcpConnection.cs b/unity/Assets/Scripts/TcpConnection.cs index 3514a5a..67dc89a 100644 --- a/unity/Assets/Scripts/TcpConnection.cs +++ b/unity/Assets/Scripts/TcpConnection.cs @@ -1,3 +1,4 @@ +using System.IO; using System.Net.Sockets; using System.Collections.Concurrent; using System.Threading; @@ -37,6 +38,10 @@ public class TcpConnection { } } + public void Close() { + receiverThread.Join(); + } + public TcpConnection(string address) { string[] ipAndPort = address.Split(':'); c = new TcpClient(ipAndPort[0], int.Parse(ipAndPort[1]));