Unity Game Session MVP
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
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<UnityGameSession>
|
||||
{
|
||||
private GameSession _gameSession;
|
||||
|
||||
private bool _isInitialized = false;
|
||||
private string _ip = "";
|
||||
private int _port = 0;
|
||||
private string _username = "";
|
||||
private string _password = "";
|
||||
|
||||
/// <summary>
|
||||
/// Send a
|
||||
/// </summary>
|
||||
/// <param name="protocol"></param>
|
||||
/// <param name="complete"></param>
|
||||
public static void SendProtocol(Protocol protocol, Action complete = null)
|
||||
{
|
||||
if (!Instance._isInitialized)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Instance._gameSession.SendProtocol(protocol, complete);
|
||||
}
|
||||
|
||||
/// <summary>Set the connection info. This MUST be called call before login</summary>
|
||||
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<bool> 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);
|
||||
}
|
||||
|
||||
/// <summary>Make sure username and password is set before calling this method</summary>
|
||||
private async Task ConnectAsync(string ip, int port)
|
||||
{
|
||||
if (!Instance._isInitialized)
|
||||
{
|
||||
Logger.LogError("GameSession is not initialized");
|
||||
return;
|
||||
}
|
||||
await Instance._gameSession.ConnectAsync(ip, port);
|
||||
}
|
||||
|
||||
/// <summary>Get the list of created characters</summary>
|
||||
public static void GetRoleListAsync(Action<List<RoleInfo>> callback = null)
|
||||
{
|
||||
Instance._gameSession.GetRoleListAsync(callback);
|
||||
}
|
||||
|
||||
public static void SelectRoleAsync(RoleInfo roleInfo, Action<RoleInfo> callback = null)
|
||||
{
|
||||
Instance._gameSession.SelectRoleAsync(roleInfo, callback);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user