diff --git a/FunGame.Desktop/Controller/RunTimeController.cs b/FunGame.Desktop/Controller/RunTimeController.cs index 9dc8880..e2e0150 100644 --- a/FunGame.Desktop/Controller/RunTimeController.cs +++ b/FunGame.Desktop/Controller/RunTimeController.cs @@ -1,4 +1,5 @@ -using Milimoe.FunGame.Core.Library.Common.Event; +using Milimoe.FunGame.Core.Api.Transmittal; +using Milimoe.FunGame.Core.Library.Common.Event; using Milimoe.FunGame.Core.Library.Constant; using Milimoe.FunGame.Core.Library.Exception; using Milimoe.FunGame.Desktop.Library; @@ -20,30 +21,13 @@ namespace Milimoe.FunGame.Desktop.Controller RunTimeModel = new RunTimeModel(Main); } - public override async Task GetServerConnection() - { - bool result = false; - - try - { - RunTimeModel.GetServerConnection(); - result = await Connect() == ConnectResult.Success; - } - catch (Exception e) - { - Main.GetMessage(e.GetErrorInfo(), TimeType.None); - } - - return result; - } - public override async Task Connect() { ConnectResult result = ConnectResult.ConnectFailed; try { - ConnectEventArgs EventArgs = new(Constant.Server_IP, Constant.Server_Port); + ConnectEventArgs EventArgs = new(RunTime.Session.Server_IP, RunTime.Session.Server_Port); if (Main.OnBeforeConnectEvent(EventArgs) == EventResult.Fail) return ConnectResult.ConnectFailed; result = await RunTimeModel.Connect(); @@ -121,5 +105,11 @@ namespace Milimoe.FunGame.Desktop.Controller { Main?.GetMessage(msg); } + + public override DataRequest NewDataRequest(DataRequestType RequestType) + { + DataRequest? request = RunTimeModel?.NewDataRequest(RequestType); + return request is null ? throw new ConnectFailedException() : request; + } } } diff --git a/FunGame.Desktop/Library/Config/Config.cs b/FunGame.Desktop/Library/Config/Config.cs index bbdad6e..aa21df8 100644 --- a/FunGame.Desktop/Library/Config/Config.cs +++ b/FunGame.Desktop/Library/Config/Config.cs @@ -2,19 +2,112 @@ { public class Config { - public static bool FunGame_isAutoConnect { get; set; } = true; // 是否自动连接服务器 - public static bool FunGame_isAutoLogin { get; set; } = false; // 是否自动登录 - public static bool FunGame_isMatching { get; set; } = false; // 是否在匹配中 - public static bool FunGame_isConnected { get; set; } = false; // 是否连接上服务器 - public static bool FunGame_isRetrying { get; set; } = false; // 是否正在重连 - public static bool FunGame_isAutoRetry { get; set; } = true; // 是否自动重连 - public static bool Match_Mix { get; set; } = false; // 混战模式选项 - public static bool Match_Team { get; set; } = false; // 团队模式选项 - public static bool Match_HasPass { get; set; } = false; // 密码房间选项 - public static string FunGame_ServerName { get; set; } = ""; // 服务器名称 - public static string FunGame_Notice { get; set; } = ""; // 公告 - public static string FunGame_AutoLoginUser { get; set; } = ""; // 自动登录的账号 - public static string FunGame_AutoLoginPassword { get; set; } = ""; // 自动登录的密码 - public static string FunGame_AutoLoginKey { get; set; } = ""; // 自动登录的秘钥 + /// + /// 是否自动连接服务器 + /// + public static bool FunGame_isAutoConnect + { + get => RunTime.Config.FunGame_isAutoConnect; + set => RunTime.Config.FunGame_isAutoConnect = value; + } + + /// + /// 是否自动登录 + /// + public static bool FunGame_isAutoLogin + { + get => RunTime.Config.FunGame_isAutoLogin; + set => RunTime.Config.FunGame_isAutoLogin = value; + } + + /// + /// 是否在匹配中 + /// + public static bool FunGame_isMatching + { + get => RunTime.Config.FunGame_isMatching; + set => RunTime.Config.FunGame_isMatching = value; + } + + /// + /// 是否连接上服务器 + /// + public static bool FunGame_isConnected + { + get => RunTime.Config.FunGame_isConnected; + set => RunTime.Config.FunGame_isConnected = value; + } + + /// + /// 是否正在重连 + /// + public static bool FunGame_isRetrying + { + get => RunTime.Config.FunGame_isRetrying; + set => RunTime.Config.FunGame_isRetrying = value; + } + + /// + /// 是否自动重连 + /// + public static bool FunGame_isAutoRetry + { + get => RunTime.Config.FunGame_isAutoRetry; + set => RunTime.Config.FunGame_isAutoRetry = value; + } + + /// + /// 当前游戏模式 + /// + public static string FunGame_GameMode + { + get => RunTime.Config.FunGame_GameMode; + set => RunTime.Config.FunGame_GameMode = value; + } + + /// + /// 服务器名称 + /// + public static string FunGame_ServerName + { + get => RunTime.Config.FunGame_ServerName; + set => RunTime.Config.FunGame_ServerName = value; + } + + /// + /// 公告 + /// + public static string FunGame_Notice + { + get => RunTime.Config.FunGame_Notice; + set => RunTime.Config.FunGame_Notice = value; + } + + /// + /// 自动登录的账号 + /// + public static string FunGame_AutoLoginUser + { + get => RunTime.Config.FunGame_AutoLoginUser; + set => RunTime.Config.FunGame_AutoLoginUser = value; + } + + /// + /// 自动登录的密码 + /// + public static string FunGame_AutoLoginPassword + { + get => RunTime.Config.FunGame_AutoLoginPassword; + set => RunTime.Config.FunGame_AutoLoginPassword = value; + } + + /// + /// 自动登录的秘钥 + /// + public static string FunGame_AutoLoginKey + { + get => RunTime.Config.FunGame_AutoLoginKey; + set => RunTime.Config.FunGame_AutoLoginKey = value; + } } } diff --git a/FunGame.Desktop/Library/Config/Constant.cs b/FunGame.Desktop/Library/Config/Constant.cs index ed4561f..68a61b6 100644 --- a/FunGame.Desktop/Library/Config/Constant.cs +++ b/FunGame.Desktop/Library/Config/Constant.cs @@ -10,13 +10,6 @@ namespace Milimoe.FunGame.Desktop.Library */ public static int FunGameType { get; } = (int)FunGameInfo.FunGame.FunGame_Desktop; - /** - * Socket Configs - */ - public static string Server_IP { get; set; } = ""; // 服务器IP地址 - public static int Server_Port { get; set; } = 0; // 服务器端口号 - public static Encoding Default_Encoding { get; } = General.DefaultEncoding; - /** * FunGame Configs */ diff --git a/FunGame.Desktop/Library/Config/RunTime.cs b/FunGame.Desktop/Library/Config/RunTime.cs index a1944a9..ec02e0f 100644 --- a/FunGame.Desktop/Library/Config/RunTime.cs +++ b/FunGame.Desktop/Library/Config/RunTime.cs @@ -1,5 +1,6 @@ using Milimoe.FunGame.Core.Api.Transmittal; using Milimoe.FunGame.Core.Library.Constant; +using Milimoe.FunGame.Desktop.Model; namespace Milimoe.FunGame.Desktop.Library { @@ -11,6 +12,7 @@ namespace Milimoe.FunGame.Desktop.Library { public static Core.Model.RoomList RoomList { get; } = new(); public static Core.Model.Session Session { get; } = new(); + public static Core.Model.FunGameConfig Config { get; } = new(); public static Core.Library.Common.Network.Socket? Socket { get; set; } = null; public static Controller.RunTimeController? Controller { get; set; } = null; public static UI.Main? Main { get; set; } = null; @@ -28,12 +30,8 @@ namespace Milimoe.FunGame.Desktop.Library public static DataRequest NewDataRequest(DataRequestType RequestType) { - if (Socket != null) - { - DataRequest request = new(Socket, RequestType); - return request; - } - throw new ConnectFailedException(); + DataRequest? request = Controller?.NewDataRequest(RequestType); + return request is null ? throw new ConnectFailedException() : request; } } } diff --git a/FunGame.Desktop/Model/RunTimeModel.cs b/FunGame.Desktop/Model/RunTimeModel.cs index 1b6a552..2489560 100644 --- a/FunGame.Desktop/Model/RunTimeModel.cs +++ b/FunGame.Desktop/Model/RunTimeModel.cs @@ -1,5 +1,4 @@ -using Milimoe.FunGame.Core.Api.Utility; -using Milimoe.FunGame.Core.Library.Common.Event; +using Milimoe.FunGame.Core.Library.Common.Event; using Milimoe.FunGame.Core.Library.Common.Network; using Milimoe.FunGame.Core.Library.Constant; using Milimoe.FunGame.Core.Library.Exception; @@ -14,8 +13,6 @@ namespace Milimoe.FunGame.Desktop.Model /// public class RunTimeModel : Core.Model.RunTime { - public override Socket? Socket => _Socket; - private readonly Main Main; private readonly Core.Model.Session Usercfg = RunTime.Session; @@ -26,10 +23,14 @@ namespace Milimoe.FunGame.Desktop.Model public override async Task Connect() { - if (Constant.Server_IP == "" || Constant.Server_Port <= 0) + if (RunTime.Session.Server_IP == "" || RunTime.Session.Server_Port <= 0) { - ShowMessage.ErrorMessage("查找可用的服务器失败!"); - return ConnectResult.FindServerFailed; + (RunTime.Session.Server_IP, RunTime.Session.Server_Port) = GetServerAddress(); + if (RunTime.Session.Server_IP == "" || RunTime.Session.Server_Port <= 0) + { + ShowMessage.ErrorMessage("查找可用的服务器失败!"); + return ConnectResult.FindServerFailed; + } } try { @@ -52,7 +53,7 @@ namespace Milimoe.FunGame.Desktop.Model // 与服务器建立连接 Socket?.Close(); Config.FunGame_isRetrying = true; - _Socket = Socket.Connect(Constant.Server_IP, Constant.Server_Port); + _Socket = Socket.Connect(RunTime.Session.Server_IP, RunTime.Session.Server_Port); if (Socket != null && Socket.Connected) { // 设置可复用Socket @@ -106,38 +107,10 @@ namespace Milimoe.FunGame.Desktop.Model { Main.GetMessage(e.GetErrorInfo(), TimeType.None); Main.UpdateUI(MainInvokeType.Disconnected); - Main.OnFailedConnectEvent(new ConnectEventArgs(Constant.Server_IP, Constant.Server_Port)); + Main.OnFailedConnectEvent(new ConnectEventArgs(RunTime.Session.Server_IP, RunTime.Session.Server_Port)); Close(); } - public override void GetServerConnection() - { - try - { - // 获取服务器IP - string? ipaddress = (string?)Implement.GetFunGameImplValue(InterfaceType.IClient, InterfaceMethod.RemoteServerIP); - if (ipaddress != null) - { - string[] s = ipaddress.Split(':'); - if (s != null && s.Length > 1) - { - Constant.Server_IP = s[0]; - Constant.Server_Port = Convert.ToInt32(s[1]); - } - } - else - { - ShowMessage.ErrorMessage("查找可用的服务器失败!"); - Config.FunGame_isRetrying = false; - throw new FindServerFailedException(); - } - } - catch (Exception e) - { - Main.GetMessage(e.GetErrorInfo(), TimeType.None); - } - } - protected override bool SocketHandler_Connect(SocketObject ServerMessage) { string msg = ""; diff --git a/FunGame.Desktop/UI/Main/Main.Designer.cs b/FunGame.Desktop/UI/Main/Main.Designer.cs index a55e16c..b6c57f3 100644 --- a/FunGame.Desktop/UI/Main/Main.Designer.cs +++ b/FunGame.Desktop/UI/Main/Main.Designer.cs @@ -204,7 +204,7 @@ namespace Milimoe.FunGame.Desktop.UI CheckMix.Text = "混战模式房间"; CheckMix.TextAlign = ContentAlignment.BottomLeft; CheckMix.UseVisualStyleBackColor = false; - CheckMix.CheckedChanged += CheckMix_CheckedChanged; + CheckMix.CheckedChanged += CheckGameMode_CheckedChanged; // // CheckTeam // @@ -217,7 +217,7 @@ namespace Milimoe.FunGame.Desktop.UI CheckTeam.Text = "团队模式房间"; CheckTeam.TextAlign = ContentAlignment.BottomLeft; CheckTeam.UseVisualStyleBackColor = false; - CheckTeam.CheckedChanged += CheckTeam_CheckedChanged; + CheckTeam.CheckedChanged += CheckGameMode_CheckedChanged; // // RoomSetting // @@ -491,7 +491,7 @@ namespace Milimoe.FunGame.Desktop.UI CheckHasPass.Text = "带密码的房间"; CheckHasPass.TextAlign = ContentAlignment.BottomLeft; CheckHasPass.UseVisualStyleBackColor = false; - CheckHasPass.CheckedChanged += CheckHasPass_CheckedChanged; + CheckHasPass.CheckedChanged += CheckGameMode_CheckedChanged; // // Stock // diff --git a/FunGame.Desktop/UI/Main/Main.cs b/FunGame.Desktop/UI/Main/Main.cs index 18f7bd0..fe375c1 100644 --- a/FunGame.Desktop/UI/Main/Main.cs +++ b/FunGame.Desktop/UI/Main/Main.cs @@ -64,7 +64,7 @@ namespace Milimoe.FunGame.Desktop.UI break; } } - if (Config.FunGame_isAutoConnect) RunTime.Controller?.GetServerConnection(); + if (Config.FunGame_isAutoConnect) RunTime.Controller?.Connect(); }); } @@ -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_IP)); + SetServerStatusLight((int)LightType.Green, ping: NetworkUtility.GetServerPing(RunTime.Session.Server_IP)); SetButtonEnableIfLogon(true, ClientState.Online); Config.FunGame_isConnected = true; CurrentRetryTimes = 0; @@ -900,13 +900,7 @@ namespace Milimoe.FunGame.Desktop.UI WritelnGameInfo(DateTimeUtility.GetNowShortTime() + " 开始匹配"); WritelnGameInfo("[ " + Usercfg.LoginUserName + " ] 开始匹配"); WriteGameInfo(">> 匹配参数:"); - if (!Config.Match_Mix && !Config.Match_Team && !Config.Match_HasPass) - WritelnGameInfo("无"); - else - { - WriteGameInfo((Config.Match_Mix ? " 混战房间 " : "") + (Config.Match_Team ? " 团队房间 " : "") + (Config.Match_HasPass ? " 密码房间 " : "")); - WritelnGameInfo(); - } + WritelnGameInfo(Config.FunGame_GameMode); // 显示停止匹配按钮 StartMatch.Visible = false; StopMatch.Visible = true; @@ -939,30 +933,13 @@ namespace Milimoe.FunGame.Desktop.UI /// private async void CreateRoom_Click(object sender, EventArgs e) { - string roomtype = ""; string password = ""; - if (Config.Match_Mix && Config.Match_Team) + if (CheckMix.Checked && CheckTeam.Checked) { ShowMessage.WarningMessage("创建房间不允许同时勾选混战和团队!"); return; } - else if (Config.Match_Mix && !Config.Match_Team && !Config.Match_HasPass) - { - roomtype = GameMode.GameMode_Mix; - } - else if (!Config.Match_Mix && Config.Match_Team && !Config.Match_HasPass) - { - roomtype = GameMode.GameMode_Team; - } - else if (Config.Match_Mix && !Config.Match_Team && Config.Match_HasPass) - { - roomtype = GameMode.GameMode_MixHasPass; - } - else if (!Config.Match_Mix && Config.Match_Team && Config.Match_HasPass) - { - roomtype = GameMode.GameMode_TeamHasPass; - } - if (Config.Match_HasPass) + if (CheckHasPass.Checked) { password = ShowMessage.InputMessage("请输入该房间的密码:", "创建密码房间").Trim(); if (password == "" || password.Length > 10) @@ -971,12 +948,12 @@ namespace Milimoe.FunGame.Desktop.UI return; } } - if (roomtype.Equals("")) + if (Config.FunGame_GameMode.Equals("")) { ShowMessage.WarningMessage("请勾选你要创建的房间类型!"); return; } - await CreateRoom_Handler(roomtype, password); + await CreateRoom_Handler(Config.FunGame_GameMode, password); } /// @@ -1094,36 +1071,22 @@ namespace Milimoe.FunGame.Desktop.UI } /// - /// 勾选混战选项 + /// 勾选任意模式选项 /// /// /// - private void CheckMix_CheckedChanged(object sender, EventArgs e) + private void CheckGameMode_CheckedChanged(object sender, EventArgs e) { - if (CheckMix.Checked) Config.Match_Mix = true; - else Config.Match_Mix = false; - } - - /// - /// 勾选团队选项 - /// - /// - /// - private void CheckTeam_CheckedChanged(object sender, EventArgs e) - { - if (CheckTeam.Checked) Config.Match_Team = true; - else Config.Match_Team = false; - } - - /// - /// 勾选密码选项 - /// - /// - /// - private void CheckHasPass_CheckedChanged(object sender, EventArgs e) - { - if (CheckHasPass.Checked) Config.Match_HasPass = true; - else Config.Match_HasPass = false; + bool IsMix = CheckMix.Checked; + bool IsTeam = CheckTeam.Checked; + bool IsHasPass = CheckHasPass.Checked; + if (IsMix && IsTeam && !IsHasPass) Config.FunGame_GameMode = GameMode.GameMode_All; + else if (IsMix && IsTeam && IsHasPass) Config.FunGame_GameMode = GameMode.GameMode_AllHasPass; + else if (IsMix && !IsTeam && !IsHasPass) Config.FunGame_GameMode = GameMode.GameMode_Mix; + else if (IsMix && !IsTeam && IsHasPass) Config.FunGame_GameMode = GameMode.GameMode_MixHasPass; + else if (!IsMix && IsTeam && !IsHasPass) Config.FunGame_GameMode = GameMode.GameMode_Team; + else if (!IsMix && IsTeam && IsHasPass) Config.FunGame_GameMode = GameMode.GameMode_TeamHasPass; + else Config.FunGame_GameMode = GameMode.GameMode_All; } /// @@ -1370,7 +1333,7 @@ namespace Milimoe.FunGame.Desktop.UI { CurrentRetryTimes = -1; Config.FunGame_isAutoRetry = true; - RunTime.Controller?.GetServerConnection(); + RunTime.Controller?.Connect(); } break; case Constant.FunGame_Disconnect: @@ -1410,8 +1373,8 @@ namespace Milimoe.FunGame.Desktop.UI ErrorType ErrorType = NetworkUtility.IsServerAddress(ip, port); if (ErrorType == Core.Library.Constant.ErrorType.None) { - Constant.Server_IP = ip; - Constant.Server_Port = port; + RunTime.Session.Server_IP = ip; + RunTime.Session.Server_Port = port; CurrentRetryTimes = -1; Config.FunGame_isAutoRetry = true; RunTime.Controller?.Connect();