修复一些登录的BUG

This commit is contained in:
Mili 2023-02-25 18:15:56 +08:00
parent fc43f91ad2
commit 34379df886
13 changed files with 230 additions and 87 deletions

View File

@ -1,9 +1,8 @@
using System.Net.NetworkInformation; using Milimoe.FunGame.Core.Library.Constant;
using System.Net.NetworkInformation;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text;
using System.Text.Json; using System.Text.Json;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Milimoe.FunGame.Core.Library.Constant;
// 通用工具类,客户端和服务器端都可以直接调用的工具方法都可以写在这里 // 通用工具类,客户端和服务器端都可以直接调用的工具方法都可以写在这里
namespace Milimoe.FunGame.Core.Api.Utility namespace Milimoe.FunGame.Core.Api.Utility

View File

@ -1,6 +1,5 @@
using Milimoe.FunGame.Core.Library.Constant; using Milimoe.FunGame.Core.Library.Constant;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text;
namespace Milimoe.FunGame.Core.Api.Utility namespace Milimoe.FunGame.Core.Api.Utility
{ {

View File

@ -47,16 +47,17 @@
Red Red
} }
[Flags]
public enum SocketMessageType public enum SocketMessageType
{ {
Unknown, Unknown = 0,
Connect, Connect = 1 << 0,
GetNotice, GetNotice = 1 << 1,
Login, Login = 1 << 2,
CheckLogin, CheckLogin = 1 << 4,
Logout, Logout = 1 << 5,
Disconnect, Disconnect = 1 << 6,
HeartBeat HeartBeat = 1 << 7
} }
public enum SocketRuntimeType public enum SocketRuntimeType

View File

@ -2,96 +2,106 @@
{ {
public class SystemError : Exception public class SystemError : Exception
{ {
public override string Message => "系统错误 (#00001)"; public override string Message => "系统错误 (#10001)";
} }
public class CanNotConnectException : Exception public class CanNotConnectException : Exception
{ {
public override string Message => "无法连接至服务器,请检查网络并重启游戏再试 (#00002)"; public override string Message => "无法连接至服务器,请检查网络并重启游戏再试 (#10002)";
} }
public class TimeOutException : Exception public class TimeOutException : Exception
{ {
public override string Message => "连接超时 (#00003)"; public override string Message => "连接超时 (#10003)";
} }
public class UnknownException : Exception public class UnknownException : Exception
{ {
public override string Message => "未知错误 (#00004)"; public override string Message => "未知错误 (#10004)";
} }
public class ReadConfigException : Exception public class ReadConfigException : Exception
{ {
public override string Message => "读取配置文件出错,参数格式不正确 (#00005)"; public override string Message => "读取配置文件出错,参数格式不正确 (#10005)";
} }
public class SingletonAddException : Exception public class SingletonAddException : Exception
{ {
public override string Message => "添加单例到单例表时遇到错误 (#00006)"; public override string Message => "添加单例到单例表时遇到错误 (#10006)";
} }
public class SingletonGetException : Exception public class SingletonGetException : Exception
{ {
public override string Message => "不能从单例表中获取到指定的单例 (#00007)"; public override string Message => "不能从单例表中获取到指定的单例 (#10007)";
} }
public class SocketWrongInfoException : Exception public class SocketWrongInfoException : Exception
{ {
public override string Message => "收到错误的返回信息 (#00008)"; public override string Message => "收到错误的返回信息 (#10008)";
} }
public class SocketCreateListenException : Exception public class SocketCreateListenException : Exception
{ {
public override string Message => "无法创建监听,请重新启动服务器再试 (#00009)"; public override string Message => "无法创建监听,请重新启动服务器再试 (#10009)";
} }
public class SocketGetClientException : Exception public class SocketGetClientException : Exception
{ {
public override string Message => "无法获取客户端信息 (#00010)"; public override string Message => "无法获取客户端信息 (#10010)";
} }
public class ListeningSocketCanNotSendException : Exception public class ListeningSocketCanNotSendException : Exception
{ {
public override string Message => "监听Socket不能用于发送信息 (#00011)"; public override string Message => "监听Socket不能用于发送信息 (#10011)";
} }
public class ConnectFailedException : Exception public class ConnectFailedException : Exception
{ {
public override string Message => "连接到服务器失败 (#00012)"; public override string Message => "连接到服务器失败 (#10012)";
} }
public class LostConnectException : Exception public class LostConnectException : Exception
{ {
public override string Message => "与服务器连接中断 (#00013)"; public override string Message => "与服务器连接中断 (#10013)";
} }
public class FindServerFailedException : Exception public class FindServerFailedException : Exception
{ {
public override string Message => "查找可用的服务器失败请重启FunGame (#00014)"; public override string Message => "查找可用的服务器失败请重启FunGame (#10014)";
} }
public class FormHasBeenOpenedException : Exception public class FormHasBeenOpenedException : Exception
{ {
public override string Message => "目标窗口可能已处于打开状态 (#00015)"; public override string Message => "目标窗口可能已处于打开状态 (#10015)";
} }
public class FormCanNotOpenException : Exception public class FormCanNotOpenException : Exception
{ {
public override string Message => "无法打开指定窗口 (#00016)"; public override string Message => "无法打开指定窗口 (#10016)";
} }
public class ServerErrorException : Exception public class ServerErrorException : Exception
{ {
public override string Message => "服务器遇到问题需要关闭,请重新启动服务器! (#00017)"; public override string Message => "服务器遇到问题需要关闭,请重新启动服务器! (#10017)";
} }
public class CanNotSendToClientException : Exception public class CanNotSendToClientException : Exception
{ {
public override string Message => "无法向客户端传输消息 (#00018)"; public override string Message => "无法向客户端传输消息 (#10018)";
} }
public class MySQLConfigException : Exception public class MySQLConfigException : Exception
{ {
public override string Message => "MySQL服务启动失败无法找到MySQL配置文件 (#00019)"; public override string Message => "MySQL服务启动失败无法找到MySQL配置文件 (#10019)";
}
public class CanNotLogOutException : Exception
{
public override string Message => "无法登出您的账号,请联系服务器管理员 (#10020)";
}
public class NoUserLogonException : Exception
{
public override string Message => "用户未登录 (#10021)";
} }
} }

