更新了公屏聊天,和一些杂项

This commit is contained in:
Mili 2023-02-28 23:04:25 +08:00
parent ebfd51b761
commit 7ece72bcc8
13 changed files with 173 additions and 50 deletions

View File

@ -10,6 +10,7 @@ namespace Milimoe.FunGame.Core.Api.Data
/// </summary> /// </summary>
public abstract class SQLHelper : ISQLHelper public abstract class SQLHelper : ISQLHelper
{ {
public abstract FunGameInfo.FunGame FunGameType { get; }
public abstract string Script { get; set; } public abstract string Script { get; set; }
public abstract CommandType CommandType { get; set; } public abstract CommandType CommandType { get; set; }
public abstract SQLResult Result { get; } public abstract SQLResult Result { get; }

View File

@ -6,11 +6,16 @@ namespace Milimoe.FunGame.Core.Interface.Base
{ {
public interface ISQLHelper public interface ISQLHelper
{ {
public FunGameInfo.FunGame FunGameType { get; }
public string Script { get; set; } public string Script { get; set; }
public CommandType CommandType { get; set; } public CommandType CommandType { get; set; }
public SQLResult Result { get; } public SQLResult Result { get; }
public SQLServerInfo ServerInfo { get; } public SQLServerInfo ServerInfo { get; }
public int UpdateRows { get; } public int UpdateRows { get; }
public DataSet DataSet { get; } public DataSet DataSet { get; }
public int Execute(out SQLResult Result);
public DataSet ExecuteDataSet(out SQLResult Result);
public void Close();
} }
} }

View File

@ -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 namespace Milimoe.FunGame.Core.Interface.Base
{ {
@ -11,13 +12,7 @@ namespace Milimoe.FunGame.Core.Interface.Base
public int ServerPort { get; } public int ServerPort { get; }
public string ServerName { get; } public string ServerName { get; }
public string ServerNotice { get; } public string ServerNotice { get; }
public bool Connected public bool Connected => Instance != null && Instance.Connected;
{
get
{
return Instance != null && Instance.Connected;
}
}
public bool Receiving { get; } public bool Receiving { get; }
public SocketResult Send(SocketMessageType type, params object[] objs); public SocketResult Send(SocketMessageType type, params object[] objs);
public object[] Receive(); public object[] Receive();

View File

@ -16,8 +16,10 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
public string ServerNotice { get; } = ""; public string ServerNotice { get; } = "";
public bool Connected => Instance != null && Instance.Connected; public bool Connected => Instance != null && Instance.Connected;
public bool Receiving => _Receiving; public bool Receiving => _Receiving;
public List<BaseModel> GetUsersList => UserThreads.GetList();
public int UsersCount => UserThreads.Count;
private readonly ThreadManager PlayerThreads; private readonly ThreadManager UserThreads;
private bool _Receiving = false; private bool _Receiving = false;
private ServerSocket(System.Net.Sockets.Socket Instance, int ServerPort, int MaxConnection = 0) 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.Instance = Instance;
this.ServerPort = ServerPort; this.ServerPort = ServerPort;
if (MaxConnection <= 0) if (MaxConnection <= 0)
PlayerThreads = new ThreadManager(); UserThreads = new ThreadManager();
else else
PlayerThreads = new ThreadManager(MaxConnection); UserThreads = new ThreadManager(MaxConnection);
} }
public static ServerSocket StartListening(int Port = 22222, int MaxConnection = 0) public static ServerSocket StartListening(int Port = 22222, int MaxConnection = 0)
@ -50,14 +52,24 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
throw new SocketGetClientException(); 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) public SocketResult Send(SocketMessageType type, params object[] objs)

View File

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

View File

@ -114,4 +114,9 @@
{ {
public override string Message => "无法加入指定房间 (#10023)"; public override string Message => "无法加入指定房间 (#10023)";
} }
public class CanNotSendTalkException : Exception
{
public override string Message => "无法发送公共信息 (#10024)";
}
} }

View File

