diff --git a/FunGame.Core/Api/Data/SQLHelper.cs b/FunGame.Core/Api/Data/SQLHelper.cs index c920d67..72a7db4 100644 --- a/FunGame.Core/Api/Data/SQLHelper.cs +++ b/FunGame.Core/Api/Data/SQLHelper.cs @@ -10,6 +10,7 @@ namespace Milimoe.FunGame.Core.Api.Data /// public abstract class SQLHelper : ISQLHelper { + public abstract FunGameInfo.FunGame FunGameType { get; } public abstract string Script { get; set; } public abstract CommandType CommandType { get; set; } public abstract SQLResult Result { get; } diff --git a/FunGame.Core/Interface/Base/ISQLHelper.cs b/FunGame.Core/Interface/Base/ISQLHelper.cs index 0227d51..a671483 100644 --- a/FunGame.Core/Interface/Base/ISQLHelper.cs +++ b/FunGame.Core/Interface/Base/ISQLHelper.cs @@ -6,11 +6,16 @@ namespace Milimoe.FunGame.Core.Interface.Base { public interface ISQLHelper { + public FunGameInfo.FunGame FunGameType { get; } public string Script { get; set; } public CommandType CommandType { get; set; } public SQLResult Result { get; } public SQLServerInfo ServerInfo { get; } public int UpdateRows { get; } public DataSet DataSet { get; } + + public int Execute(out SQLResult Result); + public DataSet ExecuteDataSet(out SQLResult Result); + public void Close(); } } diff --git a/FunGame.Core/Interface/Base/ISocket.cs b/FunGame.Core/Interface/Base/ISocket.cs index ff63a2e..c6ee270 100644 --- a/FunGame.Core/Interface/Base/ISocket.cs +++ b/FunGame.Core/Interface/Base/ISocket.cs @@ -1,4 +1,5 @@ -using Milimoe.FunGame.Core.Library.Constant; +using System.Collections; +using Milimoe.FunGame.Core.Library.Constant; namespace Milimoe.FunGame.Core.Interface.Base { @@ -11,13 +12,7 @@ namespace Milimoe.FunGame.Core.Interface.Base public int ServerPort { get; } public string ServerName { get; } public string ServerNotice { get; } - public bool Connected - { - get - { - return Instance != null && Instance.Connected; - } - } + public bool Connected => Instance != null && Instance.Connected; public bool Receiving { get; } public SocketResult Send(SocketMessageType type, params object[] objs); public object[] Receive(); diff --git a/FunGame.Core/Library/Common/Network/ServerSocket.cs b/FunGame.Core/Library/Common/Network/ServerSocket.cs index d05f3a7..a232b07 100644 --- a/FunGame.Core/Library/Common/Network/ServerSocket.cs +++ b/FunGame.Core/Library/Common/Network/ServerSocket.cs @@ -16,8 +16,10 @@ namespace Milimoe.FunGame.Core.Library.Common.Network public string ServerNotice { get; } = ""; public bool Connected => Instance != null && Instance.Connected; public bool Receiving => _Receiving; + public List GetUsersList => UserThreads.GetList(); + public int UsersCount => UserThreads.Count; - private readonly ThreadManager PlayerThreads; + private readonly ThreadManager UserThreads; private bool _Receiving = false; private ServerSocket(System.Net.Sockets.Socket Instance, int ServerPort, int MaxConnection = 0) @@ -25,9 +27,9 @@ namespace Milimoe.FunGame.Core.Library.Common.Network this.Instance = Instance; this.ServerPort = ServerPort; if (MaxConnection <= 0) - PlayerThreads = new ThreadManager(); + UserThreads = new ThreadManager(); else - PlayerThreads = new ThreadManager(MaxConnection); + UserThreads = new ThreadManager(MaxConnection); } public static ServerSocket StartListening(int Port = 22222, int MaxConnection = 0) @@ -50,14 +52,24 @@ namespace Milimoe.FunGame.Core.Library.Common.Network throw new SocketGetClientException(); } - public bool AddClient(string ClientName, BaseModel t) + public bool AddUser(string UserName, BaseModel t) { - return PlayerThreads.Add(ClientName, t); + return UserThreads.Add(UserName, t); } - public bool RemoveClient(string ClientName) + public bool RemoveUser(string UserName) { - return PlayerThreads.Remove(ClientName); + return UserThreads.Remove(UserName); + } + + public bool ContainsUser(string UserName) + { + return UserThreads.ContainsKey(UserName); + } + + public BaseModel GetUser(string UserName) + { + return UserThreads[UserName]; } public SocketResult Send(SocketMessageType type, params object[] objs) diff --git a/FunGame.Core/Library/Constant/TypeEnum.cs b/FunGame.Core/Library/Constant/TypeEnum.cs index 70d0c57..4c58996 100644 --- a/FunGame.Core/Library/Constant/TypeEnum.cs +++ b/FunGame.Core/Library/Constant/TypeEnum.cs @@ -47,19 +47,18 @@ Red } - [Flags] public enum SocketMessageType { - Unknown = 0, - Connect = 1 << 0, - GetNotice = 1 << 1, - Login = 1 << 2, - CheckLogin = 1 << 4, - Logout = 1 << 5, - Disconnect = 1 << 6, - HeartBeat = 1 << 7, - IntoRoom = 1 << 8, - Chat = 1 << 9 + Unknown, + Connect, + GetNotice, + Login, + CheckLogin, + Logout, + Disconnect, + HeartBeat, + IntoRoom, + Chat } public enum SocketRuntimeType diff --git a/FunGame.Core/Library/Exception/Exception.cs b/FunGame.Core/Library/Exception/Exception.cs index a3525c7..00ac5ee 100644 --- a/FunGame.Core/Library/Exception/Exception.cs +++ b/FunGame.Core/Library/Exception/Exception.cs @@ -114,4 +114,9 @@ { public override string Message => "无法加入指定房间 (#10023)"; } + + public class CanNotSendTalkException : Exception + { + public override string Message => "无法发送公共信息 (#10024)"; + } } diff --git a/FunGame.Core/Library/Server/BaseServerModel.cs b/FunGame.Core/Library/Server/BaseServerModel.cs index 8ed748f..1b977fe 100644 --- a/FunGame.Core/Library/Server/BaseServerModel.cs +++ b/FunGame.Core/Library/Server/BaseServerModel.cs @@ -1,19 +1,45 @@ -using Milimoe.FunGame.Core.Library.Common.Network; +using Milimoe.FunGame.Core.Entity; +using Milimoe.FunGame.Core.Library.Common.Network; using Milimoe.FunGame.Core.Library.Constant; +using Milimoe.FunGame.Core.Service; namespace Milimoe.FunGame.Core.Library.Server { public abstract class BaseModel { - public bool Running = false; - public ClientSocket? Socket = null; - public Task? Task = null; - public string ClientName = ""; + public abstract bool Running { get; } + public abstract ClientSocket? Socket { get; } + public abstract Task? Task { get; } + public abstract string ClientName { get; } + public abstract User? User { get; } + public List GetClientsList => ClientThreads.GetList(); + public int ClientsCount => ClientThreads.Count; public abstract bool Read(ClientSocket socket); - public abstract bool Send(ClientSocket socket, SocketMessageType type, params object[] objs); - public abstract void Start(); + + private readonly ThreadManager ClientThreads = new(); + + public bool AddClient(string ClientName, BaseModel t) + { + return ClientThreads.Add(ClientName, t); + } + + public bool RemoveClient(string ClientName) + { + return ClientThreads.Remove(ClientName); + } + + public bool ContainsClient(string ClientName) + { + return ClientThreads.ContainsKey(ClientName); + } + + public BaseModel GetClient(string ClientName) + { + return ClientThreads[ClientName]; + } + } } diff --git a/FunGame.Core/Service/ThreadManager.cs b/FunGame.Core/Service/ThreadManager.cs index e91384d..900e093 100644 --- a/FunGame.Core/Service/ThreadManager.cs +++ b/FunGame.Core/Service/ThreadManager.cs @@ -5,6 +5,11 @@ namespace Milimoe.FunGame.Core.Service { internal class ThreadManager { + /// + /// 目前的线程数量 + /// + internal int Count => Threads.Count; + /// /// 最大接受的线程数量 /// @@ -80,6 +85,11 @@ namespace Milimoe.FunGame.Core.Service return result; } + internal bool ContainsKey(string name) + { + return Threads.ContainsKey(name); + } + /// /// 清空线程管理器 /// @@ -88,5 +98,13 @@ namespace Milimoe.FunGame.Core.Service Threads.Clear(); } + + /// + /// 获取线程对象的列表 + /// + internal List GetList() + { + return Threads.Values.ToList(); + } } } diff --git a/FunGame.Desktop/Controller/MainController.cs b/FunGame.Desktop/Controller/MainController.cs index 1e61dd2..7cdee5d 100644 --- a/FunGame.Desktop/Controller/MainController.cs +++ b/FunGame.Desktop/Controller/MainController.cs @@ -23,7 +23,7 @@ namespace Milimoe.FunGame.Desktop.Controller /** * 从内部去调用Model的方法,并记录日志。 */ - private T Do(string DoType) + private T Do(string DoType, params object[] args) { object result = new(); switch(DoType) @@ -80,9 +80,16 @@ namespace Milimoe.FunGame.Desktop.Controller result = MainModel.Close(); break; - case MainSet.JoinRoom: + case MainSet.IntoRoom: + Main.OnBeforeIntoRoomEvent(new GeneralEventArgs()); result = MainModel.IntoRoom(); break; + + case MainSet.Chat: + Main.OnBeforeSendTalkEvent(new GeneralEventArgs()); + if (args != null && args.Length > 0) + result = MainModel.Chat((string)args[0]); + break; default: break; @@ -140,11 +147,6 @@ namespace Milimoe.FunGame.Desktop.Controller throw new NotImplementedException(); } - public void SetUser() - { - throw new NotImplementedException(); - } - public bool LogOut() { return Do(MainSet.LogOut); @@ -157,7 +159,12 @@ namespace Milimoe.FunGame.Desktop.Controller public bool IntoRoom() { - return Do(MainSet.JoinRoom); + return Do(MainSet.IntoRoom); + } + + public bool Chat(string msg) + { + return Do(MainSet.Chat, msg); } } } diff --git a/FunGame.Desktop/Library/Config/Constant.cs b/FunGame.Desktop/Library/Config/Constant.cs index 4d2f9c7..3e46a34 100644 --- a/FunGame.Desktop/Library/Config/Constant.cs +++ b/FunGame.Desktop/Library/Config/Constant.cs @@ -20,7 +20,8 @@ namespace Milimoe.FunGame.Desktop.Library public const string Connect = ".connect"; public const string GetServerConnection = ".getserverconnection"; public const string Close = ".close"; - public const string JoinRoom = ".joinroom"; + public const string IntoRoom = ".intoroom"; + public const string Chat = ".chat"; } /// diff --git a/FunGame.Desktop/Library/Interface/IMain.cs b/FunGame.Desktop/Library/Interface/IMain.cs index 21e90a1..758e47b 100644 --- a/FunGame.Desktop/Library/Interface/IMain.cs +++ b/FunGame.Desktop/Library/Interface/IMain.cs @@ -19,5 +19,6 @@ namespace Milimoe.FunGame.Desktop.Library.Interface public bool LogOut(); public bool Close(); public bool IntoRoom(); + public bool Chat(string msg); } } diff --git a/FunGame.Desktop/Model/MainModel.cs b/FunGame.Desktop/Model/MainModel.cs index 5242d13..90cffeb 100644 --- a/FunGame.Desktop/Model/MainModel.cs +++ b/FunGame.Desktop/Model/MainModel.cs @@ -258,6 +258,23 @@ namespace Milimoe.FunGame.Desktop.Model return false; } } + + public bool Chat(string msg) + { + try + { + if (Socket?.Send(SocketMessageType.Chat, msg) == SocketResult.Success) + return true; + else throw new CanNotSendTalkException(); + } + catch (Exception e) + { + Main.GetMessage(e.GetErrorInfo()); + Main.OnFailedSendTalkEvent(new GeneralEventArgs()); + Main.OnAfterSendTalkEvent(new GeneralEventArgs()); + return false; + } + } #endregion @@ -331,6 +348,10 @@ namespace Milimoe.FunGame.Desktop.Model case SocketMessageType.IntoRoom: SocketHandler_IntoRoom(objs); break; + + case SocketMessageType.Chat: + SocketHandler_Chat(objs); + break; case SocketMessageType.Unknown: default: @@ -455,9 +476,9 @@ namespace Milimoe.FunGame.Desktop.Model { string roomid = ""; if (objs.Length > 0) roomid = NetworkUtility.ConvertJsonObject(objs[0])!; - if (roomid == "-1") + if (roomid.Trim() != "" && roomid == "-1") { - Main.GetMessage($"已连接到公共聊天服务器。"); + Main.GetMessage($"已连接至公共聊天室。"); } else { @@ -466,6 +487,24 @@ namespace Milimoe.FunGame.Desktop.Model Main.OnSucceedIntoRoomEvent(new GeneralEventArgs()); Main.OnAfterIntoRoomEvent(new GeneralEventArgs()); } + + private void SocketHandler_Chat(object[] objs) + { + if (objs != null && objs.Length > 1) + { + string user = NetworkUtility.ConvertJsonObject(objs[0])!; + string msg = NetworkUtility.ConvertJsonObject(objs[1])!; + if (user != Usercfg.LoginUserName) + { + Main.GetMessage(msg, TimeType.None); + } + Main.OnSucceedSendTalkEvent(new GeneralEventArgs()); + Main.OnAfterSendTalkEvent(new GeneralEventArgs()); + return; + } + Main.OnFailedSendTalkEvent(new GeneralEventArgs()); + Main.OnAfterSendTalkEvent(new GeneralEventArgs()); + } #endregion } diff --git a/FunGame.Desktop/UI/Main/Main.cs b/FunGame.Desktop/UI/Main/Main.cs index d00c54c..20ea1e0 100644 --- a/FunGame.Desktop/UI/Main/Main.cs +++ b/FunGame.Desktop/UI/Main/Main.cs @@ -682,10 +682,23 @@ namespace Milimoe.FunGame.Desktop.UI private void SendTalkText_Click(bool isLeave) { // 向消息队列发送消息 - if (!TalkText.Text.Trim().Equals("") && !TalkText.ForeColor.Equals(Color.DarkGray)) + string text = TalkText.Text; + if (!text.Trim().Equals("") && !TalkText.ForeColor.Equals(Color.DarkGray)) { - WritelnGameInfo((!Usercfg.LoginUserName.Equals("") ? DateTimeUtility.GetNowShortTime() + " [ " + Usercfg.LoginUserName + " ] 说: ": ":> ") + TalkText.Text); - SwitchTalkMessage(TalkText.Text); + string msg = ""; + if (Usercfg.LoginUserName.Equals("")) + { + msg = ":> " + text; + } + else + { + msg = DateTimeUtility.GetNowShortTime() + " [ " + Usercfg.LoginUserName + " ] 说: " + text; + } + WritelnGameInfo(msg); + if (!SwitchTalkMessage(text)) + { + MainController?.Chat(msg); + } TalkText.Text = ""; if (isLeave) TalkText_Leave(); // 回车不离开焦点 } @@ -1243,7 +1256,7 @@ namespace Milimoe.FunGame.Desktop.UI /// 判断快捷消息 /// /// - private void SwitchTalkMessage(string s) + private bool SwitchTalkMessage(string s) { switch (s) { @@ -1326,7 +1339,7 @@ namespace Milimoe.FunGame.Desktop.UI break; case Constant.FunGame_ConnectTo: string msg = ShowMessage.InputMessage("请输入服务器IP地址和端口号,如: 127.0.0.1:22222。", "连接指定服务器"); - if (msg.Equals("")) return; + if (msg.Equals("")) return true; string[] addr = msg.Split(':'); string ip; int port; @@ -1343,7 +1356,7 @@ namespace Milimoe.FunGame.Desktop.UI else { ShowMessage.ErrorMessage("格式错误!\n这不是一个服务器地址。"); - return; + return true; } ErrorType ErrorType = NetworkUtility.IsServerAddress(ip, port); if (ErrorType == Core.Library.Constant.ErrorType.None) @@ -1361,6 +1374,7 @@ namespace Milimoe.FunGame.Desktop.UI default: break; } + return false; } #endregion