mirror of
https://github.com/project-redbud/FunGame-Desktop.git
synced 2025-04-23 13:39:36 +08:00
重构MainModel 添加服务器强制将玩家下线 (#1)
This commit is contained in:
commit
b573adc215
@ -3,14 +3,12 @@ using Milimoe.FunGame.Core.Library.Common.Architecture;
|
|||||||
using Milimoe.FunGame.Core.Library.Constant;
|
using Milimoe.FunGame.Core.Library.Constant;
|
||||||
using Milimoe.FunGame.Desktop.Model;
|
using Milimoe.FunGame.Desktop.Model;
|
||||||
using Milimoe.FunGame.Desktop.UI;
|
using Milimoe.FunGame.Desktop.UI;
|
||||||
using Milimoe.FunGame.Desktop.Library;
|
using Milimoe.FunGame.Core.Library.Exception;
|
||||||
|
|
||||||
namespace Milimoe.FunGame.Desktop.Controller
|
namespace Milimoe.FunGame.Desktop.Controller
|
||||||
{
|
{
|
||||||
public class MainController : BaseController
|
public class MainController : BaseController
|
||||||
{
|
{
|
||||||
public bool Connected => Do<bool>(MainInvokeType.Connected);
|
|
||||||
|
|
||||||
private MainModel MainModel { get; }
|
private MainModel MainModel { get; }
|
||||||
private Main Main { get; }
|
private Main Main { get; }
|
||||||
|
|
||||||
@ -25,83 +23,127 @@ namespace Milimoe.FunGame.Desktop.Controller
|
|||||||
MainModel.Dispose();
|
MainModel.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public async Task<bool> LogOut()
|
||||||
* 从内部去调用Model的方法,并记录日志。
|
|
||||||
*/
|
|
||||||
private T Do<T>(MainInvokeType DoType, params object[] args)
|
|
||||||
{
|
{
|
||||||
object result = new();
|
bool result = false;
|
||||||
switch(DoType)
|
|
||||||
|
try
|
||||||
{
|
{
|
||||||
case MainInvokeType.LogOut:
|
GeneralEventArgs EventArgs = new();
|
||||||
if (Main.OnBeforeLogoutEvent(new GeneralEventArgs()) == EventResult.Fail) return (T)result;
|
if (Main.OnBeforeLogoutEvent(EventArgs) == EventResult.Fail) return result;
|
||||||
result = MainModel.LogOut();
|
|
||||||
break;
|
|
||||||
|
|
||||||
case MainInvokeType.IntoRoom:
|
result = await MainModel.LogOut();
|
||||||
string roomid = new("-1");
|
|
||||||
if (args != null && args.Length > 0) roomid = (string)args[0];
|
|
||||||
if (Main.OnBeforeIntoRoomEvent(new RoomEventArgs(roomid)) == EventResult.Fail) return (T)result;
|
|
||||||
result = MainModel.IntoRoom(roomid);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case MainInvokeType.UpdateRoom:
|
if (result) Main.OnSucceedLogoutEvent(EventArgs);
|
||||||
result = MainModel.UpdateRoom();
|
else Main.OnFailedLogoutEvent(EventArgs);
|
||||||
break;
|
Main.OnAfterLogoutEvent(EventArgs);
|
||||||
|
|
||||||
case MainInvokeType.QuitRoom:
|
|
||||||
roomid = new("-1");
|
|
||||||
if (args != null && args.Length > 0) roomid = (string)args[0];
|
|
||||||
if (Main.OnBeforeQuitRoomEvent(new RoomEventArgs(roomid)) == EventResult.Fail) return (T)result;
|
|
||||||
result = MainModel.QuitRoom(roomid);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case MainInvokeType.CreateRoom:
|
|
||||||
if (Main.OnBeforeCreateRoomEvent(new RoomEventArgs()) == EventResult.Fail) return (T)result;
|
|
||||||
result = MainModel.CreateRoom();
|
|
||||||
break;
|
|
||||||
|
|
||||||
case MainInvokeType.Chat:
|
|
||||||
string msg = "";
|
|
||||||
if (args != null && args.Length > 0) msg = (string)args[0];
|
|
||||||
if (Main.OnBeforeSendTalkEvent(new SendTalkEventArgs(msg)) == EventResult.Fail) return (T)result;
|
|
||||||
if (msg.Trim() != "") result = MainModel.Chat(msg);
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
return (T)result;
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Main.GetMessage(e.GetErrorInfo(), TimeType.None);
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool LogOut()
|
return result;
|
||||||
{
|
|
||||||
return Do<bool>(MainInvokeType.LogOut);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool UpdateRoom()
|
public async Task<bool> UpdateRoom()
|
||||||
{
|
{
|
||||||
return Do<bool>(MainInvokeType.UpdateRoom);
|
return await MainModel.UpdateRoom();
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool IntoRoom(string roomid)
|
public async Task<bool> IntoRoom(string roomid)
|
||||||
{
|
{
|
||||||
return Do<bool>(MainInvokeType.IntoRoom, roomid);
|
bool result = false;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
RoomEventArgs EventArgs = new(roomid);
|
||||||
|
if (Main.OnBeforeIntoRoomEvent(EventArgs) == EventResult.Fail) return result;
|
||||||
|
|
||||||
|
result = await MainModel.IntoRoom(roomid);
|
||||||
|
|
||||||
|
if (result) Main.OnSucceedIntoRoomEvent(EventArgs);
|
||||||
|
else Main.OnFailedIntoRoomEvent(EventArgs);
|
||||||
|
Main.OnAfterIntoRoomEvent(EventArgs);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Main.GetMessage(e.GetErrorInfo(), TimeType.None);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool QuitRoom(string roomid)
|
public bool QuitRoom(string roomid)
|
||||||
{
|
{
|
||||||
return Do<bool>(MainInvokeType.QuitRoom, roomid);
|
bool result = false;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
RoomEventArgs EventArgs = new(roomid);
|
||||||
|
if (Main.OnBeforeQuitRoomEvent(EventArgs) == EventResult.Fail) return result;
|
||||||
|
|
||||||
|
result = MainModel.QuitRoom(roomid);
|
||||||
|
|
||||||
|
if (result) Main.OnSucceedQuitRoomEvent(EventArgs);
|
||||||
|
else Main.OnFailedQuitRoomEvent(EventArgs);
|
||||||
|
Main.OnAfterQuitRoomEvent(EventArgs);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Main.GetMessage(e.GetErrorInfo(), TimeType.None);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool CreateRoom()
|
public bool CreateRoom()
|
||||||
{
|
{
|
||||||
return Do<bool>(MainInvokeType.CreateRoom);
|
bool result = false;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
RoomEventArgs EventArgs = new();
|
||||||
|
if (Main.OnBeforeCreateRoomEvent(EventArgs) == EventResult.Fail) return result;
|
||||||
|
|
||||||
|
result = MainModel.CreateRoom();
|
||||||
|
|
||||||
|
if (result) Main.OnSucceedCreateRoomEvent(EventArgs);
|
||||||
|
else Main.OnFailedCreateRoomEvent(EventArgs);
|
||||||
|
Main.OnAfterCreateRoomEvent(EventArgs);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Main.GetMessage(e.GetErrorInfo(), TimeType.None);
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Chat(string msg)
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> Chat(string msg)
|
||||||
{
|
{
|
||||||
return Do<bool>(MainInvokeType.Chat, msg);
|
bool result = false;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
SendTalkEventArgs EventArgs = new(msg);
|
||||||
|
if (Main.OnBeforeSendTalkEvent(EventArgs) == EventResult.Fail) return result;
|
||||||
|
|
||||||
|
if (msg.Trim() != "")
|
||||||
|
{
|
||||||
|
result = await MainModel.Chat(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result) Main.OnSucceedSendTalkEvent(EventArgs);
|
||||||
|
else Main.OnFailedSendTalkEvent(EventArgs);
|
||||||
|
Main.OnAfterSendTalkEvent(EventArgs);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Main.GetMessage(e.GetErrorInfo(), TimeType.None);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -82,15 +82,15 @@ namespace Milimoe.FunGame.Desktop.Controller
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Close(params object[] args)
|
public bool Close(Exception? e = null)
|
||||||
{
|
{
|
||||||
bool result;
|
bool result;
|
||||||
|
|
||||||
if (Connected) Disconnect();
|
if (Connected) Disconnect();
|
||||||
|
|
||||||
if (args != null && args.Length > 0)
|
if (e != null)
|
||||||
{
|
{
|
||||||
RunTimeModel.Error((Exception)args[0]);
|
RunTimeModel.Error(e);
|
||||||
result = true;
|
result = true;
|
||||||
}
|
}
|
||||||
else result = RunTimeModel.Close();
|
else result = RunTimeModel.Close();
|
||||||
@ -103,12 +103,12 @@ namespace Milimoe.FunGame.Desktop.Controller
|
|||||||
return Close(e);
|
return Close(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task AutoLogin(params object[] objs)
|
public async Task AutoLogin(string Username, string Password, string AutoKey)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
LoginController LoginController = new();
|
LoginController LoginController = new();
|
||||||
await LoginController.LoginAccount(objs);
|
await LoginController.LoginAccount(Username, Password, AutoKey);
|
||||||
LoginController.Dispose();
|
LoginController.Dispose();
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
|
@ -93,10 +93,7 @@ namespace Milimoe.FunGame.Desktop.Model
|
|||||||
string? msg = "";
|
string? msg = "";
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
while (true)
|
WaitForWorkDone();
|
||||||
{
|
|
||||||
if (!Working) break;
|
|
||||||
}
|
|
||||||
// 返回一个确认登录的Key
|
// 返回一个确认登录的Key
|
||||||
if (Work.Length > 0) key = Work.GetParam<Guid>(0);
|
if (Work.Length > 0) key = Work.GetParam<Guid>(0);
|
||||||
if (Work.Length > 1) msg = Work.GetParam<string>(1);
|
if (Work.Length > 1) msg = Work.GetParam<string>(1);
|
||||||
@ -118,10 +115,7 @@ namespace Milimoe.FunGame.Desktop.Model
|
|||||||
DataSet? ds = new();
|
DataSet? ds = new();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
while (true)
|
WaitForWorkDone();
|
||||||
{
|
|
||||||
if (!Working) break;
|
|
||||||
}
|
|
||||||
// 返回构造User对象的DataSet
|
// 返回构造User对象的DataSet
|
||||||
if (Work.Length > 0) ds = Work.GetParam<DataSet>(0);
|
if (Work.Length > 0) ds = Work.GetParam<DataSet>(0);
|
||||||
}
|
}
|
||||||
@ -138,5 +132,13 @@ namespace Milimoe.FunGame.Desktop.Model
|
|||||||
Working = true;
|
Working = true;
|
||||||
Work = default;
|
Work = default;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static new void WaitForWorkDone()
|
||||||
|
{
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
if (!Working) break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,13 +6,13 @@ using Milimoe.FunGame.Core.Library.Exception;
|
|||||||
using Milimoe.FunGame.Desktop.Library;
|
using Milimoe.FunGame.Desktop.Library;
|
||||||
using Milimoe.FunGame.Desktop.Library.Component;
|
using Milimoe.FunGame.Desktop.Library.Component;
|
||||||
using Milimoe.FunGame.Desktop.UI;
|
using Milimoe.FunGame.Desktop.UI;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace Milimoe.FunGame.Desktop.Model
|
namespace Milimoe.FunGame.Desktop.Model
|
||||||
{
|
{
|
||||||
public class MainModel : BaseModel
|
public class MainModel : BaseModel
|
||||||
{
|
{
|
||||||
private readonly Main Main;
|
private readonly Main Main;
|
||||||
private string LastSendTalkMessage = "";
|
|
||||||
|
|
||||||
public MainModel(Main main) : base(RunTime.Socket)
|
public MainModel(Main main) : base(RunTime.Socket)
|
||||||
{
|
{
|
||||||
@ -21,55 +21,76 @@ namespace Milimoe.FunGame.Desktop.Model
|
|||||||
|
|
||||||
#region 公开方法
|
#region 公开方法
|
||||||
|
|
||||||
public bool LogOut()
|
public async Task<bool> LogOut()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// 需要当时登录给的Key发回去,确定是账号本人在操作才允许登出
|
// 需要当时登录给的Key发回去,确定是账号本人在操作才允许登出
|
||||||
if (Config.Guid_LoginKey != Guid.Empty)
|
if (Config.Guid_LoginKey != Guid.Empty)
|
||||||
{
|
{
|
||||||
|
SetWorking();
|
||||||
if (RunTime.Socket?.Send(SocketMessageType.Logout, Config.Guid_LoginKey) == SocketResult.Success)
|
if (RunTime.Socket?.Send(SocketMessageType.Logout, Config.Guid_LoginKey) == SocketResult.Success)
|
||||||
{
|
{
|
||||||
|
string msg = "";
|
||||||
|
Guid key = Guid.Empty;
|
||||||
|
(msg, key) = await Task.Factory.StartNew(SocketHandler_LogOut);
|
||||||
|
if (key == Config.Guid_LoginKey)
|
||||||
|
{
|
||||||
|
Config.Guid_LoginKey = Guid.Empty;
|
||||||
|
Main.UpdateUI(MainInvokeType.LogOut, msg ?? "");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else throw new CanNotLogOutException();
|
}
|
||||||
|
throw new CanNotLogOutException();
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
ShowMessage.ErrorMessage("无法登出您的账号,请联系服务器管理员。", "登出失败", 5);
|
ShowMessage.ErrorMessage("无法登出您的账号,请联系服务器管理员。", "登出失败", 5);
|
||||||
Main.GetMessage(e.GetErrorInfo());
|
Main.GetMessage(e.GetErrorInfo());
|
||||||
Main.OnFailedLogoutEvent(new GeneralEventArgs());
|
|
||||||
Main.OnAfterLogoutEvent(new GeneralEventArgs());
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool IntoRoom(string roomid)
|
public async Task<bool> IntoRoom(string roomid)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
SetWorking();
|
||||||
if (RunTime.Socket?.Send(SocketMessageType.IntoRoom, roomid) == SocketResult.Success)
|
if (RunTime.Socket?.Send(SocketMessageType.IntoRoom, roomid) == SocketResult.Success)
|
||||||
|
{
|
||||||
|
roomid = await Task.Factory.StartNew(SocketHandler_IntoRoom);
|
||||||
|
if (roomid.Trim() != "" && roomid == "-1")
|
||||||
|
{
|
||||||
|
Main.GetMessage($"已连接至公共聊天室。");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Config.FunGame_Roomid = roomid;
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
else throw new CanNotIntoRoomException();
|
}
|
||||||
|
throw new CanNotIntoRoomException();
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Main.GetMessage(e.GetErrorInfo());
|
Main.GetMessage(e.GetErrorInfo());
|
||||||
RoomEventArgs args = new(roomid);
|
|
||||||
Main.OnFailedIntoRoomEvent(args);
|
|
||||||
Main.OnAfterIntoRoomEvent(args);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool UpdateRoom()
|
public async Task<bool> UpdateRoom()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
SetWorking();
|
||||||
if (RunTime.Socket?.Send(SocketMessageType.UpdateRoom) == SocketResult.Success)
|
if (RunTime.Socket?.Send(SocketMessageType.UpdateRoom) == SocketResult.Success)
|
||||||
|
{
|
||||||
|
List<string> list = await Task.Factory.StartNew(SocketHandler_UpdateRoom);
|
||||||
|
Main.UpdateUI(MainInvokeType.UpdateRoom, list);
|
||||||
return true;
|
return true;
|
||||||
else throw new GetRoomListException();
|
}
|
||||||
|
throw new GetRoomListException();
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
@ -82,6 +103,7 @@ namespace Milimoe.FunGame.Desktop.Model
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
SetWorking();
|
||||||
if (RunTime.Socket?.Send(SocketMessageType.QuitRoom, roomid) == SocketResult.Success)
|
if (RunTime.Socket?.Send(SocketMessageType.QuitRoom, roomid) == SocketResult.Success)
|
||||||
return true;
|
return true;
|
||||||
else throw new QuitRoomException();
|
else throw new QuitRoomException();
|
||||||
@ -89,9 +111,6 @@ namespace Milimoe.FunGame.Desktop.Model
|
|||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Main.GetMessage(e.GetErrorInfo());
|
Main.GetMessage(e.GetErrorInfo());
|
||||||
RoomEventArgs args = new(roomid);
|
|
||||||
Main.OnFailedQuitRoomEvent(args);
|
|
||||||
Main.OnAfterQuitRoomEvent(args);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -100,35 +119,39 @@ namespace Milimoe.FunGame.Desktop.Model
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
SetWorking();
|
||||||
if (RunTime.Socket?.Send(SocketMessageType.CreateRoom) == SocketResult.Success)
|
if (RunTime.Socket?.Send(SocketMessageType.CreateRoom) == SocketResult.Success)
|
||||||
return true;
|
return true;
|
||||||
else throw new QuitRoomException();
|
else throw new CreateRoomException();
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Main.GetMessage(e.GetErrorInfo());
|
Main.GetMessage(e.GetErrorInfo());
|
||||||
RoomEventArgs args = new();
|
|
||||||
Main.OnFailedCreateRoomEvent(args);
|
|
||||||
Main.OnAfterCreateRoomEvent(args);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Chat(string msg)
|
public async Task<bool> Chat(string msg)
|
||||||
{
|
{
|
||||||
LastSendTalkMessage = msg;
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
SetWorking();
|
||||||
if (RunTime.Socket?.Send(SocketMessageType.Chat, msg) == SocketResult.Success)
|
if (RunTime.Socket?.Send(SocketMessageType.Chat, msg) == SocketResult.Success)
|
||||||
|
{
|
||||||
|
string user = "";
|
||||||
|
(user, msg) = await Task.Factory.StartNew(SocketHandler_Chat);
|
||||||
|
if (user != Usercfg.LoginUserName)
|
||||||
|
{
|
||||||
|
Main.GetMessage(msg, TimeType.None);
|
||||||
return true;
|
return true;
|
||||||
else throw new CanNotSendTalkException();
|
}
|
||||||
|
else return false;
|
||||||
|
}
|
||||||
|
throw new CanNotSendTalkException();
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Main.GetMessage(e.GetErrorInfo());
|
Main.GetMessage(e.GetErrorInfo());
|
||||||
SendTalkEventArgs SendTalkEventArgs = new(LastSendTalkMessage);
|
|
||||||
Main.OnFailedSendTalkEvent(SendTalkEventArgs);
|
|
||||||
Main.OnAfterSendTalkEvent(SendTalkEventArgs);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -137,39 +160,33 @@ namespace Milimoe.FunGame.Desktop.Model
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
switch (SocketObject.SocketType)
|
// 定义接收的通信类型
|
||||||
|
SocketMessageType[] SocketMessageTypes = new SocketMessageType[] { SocketMessageType.GetNotice, SocketMessageType.Logout, SocketMessageType.IntoRoom, SocketMessageType.QuitRoom,
|
||||||
|
SocketMessageType.Chat, SocketMessageType.UpdateRoom };
|
||||||
|
if (SocketObject.SocketType == SocketMessageType.HeartBeat)
|
||||||
{
|
{
|
||||||
case SocketMessageType.GetNotice:
|
// 心跳包单独处理
|
||||||
SocketHandler_GetNotice(SocketObject);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case SocketMessageType.Logout:
|
|
||||||
SocketHandler_LogOut(SocketObject);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case SocketMessageType.HeartBeat:
|
|
||||||
if ((RunTime.Socket?.Connected ?? false) && Usercfg.LoginUser != null)
|
if ((RunTime.Socket?.Connected ?? false) && Usercfg.LoginUser != null)
|
||||||
Main.UpdateUI(MainInvokeType.SetGreenAndPing);
|
Main.UpdateUI(MainInvokeType.SetGreenAndPing);
|
||||||
break;
|
}
|
||||||
|
else if (SocketObject.SocketType == SocketMessageType.ForceLogout)
|
||||||
case SocketMessageType.IntoRoom:
|
{
|
||||||
SocketHandler_IntoRoom(SocketObject);
|
// 服务器强制下线登录
|
||||||
break;
|
Guid key = Guid.Empty;
|
||||||
|
string? msg = "";
|
||||||
case SocketMessageType.QuitRoom:
|
if (SocketObject.Length > 0) key = SocketObject.GetParam<Guid>(0);
|
||||||
break;
|
if (SocketObject.Length > 1) msg = SocketObject.GetParam<string>(1);
|
||||||
|
msg ??= "";
|
||||||
case SocketMessageType.Chat:
|
if (key == Config.Guid_LoginKey)
|
||||||
SocketHandler_Chat(SocketObject);
|
{
|
||||||
break;
|
Config.Guid_LoginKey = Guid.Empty;
|
||||||
|
Main.UpdateUI(MainInvokeType.LogOut, msg ?? "");
|
||||||
case SocketMessageType.UpdateRoom:
|
}
|
||||||
SocketHandler_UpdateRoom(SocketObject);
|
}
|
||||||
break;
|
else if (SocketMessageTypes.Contains(SocketObject.SocketType))
|
||||||
|
{
|
||||||
case SocketMessageType.Unknown:
|
Work = SocketObject;
|
||||||
default:
|
Working = false;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
@ -182,73 +199,73 @@ namespace Milimoe.FunGame.Desktop.Model
|
|||||||
|
|
||||||
#region SocketHandler
|
#region SocketHandler
|
||||||
|
|
||||||
private void SocketHandler_GetNotice(SocketObject SocketObject)
|
private (string, Guid) SocketHandler_LogOut()
|
||||||
{
|
{
|
||||||
if (SocketObject.Length > 0) Config.FunGame_Notice = SocketObject.GetParam<string>(0)!;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void SocketHandler_LogOut(SocketObject SocketObject)
|
|
||||||
{
|
|
||||||
Guid key = Guid.Empty;
|
|
||||||
string? msg = "";
|
string? msg = "";
|
||||||
|
Guid key = Guid.Empty;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
WaitForWorkDone();
|
||||||
// 返回一个Key,如果这个Key是空的,登出失败
|
// 返回一个Key,如果这个Key是空的,登出失败
|
||||||
if (SocketObject.Parameters != null && SocketObject.Length > 0) key = SocketObject.GetParam<Guid>(0);
|
if (Work.Length > 0) key = Work.GetParam<Guid>(0);
|
||||||
if (SocketObject.Parameters != null && SocketObject.Length > 1) msg = SocketObject.GetParam<string>(1);
|
if (Work.Length > 1) msg = Work.GetParam<string>(1);
|
||||||
if (key != Guid.Empty)
|
|
||||||
{
|
|
||||||
Config.Guid_LoginKey = Guid.Empty;
|
|
||||||
Main.UpdateUI(MainInvokeType.LogOut, msg ?? "");
|
|
||||||
Main.OnSucceedLogoutEvent(new GeneralEventArgs());
|
|
||||||
}
|
}
|
||||||
else
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
ShowMessage.ErrorMessage("无法登出您的账号,请联系服务器管理员。", "登出失败", 5);
|
Main.GetMessage(e.GetErrorInfo());
|
||||||
Main.OnFailedLogoutEvent(new GeneralEventArgs());
|
|
||||||
}
|
}
|
||||||
Main.OnAfterLogoutEvent(new GeneralEventArgs());
|
msg ??= "";
|
||||||
|
return (msg, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SocketHandler_IntoRoom(SocketObject SocketObject)
|
private string SocketHandler_IntoRoom()
|
||||||
{
|
{
|
||||||
string roomid = "";
|
string? roomid = "";
|
||||||
if (SocketObject.Length > 0) roomid = SocketObject.GetParam<string>(0)!;
|
try
|
||||||
if (roomid.Trim() != "" && roomid == "-1")
|
|
||||||
{
|
{
|
||||||
Main.GetMessage($"已连接至公共聊天室。");
|
WaitForWorkDone();
|
||||||
|
if (Work.Length > 0) roomid = Work.GetParam<string>(0);
|
||||||
}
|
}
|
||||||
else
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Config.FunGame_Roomid = roomid;
|
Main.GetMessage(e.GetErrorInfo());
|
||||||
}
|
}
|
||||||
RoomEventArgs args = new(roomid);
|
roomid ??= "";
|
||||||
Main.OnSucceedIntoRoomEvent(args);
|
return roomid;
|
||||||
Main.OnAfterIntoRoomEvent(args);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SocketHandler_UpdateRoom(SocketObject SocketObject)
|
private List<string> SocketHandler_UpdateRoom()
|
||||||
{
|
{
|
||||||
List<string>? list = SocketObject.GetParam<List<string>>(0);
|
List<string>? list = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
WaitForWorkDone();
|
||||||
|
if (Work.Length > 0) list = Work.GetParam<List<string>>(0);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Main.GetMessage(e.GetErrorInfo());
|
||||||
|
}
|
||||||
list ??= new List<string>();
|
list ??= new List<string>();
|
||||||
Main.UpdateUI(MainInvokeType.UpdateRoom, list);
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SocketHandler_Chat(SocketObject SocketObject)
|
private (string, string) SocketHandler_Chat()
|
||||||
{
|
{
|
||||||
SendTalkEventArgs SendTalkEventArgs = new(LastSendTalkMessage);
|
string? user = "", msg = "";
|
||||||
if (SocketObject.Parameters != null && SocketObject.Length > 1)
|
try
|
||||||
{
|
{
|
||||||
string user = SocketObject.GetParam<string>(0)!;
|
WaitForWorkDone();
|
||||||
string msg = SocketObject.GetParam<string>(1)!;
|
if (Work.Length > 0) user = Work.GetParam<string>(0);
|
||||||
if (user != Usercfg.LoginUserName)
|
if (Work.Length > 1) msg = Work.GetParam<string>(1);
|
||||||
{
|
|
||||||
Main.GetMessage(msg, TimeType.None);
|
|
||||||
}
|
}
|
||||||
Main.OnSucceedSendTalkEvent(SendTalkEventArgs);
|
catch (Exception e)
|
||||||
Main.OnAfterSendTalkEvent(SendTalkEventArgs);
|
{
|
||||||
return;
|
Main.GetMessage(e.GetErrorInfo());
|
||||||
}
|
}
|
||||||
Main.OnFailedSendTalkEvent(SendTalkEventArgs);
|
user ??= "";
|
||||||
Main.OnAfterSendTalkEvent(SendTalkEventArgs);
|
msg ??= "";
|
||||||
|
return (user, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
@ -95,10 +95,7 @@ namespace Milimoe.FunGame.Desktop.Model
|
|||||||
RegInvokeType type = RegInvokeType.None;
|
RegInvokeType type = RegInvokeType.None;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
while (true)
|
WaitForWorkDone();
|
||||||
{
|
|
||||||
if (!Working) break;
|
|
||||||
}
|
|
||||||
if (Work.Length > 0) type = Work.GetParam<RegInvokeType>(0);
|
if (Work.Length > 0) type = Work.GetParam<RegInvokeType>(0);
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
@ -114,10 +111,7 @@ namespace Milimoe.FunGame.Desktop.Model
|
|||||||
string? msg = "";
|
string? msg = "";
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
while (true)
|
WaitForWorkDone();
|
||||||
{
|
|
||||||
if (!Working) break;
|
|
||||||
}
|
|
||||||
if (Work.Length > 0) success = Work.GetParam<bool>(0);
|
if (Work.Length > 0) success = Work.GetParam<bool>(0);
|
||||||
if (Work.Length > 1) msg = Work.GetParam<string>(1) ?? "";
|
if (Work.Length > 1) msg = Work.GetParam<string>(1) ?? "";
|
||||||
}
|
}
|
||||||
|
@ -667,7 +667,7 @@ namespace Milimoe.FunGame.Desktop.UI
|
|||||||
/// 发送消息实现
|
/// 发送消息实现
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="isLeave">是否离开焦点</param>
|
/// <param name="isLeave">是否离开焦点</param>
|
||||||
private void SendTalkText_Click(bool isLeave)
|
private async void SendTalkText_Click(bool isLeave)
|
||||||
{
|
{
|
||||||
// 向消息队列发送消息
|
// 向消息队列发送消息
|
||||||
string text = TalkText.Text;
|
string text = TalkText.Text;
|
||||||
@ -683,9 +683,9 @@ namespace Milimoe.FunGame.Desktop.UI
|
|||||||
msg = DateTimeUtility.GetNowShortTime() + " [ " + Usercfg.LoginUserName + " ] 说: " + text;
|
msg = DateTimeUtility.GetNowShortTime() + " [ " + Usercfg.LoginUserName + " ] 说: " + text;
|
||||||
}
|
}
|
||||||
WritelnGameInfo(msg);
|
WritelnGameInfo(msg);
|
||||||
if (Usercfg.LoginUser != null && !SwitchTalkMessage(text))
|
if (Usercfg.LoginUser != null && !await SwitchTalkMessage(text))
|
||||||
{
|
{
|
||||||
MainController?.Chat(" [ " + Usercfg.LoginUserName + " ] 说: " + text);
|
_ = MainController?.Chat(" [ " + Usercfg.LoginUserName + " ] 说: " + text);
|
||||||
}
|
}
|
||||||
TalkText.Text = "";
|
TalkText.Text = "";
|
||||||
if (isLeave) TalkText_Leave(); // 回车不离开焦点
|
if (isLeave) TalkText_Leave(); // 回车不离开焦点
|
||||||
@ -967,11 +967,11 @@ namespace Milimoe.FunGame.Desktop.UI
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="sender"></param>
|
/// <param name="sender"></param>
|
||||||
/// <param name="e"></param>
|
/// <param name="e"></param>
|
||||||
private void Logout_Click(object sender, EventArgs e)
|
private async void Logout_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
if (ShowMessage.OKCancelMessage("你确定要退出登录吗?", "退出登录") == MessageResult.OK)
|
if (ShowMessage.OKCancelMessage("你确定要退出登录吗?", "退出登录") == MessageResult.OK)
|
||||||
{
|
{
|
||||||
if (MainController == null || !MainController.LogOut())
|
if (MainController == null || !await MainController.LogOut())
|
||||||
ShowMessage.WarningMessage("请求无效:退出登录失败!");
|
ShowMessage.WarningMessage("请求无效:退出登录失败!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1152,14 +1152,14 @@ namespace Milimoe.FunGame.Desktop.UI
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="sender"></param>
|
/// <param name="sender"></param>
|
||||||
/// <param name="e"></param>
|
/// <param name="e"></param>
|
||||||
private void PresetText_SelectedIndexChanged(object sender, EventArgs e)
|
private async void PresetText_SelectedIndexChanged(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
// 发送快捷消息并执行功能
|
// 发送快捷消息并执行功能
|
||||||
if (PresetText.SelectedIndex != 0)
|
if (PresetText.SelectedIndex != 0)
|
||||||
{
|
{
|
||||||
string s = PresetText.SelectedItem.ToString();
|
string s = PresetText.SelectedItem.ToString();
|
||||||
SendTalkText_Click(s);
|
SendTalkText_Click(s);
|
||||||
SwitchTalkMessage(s);
|
await SwitchTalkMessage(s);
|
||||||
PresetText.SelectedIndex = 0;
|
PresetText.SelectedIndex = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1218,9 +1218,9 @@ namespace Milimoe.FunGame.Desktop.UI
|
|||||||
private EventResult SucceedLoginEvent(object sender, GeneralEventArgs e)
|
private EventResult SucceedLoginEvent(object sender, GeneralEventArgs e)
|
||||||
{
|
{
|
||||||
// 接入-1号房间聊天室
|
// 接入-1号房间聊天室
|
||||||
MainController?.IntoRoom("-1");
|
_ = MainController?.IntoRoom("-1");
|
||||||
// 获取在线的房间列表
|
// 获取在线的房间列表
|
||||||
MainController?.UpdateRoom();
|
_ = MainController?.UpdateRoom();
|
||||||
return EventResult.Success;
|
return EventResult.Success;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1249,7 +1249,7 @@ namespace Milimoe.FunGame.Desktop.UI
|
|||||||
/// 判断快捷消息
|
/// 判断快捷消息
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="s"></param>
|
/// <param name="s"></param>
|
||||||
private bool SwitchTalkMessage(string s)
|
private async Task<bool> SwitchTalkMessage(string s)
|
||||||
{
|
{
|
||||||
switch (s)
|
switch (s)
|
||||||
{
|
{
|
||||||
@ -1321,7 +1321,7 @@ namespace Milimoe.FunGame.Desktop.UI
|
|||||||
if (Config.FunGame_isConnected && MainController != null)
|
if (Config.FunGame_isConnected && MainController != null)
|
||||||
{
|
{
|
||||||
// 先退出登录再断开连接
|
// 先退出登录再断开连接
|
||||||
if (MainController?.LogOut() ?? false) RunTime.Connector?.Disconnect();
|
if (MainController != null && await MainController.LogOut()) RunTime.Connector?.Disconnect();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case Constant.FunGame_DisconnectWhenNotLogin:
|
case Constant.FunGame_DisconnectWhenNotLogin:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user