@ -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.Library.Constant;
using Milimoe.FunGame.Core.Service;
namespace Milimoe.FunGame.Core.Library.Server namespace Milimoe.FunGame.Core.Library.Server
{ {
public abstract class BaseModel public abstract class BaseModel
{ {
public bool Running = false; public abstract bool Running { get; }
public ClientSocket? Socket = null; public abstract ClientSocket? Socket { get; }
public Task? Task = null; public abstract Task? Task { get; }
public string ClientName = ""; public abstract string ClientName { get; }
public abstract User? User { get; }
public List<BaseModel> GetClientsList => ClientThreads.GetList();
public int ClientsCount => ClientThreads.Count;
public abstract bool Read(ClientSocket socket); public abstract bool Read(ClientSocket socket);
public abstract bool Send(ClientSocket socket, SocketMessageType type, params object[] objs); public abstract bool Send(ClientSocket socket, SocketMessageType type, params object[] objs);
public abstract void Start(); 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];
}
} }
} }

View File

@ -5,6 +5,11 @@ namespace Milimoe.FunGame.Core.Service
{ {
internal class ThreadManager internal class ThreadManager
{ {
/// <summary>
/// 目前的线程数量
/// </summary>
internal int Count => Threads.Count;
/// <summary> /// <summary>
/// 最大接受的线程数量 /// 最大接受的线程数量
/// </summary> /// </summary>
@ -80,6 +85,11 @@ namespace Milimoe.FunGame.Core.Service
return result; return result;
} }
internal bool ContainsKey(string name)
{
return Threads.ContainsKey(name);
}
/// <summary> /// <summary>
/// 清空线程管理器 /// 清空线程管理器
/// </summary> /// </summary>
@ -88,5 +98,13 @@ namespace Milimoe.FunGame.Core.Service
Threads.Clear(); Threads.Clear();
} }
/// <summary>
/// 获取线程对象的列表
/// </summary>
internal List<BaseModel> GetList()
{
return Threads.Values.ToList();
}
} }
} }

View File

@ -23,7 +23,7 @@ namespace Milimoe.FunGame.Desktop.Controller
/** /**
* Model的方法 * Model的方法
*/ */
private T Do<T>(string DoType) private T Do<T>(string DoType, params object[] args)
{ {
object result = new(); object result = new();
switch(DoType) switch(DoType)
@ -80,10 +80,17 @@ namespace Milimoe.FunGame.Desktop.Controller
result = MainModel.Close(); result = MainModel.Close();
break; break;
case MainSet.JoinRoom: case MainSet.IntoRoom:
Main.OnBeforeIntoRoomEvent(new GeneralEventArgs());
result = MainModel.IntoRoom(); result = MainModel.IntoRoom();
break; break;
case MainSet.Chat:
Main.OnBeforeSendTalkEvent(new GeneralEventArgs());
if (args != null && args.Length > 0)
result = MainModel.Chat((string)args[0]);
break;
default: default:
break; break;
} }
@ -140,11 +147,6 @@ namespace Milimoe.FunGame.Desktop.Controller
throw new NotImplementedException(); throw new NotImplementedException();
} }
public void SetUser()
{
throw new NotImplementedException();
}
public bool LogOut() public bool LogOut()
{ {
return Do<bool>(MainSet.LogOut); return Do<bool>(MainSet.LogOut);
@ -157,7 +159,12 @@ namespace Milimoe.FunGame.Desktop.Controller
public bool IntoRoom() public bool IntoRoom()
{ {
return Do<bool>(MainSet.JoinRoom); return Do<bool>(MainSet.IntoRoom);
}
public bool Chat(string msg)
{
return Do<bool>(MainSet.Chat, msg);
} }
} }
} }

View File

@ -20,7 +20,8 @@ namespace Milimoe.FunGame.Desktop.Library
public const string Connect = ".connect"; public const string Connect = ".connect";
public const string GetServerConnection = ".getserverconnection"; public const string GetServerConnection = ".getserverconnection";
public const string Close = ".close"; public const string Close = ".close";
public const string JoinRoom = ".joinroom"; public const string IntoRoom = ".intoroom";
public const string Chat = ".chat";
} }
/// <summary> /// <summary>

View File

@ -19,5 +19,6 @@ namespace Milimoe.FunGame.Desktop.Library.Interface
public bool LogOut(); public bool LogOut();
public bool Close(); public bool Close();
public bool IntoRoom(); public bool IntoRoom();
public bool Chat(string msg);
} }
} }

View File

