mirror of
https://github.com/project-redbud/FunGame-Desktop.git
synced 2025-12-05 08:09:06 +00:00
Rebuild RunTime's Controller Model
This commit is contained in:
parent
b79a7c4b63
commit
751e436869
@ -9,7 +9,7 @@ namespace Milimoe.FunGame.Desktop.Controller
|
||||
{
|
||||
public class RunTimeController
|
||||
{
|
||||
public bool Connected => Do<bool>(RunTimeInvokeType.Connected);
|
||||
public bool Connected => RunTimeModel.Connected;
|
||||
|
||||
private RunTimeModel RunTimeModel { get; }
|
||||
private Main Main { get; }
|
||||
@ -20,100 +20,100 @@ namespace Milimoe.FunGame.Desktop.Controller
|
||||
RunTimeModel = new RunTimeModel(Main);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从内部去调用Model的方法,并记录日志。
|
||||
*/
|
||||
private T Do<T>(RunTimeInvokeType DoType, params object[] args)
|
||||
public async Task<bool> GetServerConnection()
|
||||
{
|
||||
object result = new();
|
||||
switch (DoType)
|
||||
{
|
||||
case RunTimeInvokeType.GetServerConnection:
|
||||
result = RunTimeModel.GetServerConnection();
|
||||
break;
|
||||
bool result = false;
|
||||
|
||||
case RunTimeInvokeType.Connect:
|
||||
result = RunTimeModel.Connect();
|
||||
if ((ConnectResult)result != ConnectResult.Success)
|
||||
try
|
||||
{
|
||||
Main.OnFailedConnectEvent(new GeneralEventArgs());
|
||||
Main.OnAfterConnectEvent(new GeneralEventArgs());
|
||||
RunTimeModel.GetServerConnection();
|
||||
result = await Connect() == ConnectResult.Success;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Main.GetMessage(e.GetErrorInfo(), TimeType.None);
|
||||
}
|
||||
break;
|
||||
|
||||
case RunTimeInvokeType.Connected:
|
||||
result = RunTimeModel.Connected;
|
||||
break;
|
||||
return result;
|
||||
}
|
||||
|
||||
case RunTimeInvokeType.Disconnect:
|
||||
if (Main.OnBeforeDisconnectEvent(new GeneralEventArgs()) == EventResult.Fail) return (T)result;
|
||||
RunTimeModel.Disconnect();
|
||||
break;
|
||||
public async Task<ConnectResult> Connect()
|
||||
{
|
||||
ConnectResult result = ConnectResult.ConnectFailed;
|
||||
|
||||
case RunTimeInvokeType.Disconnected:
|
||||
break;
|
||||
try
|
||||
{
|
||||
ConnectEventArgs EventArgs = new(Constant.Server_IP, Constant.Server_Port);
|
||||
if (Main.OnBeforeConnectEvent(EventArgs) == EventResult.Fail) return ConnectResult.ConnectFailed;
|
||||
|
||||
case RunTimeInvokeType.AutoLogin:
|
||||
break;
|
||||
result = await RunTimeModel.Connect();
|
||||
|
||||
if (result == ConnectResult.Success) Main.OnSucceedConnectEvent(EventArgs);
|
||||
else Main.OnFailedConnectEvent(EventArgs);
|
||||
Main.OnAfterConnectEvent(EventArgs);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Main.GetMessage(e.GetErrorInfo(), TimeType.None);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public bool Disconnect()
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
try
|
||||
{
|
||||
if (Main.OnBeforeDisconnectEvent(new GeneralEventArgs()) == EventResult.Fail) return result;
|
||||
|
||||
result = RunTimeModel.Disconnect();
|
||||
|
||||
if (result) Main.OnSucceedDisconnectEvent(new GeneralEventArgs());
|
||||
else Main.OnFailedDisconnectEvent(new GeneralEventArgs());
|
||||
Main.OnAfterDisconnectEvent(new GeneralEventArgs());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Main.GetMessage(e.GetErrorInfo(), TimeType.None);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public bool Close(params object[] args)
|
||||
{
|
||||
bool result;
|
||||
|
||||
if (Connected) Disconnect();
|
||||
|
||||
case RunTimeInvokeType.Close:
|
||||
if (args != null && args.Length > 0)
|
||||
{
|
||||
RunTimeModel.Error((Exception)args[0]);
|
||||
result = true;
|
||||
}
|
||||
else
|
||||
result = RunTimeModel.Close();
|
||||
break;
|
||||
else result = RunTimeModel.Close();
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return (T)result;
|
||||
}
|
||||
|
||||
public bool GetServerConnection()
|
||||
{
|
||||
return Do<bool>(RunTimeInvokeType.GetServerConnection);
|
||||
}
|
||||
|
||||
public ConnectResult Connect()
|
||||
{
|
||||
return Do<ConnectResult>(RunTimeInvokeType.Connect);
|
||||
}
|
||||
|
||||
public void Disconnect()
|
||||
{
|
||||
Do<object>(RunTimeInvokeType.Disconnect);
|
||||
}
|
||||
|
||||
public void Disconnected()
|
||||
{
|
||||
Do<object>(RunTimeInvokeType.Disconnected);
|
||||
}
|
||||
|
||||
public bool Close()
|
||||
{
|
||||
return Do<bool>(RunTimeInvokeType.Close);
|
||||
return result;
|
||||
}
|
||||
|
||||
public bool Error(Exception e)
|
||||
{
|
||||
return Do<bool>(RunTimeInvokeType.Close, e);
|
||||
return Close(e);
|
||||
}
|
||||
|
||||
public async Task AutoLogin(params object[] objs)
|
||||
{
|
||||
try
|
||||
{
|
||||
Do<object>(RunTimeInvokeType.AutoLogin);
|
||||
LoginController LoginController = new();
|
||||
await LoginController.LoginAccount(objs);
|
||||
LoginController.Dispose();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Main.GetMessage(e.GetErrorInfo());
|
||||
Main.GetMessage(e.GetErrorInfo(), TimeType.None);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,12 +8,12 @@ namespace Milimoe.FunGame.Desktop.Library.Base
|
||||
public class BaseMain : GeneralForm, IConnectEventHandler, IDisconnectEventHandler, ILoginEventHandler, ILogoutEventHandler, IIntoRoomEventHandler, ISendTalkEventHandler,
|
||||
ICreateRoomEventHandler, IQuitRoomEventHandler, IStartMatchEventHandler, IStartGameEventHandler, IOpenInventoryEventHandler, IOpenStoreEventHandler
|
||||
{
|
||||
public event IEventHandler.BeforeEventHandler? BeforeConnect;
|
||||
public event IEventHandler.AfterEventHandler? AfterConnect;
|
||||
public event IEventHandler.SucceedEventHandler? SucceedConnect;
|
||||
public event IEventHandler.FailedEventHandler? FailedConnect;
|
||||
public event IConnectEventHandler.BeforeEventHandler? BeforeConnect;
|
||||
public event IConnectEventHandler.AfterEventHandler? AfterConnect;
|
||||
public event IConnectEventHandler.SucceedEventHandler? SucceedConnect;
|
||||
public event IConnectEventHandler.FailedEventHandler? FailedConnect;
|
||||
|
||||
public EventResult OnAfterConnectEvent(GeneralEventArgs e)
|
||||
public EventResult OnAfterConnectEvent(ConnectEventArgs e)
|
||||
{
|
||||
if (AfterConnect != null)
|
||||
{
|
||||
@ -22,7 +22,7 @@ namespace Milimoe.FunGame.Desktop.Library.Base
|
||||
else return EventResult.NoEventImplement;
|
||||
}
|
||||
|
||||
public EventResult OnBeforeConnectEvent(GeneralEventArgs e)
|
||||
public EventResult OnBeforeConnectEvent(ConnectEventArgs e)
|
||||
{
|
||||
if (BeforeConnect != null)
|
||||
{
|
||||
@ -31,7 +31,7 @@ namespace Milimoe.FunGame.Desktop.Library.Base
|
||||
else return EventResult.NoEventImplement;
|
||||
}
|
||||
|
||||
public EventResult OnSucceedConnectEvent(GeneralEventArgs e)
|
||||
public EventResult OnSucceedConnectEvent(ConnectEventArgs e)
|
||||
{
|
||||
if (SucceedConnect != null)
|
||||
{
|
||||
@ -40,7 +40,7 @@ namespace Milimoe.FunGame.Desktop.Library.Base
|
||||
else return EventResult.NoEventImplement;
|
||||
}
|
||||
|
||||
public EventResult OnFailedConnectEvent(GeneralEventArgs e)
|
||||
public EventResult OnFailedConnectEvent(ConnectEventArgs e)
|
||||
{
|
||||
if (FailedConnect != null)
|
||||
{
|
||||
|
||||
@ -13,7 +13,7 @@ namespace Milimoe.FunGame.Desktop.Library
|
||||
/**
|
||||
* Socket Configs
|
||||
*/
|
||||
public static string Server_Address { get; set; } = ""; // 服务器IP地址
|
||||
public static string Server_IP { get; set; } = ""; // 服务器IP地址
|
||||
public static int Server_Port { get; set; } = 0; // 服务器端口号
|
||||
public static Encoding Default_Encoding { get; } = General.DefaultEncoding;
|
||||
|
||||
|
||||
@ -16,8 +16,8 @@ namespace Milimoe.FunGame.Desktop.Model
|
||||
/// </summary>
|
||||
public class LoginModel : BaseModel
|
||||
{
|
||||
private static SocketObject Work;
|
||||
private static bool Working = false;
|
||||
private static new SocketObject Work;
|
||||
private static new bool Working = false;
|
||||
|
||||
public LoginModel() : base(RunTime.Socket)
|
||||
{
|
||||
@ -133,7 +133,7 @@ namespace Milimoe.FunGame.Desktop.Model
|
||||
return ds;
|
||||
}
|
||||
|
||||
private static void SetWorking()
|
||||
private static new void SetWorking()
|
||||
{
|
||||
Working = true;
|
||||
Work = default;
|
||||
|
||||
@ -12,8 +12,6 @@ namespace Milimoe.FunGame.Desktop.Model
|
||||
public class RegisterModel : BaseModel
|
||||
{
|
||||
private readonly Register Register;
|
||||
private SocketObject Work;
|
||||
private bool Working = false;
|
||||
|
||||
public RegisterModel(Register reg) : base(RunTime.Socket)
|
||||
{
|
||||
@ -129,11 +127,5 @@ namespace Milimoe.FunGame.Desktop.Model
|
||||
}
|
||||
return (success, msg);
|
||||
}
|
||||
|
||||
private void SetWorking()
|
||||
{
|
||||
Working = true;
|
||||
Work = default;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -28,18 +28,20 @@ namespace Milimoe.FunGame.Desktop.Model
|
||||
|
||||
#region 公开方法
|
||||
|
||||
public void Disconnect()
|
||||
public bool Disconnect()
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
try
|
||||
{
|
||||
Socket?.Send(SocketMessageType.Disconnect, "");
|
||||
result = Socket?.Send(SocketMessageType.Disconnect, "") == SocketResult.Success;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Main.GetMessage(e.GetErrorInfo());
|
||||
Main.OnFailedDisconnectEvent(new GeneralEventArgs());
|
||||
Main.OnAfterDisconnectEvent(new GeneralEventArgs());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public void Disconnected()
|
||||
@ -47,7 +49,7 @@ namespace Milimoe.FunGame.Desktop.Model
|
||||
Disconnect();
|
||||
}
|
||||
|
||||
public bool GetServerConnection()
|
||||
public void GetServerConnection()
|
||||
{
|
||||
try
|
||||
{
|
||||
@ -58,9 +60,8 @@ namespace Milimoe.FunGame.Desktop.Model
|
||||
string[] s = ipaddress.Split(':');
|
||||
if (s != null && s.Length > 1)
|
||||
{
|
||||
Constant.Server_Address = s[0];
|
||||
Constant.Server_IP = s[0];
|
||||
Constant.Server_Port = Convert.ToInt32(s[1]);
|
||||
if (Connect() == ConnectResult.Success) return true; // 连接服务器
|
||||
}
|
||||
}
|
||||
else
|
||||
@ -74,18 +75,13 @@ namespace Milimoe.FunGame.Desktop.Model
|
||||
{
|
||||
Main.GetMessage(e.GetErrorInfo(), TimeType.None);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public ConnectResult Connect()
|
||||
public async Task<ConnectResult> Connect()
|
||||
{
|
||||
if (Main.OnBeforeConnectEvent(new GeneralEventArgs()) == EventResult.Fail) return ConnectResult.ConnectFailed;
|
||||
if (Constant.Server_Address == "" || Constant.Server_Port <= 0)
|
||||
if (Constant.Server_IP == "" || Constant.Server_Port <= 0)
|
||||
{
|
||||
ShowMessage.ErrorMessage("查找可用的服务器失败!");
|
||||
Main.OnFailedConnectEvent(new GeneralEventArgs());
|
||||
Main.OnAfterConnectEvent(new GeneralEventArgs());
|
||||
return ConnectResult.FindServerFailed;
|
||||
}
|
||||
try
|
||||
@ -94,8 +90,6 @@ namespace Milimoe.FunGame.Desktop.Model
|
||||
{
|
||||
Main.GetMessage("正在连接服务器,请耐心等待。");
|
||||
Config.FunGame_isRetrying = false;
|
||||
Main.OnFailedConnectEvent(new GeneralEventArgs());
|
||||
Main.OnAfterConnectEvent(new GeneralEventArgs());
|
||||
return ConnectResult.CanNotConnect;
|
||||
}
|
||||
if (!Config.FunGame_isConnected)
|
||||
@ -111,7 +105,7 @@ namespace Milimoe.FunGame.Desktop.Model
|
||||
// 与服务器建立连接
|
||||
Socket?.Close();
|
||||
Config.FunGame_isRetrying = true;
|
||||
Socket = Socket.Connect(Constant.Server_Address, Constant.Server_Port);
|
||||
Socket = Socket.Connect(Constant.Server_IP, Constant.Server_Port);
|
||||
if (Socket != null && Socket.Connected)
|
||||
{
|
||||
// 设置可复用Socket
|
||||
@ -119,36 +113,33 @@ namespace Milimoe.FunGame.Desktop.Model
|
||||
// 发送连接请求
|
||||
if (Socket.Send(SocketMessageType.Connect) == SocketResult.Success)
|
||||
{
|
||||
Task t = Task.Factory.StartNew(() =>
|
||||
{
|
||||
if (Receiving() == SocketMessageType.Connect)
|
||||
SocketMessageType Result = Receiving();
|
||||
if (Result == SocketMessageType.Connect)
|
||||
{
|
||||
Main.GetMessage("连接服务器成功,请登录账号以体验FunGame。");
|
||||
Main.UpdateUI(MainInvokeType.Connected);
|
||||
StartReceiving();
|
||||
await Task.Factory.StartNew(() =>
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
if (IsReceiving)
|
||||
{
|
||||
Main.OnSucceedConnectEvent(new GeneralEventArgs());
|
||||
Main.OnAfterConnectEvent(new GeneralEventArgs());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Config.FunGame_isRetrying = false;
|
||||
Socket.Close();
|
||||
}
|
||||
});
|
||||
return ConnectResult.Success;
|
||||
}
|
||||
}
|
||||
Config.FunGame_isRetrying = false;
|
||||
Socket.Close();
|
||||
return ConnectResult.ConnectFailed;
|
||||
}
|
||||
Socket?.Close();
|
||||
Config.FunGame_isRetrying = false;
|
||||
throw new CanNotConnectException();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Main.GetMessage("已连接至服务器,请勿重复连接。");
|
||||
@ -160,14 +151,8 @@ namespace Milimoe.FunGame.Desktop.Model
|
||||
Main.GetMessage(e.GetErrorInfo(), TimeType.None);
|
||||
Main.UpdateUI(MainInvokeType.SetRed);
|
||||
Config.FunGame_isRetrying = false;
|
||||
Task.Factory.StartNew(() =>
|
||||
{
|
||||
Main.OnFailedConnectEvent(new GeneralEventArgs());
|
||||
Main.OnAfterConnectEvent(new GeneralEventArgs());
|
||||
});
|
||||
return ConnectResult.ConnectFailed;
|
||||
}
|
||||
return ConnectResult.CanNotConnect;
|
||||
}
|
||||
|
||||
public bool Close()
|
||||
@ -198,7 +183,7 @@ namespace Milimoe.FunGame.Desktop.Model
|
||||
{
|
||||
Main.GetMessage(e.GetErrorInfo(), TimeType.None);
|
||||
Main.UpdateUI(MainInvokeType.Disconnected);
|
||||
Main.OnFailedConnectEvent(new GeneralEventArgs());
|
||||
Main.OnFailedConnectEvent(new ConnectEventArgs(Constant.Server_IP, Constant.Server_Port));
|
||||
Close();
|
||||
}
|
||||
|
||||
|
||||
@ -107,7 +107,7 @@ namespace Milimoe.FunGame.Desktop.UI
|
||||
|
||||
case MainInvokeType.SetGreenAndPing:
|
||||
Config.FunGame_isRetrying = false;
|
||||
SetServerStatusLight((int)LightType.Green, ping: NetworkUtility.GetServerPing(Constant.Server_Address));
|
||||
SetServerStatusLight((int)LightType.Green, ping: NetworkUtility.GetServerPing(Constant.Server_IP));
|
||||
SetButtonEnableIfLogon(true, ClientState.Online);
|
||||
Config.FunGame_isConnected = true;
|
||||
CurrentRetryTimes = 0;
|
||||
@ -1354,7 +1354,7 @@ namespace Milimoe.FunGame.Desktop.UI
|
||||
ErrorType ErrorType = NetworkUtility.IsServerAddress(ip, port);
|
||||
if (ErrorType == Core.Library.Constant.ErrorType.None)
|
||||
{
|
||||
Constant.Server_Address = ip;
|
||||
Constant.Server_IP = ip;
|
||||
Constant.Server_Port = port;
|
||||
CurrentRetryTimes = -1;
|
||||
Config.FunGame_isAutoRetry = true;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user