View File

@ -33,17 +33,12 @@ namespace Milimoe.FunGame.Desktop.Controller
break; break;
case MainSet.Connect: case MainSet.Connect:
Main.OnBeforeConnectEvent(new GeneralEventArgs());
result = MainModel.Connect(); result = MainModel.Connect();
if ((ConnectResult)result == ConnectResult.Success) if ((ConnectResult)result != ConnectResult.Success)
{
Main.OnSucceedConnectEvent(new GeneralEventArgs());
}
else if ((ConnectResult)result == ConnectResult.ConnectFailed)
{ {
Main.OnFailedConnectEvent(new GeneralEventArgs()); Main.OnFailedConnectEvent(new GeneralEventArgs());
}
Main.OnAfterConnectEvent(new GeneralEventArgs()); Main.OnAfterConnectEvent(new GeneralEventArgs());
}
break; break;
case MainSet.Connected: case MainSet.Connected:
@ -53,11 +48,9 @@ namespace Milimoe.FunGame.Desktop.Controller
case MainSet.Disconnect: case MainSet.Disconnect:
Main.OnBeforeDisconnectEvent(new GeneralEventArgs()); Main.OnBeforeDisconnectEvent(new GeneralEventArgs());
MainModel.Disconnect(); MainModel.Disconnect();
Main.OnAfterDisconnectEvent(new GeneralEventArgs());
break; break;
case MainSet.Disconnected: case MainSet.Disconnected:
MainModel.Disconnect();
break; break;
case MainSet.WaitConnectAndSetYellow: case MainSet.WaitConnectAndSetYellow:
@ -82,7 +75,8 @@ namespace Milimoe.FunGame.Desktop.Controller
break; break;
case MainSet.LogOut: case MainSet.LogOut:
result = MainModel.Logout(); Main.OnBeforeLogoutEvent(new GeneralEventArgs());
result = MainModel.LogOut();
break; break;
case MainSet.Close: case MainSet.Close:

View File

@ -46,6 +46,7 @@
this.Name = "GeneralForm"; this.Name = "GeneralForm";
this.Text = "GeneralForm"; this.Text = "GeneralForm";
this.FormClosed += FormClosedEvent; this.FormClosed += FormClosedEvent;
this.Load += LoadEvent;
this.ResumeLayout(false); this.ResumeLayout(false);
} }

View File

@ -90,5 +90,15 @@ namespace Milimoe.FunGame.Desktop.Library.Component
RunTime.UserCenter = null; RunTime.UserCenter = null;
} }
} }
/// <summary>
/// 窗体加载事件触发BindEvent()
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected virtual void LoadEvent(object? sender, EventArgs e)
{
BindEvent();
}
} }
} }

View File