@ -259,6 +259,23 @@ namespace Milimoe.FunGame.Desktop.Model
} }
} }
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 #endregion
#region #region
@ -332,6 +349,10 @@ namespace Milimoe.FunGame.Desktop.Model
SocketHandler_IntoRoom(objs); SocketHandler_IntoRoom(objs);
break; break;
case SocketMessageType.Chat:
SocketHandler_Chat(objs);
break;
case SocketMessageType.Unknown: case SocketMessageType.Unknown:
default: default:
break; break;
@ -455,9 +476,9 @@ namespace Milimoe.FunGame.Desktop.Model
{ {
string roomid = ""; string roomid = "";
if (objs.Length > 0) roomid = NetworkUtility.ConvertJsonObject<string>(objs[0])!; if (objs.Length > 0) roomid = NetworkUtility.ConvertJsonObject<string>(objs[0])!;
if (roomid == "-1") if (roomid.Trim() != "" && roomid == "-1")
{ {
Main.GetMessage($"已连接到公共聊天服务器。"); Main.GetMessage($"已连接至公共聊天室。");
} }
else else
{ {
@ -467,6 +488,24 @@ namespace Milimoe.FunGame.Desktop.Model
Main.OnAfterIntoRoomEvent(new GeneralEventArgs()); Main.OnAfterIntoRoomEvent(new GeneralEventArgs());
} }
private void SocketHandler_Chat(object[] objs)
{
if (objs != null && objs.Length > 1)
{
string user = NetworkUtility.ConvertJsonObject<string>(objs[0])!;
string msg = NetworkUtility.ConvertJsonObject<string>(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 #endregion
} }
} }

View File

@ -682,10 +682,23 @@ namespace Milimoe.FunGame.Desktop.UI
private void SendTalkText_Click(bool isLeave) 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); string msg = "";
SwitchTalkMessage(TalkText.Text); if (Usercfg.LoginUserName.Equals(""))
{
msg = ":> " + text;
}
else
{
msg = DateTimeUtility.GetNowShortTime() + " [ " + Usercfg.LoginUserName + " ] 说: " + text;
}
WritelnGameInfo(msg);
if (!SwitchTalkMessage(text))
{
MainController?.Chat(msg);
}
TalkText.Text = ""; TalkText.Text = "";
if (isLeave) TalkText_Leave(); // 回车不离开焦点 if (isLeave) TalkText_Leave(); // 回车不离开焦点
} }
@ -1243,7 +1256,7 @@ namespace Milimoe.FunGame.Desktop.UI
/// 判断快捷消息 /// 判断快捷消息
/// </summary> /// </summary>
/// <param name="s"></param> /// <param name="s"></param>
private void SwitchTalkMessage(string s) private bool SwitchTalkMessage(string s)
{ {
switch (s) switch (s)
{ {
@ -1326,7 +1339,7 @@ namespace Milimoe.FunGame.Desktop.UI
break; break;
case Constant.FunGame_ConnectTo: case Constant.FunGame_ConnectTo:
string msg = ShowMessage.InputMessage("请输入服务器IP地址和端口号如: 127.0.0.1:22222。", "连接指定服务器"); string msg = ShowMessage.InputMessage("请输入服务器IP地址和端口号如: 127.0.0.1:22222。", "连接指定服务器");
if (msg.Equals("")) return; if (msg.Equals("")) return true;
string[] addr = msg.Split(':'); string[] addr = msg.Split(':');
string ip; string ip;
int port; int port;
@ -1343,7 +1356,7 @@ namespace Milimoe.FunGame.Desktop.UI
else else
{ {
ShowMessage.ErrorMessage("格式错误!\n这不是一个服务器地址。"); ShowMessage.ErrorMessage("格式错误!\n这不是一个服务器地址。");
return; return true;
} }
ErrorType ErrorType = NetworkUtility.IsServerAddress(ip, port); ErrorType ErrorType = NetworkUtility.IsServerAddress(ip, port);
if (ErrorType == Core.Library.Constant.ErrorType.None) if (ErrorType == Core.Library.Constant.ErrorType.None)
@ -1361,6 +1374,7 @@ namespace Milimoe.FunGame.Desktop.UI
default: default:
break; break;
} }
return false;
} }
#endregion #endregion