using CSNetwork; using CSNetwork.Protocols; using System; using BrewMonster; using CSNetwork.Security; using System.Threading.Tasks; using System.Collections.Generic; using CSNetwork.Protocols.RPCData; namespace BrewMonster.Network { // How to connect to the server: // 1. Set the connection info // 2. Login public class UnityGameSession : MonoSingleton { private GameSession _gameSession; private bool _isInitialized = false; private string _ip = ""; private int _port = 0; private string _username = ""; private string _password = ""; /// /// Send a /// /// /// public static void SendProtocol(Protocol protocol, Action complete = null) { if (!Instance._isInitialized) { return; } Instance._gameSession.SendProtocol(protocol, complete); } /// Set the connection info. This MUST be called call before login public static void SetConnectionInfo(string ip, int port) { Logger.Log($"Set connection info {ip} {port}"); Instance._ip = ip; Instance._port = port; } public static async Task Login(string username, string password, Action onLoginComplete = null) { Instance._username = username; Instance._password = password; if (Instance._ip == "" || Instance._port == 0) { Logger.LogError($"IP or port is not set {Instance._ip} {Instance._port}"); onLoginComplete?.Invoke(false); return; } await Instance.ConnectAsync(Instance._ip, Instance._port); if (!Instance._gameSession.IsConnected) { Logger.LogError($"Failed to connect to {Instance._ip} {Instance._port}"); onLoginComplete?.Invoke(false); return; } Instance._gameSession.LoginAsync(username, password, onLoginComplete); } protected override void Initialize() { BaseSecurity.Initizalize(); ProtocolFactory.RegisterAllProtocols(); _gameSession = new GameSession(); _isInitialized = true; DontDestroyOnLoad(gameObject); } /// Make sure username and password is set before calling this method private async Task ConnectAsync(string ip, int port) { if (!Instance._isInitialized) { Logger.LogError("GameSession is not initialized"); return; } await Instance._gameSession.ConnectAsync(ip, port); } /// Get the list of created characters public static void GetRoleListAsync(Action> callback = null) { Instance._gameSession.GetRoleListAsync(callback); } public static void SelectRoleAsync(RoleInfo roleInfo, Action callback = null) { Instance._gameSession.SelectRoleAsync(roleInfo, callback); } } }