@ -115,7 +115,7 @@ namespace Milimoe.FunGame.Desktop.Library.Component
{ {
TaskAutoClose = Task.Factory.StartNew(() => TaskAutoClose = Task.Factory.StartNew(() =>
{ {
Thread.Sleep(100); Thread.Sleep(1);
string msg = MsgText.Text; string msg = MsgText.Text;
int s = AutoClose; int s = AutoClose;
BeginInvoke(() => ChangeSecond(msg, s)); BeginInvoke(() => ChangeSecond(msg, s));

View File

@ -23,5 +23,6 @@
/*** GUID For Socket ***/ /*** GUID For Socket ***/
public static Guid Guid_Socket { get; set; } = Guid.Empty; public static Guid Guid_Socket { get; set; } = Guid.Empty;
public static Guid Guid_LoginKey { get; set; } = Guid.Empty;
} }
} }

View File

@ -1,4 +1,5 @@
using Milimoe.FunGame.Core.Library.Constant; using Milimoe.FunGame.Core.Api.Utility;
using Milimoe.FunGame.Core.Library.Constant;
using Milimoe.FunGame.Core.Library.Exception; using Milimoe.FunGame.Core.Library.Exception;
using Milimoe.FunGame.Desktop.Library; using Milimoe.FunGame.Desktop.Library;
@ -22,6 +23,7 @@ namespace Milimoe.FunGame.Desktop.Model
if (objs.Length > 0) username = (string)objs[0]; if (objs.Length > 0) username = (string)objs[0];
if (objs.Length > 1) password = (string)objs[1]; if (objs.Length > 1) password = (string)objs[1];
if (objs.Length > 2) autokey = (string)objs[2]; if (objs.Length > 2) autokey = (string)objs[2];
password = password.Encrypt(username);
if (Socket.Send(SocketMessageType.Login, username, password, autokey) == SocketResult.Success) if (Socket.Send(SocketMessageType.Login, username, password, autokey) == SocketResult.Success)
{ {
return true; return true;

View File

@ -18,21 +18,35 @@ namespace Milimoe.FunGame.Desktop.Model
private readonly Main Main; private readonly Main Main;
private Task? ReceivingTask; private Task? ReceivingTask;
private Core.Library.Common.Network.Socket? Socket; private Core.Library.Common.Network.Socket? Socket;
private bool IsReceiving = false;
public MainModel(Main main) public MainModel(Main main)
{ {
Main = main; Main = main;
} }
public bool Logout() #region
public bool LogOut()
{ {
try try
{ {
//Socket?.Send(SocketMessageType.Logout, ""); // 需要当时登录给的Key发回去确定是账号本人在操作才允许登出
if (Config.Guid_LoginKey != Guid.Empty)
{
if (Socket?.Send(SocketMessageType.Logout, Config.Guid_LoginKey) == SocketResult.Success)
{
return true;
}
}
else throw new CanNotLogOutException();
} }
catch (Exception e) catch (Exception e)
{ {
ShowMessage.ErrorMessage("无法登出您的账号,请联系服务器管理员。", "登出失败", 5);
Main.GetMessage(e.GetErrorInfo()); Main.GetMessage(e.GetErrorInfo());
Main.OnFailedLogoutEvent(new GeneralEventArgs());
Main.OnAfterLogoutEvent(new GeneralEventArgs());
} }
return false; return false;
} }
@ -41,15 +55,13 @@ namespace Milimoe.FunGame.Desktop.Model
{ {
try try
{ {
if (Socket?.Send(SocketMessageType.Disconnect, "") == SocketResult.Success) Socket?.Send(SocketMessageType.Disconnect, "");
{
Main.OnSucceedDisconnectEvent(new GeneralEventArgs());
}
} }
catch (Exception e) catch (Exception e)
{ {
Main.GetMessage(e.GetErrorInfo()); Main.GetMessage(e.GetErrorInfo());
Main.OnFailedDisconnectEvent(new GeneralEventArgs()); Main.OnFailedDisconnectEvent(new GeneralEventArgs());
Main.OnAfterDisconnectEvent(new GeneralEventArgs());
} }
} }
@ -91,9 +103,12 @@ namespace Milimoe.FunGame.Desktop.Model
public ConnectResult Connect() public ConnectResult Connect()
{ {
Main.OnBeforeConnectEvent(new GeneralEventArgs());
if (Constant.Server_Address == "" || Constant.Server_Port <= 0) if (Constant.Server_Address == "" || Constant.Server_Port <= 0)
{ {
ShowMessage.ErrorMessage("查找可用的服务器失败!"); ShowMessage.ErrorMessage("查找可用的服务器失败!");
Main.OnFailedConnectEvent(new GeneralEventArgs());
Main.OnAfterConnectEvent(new GeneralEventArgs());
return ConnectResult.FindServerFailed; return ConnectResult.FindServerFailed;
} }
try try
@ -102,6 +117,8 @@ namespace Milimoe.FunGame.Desktop.Model
{ {
Main.GetMessage("正在连接服务器,请耐心等待。"); Main.GetMessage("正在连接服务器,请耐心等待。");
Config.FunGame_isRetrying = false; Config.FunGame_isRetrying = false;
Main.OnFailedConnectEvent(new GeneralEventArgs());
Main.OnAfterConnectEvent(new GeneralEventArgs());
return ConnectResult.CanNotConnect; return ConnectResult.CanNotConnect;
} }
if (!Config.FunGame_isConnected) if (!Config.FunGame_isConnected)
@ -132,6 +149,15 @@ namespace Milimoe.FunGame.Desktop.Model
Main.GetMessage("连接服务器成功请登录账号以体验FunGame。"); Main.GetMessage("连接服务器成功请登录账号以体验FunGame。");
Main.UpdateUI(MainSet.Connected); Main.UpdateUI(MainSet.Connected);
StartReceiving(); StartReceiving();
while (true)
{
if (IsReceiving)
{
Main.OnSucceedConnectEvent(new GeneralEventArgs());
Main.OnAfterConnectEvent(new GeneralEventArgs());
break;
}
}
} }
}); });
return ConnectResult.Success; return ConnectResult.Success;
@ -152,6 +178,11 @@ namespace Milimoe.FunGame.Desktop.Model
Main.GetMessage(e.GetErrorInfo(), TimeType.None); Main.GetMessage(e.GetErrorInfo(), TimeType.None);
Main.UpdateUI(MainSet.SetRed); Main.UpdateUI(MainSet.SetRed);
Config.FunGame_isRetrying = false; Config.FunGame_isRetrying = false;
Task.Factory.StartNew(() =>
{
Main.OnFailedConnectEvent(new GeneralEventArgs());
Main.OnAfterConnectEvent(new GeneralEventArgs());
});
return ConnectResult.ConnectFailed; return ConnectResult.ConnectFailed;
} }
return ConnectResult.CanNotConnect; return ConnectResult.CanNotConnect;
@ -170,6 +201,7 @@ namespace Milimoe.FunGame.Desktop.Model
{ {
ReceivingTask.Wait(1); ReceivingTask.Wait(1);
ReceivingTask = null; ReceivingTask = null;
IsReceiving = false;
} }
} }
catch (Exception e) catch (Exception e)
@ -215,16 +247,16 @@ namespace Milimoe.FunGame.Desktop.Model
throw new NotImplementedException(); throw new NotImplementedException();
} }
public bool LogOut() #endregion
{
throw new NotImplementedException(); #region
}
private void StartReceiving() private void StartReceiving()
{ {
ReceivingTask = Task.Factory.StartNew(() => ReceivingTask = Task.Factory.StartNew(() =>
{ {
Thread.Sleep(100); Thread.Sleep(100);
IsReceiving = true;
while (Socket != null && Socket.Connected) while (Socket != null && Socket.Connected)
{ {
Receiving(); Receiving();
@ -272,6 +304,7 @@ namespace Milimoe.FunGame.Desktop.Model
break; break;
case SocketMessageType.Logout: case SocketMessageType.Logout:
SocketHandler_LogOut(objs);
break; break;
case SocketMessageType.Disconnect: case SocketMessageType.Disconnect:
@ -300,6 +333,10 @@ namespace Milimoe.FunGame.Desktop.Model
return result; return result;
} }
#endregion
#region SocketHandler
private void SocketHandler_Connect(object[] objs) private void SocketHandler_Connect(object[] objs)
{ {
string msg = ""; string msg = "";
@ -324,9 +361,24 @@ namespace Milimoe.FunGame.Desktop.Model
private void SocketHandler_Login(object[] objs) private void SocketHandler_Login(object[] objs)
{ {
Guid key = Guid.Empty; Guid key = Guid.Empty;
string? msg = "";
// 返回一个Key再发回去给服务器就行了 // 返回一个Key再发回去给服务器就行了
if (objs.Length > 0) key = NetworkUtility.ConvertJsonObject<Guid>(objs[0])!; if (objs.Length > 0) key = NetworkUtility.ConvertJsonObject<Guid>(objs[0]);
if (key != Guid.Empty) LoginController.CheckLogin(key); if (objs.Length > 1) msg = NetworkUtility.ConvertJsonObject<string>(objs[1]);
// 如果返回了msg说明验证错误。
if (msg != null && msg.Trim() != "")
{
ShowMessage.ErrorMessage(msg, "登录失败");
RunTime.Login?.OnFailedLoginEvent(new GeneralEventArgs());
RunTime.Login?.OnAfterLoginEvent(new GeneralEventArgs());
}
else
{
if (key != Guid.Empty)
{
Config.Guid_LoginKey = key;
LoginController.CheckLogin(key);
}
else else
{ {
ShowMessage.ErrorMessage("登录失败!!", "登录失败", 5); ShowMessage.ErrorMessage("登录失败!!", "登录失败", 5);
@ -334,6 +386,28 @@ namespace Milimoe.FunGame.Desktop.Model
RunTime.Login?.OnAfterLoginEvent(new GeneralEventArgs()); RunTime.Login?.OnAfterLoginEvent(new GeneralEventArgs());
} }
} }
}
private void SocketHandler_LogOut(object[] objs)
{
Guid key = Guid.Empty;
string? msg = "";
// 返回一个Key如果这个Key是空的登出失败
if (objs != null && objs.Length > 0) key = NetworkUtility.ConvertJsonObject<Guid>(objs[0]);
if (objs != null && objs.Length > 1) msg = NetworkUtility.ConvertJsonObject<string>(objs[1]);
if (key != Guid.Empty)
{
Config.Guid_LoginKey = Guid.Empty;
Main.UpdateUI(MainSet.LogOut, msg ?? "");
Main.OnSucceedLogoutEvent(new GeneralEventArgs());
}
else
{
ShowMessage.ErrorMessage("无法登出您的账号,请联系服务器管理员。", "登出失败", 5);
Main.OnFailedLogoutEvent(new GeneralEventArgs());
}
Main.OnAfterLogoutEvent(new GeneralEventArgs());
}
private void SocketHandler_CheckLogin(object[] objs) private void SocketHandler_CheckLogin(object[] objs)
{ {
@ -356,6 +430,10 @@ namespace Milimoe.FunGame.Desktop.Model
Main.GetMessage(msg); Main.GetMessage(msg);
Main.UpdateUI(MainSet.Disconnect); Main.UpdateUI(MainSet.Disconnect);
Close(); Close();
} Main.OnSucceedDisconnectEvent(new GeneralEventArgs());
Main.OnAfterDisconnectEvent(new GeneralEventArgs());
}
#endregion
} }
} }

View File

@ -1,4 +1,5 @@
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.Core.Library.Exception;
using Milimoe.FunGame.Desktop.Controller; using Milimoe.FunGame.Desktop.Controller;
using Milimoe.FunGame.Desktop.Library; using Milimoe.FunGame.Desktop.Library;
@ -15,7 +16,13 @@ namespace Milimoe.FunGame.Desktop.UI
InitializeComponent(); InitializeComponent();
} }
private void Login_Handler() protected override void BindEvent()
{
base.BindEvent();
FailedLogin += FailedLoginEvent;
}
private bool Login_Handler()
{ {
try try
{ {
@ -25,16 +32,20 @@ namespace Milimoe.FunGame.Desktop.UI
{ {
ShowMessage.ErrorMessage("账号或密码不能为空!"); ShowMessage.ErrorMessage("账号或密码不能为空!");
UsernameText.Focus(); UsernameText.Focus();
return; return false;
} }
password = password.Encrypt(username);
if (!LoginController.LoginAccount(username, password)) if (!LoginController.LoginAccount(username, password))
{
ShowMessage.Message("登录失败!!", "登录失败"); ShowMessage.Message("登录失败!!", "登录失败");
return false;
}
} }
catch (Exception e) catch (Exception e)
{ {
RunTime.WritelnSystemInfo(e.GetErrorInfo()); RunTime.WritelnSystemInfo(e.GetErrorInfo());
return false;
} }
return true;
} }
/// <summary> /// <summary>
@ -54,12 +65,19 @@ namespace Milimoe.FunGame.Desktop.UI
private void GoToLogin_Click(object sender, EventArgs e) private void GoToLogin_Click(object sender, EventArgs e)
{ {
Login_Handler(); GoToLogin.Enabled = false;
if (!Login_Handler()) GoToLogin.Enabled = true;
} }
private void ForgetPassword_Click(object sender, EventArgs e) private void ForgetPassword_Click(object sender, EventArgs e)
{ {
ShowMessage.TipMessage("暂不支持找回密码~"); ShowMessage.TipMessage("暂不支持找回密码~");
} }
public EventResult FailedLoginEvent(object sender, GeneralEventArgs e)
{
GoToLogin.Enabled = true;
return EventResult.Success;
}
} }
} }

