From 34215a8a6e1a5e1eb522e9f977520d016857c929 Mon Sep 17 00:00:00 2001 From: Palnit <105753906+Palnit@users.noreply.github.com> Date: Wed, 24 May 2023 23:35:26 +0200 Subject: [PATCH] Clean Code WIP --- .../Logic/Managers/GameNetworkManager.cs | 75 ++++++++++++++++++- .../Logic/MultiPlayer/Authentication.cs | 3 + .../MultiPlayer/LobbyScripts/LobbyManager.cs | 9 ++- .../Logic/MultiPlayer/Relay/RelayManager.cs | 14 +++- .../Scripts/Logic/MultiPlayer/SceneLoader.cs | 24 +++++- ChaoticCrates/Assets/Scripts/QuickStart.cs | 14 +++- ChaoticCrates/Assets/Scripts/main.asmdef | 22 ++++++ ChaoticCrates/Assets/Scripts/main.asmdef.meta | 7 ++ 8 files changed, 157 insertions(+), 11 deletions(-) create mode 100644 ChaoticCrates/Assets/Scripts/main.asmdef create mode 100644 ChaoticCrates/Assets/Scripts/main.asmdef.meta diff --git a/ChaoticCrates/Assets/Scripts/Logic/Managers/GameNetworkManager.cs b/ChaoticCrates/Assets/Scripts/Logic/Managers/GameNetworkManager.cs index e00edec..4af7121 100644 --- a/ChaoticCrates/Assets/Scripts/Logic/Managers/GameNetworkManager.cs +++ b/ChaoticCrates/Assets/Scripts/Logic/Managers/GameNetworkManager.cs @@ -15,24 +15,47 @@ namespace Logic.Managers { public class GameNetworkManager : NetworkBehaviour { + /// <summary> + /// Length of a turn got from lobby + /// </summary> public float turnTime; + + /// <summary> + /// Number of turn got from lobby + /// </summary> public int turnCount; + /// <summary> + /// Counts the number of connected players + /// </summary> [FormerlySerializedAs("_connectedCounter")] public ulong connectedCounter = 0; /// <summary> /// Players id to their name and team /// </summary> public Dictionary<ulong,KeyValuePair<string,string>> _nameToIDDictionary; + + /// <summary> + /// Check to get data from lobby only once + /// </summary> private bool _gotDataFromLobby = false; + + /// <summary> + /// Check for a spectator player + /// </summary> public bool isSpec = false; - - public int NumberOfPlayer => _nameToIDDictionary.Count; - + + /// <summary> + /// Current input data from input system + /// </summary> public List<InputData> CurrentInputDatas { get; private set; } + public static event Action<Dictionary<ulong,KeyValuePair<string,string>>> OnReady; + /// <summary> + /// Singleton instance + /// </summary> public static GameNetworkManager Instance { get; private set; } private void Awake() @@ -48,7 +71,10 @@ namespace Logic.Managers GameManager.OnGameStateChanged += GameManagerOnGameStateChanged; } - + /// <summary> + /// Handel GameState change to clear data + /// </summary> + /// <param name="state">Game state</param> private void GameManagerOnGameStateChanged(GameState state) { if (state == GameState.Turn) @@ -57,6 +83,10 @@ namespace Logic.Managers } } + /// <summary> + /// Handel Spectator players + /// </summary> + /// <param name="clientRpcParams">Default client rpc params</param> [ClientRpc] public void SetSpecClientRpc(ClientRpcParams clientRpcParams = default) { @@ -64,6 +94,9 @@ namespace Logic.Managers GameEventSystem.OnSetSpec(); } + /// <summary> + /// Gets data from lobby + /// </summary> private void GetData() { string time = LobbyManager.Instance._hostLobby.Data["TurnTime"].Value; @@ -94,16 +127,29 @@ namespace Logic.Managers }; } + /// <summary> + /// Raises event when to set up scene after a load + /// </summary> public void SetUpScene() { OnReady?.Invoke(_nameToIDDictionary); } + /// <summary> + /// Starts loading next scene + /// </summary> private void LoadNextScene() { SceneLoader.Instance.LoadScene(); } + + /// <summary> + /// Gets each clients name and team that are stored local to server + /// </summary> + /// <param name="playerName">the name of the player</param> + /// <param name="team">the team of the player</param> + /// <param name="serverRpcParams">server rpc params</param> [ServerRpc(RequireOwnership = false)] private void SendNameServerRpc(string playerName, string team, ServerRpcParams serverRpcParams = default) { @@ -115,6 +161,9 @@ namespace Logic.Managers } } + /// <summary> + /// Gives command to clients to send their names trough the SendNameServerRpc + /// </summary> [ClientRpc] private void GetNameClientRpc() { @@ -123,11 +172,17 @@ namespace Logic.Managers } + /// <summary> + /// Clears turn data + /// </summary> public void ClearTurnData() { CurrentInputDatas.Clear(); } + /// <summary> + /// Debuging utility + /// </summary> [DevCommands("List")] public void ListCurrentInputData() { @@ -137,6 +192,14 @@ namespace Logic.Managers } } + + /// <summary> + /// Sends input data to server for validation + /// </summary> + /// <param name="playerObject">the player object of the sender</param> + /// <param name="inputs">the input locations of the sender</param> + /// <param name="type">the type of action</param> + /// <param name="serverRpcParams">server rpc params</param> [ServerRpc(RequireOwnership = false)] public void SendDataServerRpc(NetworkObjectReference playerObject , Vector3[] inputs, ActionType type ,ServerRpcParams serverRpcParams = default) { @@ -149,6 +212,10 @@ namespace Logic.Managers } } + + /// <summary> + /// Input data struct to handel input data + /// </summary> public struct InputData : IComparable<InputData> { public ulong Invoker; diff --git a/ChaoticCrates/Assets/Scripts/Logic/MultiPlayer/Authentication.cs b/ChaoticCrates/Assets/Scripts/Logic/MultiPlayer/Authentication.cs index 16fee61..6196be6 100644 --- a/ChaoticCrates/Assets/Scripts/Logic/MultiPlayer/Authentication.cs +++ b/ChaoticCrates/Assets/Scripts/Logic/MultiPlayer/Authentication.cs @@ -7,6 +7,9 @@ namespace Logic.MultiPlayer { public class Authentication : MonoBehaviour { + /// <summary> + /// Singleton Instance + /// </summary> public static Authentication Instance { get; private set; } void Awake() diff --git a/ChaoticCrates/Assets/Scripts/Logic/MultiPlayer/LobbyScripts/LobbyManager.cs b/ChaoticCrates/Assets/Scripts/Logic/MultiPlayer/LobbyScripts/LobbyManager.cs index a5ae47a..9c805f2 100644 --- a/ChaoticCrates/Assets/Scripts/Logic/MultiPlayer/LobbyScripts/LobbyManager.cs +++ b/ChaoticCrates/Assets/Scripts/Logic/MultiPlayer/LobbyScripts/LobbyManager.cs @@ -11,8 +11,15 @@ using UnityEngine; namespace Logic.MultiPlayer.LobbyScripts { public class LobbyManager : MonoBehaviour - { + { + /// <summary> + /// Singleton Instance + /// </summary> public static LobbyManager Instance { get; private set; } + + /// <summary> + /// Event to handel when the list of lobbies changed + /// </summary> public event EventHandler<LobbyEventArgs> LobbyListChangedEvent; public event EventHandler<LobbyEventArgs> LobbyChangedEvent; diff --git a/ChaoticCrates/Assets/Scripts/Logic/MultiPlayer/Relay/RelayManager.cs b/ChaoticCrates/Assets/Scripts/Logic/MultiPlayer/Relay/RelayManager.cs index 0a59dcf..2ac085b 100644 --- a/ChaoticCrates/Assets/Scripts/Logic/MultiPlayer/Relay/RelayManager.cs +++ b/ChaoticCrates/Assets/Scripts/Logic/MultiPlayer/Relay/RelayManager.cs @@ -11,6 +11,9 @@ namespace Logic.MultiPlayer.Relay { public class RelayManager : MonoBehaviour { + /// <summary> + /// Singleton Instance + /// </summary> public static RelayManager Instance { get; private set; } @@ -27,6 +30,12 @@ namespace Logic.MultiPlayer.Relay DontDestroyOnLoad(this); } + + /// <summary> + /// Async function for the creation of a relay allocation + /// </summary> + /// <param name="maxPlayers">max number of players for the relay</param> + /// <returns>the join code of the relay</returns> public async Task<string> CreateRelay(int maxPlayers) { try @@ -51,7 +60,10 @@ namespace Logic.MultiPlayer.Relay } - + /// <summary> + /// async join function to a relay trough join code + /// </summary> + /// <param name="joinCode">the join code of the relay someone wishes to join</param> public async void JoinRelay(string joinCode) { try diff --git a/ChaoticCrates/Assets/Scripts/Logic/MultiPlayer/SceneLoader.cs b/ChaoticCrates/Assets/Scripts/Logic/MultiPlayer/SceneLoader.cs index 2b4bb26..7d802a5 100644 --- a/ChaoticCrates/Assets/Scripts/Logic/MultiPlayer/SceneLoader.cs +++ b/ChaoticCrates/Assets/Scripts/Logic/MultiPlayer/SceneLoader.cs @@ -10,6 +10,9 @@ namespace Logic.MultiPlayer { public class SceneLoader : NetworkBehaviour { + /// <summary> + /// Singleton Instance + /// </summary> public static SceneLoader Instance {get; private set; } private void Awake() @@ -23,17 +26,27 @@ namespace Logic.MultiPlayer GameEventSystem.CountDownFinished += CountDownFinished; } + /// <summary> + /// When the count down finished updates game state to turn to start the game + /// </summary> private void CountDownFinished() { GameManager.Instance.UpdateGameState(GameState.Turn); } + /// <summary> + /// Starts loading the Game scene + /// </summary> public void LoadScene() { Debug.Log("<color=yellow>Loading Scene</color>"); NetworkManager.Singleton.SceneManager.LoadScene("gameScene",LoadSceneMode.Single); } + /// <summary> + /// Coroutine to handel loading progression and raising event to display it on the screen + /// </summary> + /// <param name="asyncOperation">async operator of loading progress </param> private IEnumerator GetLoadingProgress(AsyncOperation asyncOperation) { GameEventSystem.OnIsLoading(true); @@ -44,6 +57,9 @@ namespace Logic.MultiPlayer } } + /// <summary> + /// Coroutine to handel loading progression of the map setup + /// </summary> IEnumerator GetMapProgeress() { while (!MapManager.Instance.isDone.Value) @@ -53,6 +69,9 @@ namespace Logic.MultiPlayer } } + /// <summary> + /// Coroutine waiter for the different kind of coroutines that make up the whole loading progress + /// </summary> IEnumerator CorutinWaiter() { yield return StartCoroutine(GetMapProgeress()); @@ -64,7 +83,10 @@ namespace Logic.MultiPlayer - + /// <summary> + /// callback function for the Scene event types + /// </summary> + /// <param name="sceneEvent">the current scene Loading event</param> public void SceneEventCallback(SceneEvent sceneEvent) { Debug.Log(sceneEvent.SceneEventType); diff --git a/ChaoticCrates/Assets/Scripts/QuickStart.cs b/ChaoticCrates/Assets/Scripts/QuickStart.cs index 574c748..ea9bba5 100644 --- a/ChaoticCrates/Assets/Scripts/QuickStart.cs +++ b/ChaoticCrates/Assets/Scripts/QuickStart.cs @@ -1,3 +1,4 @@ +using System.Threading.Tasks; using Logic.MultiPlayer; using Logic.MultiPlayer.LobbyScripts; using UI.LobbyScripts; @@ -17,10 +18,7 @@ public class QuickStart : MonoBehaviour if (Input.GetKeyDown(host)) { - await Authentication.Instance.Authenticate("asd"); - - LobbyManager.Instance.CreateLobby("Quick Start Lobby",2,"02:00","10000"); - JoinedLobbyUI.Instance.gameObject.SetActive(true); + await StartHost(); } if (Input.GetKeyDown(join)) @@ -34,4 +32,12 @@ public class QuickStart : MonoBehaviour } } + + public async Task StartHost() + { + await Authentication.Instance.Authenticate("asd"); + + LobbyManager.Instance.CreateLobby("Quick Start Lobby",2,"02:00","10000"); + JoinedLobbyUI.Instance.gameObject.SetActive(true); + } } diff --git a/ChaoticCrates/Assets/Scripts/main.asmdef b/ChaoticCrates/Assets/Scripts/main.asmdef new file mode 100644 index 0000000..4d5374f --- /dev/null +++ b/ChaoticCrates/Assets/Scripts/main.asmdef @@ -0,0 +1,22 @@ +{ + "name": "main", + "rootNamespace": "", + "references": [ + "GUID:3b8ed52f1b5c64994af4c4e0aa4b6c4b", + "GUID:1491147abca9d7d4bb7105af628b223e", + "GUID:5540e30183c82e84b954c033c388e06c", + "GUID:a7bd734826f425647b50738e89d1cf9c", + "GUID:84dd41370e50e46aa84025cec29abd60", + "GUID:4c3f49d89436d478ea78315c03159dcc", + "GUID:52d45d90aeae1ee489a3b5765d20127a" + ], + "includePlatforms": [], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": true, + "defineConstraints": [], + "versionDefines": [], + "noEngineReferences": false +} \ No newline at end of file diff --git a/ChaoticCrates/Assets/Scripts/main.asmdef.meta b/ChaoticCrates/Assets/Scripts/main.asmdef.meta new file mode 100644 index 0000000..a6bd444 --- /dev/null +++ b/ChaoticCrates/Assets/Scripts/main.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: ab108d9737afe6a4897e78fed9e4d9b4 +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: -- GitLab