mirror of
https://github.com/project-redbud/FunGame-Desktop.git
synced 2025-04-20 20:19:34 +08:00
修改架构 第二部分 (#16)
* 移除多余实现 * 修改方法名 * 更新连接服务器的逻辑 * 添加异常处理 * 修改架构之Login * 修复IntoRoom Bug * 修复ForceLogOut Bug * 修复LogOut Bug * 修复CreateRoom QuitRoom IntoRoom * 断开连接后,应该关闭Socket连接 * 修复Reg Main Bug
This commit is contained in:
parent
a72881d15f
commit
7061996177
@ -26,20 +26,23 @@ namespace Milimoe.FunGame.Desktop.Controller
|
||||
{
|
||||
bool result = false;
|
||||
string msg = "";
|
||||
LoginEventArgs args = new(username, password, autokey);
|
||||
|
||||
try
|
||||
{
|
||||
password = password.Encrypt(username);
|
||||
LoginEventArgs args = new(username, password, autokey);
|
||||
|
||||
if (OnBeforeLoginEvent(args))
|
||||
{
|
||||
DataRequest request = RunTime.NewLongRunningDataRequest(DataRequestType.RunTime_Login);
|
||||
DataRequest request = RunTime.NewLongRunningDataRequest(DataRequestType.Login_Login);
|
||||
request.AddRequestData("username", username);
|
||||
request.AddRequestData("password", password);
|
||||
request.AddRequestData("autokey", autokey);
|
||||
request.AddRequestData("key", Guid.Empty);
|
||||
await request.SendRequestAsync();
|
||||
if (request.Result == RequestResult.Success)
|
||||
{
|
||||
Guid key = request.GetResult<Guid>("checkloginkey");
|
||||
Guid key = request.GetResult<Guid>("key");
|
||||
msg = request.GetResult<string>("msg") ?? "";
|
||||
if (msg != "")
|
||||
{
|
||||
@ -47,7 +50,7 @@ namespace Milimoe.FunGame.Desktop.Controller
|
||||
}
|
||||
else if (key != Guid.Empty)
|
||||
{
|
||||
request.AddRequestData("checkloginkey", key);
|
||||
request.AddRequestData("key", key);
|
||||
await request.SendRequestAsync();
|
||||
if (request.Result == RequestResult.Success)
|
||||
{
|
||||
@ -62,6 +65,7 @@ namespace Milimoe.FunGame.Desktop.Controller
|
||||
if (user.Id != 0)
|
||||
{
|
||||
// 创建User对象并返回到Main
|
||||
RunTime.Session.LoginKey = key;
|
||||
RunTime.Main?.UpdateUI(MainInvokeType.SetUser, user);
|
||||
result = true;
|
||||
}
|
||||
@ -71,11 +75,6 @@ namespace Milimoe.FunGame.Desktop.Controller
|
||||
}
|
||||
request.Dispose();
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
RunTime.WritelnSystemInfo(e.GetErrorInfo());
|
||||
}
|
||||
|
||||
if (!result && msg == "")
|
||||
{
|
||||
@ -83,6 +82,11 @@ namespace Milimoe.FunGame.Desktop.Controller
|
||||
}
|
||||
|
||||
OnAfterLoginEvent(result, args);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
RunTime.WritelnSystemInfo(e.GetErrorInfo());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
@ -1,7 +1,5 @@
|
||||
using Milimoe.FunGame.Core.Api.Transmittal;
|
||||
using Milimoe.FunGame.Core.Controller;
|
||||
using Milimoe.FunGame.Core.Entity;
|
||||
using Milimoe.FunGame.Core.Library.Common.Network;
|
||||
using Milimoe.FunGame.Core.Library.Constant;
|
||||
using Milimoe.FunGame.Core.Library.Exception;
|
||||
using Milimoe.FunGame.Core.Model;
|
||||
@ -11,7 +9,7 @@ using Milimoe.FunGame.Desktop.UI;
|
||||
|
||||
namespace Milimoe.FunGame.Desktop.Controller
|
||||
{
|
||||
public class MainController : SocketHandlerController
|
||||
public class MainController
|
||||
{
|
||||
private readonly Main Main;
|
||||
private readonly Session Usercfg = RunTime.Session;
|
||||
@ -22,7 +20,7 @@ namespace Milimoe.FunGame.Desktop.Controller
|
||||
private readonly DataRequest IntoRoomRequest;
|
||||
private readonly DataRequest QuitRoomRequest;
|
||||
|
||||
public MainController(Main main) : base(RunTime.Socket)
|
||||
public MainController(Main main)
|
||||
{
|
||||
Main = main;
|
||||
ChatRequest = RunTime.NewLongRunningDataRequest(DataRequestType.Main_Chat);
|
||||
@ -31,11 +29,20 @@ namespace Milimoe.FunGame.Desktop.Controller
|
||||
UpdateRoomRequest = RunTime.NewLongRunningDataRequest(DataRequestType.Main_UpdateRoom);
|
||||
IntoRoomRequest = RunTime.NewLongRunningDataRequest(DataRequestType.Main_IntoRoom);
|
||||
QuitRoomRequest = RunTime.NewLongRunningDataRequest(DataRequestType.Main_QuitRoom);
|
||||
Disposed += MainController_Disposed;
|
||||
}
|
||||
|
||||
#region 公开方法
|
||||
|
||||
public void MainController_Disposed()
|
||||
{
|
||||
ChatRequest.Dispose();
|
||||
CreateRoomRequest.Dispose();
|
||||
GetRoomPlayerCountRequest.Dispose();
|
||||
UpdateRoomRequest.Dispose();
|
||||
IntoRoomRequest.Dispose();
|
||||
QuitRoomRequest.Dispose();
|
||||
}
|
||||
|
||||
public async Task<bool> LogOutAsync()
|
||||
{
|
||||
try
|
||||
@ -44,7 +51,7 @@ namespace Milimoe.FunGame.Desktop.Controller
|
||||
if (Usercfg.LoginKey != Guid.Empty)
|
||||
{
|
||||
DataRequest request = RunTime.NewDataRequest(DataRequestType.RunTime_Logout);
|
||||
request.AddRequestData("loginkey", Usercfg.LoginKey);
|
||||
request.AddRequestData("key", Usercfg.LoginKey);
|
||||
await request.SendRequestAsync();
|
||||
if (request.Result == RequestResult.Success)
|
||||
{
|
||||
@ -74,31 +81,17 @@ namespace Milimoe.FunGame.Desktop.Controller
|
||||
{
|
||||
try
|
||||
{
|
||||
string rid = room.Roomid;
|
||||
IntoRoomRequest.AddRequestData("room", rid);
|
||||
IntoRoomRequest.AddRequestData("roomid", room.Roomid);
|
||||
await IntoRoomRequest.SendRequestAsync();
|
||||
if (IntoRoomRequest.Result == RequestResult.Success)
|
||||
{
|
||||
string roomid = IntoRoomRequest.GetResult<string>("roomid") ?? "";
|
||||
if (rid == roomid)
|
||||
{
|
||||
// 先确认是否是加入的房间,防止服务端返回错误的房间
|
||||
if (roomid.Trim() != "" && roomid == "-1")
|
||||
{
|
||||
Main.GetMessage($"已连接至公共聊天室。");
|
||||
}
|
||||
else
|
||||
{
|
||||
Usercfg.InRoom = room;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return IntoRoomRequest.GetResult<bool>("result");
|
||||
}
|
||||
throw new CanNotIntoRoomException();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Main.GetMessage(e.GetErrorInfo());
|
||||
Main.GetMessage(e.GetErrorInfo(), TimeType.None);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -113,14 +106,14 @@ namespace Milimoe.FunGame.Desktop.Controller
|
||||
await UpdateRoomRequest.SendRequestAsync();
|
||||
if (UpdateRoomRequest.Result == RequestResult.Success)
|
||||
{
|
||||
list = UpdateRoomRequest.GetResult<List<Room>>("roomid") ?? new();
|
||||
list = UpdateRoomRequest.GetResult<List<Room>>("rooms") ?? new();
|
||||
Main.UpdateUI(MainInvokeType.UpdateRoom, list);
|
||||
}
|
||||
else throw new CanNotIntoRoomException();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Main.GetMessage(e.GetErrorInfo());
|
||||
Main.GetMessage(e.GetErrorInfo(), TimeType.None);
|
||||
}
|
||||
|
||||
return result;
|
||||
@ -136,7 +129,7 @@ namespace Milimoe.FunGame.Desktop.Controller
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Main.GetMessage(e.GetErrorInfo());
|
||||
Main.GetMessage(e.GetErrorInfo(), TimeType.None);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@ -159,11 +152,11 @@ namespace Milimoe.FunGame.Desktop.Controller
|
||||
return result;
|
||||
}
|
||||
}
|
||||
throw new CanNotIntoRoomException();
|
||||
throw new QuitRoomException();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Main.GetMessage(e.GetErrorInfo());
|
||||
Main.GetMessage(e.GetErrorInfo(), TimeType.None);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@ -175,7 +168,7 @@ namespace Milimoe.FunGame.Desktop.Controller
|
||||
try
|
||||
{
|
||||
CreateRoomRequest.AddRequestData("roomtype", RoomType);
|
||||
CreateRoomRequest.AddRequestData("user", Usercfg.LoginUser);
|
||||
CreateRoomRequest.AddRequestData("master", Usercfg.LoginUser);
|
||||
CreateRoomRequest.AddRequestData("password", Password);
|
||||
await CreateRoomRequest.SendRequestAsync();
|
||||
if (CreateRoomRequest.Result == RequestResult.Success)
|
||||
@ -185,7 +178,7 @@ namespace Milimoe.FunGame.Desktop.Controller
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Main.GetMessage(e.GetErrorInfo());
|
||||
Main.GetMessage(e.GetErrorInfo(), TimeType.None);
|
||||
}
|
||||
|
||||
return roomid;
|
||||
@ -205,75 +198,11 @@ namespace Milimoe.FunGame.Desktop.Controller
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Main.GetMessage(e.GetErrorInfo());
|
||||
Main.GetMessage(e.GetErrorInfo(), TimeType.None);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override void SocketHandler(SocketObject SocketObject)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (SocketObject.SocketType == SocketMessageType.HeartBeat)
|
||||
{
|
||||
// 心跳包单独处理
|
||||
if ((RunTime.Socket?.Connected ?? false) && Usercfg.LoginUser.Id != 0)
|
||||
Main.UpdateUI(MainInvokeType.SetGreenAndPing);
|
||||
}
|
||||
else if (SocketObject.SocketType == SocketMessageType.ForceLogout)
|
||||
{
|
||||
// 服务器强制下线登录
|
||||
Guid key = Guid.Empty;
|
||||
string msg = "";
|
||||
if (SocketObject.Length > 0) key = SocketObject.GetParam<Guid>(0);
|
||||
if (SocketObject.Length > 1) msg = SocketObject.GetParam<string>(1) ?? "";
|
||||
if (key == Usercfg.LoginKey)
|
||||
{
|
||||
Usercfg.LoginKey = Guid.Empty;
|
||||
Main.UpdateUI(MainInvokeType.LogOut, msg ?? "");
|
||||
}
|
||||
}
|
||||
else if (SocketObject.SocketType == SocketMessageType.Chat)
|
||||
{
|
||||
// 收到房间聊天信息
|
||||
string user = "", msg = "";
|
||||
if (SocketObject.Length > 0) user = SocketObject.GetParam<string>(0) ?? "";
|
||||
if (SocketObject.Length > 1) msg = SocketObject.GetParam<string>(1) ?? "";
|
||||
if (user != Usercfg.LoginUserName)
|
||||
{
|
||||
Main.GetMessage(msg, TimeType.None);
|
||||
}
|
||||
}
|
||||
else if (SocketObject.SocketType == SocketMessageType.UpdateRoomMaster)
|
||||
{
|
||||
// 收到房间更换房主的信息
|
||||
User user = General.UnknownUserInstance;
|
||||
Room room = General.HallInstance;
|
||||
if (SocketObject.Length > 0) user = SocketObject.GetParam<User>(0) ?? General.UnknownUserInstance;
|
||||
if (SocketObject.Length > 1) room = SocketObject.GetParam<Room>(1) ?? General.HallInstance;
|
||||
if (room.Roomid != "-1" && room.Roomid == Usercfg.InRoom.Roomid) Main.UpdateUI(MainInvokeType.UpdateRoomMaster, room);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
RunTime.Controller?.Error(e);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 私有方法
|
||||
|
||||
private void MainController_Disposed()
|
||||
{
|
||||
ChatRequest.Dispose();
|
||||
CreateRoomRequest.Dispose();
|
||||
GetRoomPlayerCountRequest.Dispose();
|
||||
UpdateRoomRequest.Dispose();
|
||||
IntoRoomRequest.Dispose();
|
||||
QuitRoomRequest.Dispose();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,11 @@
|
||||
using Milimoe.FunGame.Core.Library.Constant;
|
||||
using Milimoe.FunGame.Desktop.UI;
|
||||
using Milimoe.FunGame.Core.Library.Exception;
|
||||
using Milimoe.FunGame.Core.Library.Common.Event;
|
||||
using Milimoe.FunGame.Core.Api.Transmittal;
|
||||
using Milimoe.FunGame.Core.Api.Utility;
|
||||
using Milimoe.FunGame.Core.Library.Common.Event;
|
||||
using Milimoe.FunGame.Core.Library.Constant;
|
||||
using Milimoe.FunGame.Core.Library.Exception;
|
||||
using Milimoe.FunGame.Desktop.Library.Component;
|
||||
using Milimoe.FunGame.Core.Api.Transmittal;
|
||||
using Milimoe.FunGame.Desktop.Model;
|
||||
using Milimoe.FunGame.Desktop.UI;
|
||||
|
||||
namespace Milimoe.FunGame.Desktop.Controller
|
||||
{
|
||||
@ -37,6 +37,10 @@ namespace Milimoe.FunGame.Desktop.Controller
|
||||
if (request.Result == RequestResult.Success)
|
||||
{
|
||||
RegInvokeType InvokeType = request.GetResult<RegInvokeType>("type");
|
||||
switch (InvokeType)
|
||||
{
|
||||
case RegInvokeType.InputVerifyCode:
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
string verifycode = ShowMessage.InputMessageCancel("请输入注册邮件中的6位数字验证码", "注册验证码", out MessageResult cancel);
|
||||
@ -46,14 +50,30 @@ namespace Milimoe.FunGame.Desktop.Controller
|
||||
await request.SendRequestAsync();
|
||||
if (request.Result == RequestResult.Success)
|
||||
{
|
||||
bool success = request.GetResult<bool>("success");
|
||||
result = request.GetResult<bool>("success");
|
||||
string msg = request.GetResult<string>("msg") ?? "";
|
||||
if (msg != "") ShowMessage.Message(msg, "注册结果");
|
||||
if (success) return success;
|
||||
}
|
||||
}
|
||||
else break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case RegInvokeType.DuplicateUserName:
|
||||
{
|
||||
result = request.GetResult<bool>("success");
|
||||
string msg = request.GetResult<string>("msg") ?? "";
|
||||
ShowMessage.Message(msg, "注册结果");
|
||||
break;
|
||||
}
|
||||
case RegInvokeType.DuplicateEmail:
|
||||
{
|
||||
result = request.GetResult<bool>("success");
|
||||
string msg = request.GetResult<string>("msg") ?? "";
|
||||
ShowMessage.Message(msg, "注册结果");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
request.Dispose();
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
using Milimoe.FunGame.Core.Api.Transmittal;
|
||||
using Milimoe.FunGame.Core.Entity;
|
||||
using Milimoe.FunGame.Core.Library.Common.Event;
|
||||
using Milimoe.FunGame.Core.Library.Common.Network;
|
||||
using Milimoe.FunGame.Core.Library.Constant;
|
||||
@ -34,24 +34,12 @@ namespace Milimoe.FunGame.Desktop.Controller
|
||||
Close();
|
||||
}
|
||||
|
||||
public override ConnectResult Connect()
|
||||
{
|
||||
if (RunTime.Session.Server_IP == "" || RunTime.Session.Server_Port <= 0)
|
||||
{
|
||||
(RunTime.Session.Server_IP, RunTime.Session.Server_Port) = GetServerAddress();
|
||||
if (RunTime.Session.Server_IP == "" || RunTime.Session.Server_Port <= 0)
|
||||
{
|
||||
Main.ShowMessage(ShowMessageType.Error, "查找可用的服务器失败!");
|
||||
return ConnectResult.FindServerFailed;
|
||||
}
|
||||
}
|
||||
try
|
||||
public override bool BeforeConnect(ref string ip, ref int port)
|
||||
{
|
||||
if (Config.FunGame_isRetrying)
|
||||
{
|
||||
Main.GetMessage("正在连接服务器,请耐心等待。");
|
||||
Config.FunGame_isRetrying = false;
|
||||
return ConnectResult.CanNotConnect;
|
||||
return false;
|
||||
}
|
||||
if (!Config.FunGame_isConnected)
|
||||
{
|
||||
@ -63,83 +51,54 @@ namespace Milimoe.FunGame.Desktop.Controller
|
||||
{
|
||||
throw new CanNotConnectException();
|
||||
}
|
||||
// 与服务器建立连接
|
||||
Socket?.Close();
|
||||
Config.FunGame_isRetrying = true;
|
||||
_Socket = Socket.Connect(RunTime.Session.Server_IP, RunTime.Session.Server_Port);
|
||||
if (Socket != null && Socket.Connected)
|
||||
// 如果服务器地址为空需要获取一次地址
|
||||
if (ip == "" || port <= 0)
|
||||
{
|
||||
// 设置可复用Socket
|
||||
RunTime.Socket = Socket;
|
||||
// 发送连接请求
|
||||
DataRequest request = RunTime.NewDataRequest(DataRequestType.RunTime_Connect);
|
||||
request.SendRequest();
|
||||
if (request.Result == RequestResult.Success)
|
||||
{
|
||||
bool success = request.GetResult<bool>("success");
|
||||
string msg = request.GetResult<string>("msg") ?? "";
|
||||
if (!success)
|
||||
{
|
||||
// 服务器拒绝连接
|
||||
if (msg != "")
|
||||
{
|
||||
Main.GetMessage(msg);
|
||||
Main.ShowMessage(ShowMessageType.Error, msg);
|
||||
(ip, port) = GetServerAddress();
|
||||
RunTime.Session.Server_IP = ip;
|
||||
RunTime.Session.Server_Port = port;
|
||||
}
|
||||
return ConnectResult.ConnectFailed;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Main.GetMessage("已连接至服务器,请勿重复连接。");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override void AfterConnect(object[] ConnectArgs)
|
||||
{
|
||||
Config.FunGame_isRetrying = false;
|
||||
|
||||
ConnectResult result = (ConnectResult)ConnectArgs[0];
|
||||
string msg = (string)ConnectArgs[1];
|
||||
string servername = (string)ConnectArgs[2];
|
||||
string notice = (string)ConnectArgs[3];
|
||||
|
||||
if (msg != "")
|
||||
{
|
||||
Main.GetMessage(msg);
|
||||
if (result != ConnectResult.Success) Main.ShowMessage(ShowMessageType.Error, msg);
|
||||
}
|
||||
Guid token = request.GetResult<Guid>("token");
|
||||
string servername = request.GetResult<string>("servername") ?? "";
|
||||
string notice = request.GetResult<string>("notice") ?? "";
|
||||
|
||||
if (result == ConnectResult.Success)
|
||||
{
|
||||
// 设置可复用Socket
|
||||
RunTime.Socket = Socket;
|
||||
Config.FunGame_ServerName = servername;
|
||||
Config.FunGame_Notice = notice;
|
||||
Socket!.Token = token;
|
||||
Usercfg.SocketToken = token;
|
||||
Usercfg.SocketToken = Socket?.Token ?? Guid.Empty;
|
||||
Main.GetMessage($"已连接服务器:{servername}。\n\n********** 服务器公告 **********\n\n{notice}\n\n");
|
||||
// 设置等待登录的黄灯
|
||||
Main.UpdateUI(MainInvokeType.WaitLoginAndSetYellow);
|
||||
Main.GetMessage("连接服务器成功,请登录账号以体验FunGame。");
|
||||
Main.UpdateUI(MainInvokeType.Connected);
|
||||
StartReceiving();
|
||||
Task.Run(() =>
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
if (_IsReceiving)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
return ConnectResult.Success;
|
||||
}
|
||||
}
|
||||
Config.FunGame_isRetrying = false;
|
||||
Socket.Close();
|
||||
return ConnectResult.ConnectFailed;
|
||||
}
|
||||
Socket?.Close();
|
||||
Config.FunGame_isRetrying = false;
|
||||
throw new CanNotConnectException();
|
||||
}
|
||||
else
|
||||
{
|
||||
Main.GetMessage("已连接至服务器,请勿重复连接。");
|
||||
return ConnectResult.CanNotConnect;
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Main.GetMessage(e.GetErrorInfo(), TimeType.None);
|
||||
Main.UpdateUI(MainInvokeType.SetRed);
|
||||
Config.FunGame_isRetrying = false;
|
||||
return ConnectResult.ConnectFailed;
|
||||
}
|
||||
}
|
||||
|
||||
@ -157,8 +116,9 @@ namespace Milimoe.FunGame.Desktop.Controller
|
||||
|
||||
protected override void SocketHandler_Disconnect(SocketObject ServerMessage)
|
||||
{
|
||||
// 断开与服务器的连接
|
||||
string msg = "";
|
||||
if (ServerMessage.Parameters.Length > 0) msg = ServerMessage.GetParam<string>(0)!;
|
||||
if (ServerMessage.Parameters.Length > 0) msg = ServerMessage.GetParam<string>(0) ?? "";
|
||||
Main.GetMessage(msg);
|
||||
Main.UpdateUI(MainInvokeType.Disconnect);
|
||||
Close();
|
||||
@ -168,10 +128,40 @@ namespace Milimoe.FunGame.Desktop.Controller
|
||||
|
||||
protected override void SocketHandler_HeartBeat(SocketObject ServerMessage)
|
||||
{
|
||||
// 收到心跳包时更新与服务器的连接延迟
|
||||
if (Socket != null && Socket.Connected && Usercfg.LoginUser.Id != 0)
|
||||
{
|
||||
Main.UpdateUI(MainInvokeType.SetGreenAndPing);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void SocketHandler_ForceLogout(SocketObject ServerMessage)
|
||||
{
|
||||
// 服务器强制下线登录
|
||||
string msg = "";
|
||||
if (ServerMessage.Length > 0) msg = ServerMessage.GetParam<string>(0) ?? "";
|
||||
Usercfg.LoginKey = Guid.Empty;
|
||||
Main.UpdateUI(MainInvokeType.LogOut, msg ?? "");
|
||||
}
|
||||
|
||||
protected override void SocketHandler_Chat(SocketObject ServerMessage)
|
||||
{
|
||||
// 收到房间聊天信息
|
||||
string user = "", msg = "";
|
||||
if (ServerMessage.Length > 0) user = ServerMessage.GetParam<string>(0) ?? "";
|
||||
if (ServerMessage.Length > 1) msg = ServerMessage.GetParam<string>(1) ?? "";
|
||||
if (user != Usercfg.LoginUserName)
|
||||
{
|
||||
Main.GetMessage(msg, TimeType.None);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void SocketHandler_UpdateRoomMaster(SocketObject ServerMessage)
|
||||
{
|
||||
// 收到房间更换房主的信息
|
||||
Room room = General.HallInstance;
|
||||
if (ServerMessage.Length > 0) room = ServerMessage.GetParam<Room>(0) ?? General.HallInstance;
|
||||
if (room.Roomid != "-1" && room.Roomid == Usercfg.InRoom.Roomid) Main.UpdateUI(MainInvokeType.UpdateRoomMaster, room);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
using Milimoe.FunGame.Core.Library.Constant;
|
||||
using Milimoe.FunGame.Core.Api.Utility;
|
||||
using Milimoe.FunGame.Core.Library.Constant;
|
||||
|
||||
namespace Milimoe.FunGame.Desktop.Library.Component
|
||||
{
|
||||
@ -7,7 +8,6 @@ namespace Milimoe.FunGame.Desktop.Library.Component
|
||||
private MessageResult MessageResult = MessageResult.Cancel;
|
||||
private string InputResult = "";
|
||||
private readonly int AutoClose = 0;
|
||||
private readonly Task? TaskAutoClose;
|
||||
|
||||
private const string TITLE_TIP = "提示";
|
||||
private const string TITLE_WARNING = "警告";
|
||||
@ -113,17 +113,24 @@ namespace Milimoe.FunGame.Desktop.Library.Component
|
||||
}
|
||||
if (AutoClose > 0)
|
||||
{
|
||||
TaskAutoClose = Task.Factory.StartNew(() =>
|
||||
TaskUtility.StartAndAwaitTask(async () =>
|
||||
{
|
||||
Thread.Sleep(1);
|
||||
string msg = MsgText.Text;
|
||||
int s = AutoClose;
|
||||
BeginInvoke(() => ChangeSecond(msg, s));
|
||||
while (s > 0)
|
||||
await Task.Run(() =>
|
||||
{
|
||||
Thread.Sleep(1000);
|
||||
while (IsHandleCreated)
|
||||
{
|
||||
break;
|
||||
}
|
||||
});
|
||||
BeginInvoke(() => ChangeSecond(msg, s));
|
||||
while (!Disposing)
|
||||
{
|
||||
if (s > 0) await Task.Delay(1000);
|
||||
else break;
|
||||
s--;
|
||||
if (IsHandleCreated) BeginInvoke(() => ChangeSecond(msg, s));
|
||||
BeginInvoke(() => ChangeSecond(msg, s));
|
||||
}
|
||||
MessageResult = MessageResult.OK;
|
||||
Close();
|
||||
@ -147,7 +154,6 @@ namespace Milimoe.FunGame.Desktop.Library.Component
|
||||
BUTTON_RETRY => MessageResult.Retry,
|
||||
_ => MessageResult.Cancel
|
||||
};
|
||||
TaskAutoClose?.Wait(1);
|
||||
Dispose();
|
||||
}
|
||||
|
||||
|
@ -156,7 +156,7 @@ namespace Milimoe.FunGame.Desktop.UI
|
||||
SetServerStatusLight((int)LightType.Red);
|
||||
SetButtonEnableIfLogon(false, ClientState.WaitConnect);
|
||||
LogoutAccount();
|
||||
MainController?.Dispose();
|
||||
MainController?.MainController_Disposed();
|
||||
CloseConnectedWindows();
|
||||
break;
|
||||
|
||||
@ -171,7 +171,7 @@ namespace Milimoe.FunGame.Desktop.UI
|
||||
SetServerStatusLight((int)LightType.Yellow);
|
||||
SetButtonEnableIfLogon(false, ClientState.WaitConnect);
|
||||
LogoutAccount();
|
||||
MainController?.Dispose();
|
||||
MainController?.MainController_Disposed();
|
||||
break;
|
||||
|
||||
case MainInvokeType.LogIn:
|
||||
@ -239,7 +239,7 @@ namespace Milimoe.FunGame.Desktop.UI
|
||||
catch (Exception e)
|
||||
{
|
||||
WritelnGameInfo(e.GetErrorInfo());
|
||||
UpdateUI(MainInvokeType.SetRed);
|
||||
UpdateUI(MainInvokeType.Disconnected);
|
||||
}
|
||||
}
|
||||
InvokeUpdateUI(action);
|
||||
@ -743,10 +743,8 @@ namespace Milimoe.FunGame.Desktop.UI
|
||||
ShowMessage(ShowMessageType.Warning, "已在房间中,无法创建房间。");
|
||||
return;
|
||||
}
|
||||
if (MainController != null)
|
||||
{
|
||||
string roomid = await MainController.CreateRoomAsync(RoomType, Password);
|
||||
if (roomid != "" && roomid != "-1")
|
||||
string roomid = await InvokeController_CreateRoom(RoomType, Password);
|
||||
if (MainController is not null && roomid != "-1")
|
||||
{
|
||||
await MainController.UpdateRoomAsync();
|
||||
Room r = GetRoom(roomid);
|
||||
@ -758,7 +756,6 @@ namespace Milimoe.FunGame.Desktop.UI
|
||||
ShowMessage(ShowMessageType.General, "创建" + RoomType + "房间成功!\n房间号是 -> [ " + roomid + " ]", "创建成功");
|
||||
return;
|
||||
}
|
||||
}
|
||||
ShowMessage(ShowMessageType.General, "创建" + RoomType + "房间失败!", "创建失败");
|
||||
}
|
||||
|
||||
@ -861,14 +858,15 @@ namespace Milimoe.FunGame.Desktop.UI
|
||||
/// <summary>
|
||||
/// 退出游戏时处理
|
||||
/// </summary>
|
||||
private async Task ExitFunGame()
|
||||
private void ExitFunGame()
|
||||
{
|
||||
TaskUtility.StartAndAwaitTask(() =>
|
||||
{
|
||||
if (ShowMessage(ShowMessageType.OKCancel, "你确定关闭游戏?", "退出") == MessageResult.OK)
|
||||
{
|
||||
if (MainController != null) await LogOut();
|
||||
RunTime.Controller?.Close();
|
||||
Environment.Exit(0);
|
||||
InvokeController_Disconnect();
|
||||
}
|
||||
}).OnCompleted(() => Environment.Exit(0));
|
||||
}
|
||||
|
||||
#endregion
|
||||
@ -1208,7 +1206,7 @@ namespace Milimoe.FunGame.Desktop.UI
|
||||
/// <param name="e"></param>
|
||||
private void Main_Disposed(object? sender, EventArgs e)
|
||||
{
|
||||
MainController?.Dispose();
|
||||
MainController?.MainController_Disposed();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -1380,7 +1378,7 @@ namespace Milimoe.FunGame.Desktop.UI
|
||||
return true;
|
||||
}
|
||||
ErrorIPAddressType ErrorType = NetworkUtility.IsServerAddress(ip, port);
|
||||
if (ErrorType == Core.Library.Constant.ErrorIPAddressType.None)
|
||||
if (ErrorType == ErrorIPAddressType.None)
|
||||
{
|
||||
RunTime.Session.Server_IP = ip;
|
||||
RunTime.Session.Server_Port = port;
|
||||
@ -1388,8 +1386,8 @@ namespace Milimoe.FunGame.Desktop.UI
|
||||
Config.FunGame_isAutoRetry = true;
|
||||
InvokeController_Connect();
|
||||
}
|
||||
else if (ErrorType == Core.Library.Constant.ErrorIPAddressType.IsNotIP) ShowMessage(ShowMessageType.Error, "这不是一个IP地址!");
|
||||
else if (ErrorType == Core.Library.Constant.ErrorIPAddressType.IsNotPort) ShowMessage(ShowMessageType.Error, "这不是一个端口号!\n正确范围:1~65535");
|
||||
else if (ErrorType == ErrorIPAddressType.IsNotIP) ShowMessage(ShowMessageType.Error, "这不是一个IP地址!");
|
||||
else if (ErrorType == ErrorIPAddressType.IsNotPort) ShowMessage(ShowMessageType.Error, "这不是一个端口号!\n正确范围:1~65535");
|
||||
else ShowMessage(ShowMessageType.Error, "格式错误!\n这不是一个服务器地址。");
|
||||
break;
|
||||
default:
|
||||
@ -1416,13 +1414,18 @@ namespace Milimoe.FunGame.Desktop.UI
|
||||
TaskUtility.StartAndAwaitTask(() =>
|
||||
{
|
||||
if (OnBeforeConnectEvent(EventArgs) == EventResult.Fail) return;
|
||||
result = RunTime.Controller?.Connect() ?? result;
|
||||
result = RunTime.Controller?.Connect(RunTime.Session.Server_IP, RunTime.Session.Server_Port) ?? result;
|
||||
EventArgs.ConnectResult = result;
|
||||
}).OnCompleted(() =>
|
||||
{
|
||||
if (result == ConnectResult.Success) OnSucceedConnectEvent(EventArgs);
|
||||
else OnFailedConnectEvent(EventArgs);
|
||||
OnAfterConnectEvent(EventArgs);
|
||||
}).OnError(e =>
|
||||
{
|
||||
GetMessage(e.GetErrorInfo(), TimeType.None);
|
||||
UpdateUI(MainInvokeType.SetRed);
|
||||
Config.FunGame_isRetrying = false;
|
||||
});
|
||||
}
|
||||
catch (Exception e)
|
||||
@ -1441,9 +1444,15 @@ namespace Milimoe.FunGame.Desktop.UI
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
TaskUtility.StartAndAwaitTask(() =>
|
||||
TaskUtility.StartAndAwaitTask(async () =>
|
||||
{
|
||||
if (OnBeforeDisconnectEvent(new GeneralEventArgs()) == EventResult.Fail) return;
|
||||
|
||||
if (Usercfg.LoginUser.Id != 0)
|
||||
{
|
||||
await LogOut();
|
||||
}
|
||||
|
||||
result = RunTime.Controller?.Disconnect() ?? false;
|
||||
}).OnCompleted(() =>
|
||||
{
|
||||
@ -1486,6 +1495,34 @@ namespace Milimoe.FunGame.Desktop.UI
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建房间
|
||||
/// </summary>
|
||||
/// <param name="room"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<string> InvokeController_CreateRoom(string RoomType, string Password = "")
|
||||
{
|
||||
string roomid = "-1";
|
||||
|
||||
try
|
||||
{
|
||||
RoomEventArgs EventArgs = new(RoomType, Password);
|
||||
if (OnBeforeCreateRoomEvent(EventArgs) == EventResult.Fail) return roomid;
|
||||
|
||||
roomid = MainController is null ? "-1" : await MainController.CreateRoomAsync(RoomType, Password);
|
||||
|
||||
if (roomid != "-1") OnSucceedCreateRoomEvent(EventArgs);
|
||||
else OnFailedCreateRoomEvent(EventArgs);
|
||||
OnAfterCreateRoomEvent(EventArgs);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
GetMessage(e.GetErrorInfo(), TimeType.None);
|
||||
}
|
||||
|
||||
return roomid;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 退出房间
|
||||
/// </summary>
|
||||
|
@ -53,7 +53,7 @@ namespace Milimoe.FunGame.Desktop.UI
|
||||
}
|
||||
else
|
||||
{
|
||||
ShowMessage(ShowMessageType.Error, "账号名长度不符合要求:最多6个中文字符或12个英文字符");
|
||||
ShowMessage(ShowMessageType.Error, "账号名长度不符合要求:3~12个字符数(一个中文2个字符)");
|
||||
UsernameText.Focus();
|
||||
return false;
|
||||
}
|
||||
@ -68,7 +68,7 @@ namespace Milimoe.FunGame.Desktop.UI
|
||||
if (password != "")
|
||||
{
|
||||
int length = password.Length;
|
||||
if (length < 6 || length > 15) // 字节范围 3~12
|
||||
if (length < 6 || length > 15) // 字节范围 6~15
|
||||
{
|
||||
ShowMessage(ShowMessageType.Error, "密码长度不符合要求:6~15个字符数");
|
||||
PasswordText.Focus();
|
||||
@ -121,11 +121,14 @@ namespace Milimoe.FunGame.Desktop.UI
|
||||
return EventResult.Success;
|
||||
}
|
||||
|
||||
private async void RegButton_Click(object sender, EventArgs e)
|
||||
private void RegButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
RegButton.Enabled = false;
|
||||
TaskUtility.StartAndAwaitTask(async() =>
|
||||
{
|
||||
if (!await Reg_Handler()) RegButton.Enabled = true;
|
||||
else Dispose();
|
||||
else Close();
|
||||
});
|
||||
}
|
||||
|
||||
private void GoToLogin_Click(object sender, EventArgs e)
|
||||
|
Loading…
x
Reference in New Issue
Block a user