View File

@ -1,6 +1,5 @@
using Milimoe.FunGame.Core.Api.Utility; using Milimoe.FunGame.Core.Api.Utility;
using Milimoe.FunGame.Core.Entity; using Milimoe.FunGame.Core.Entity;
using Milimoe.FunGame.Core.Interface;
using Milimoe.FunGame.Core.Library.Common.Event; using Milimoe.FunGame.Core.Library.Common.Event;
using Milimoe.FunGame.Core.Library.Constant; using Milimoe.FunGame.Core.Library.Constant;
using Milimoe.FunGame.Core.Library.Exception; using Milimoe.FunGame.Core.Library.Exception;
@ -10,7 +9,6 @@ using Milimoe.FunGame.Desktop.Library.Base;
using Milimoe.FunGame.Desktop.Library.Component; using Milimoe.FunGame.Desktop.Library.Component;
using Milimoe.FunGame.Desktop.Utility; using Milimoe.FunGame.Desktop.Utility;
using System.Diagnostics; using System.Diagnostics;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
namespace Milimoe.FunGame.Desktop.UI namespace Milimoe.FunGame.Desktop.UI
{ {
@ -42,7 +40,6 @@ namespace Milimoe.FunGame.Desktop.UI
{ {
InitializeComponent(); InitializeComponent();
Init(); Init();
BindEvent();
} }
/// <summary> /// <summary>
@ -76,6 +73,9 @@ namespace Milimoe.FunGame.Desktop.UI
}); });
} }
/// <summary>
/// 绑定事件
/// </summary>
protected override void BindEvent() protected override void BindEvent()
{ {
base.BindEvent(); base.BindEvent();
@ -92,7 +92,7 @@ namespace Milimoe.FunGame.Desktop.UI
/// </summary> /// </summary>
/// <param name="updatetype">string?</param> /// <param name="updatetype">string?</param>
/// <param name="objs">object[]?</param> /// <param name="objs">object[]?</param>
public void UpdateUI(string? updatetype, object[]? objs = null) public void UpdateUI(string? updatetype, params object[]? objs)
{ {
void action() void action()
{ {
@ -159,6 +159,7 @@ namespace Milimoe.FunGame.Desktop.UI
SetServerStatusLight((int)LightType.Red); SetServerStatusLight((int)LightType.Red);
SetButtonEnableIfLogon(false, ClientState.WaitConnect); SetButtonEnableIfLogon(false, ClientState.WaitConnect);
LogoutAccount(); LogoutAccount();
CloseConnectedWindows();
break; break;
case MainSet.Disconnect: case MainSet.Disconnect:
@ -177,19 +178,17 @@ namespace Milimoe.FunGame.Desktop.UI
case MainSet.LogOut: case MainSet.LogOut:
Config.FunGame_isRetrying = false; Config.FunGame_isRetrying = false;
Config.FunGame_isConnected = false;
Config.FunGame_isAutoLogin = false; Config.FunGame_isAutoLogin = false;
SetServerStatusLight((int)LightType.Yellow); SetServerStatusLight((int)LightType.Yellow, true);
SetButtonEnableIfLogon(false, ClientState.WaitConnect); SetButtonEnableIfLogon(false, ClientState.WaitLogin);
LogoutAccount(); LogoutAccount();
if (Config.FunGame_isAutoConnect) if (objs != null && objs.Length > 0)
{ {
CurrentRetryTimes = -1; if (objs[0].GetType() == typeof(string))
Task.Run(() =>
{ {
Thread.Sleep(1000); WritelnSystemInfo((string)objs[0]);
MainController?.Connect(); ShowMessage.Message((string)objs[0], "退出登录成功", 5);
}); }
} }
break; break;
@ -206,7 +205,7 @@ namespace Milimoe.FunGame.Desktop.UI
default: default:
// 直接调用UpdateUI(string)相当于调用GetMessage(string)输出该string到控制台。 // 直接调用UpdateUI(string)相当于调用GetMessage(string)输出该string到控制台。
// 尽量避免使用除MainControllerSet之外的string调用此方法 // 尽量避免使用除MainSet之外的string调用此方法
WritelnSystemInfo(updatetype); WritelnSystemInfo(updatetype);
break; break;
} }
@ -221,6 +220,11 @@ namespace Milimoe.FunGame.Desktop.UI
InvokeUpdateUI(action); InvokeUpdateUI(action);
} }
/// <summary>
/// 提供公共方法给Controller发送系统信息
/// </summary>
/// <param name="msg"></param>
/// <param name="timetype"></param>
public void GetMessage(string? msg, TimeType timetype = TimeType.TimeOnly) public void GetMessage(string? msg, TimeType timetype = TimeType.TimeOnly)
{ {
void action() void action()
@ -270,9 +274,9 @@ namespace Milimoe.FunGame.Desktop.UI
{ {
string isAutoConnect = INIHelper.ReadINI("Config", "AutoConnect"); string isAutoConnect = INIHelper.ReadINI("Config", "AutoConnect");
string isAutoLogin = INIHelper.ReadINI("Config", "AutoLogin"); string isAutoLogin = INIHelper.ReadINI("Config", "AutoLogin");
string strUserName = INIHelper.ReadINI("Config", "UserName"); string strUserName = INIHelper.ReadINI("Account", "UserName");
string strPassword = INIHelper.ReadINI("Config", "Password"); string strPassword = INIHelper.ReadINI("Account", "Password");
string strAutoKey = INIHelper.ReadINI("Config", "AutoKey"); string strAutoKey = INIHelper.ReadINI("Account", "AutoKey");
if (isAutoConnect != null && isAutoConnect.Trim() != "" && (isAutoConnect.ToLower().Equals("false") || isAutoConnect.ToLower().Equals("true"))) if (isAutoConnect != null && isAutoConnect.Trim() != "" && (isAutoConnect.ToLower().Equals("false") || isAutoConnect.ToLower().Equals("true")))
Config.FunGame_isAutoConnect = Convert.ToBoolean(isAutoConnect); Config.FunGame_isAutoConnect = Convert.ToBoolean(isAutoConnect);
@ -325,7 +329,7 @@ namespace Milimoe.FunGame.Desktop.UI
/// 设置登录信息 /// 设置登录信息
/// </summary> /// </summary>
/// <param name="objs"></param> /// <param name="objs"></param>
private void SetLoginUser(object[]? objs = null) private void SetLoginUser(params object[]? objs)
{ {
if (InvokeRequired) if (InvokeRequired)
Invoke(LoginAccount, objs); Invoke(LoginAccount, objs);
@ -388,8 +392,6 @@ namespace Milimoe.FunGame.Desktop.UI
{ {
// 显示:匹配、创建房间 // 显示:匹配、创建房间
// 隐藏:退出房间、房间设定 // 隐藏:退出房间、房间设定
WritelnGameInfo(DateTimeUtility.GetNowShortTime() + " 离开房间");
WritelnGameInfo("[ " + Usercfg.LoginUserName + " ] 已离开房间 -> [ " + Config.FunGame_Roomid + " ]");
SetRoomid("-1"); SetRoomid("-1");
QuitRoom.Visible = false; QuitRoom.Visible = false;
StartMatch.Visible = true; StartMatch.Visible = true;
@ -412,7 +414,10 @@ namespace Milimoe.FunGame.Desktop.UI
/// <summary> /// <summary>
/// 未登录和离线时,停用按钮 /// 未登录和离线时,停用按钮
/// 登录的时候要激活按钮
/// </summary> /// </summary>
/// <param name="isLogon">是否登录</param>
/// <param name="status">客户端状态</param>
private void SetButtonEnableIfLogon(bool isLogon, ClientState status) private void SetButtonEnableIfLogon(bool isLogon, ClientState status)
{ {
switch (status) switch (status)
@ -615,17 +620,21 @@ namespace Milimoe.FunGame.Desktop.UI
/// <summary> /// <summary>
/// 登录账号,显示登出按钮 /// 登录账号,显示登出按钮
/// </summary> /// </summary>
private void LoginAccount(object[]? objs = null) private void LoginAccount(params object[]? objs)
{ {
if (objs != null && objs.Length > 0) if (objs != null && objs.Length > 0)
{ {
Usercfg.LoginUser = (User)objs[0]; Usercfg.LoginUser = (User)objs[0];
if (Usercfg.LoginUser is null)
{
throw new NoUserLogonException();
}
Usercfg.LoginUserName = Usercfg.LoginUser.Username; Usercfg.LoginUserName = Usercfg.LoginUser.Username;
} }
NowAccount.Text = "[ID] " + Usercfg.LoginUserName; NowAccount.Text = "[ID] " + Usercfg.LoginUserName;
Login.Visible = false; Login.Visible = false;
Logout.Visible = true; Logout.Visible = true;
SetServerStatusLight((int)LightType.Green); UpdateUI(MainSet.SetGreenAndPing);
RunTime.Login?.Close(); RunTime.Login?.Close();
Thread.Sleep(100); Thread.Sleep(100);
string welcome = $"欢迎回来, {Usercfg.LoginUserName}"; string welcome = $"欢迎回来, {Usercfg.LoginUserName}";
@ -638,6 +647,7 @@ namespace Milimoe.FunGame.Desktop.UI
/// </summary> /// </summary>
private void LogoutAccount() private void LogoutAccount()
{ {
InMain();
Usercfg.LoginUser = null; Usercfg.LoginUser = null;
Usercfg.LoginUserName = ""; Usercfg.LoginUserName = "";
NowAccount.Text = "请登录账号"; NowAccount.Text = "请登录账号";
@ -796,6 +806,19 @@ namespace Milimoe.FunGame.Desktop.UI
WritelnGameInfo(FunGameInfo.GetInfo((FunGameInfo.FunGame)Constant.FunGameType)); WritelnGameInfo(FunGameInfo.GetInfo((FunGameInfo.FunGame)Constant.FunGameType));
} }
/// <summary>
/// 关闭所有登录后才能访问的窗口
/// </summary>
private void CloseConnectedWindows()
{
RunTime.Login?.Close();
RunTime.Register?.Close();
RunTime.Store?.Close();
RunTime.Inventory?.Close();
RunTime.RoomSetting?.Close();
RunTime.UserCenter?.Close();
}
#endregion #endregion
#region #region
@ -916,6 +939,8 @@ namespace Milimoe.FunGame.Desktop.UI
/// <param name="e"></param> /// <param name="e"></param>
private void QuitRoom_Click(object sender, EventArgs e) private void QuitRoom_Click(object sender, EventArgs e)
{ {
WritelnGameInfo(DateTimeUtility.GetNowShortTime() + " 离开房间");
WritelnGameInfo("[ " + Usercfg.LoginUserName + " ] 已离开房间 -> [ " + Config.FunGame_Roomid + " ]");
InMain(); InMain();
} }
@ -1149,12 +1174,12 @@ namespace Milimoe.FunGame.Desktop.UI
/// <returns></returns> /// <returns></returns>
public EventResult FailedConnectEvent(object sender, GeneralEventArgs e) public EventResult FailedConnectEvent(object sender, GeneralEventArgs e)
{ {
if (Config.FunGame_isAutoRetry && CurrentRetryTimes <= MaxRetryTimes) if (Config.FunGame_isConnected && Config.FunGame_isAutoRetry && CurrentRetryTimes <= MaxRetryTimes)
{ {
Task.Run(() => Task.Run(() =>
{ {
Thread.Sleep(5000); Thread.Sleep(5000);
if (Config.FunGame_isAutoRetry) MainController?.Connect(); // 再次判断是否开启自动重连 if (Config.FunGame_isConnected && Config.FunGame_isAutoRetry) MainController?.Connect(); // 再次判断是否开启自动重连
}); });
GetMessage("连接服务器失败5秒后自动尝试重连。"); GetMessage("连接服务器失败5秒后自动尝试重连。");
} }
@ -1257,6 +1282,7 @@ namespace Milimoe.FunGame.Desktop.UI
if (!Config.FunGame_isRetrying) if (!Config.FunGame_isRetrying)
{ {
CurrentRetryTimes = -1; CurrentRetryTimes = -1;
Config.FunGame_isAutoLogin = true;
MainController?.Connect(); MainController?.Connect();
} }
else else
@ -1266,13 +1292,16 @@ namespace Milimoe.FunGame.Desktop.UI
if (!Config.FunGame_isConnected) if (!Config.FunGame_isConnected)
{ {
CurrentRetryTimes = -1; CurrentRetryTimes = -1;
Config.FunGame_isAutoLogin = true;
MainController?.GetServerConnection(); MainController?.GetServerConnection();
} }
break; break;
case Constant.FunGame_Disconnect: case Constant.FunGame_Disconnect:
if (Config.FunGame_isConnected && MainController != null) if (Config.FunGame_isConnected && MainController != null)
{ {
MainController?.Disconnect(); // 先退出登录再断开连接
bool? @bool = MainController?.LogOut();
if (@bool ?? false) MainController?.Disconnect();
} }
break; break;
case Constant.FunGame_DisconnectWhenNotLogin: case Constant.FunGame_DisconnectWhenNotLogin:
@ -1308,6 +1337,7 @@ namespace Milimoe.FunGame.Desktop.UI
Constant.Server_Address = ip; Constant.Server_Address = ip;
Constant.Server_Port = port; Constant.Server_Port = port;
CurrentRetryTimes = -1; CurrentRetryTimes = -1;
Config.FunGame_isAutoLogin = true;
MainController?.Connect(); MainController?.Connect();
} }
else if (ErrorType == Core.Library.Constant.ErrorType.IsNotIP) ShowMessage.ErrorMessage("这不是一个IP地址"); else if (ErrorType == Core.Library.Constant.ErrorType.IsNotIP) ShowMessage.ErrorMessage("这不是一个IP地址");