From db9437ada65ba308d42c9e7f7d7fc7d06ae56050 Mon Sep 17 00:00:00 2001 From: Mili Date: Thu, 8 Sep 2022 00:17:32 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E8=AF=95checklogin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FunGame.Core.Api/Model/Enum/CommonEnums.cs | 83 +++++++++++---------- FunGame.Core.Api/Model/Enum/FunGameEnums.cs | 54 +++++++------- FunGame.Desktop/Models/Config/Config.cs | 17 +++-- FunGame.Desktop/Models/Config/Usercfg.cs | 6 +- FunGame.Desktop/UI/Main/Main.cs | 80 ++++++++++++++++---- FunGame.Desktop/Utils/WebHelper.cs | 75 +++++++++++-------- 6 files changed, 193 insertions(+), 122 deletions(-) diff --git a/FunGame.Core.Api/Model/Enum/CommonEnums.cs b/FunGame.Core.Api/Model/Enum/CommonEnums.cs index ccc2701..4aa96b6 100644 --- a/FunGame.Core.Api/Model/Enum/CommonEnums.cs +++ b/FunGame.Core.Api/Model/Enum/CommonEnums.cs @@ -17,33 +17,33 @@ namespace FunGame.Core.Api.Model.Enum public enum StartMatch_State { - Matching = 1, - Success = 2, - Enable = 3, - Cancel = 4 + Matching, + Success, + Enable, + Cancel } public enum CreateRoom_State { - Creating = 1, - Success = 2 + Creating, + Success } public enum RoomState { - Created = 1, - Gaming = 2, - Close = 3, - Complete = 4 + Created, + Gaming, + Close, + Complete } public enum OnlineState { - Offline = 1, - Online = 2, - Matching = 3, - InRoom = 4, - Gaming = 5 + Offline, + Online, + Matching, + InRoom, + Gaming } #endregion @@ -52,33 +52,33 @@ namespace FunGame.Core.Api.Model.Enum public enum RoomType { - Mix = 1, - Team = 2, - MixHasPass = 3, - TeamHasPass = 4 + Mix, + Team, + MixHasPass, + TeamHasPass } public enum InterfaceType { - ClientConnectInterface = 1, - ServerInterface = 2 + ClientConnectInterface, + ServerInterface } public enum LightType { - Green = 1, - Yellow = 2, - Red = 3 + Green, + Yellow, + Red } public enum SocketType { - Unknown = 0, - GetNotice = 1, - Login = 2, - CheckLogin = 3, - Logout = 4, - HeartBeat = 5 + Unknown, + GetNotice, + Login, + CheckLogin, + Logout, + HeartBeat } #endregion @@ -87,11 +87,11 @@ namespace FunGame.Core.Api.Model.Enum public enum MessageResult { - OK = 1, - Cancel = 2, - Yes = 3, - No = 4, - Retry = 5 + OK, + Cancel, + Yes, + No, + Retry } #endregion @@ -100,16 +100,17 @@ namespace FunGame.Core.Api.Model.Enum public enum WebHelperMethod { - CreateSocket = 1, - CloseSocket = 2, - StartWebHelper = 3, + CreateSocket, + CloseSocket, + StartWebHelper, + Login } public enum InterfaceMethod { - RemoteServerIP = 1, - DBConnection = 2, - GetServerSettings = 3 + RemoteServerIP, + DBConnection, + GetServerSettings } #endregion diff --git a/FunGame.Core.Api/Model/Enum/FunGameEnums.cs b/FunGame.Core.Api/Model/Enum/FunGameEnums.cs index 53d3901..2a61d92 100644 --- a/FunGame.Core.Api/Model/Enum/FunGameEnums.cs +++ b/FunGame.Core.Api/Model/Enum/FunGameEnums.cs @@ -6,39 +6,41 @@ using System.Threading.Tasks; namespace FunGame.Core.Api.Model.Enum { - /// - /// 用于记录版本号和更新日志 - /// public static class FunGameEnums { - public const string FunGame_Core = "FunGame Core"; - public const string FunGame_Core_Api = "FunGame Core Api"; - public const string FunGame_Console = "FunGame Console"; - public const string FunGame_Desktop = "FunGame Desktop"; - public const string FunGame_Server = "FunGame Server"; - - public const int FirstVersion = 1; - public const int SecondVersion = 0; - public const int ThirdVersion = 0; + private const string FunGame_Core = "FunGame Core"; + private const string FunGame_Core_Api = "FunGame Core Api"; + private const string FunGame_Console = "FunGame Console"; + private const string FunGame_Desktop = "FunGame Desktop"; + private const string FunGame_Server = "FunGame Server Console"; - public enum Patch + private const string FunGame_Version = "v1.0"; + private const string FunGame_VersionPatch = ""; + + public enum FunGame { - Latest = 20221001, - Patch20220906 = 20220906 + FunGame_Core, + FunGame_Core_Api, + FunGame_Console, + FunGame_Desktop, + FunGame_Server } - public enum History + public static string GetInfo(FunGame FunGameType) { - Latest = 20221001, - R20220906 = 20220906 - } - - public static string GetVersion() - { - return "=/=\\=/=\\=/=\\=/= > FunGame版本信息 < =\\=/=\\=/=\\=/=\\=" + "\n" + - FunGame_Core + " -> v" + FirstVersion + "." + SecondVersion + ((int)Patch.Latest == (int)History.Latest ? " Patch" + (int)Patch.Latest : "") + "\n" + - FunGame_Core_Api + " -> v" + FirstVersion + "." + SecondVersion + ((int)Patch.Latest == (int)History.Latest ? " Patch" + (int)Patch.Latest : "") + "\n" + - FunGame_Desktop + " -> v" + FirstVersion + "." + SecondVersion + ((int)Patch.Latest == (int)History.Latest ? " Patch" + (int)Patch.Latest : ""); + string type = FunGameType switch + { + FunGame.FunGame_Core => FunGame_Core, + FunGame.FunGame_Core_Api => FunGame_Core_Api, + FunGame.FunGame_Console => FunGame_Console, + FunGame.FunGame_Desktop => FunGame_Desktop, + FunGame.FunGame_Server => FunGame_Server, + _ => "" + }; + if (type.Equals(FunGame_Desktop)) + return type + " [ 版本: " + FunGame_Version + FunGame_VersionPatch + " ]\n©2022 Mili.cyou. 保留所有权利\n"; + else + return type + " [ 版本: " + FunGame_Version + FunGame_VersionPatch + " ]\n(C)2022 Mili.cyou. 保留所有权利\n"; } /** diff --git a/FunGame.Desktop/Models/Config/Config.cs b/FunGame.Desktop/Models/Config/Config.cs index f14df25..eb0d333 100644 --- a/FunGame.Desktop/Models/Config/Config.cs +++ b/FunGame.Desktop/Models/Config/Config.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Security.AccessControl; using System.Text; using System.Threading.Tasks; +using FunGame.Core.Api.Model.Enum; using FunGame.Core.Api.Util; namespace FunGame.Desktop.Models.Config @@ -13,10 +14,7 @@ namespace FunGame.Desktop.Models.Config /** * Game Configs */ - public static string VERSION = ""; - public static string VERSION_TYPE = ""; - public static string VERSION_PATCH = ""; - public static string VERSION_DATE = ""; + public static FunGameEnums.FunGame FunGameType = FunGameEnums.FunGame.FunGame_Desktop; public static INIHelper DefaultINIHelper = new(); public static AssemblyHelper DefaultAssemblyHelper = new(); @@ -30,6 +28,7 @@ namespace FunGame.Desktop.Models.Config public const string WebHelper_SetYellow = "-WebHelper .set yellow"; public const string WebHelper_Disconnected = "-WebHelper .disconnected"; public const string WebHelper_GetUser = "-WebHelper .get user"; + public const string WebHelper_SetUser = "-WebHelper .set user"; public static int WebHelper_HeartBeatFaileds = 0; /** @@ -58,7 +57,7 @@ namespace FunGame.Desktop.Models.Config public const string FunGame_Retry = "重新连接"; public const string FunGame_AutoRetryOn = "开启自动重连"; public const string FunGame_AutoRetryOff = "关闭自动重连"; - public static readonly object[] PresetItems = + public static readonly object[] PresetOnineItems = { FunGame_PresetMessage, FunGame_SignIn, @@ -72,6 +71,12 @@ namespace FunGame.Desktop.Models.Config FunGame_AutoRetryOn, FunGame_AutoRetryOff }; - public const string FunGame_ConnectByDebug = "连接到Debug"; + public static readonly object[] PresetNoLoginItems = + { + FunGame_PresetMessage, + FunGame_Retry, + FunGame_AutoRetryOn, + FunGame_AutoRetryOff + }; } } diff --git a/FunGame.Desktop/Models/Config/Usercfg.cs b/FunGame.Desktop/Models/Config/Usercfg.cs index 34e583d..e083630 100644 --- a/FunGame.Desktop/Models/Config/Usercfg.cs +++ b/FunGame.Desktop/Models/Config/Usercfg.cs @@ -1,4 +1,5 @@ -using System; +using FunGame.Core.Api.Model.Entity; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -11,6 +12,8 @@ namespace FunGame.Desktop.Models.Config /** * 玩家设定内容 */ + public static User? LoginUser = null; // 已登录的用户 + public static string LoginUserName = ""; // 已登录用户名 public static bool Match_Mix = false; // 混战模式选项 public static bool Match_Team = false; // 团队模式选项 public static bool Match_HasPass = false; // 密码房间选项 @@ -19,6 +22,5 @@ namespace FunGame.Desktop.Models.Config public static bool FunGame_isRetrying = false; // 是否正在重连 public static bool FunGame_isAutoRetry = true; // 是否自动重连 public static string FunGame_roomid = "-1"; // 房间号 - public static string LoginUserName = ""; // 已登录用户名 } } diff --git a/FunGame.Desktop/UI/Main/Main.cs b/FunGame.Desktop/UI/Main/Main.cs index 7b6a3e2..f01aa76 100644 --- a/FunGame.Desktop/UI/Main/Main.cs +++ b/FunGame.Desktop/UI/Main/Main.cs @@ -29,7 +29,6 @@ namespace FunGame.Desktop.UI * 定义全局对象 */ private Task? MatchFunGame = null; // 匹配线程 - private User? LoginUser = null; // 登录的玩家对象 private WebHelper? WebHelper = null; // WebHelper /** @@ -52,7 +51,7 @@ namespace FunGame.Desktop.UI /// public void Init() { - this.PresetText.SelectedIndex = 0; // 快捷消息初始选择 + SetButtonEnableIfLogon(false); SetRoomid("-1"); // 房间号初始化 ShowFunGameInfo(); // 显示FunGame信息 GetServerConnection(); // 开始连接服务器 @@ -69,7 +68,7 @@ namespace FunGame.Desktop.UI /// /// /// - public object? GetMessage(WebHelper webHelper, string? msg, bool needTime = false) + public object? GetMessage(WebHelper webHelper, string? msg, bool needTime = false, object[]? objs = null) { try { @@ -82,6 +81,7 @@ namespace FunGame.Desktop.UI WebHelper_Action = (main) => { SetServerStatusLight((int)CommonEnums.LightType.Green); + SetButtonEnableIfLogon(true); }; if (InvokeRequired) BeginInvoke(WebHelper_Action, this); @@ -95,6 +95,7 @@ namespace FunGame.Desktop.UI WebHelper_Action = (main) => { SetServerStatusLight((int)CommonEnums.LightType.Green, GetServerPing(Config.SERVER_IPADRESS)); + SetButtonEnableIfLogon(true); }; if (InvokeRequired) BeginInvoke(WebHelper_Action, this); @@ -108,6 +109,7 @@ namespace FunGame.Desktop.UI WebHelper_Action = (main) => { SetServerStatusLight((int)CommonEnums.LightType.Yellow); + SetButtonEnableIfLogon(false); }; if (InvokeRequired) BeginInvoke(WebHelper_Action, this); @@ -120,6 +122,7 @@ namespace FunGame.Desktop.UI WebHelper_Action = (main) => { SetServerStatusLight((int)CommonEnums.LightType.Red); + SetButtonEnableIfLogon(false); }; if (InvokeRequired) BeginInvoke(WebHelper_Action, this); @@ -132,6 +135,7 @@ namespace FunGame.Desktop.UI WebHelper_Action = (main) => { SetServerStatusLight((int)CommonEnums.LightType.Red); + SetButtonEnableIfLogon(false); }; if (InvokeRequired) BeginInvoke(WebHelper_Action, this); @@ -156,8 +160,17 @@ namespace FunGame.Desktop.UI else throw new Exception("ERROR:无法连接至服务器,请检查你的网络连接。"); case Config.WebHelper_GetUser: - if (LoginUser != null) - return LoginUser; + if (Usercfg.LoginUser != null) + return Usercfg.LoginUser; + return null; + case Config.WebHelper_SetUser: + if (objs != null && objs.Length > 1) + { + if (InvokeRequired) + BeginInvoke(SetLoginUser, objs); + else + SetLoginUser(objs); + } return null; default: if (needTime) @@ -307,12 +320,10 @@ namespace FunGame.Desktop.UI /// private void SetLoginUser(object[]? objs = null) { - if (objs != null && objs.Length > 0) - { - LoginUser = (User)objs[0]; - Usercfg.LoginUserName = LoginUser.Userame; - } - LoginAccount(); + if (InvokeRequired) + BeginInvoke(LoginAccount, objs); + else + LoginAccount(objs); } /// @@ -400,6 +411,33 @@ namespace FunGame.Desktop.UI RoomSetting.Visible = true; } + /// + /// 未登录和离线时,停用按钮 + /// + private void SetButtonEnableIfLogon(bool isLogon) + { + if (isLogon) + { + PresetText.Items.Clear(); + PresetText.Items.AddRange(Config.PresetOnineItems); + } + else + { + PresetText.Items.Clear(); + PresetText.Items.AddRange(Config.PresetNoLoginItems); + } + this.PresetText.SelectedIndex = 0; + CheckMix.Enabled = isLogon; + CheckTeam.Enabled = isLogon; + CheckHasPass.Enabled = isLogon; + StartMatch.Enabled = isLogon; + CreateRoom.Enabled = isLogon; + RoomBox.Enabled = isLogon; + AccountSetting.Enabled = isLogon; + Stock.Enabled = isLogon; + Store.Enabled = isLogon; + } + /// /// 加入房间 /// @@ -572,11 +610,18 @@ namespace FunGame.Desktop.UI /// /// 登录账号,显示登出按钮 /// - private void LoginAccount() + private void LoginAccount(object[]? objs = null) { + if (objs != null && objs.Length > 0) + { + Usercfg.LoginUser = (User)objs[2]; + Usercfg.LoginUserName = Usercfg.LoginUser.Userame; + } NowAccount.Text = "[ID] " + Usercfg.LoginUserName; Login.Visible = false; Logout.Visible = true; + SetServerStatusLight((int)LightType.Green); + ShowMessage.TipMessage("欢迎回来, " + Usercfg.LoginUserName + "!", "登录成功"); } /// @@ -617,7 +662,7 @@ namespace FunGame.Desktop.UI // 向消息队列发送消息 if (!TalkText.Text.Trim().Equals("") && !TalkText.ForeColor.Equals(Color.DarkGray)) { - WritelnGameInfo(GetNowShortTime() + " [ " + Usercfg.LoginUserName + " ] 说: " + TalkText.Text); + WritelnGameInfo(GetNowShortTime() + " [ " + (!Usercfg.LoginUserName.Equals("") ? Usercfg.LoginUserName : "尚未登录") + " ] 说: " + TalkText.Text); SwitchTalkMessage(TalkText.Text); TalkText.Text = ""; if (isLeave) TalkText_Leave(); // 回车不离开焦点 @@ -636,7 +681,7 @@ namespace FunGame.Desktop.UI /// private void SendTalkText_Click(string msg) { - WritelnGameInfo(GetNowShortTime() + " [ " + Usercfg.LoginUserName + " ] 说: " + msg); + WritelnGameInfo(GetNowShortTime() + " [ " + (!Usercfg.LoginUserName.Equals("") ? Usercfg.LoginUserName : "尚未登录") + " ] 说: " + msg); } /// @@ -736,7 +781,7 @@ namespace FunGame.Desktop.UI /// private void ShowFunGameInfo() { - WritelnGameInfo(FunGameEnums.GetVersion()); + WritelnGameInfo(FunGameEnums.GetInfo(Config.FunGameType)); } #endregion @@ -930,7 +975,10 @@ namespace FunGame.Desktop.UI /// private void Login_Click(object sender, EventArgs e) { - LoginAccount(); + if (WebHelper != null) + WebHelper.WebHelpMethod((int)CommonEnums.WebHelperMethod.Login); + else + ShowMessage.WarningMessage("请先连接服务器!"); } /// diff --git a/FunGame.Desktop/Utils/WebHelper.cs b/FunGame.Desktop/Utils/WebHelper.cs index ed03872..94f8122 100644 --- a/FunGame.Desktop/Utils/WebHelper.cs +++ b/FunGame.Desktop/Utils/WebHelper.cs @@ -34,7 +34,7 @@ namespace FunGame.Desktop.Utils /// 选择WebHelp分支方法 /// /// 分支方法ID - public void WebHelpMethod(int i) + public bool WebHelpMethod(int i) { switch (i) { @@ -47,7 +47,16 @@ namespace FunGame.Desktop.Utils case (int)CommonEnums.WebHelperMethod.StartWebHelper: StartWebHelper(); break; + case (int)CommonEnums.WebHelperMethod.Login: + if (client != null) + { + Send((int)CommonEnums.SocketType.CheckLogin, new object[] { Main, client, new User("Mili") }); + return true; + } + else + return false; } + return true; } /// @@ -148,6 +157,7 @@ namespace FunGame.Desktop.Utils case (int)CommonEnums.SocketType.Login: break; case (int)CommonEnums.SocketType.CheckLogin: + Main.GetMessage(this, Config.WebHelper_SetUser, true, objs); StartWebHelper(); // 开始创建TCP流 return true; case (int)CommonEnums.SocketType.Logout: @@ -198,50 +208,38 @@ namespace FunGame.Desktop.Utils if (socket != null) { string msg = ""; - byte[] buffer; - int length; + CommonEnums.SocketType type = (CommonEnums.SocketType)i; // 发送消息给服务器端 - switch (i) + switch (type) { - case (int)CommonEnums.SocketType.GetNotice: - msg = "获取公告"; - buffer = new byte[2048]; - buffer = Config.DEFAULT_ENCODING.GetBytes(MakeMessage((int)CommonEnums.SocketType.GetNotice, msg)); - length = socket.Send(buffer); - if (length > 0) + case CommonEnums.SocketType.GetNotice: + msg = MakeMessage(type, "获取公告"); + if (Send(msg, socket) > 0) { return Read(objs); } + else + throw new Exception("ERROR:消息未送达服务器,与服务器连接可能丢失。"); + case CommonEnums.SocketType.Login: break; - case (int)CommonEnums.SocketType.Login: - break; - case (int)CommonEnums.SocketType.CheckLogin: + case CommonEnums.SocketType.CheckLogin: User user; if (objs != null && objs.Length > 2) { user = (User)objs[2]; - msg = user.Userame; + msg = MakeMessage(type, user.Userame); } else { Usercfg.FunGame_isAutoRetry = false; throw new Exception("ERROR: 请登录账号。"); } - buffer = new byte[2048]; - buffer = Config.DEFAULT_ENCODING.GetBytes(MakeMessage((int)CommonEnums.SocketType.CheckLogin, msg)); - length = socket.Send(buffer); - if (length > 0) - { - return Read(objs); - } - else - throw new Exception("ERROR:消息未送达服务器,与服务器连接可能丢失。"); - case (int)CommonEnums.SocketType.Logout: break; - case (int)CommonEnums.SocketType.HeartBeat: - buffer = new byte[2048]; - buffer = Config.DEFAULT_ENCODING.GetBytes(Convert.ToString(MakeMessage((int)CommonEnums.SocketType.HeartBeat, "心跳检测"))); - if (socket.Send(buffer) > 0) + case CommonEnums.SocketType.Logout: + break; + case CommonEnums.SocketType.HeartBeat: + msg = MakeMessage(type, "心跳检测"); + if (Send(msg, socket) > 0) { WaitHeartBeat = Task.Run(() => { @@ -251,8 +249,16 @@ namespace FunGame.Desktop.Utils return true; } AddHeartBeatFaileds(main); - break; + return false; + default: + return false; } + if (Send(msg, socket) > 0) + { + return Read(objs); + } + else + throw new Exception("ERROR:消息未送达服务器,与服务器连接可能丢失。"); } else { @@ -267,6 +273,13 @@ namespace FunGame.Desktop.Utils return false; } + private int Send(string msg, Socket socket) + { + byte[] buffer = Config.DEFAULT_ENCODING.GetBytes(msg); + int length = socket.Send(buffer); + return length; + } + private void CatchException(Main main, Exception e, bool isDisconnected) { if (isDisconnected) @@ -307,9 +320,9 @@ namespace FunGame.Desktop.Utils return msg[index..]; } - private string MakeMessage(int type, string msg) + private string MakeMessage(CommonEnums.SocketType type, string msg) { - return type + ";" + msg; + return (int)type + ";" + msg; } private void Close()