mirror of
https://github.com/project-redbud/FunGame-Core.git
synced 2025-12-05 08:09:02 +00:00
更新了公屏聊天,和一些杂项
This commit is contained in:
parent
ebfd51b761
commit
7ece72bcc8
@ -10,6 +10,7 @@ namespace Milimoe.FunGame.Core.Api.Data
|
||||
/// </summary>
|
||||
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; }
|
||||
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@ -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();
|
||||
|
||||
@ -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<BaseModel> 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)
|
||||
|
||||
@ -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
|
||||
|
||||
@ -114,4 +114,9 @@
|
||||
{
|
||||
public override string Message => "无法加入指定房间 (#10023)";
|
||||
}
|
||||
|
||||
public class CanNotSendTalkException : Exception
|
||||
{
|
||||
public override string Message => "无法发送公共信息 (#10024)";
|
||||
}
|
||||
}
|
||||
|
||||
@ -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<BaseModel> 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];
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,6 +5,11 @@ namespace Milimoe.FunGame.Core.Service
|
||||
{
|
||||
internal class ThreadManager
|
||||
{
|
||||
/// <summary>
|
||||
/// 目前的线程数量
|
||||
/// </summary>
|
||||
internal int Count => Threads.Count;
|
||||
|
||||
/// <summary>
|
||||
/// 最大接受的线程数量
|
||||
/// </summary>
|
||||
@ -80,6 +85,11 @@ namespace Milimoe.FunGame.Core.Service
|
||||
return result;
|
||||
}
|
||||
|
||||
internal bool ContainsKey(string name)
|
||||
{
|
||||
return Threads.ContainsKey(name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清空线程管理器
|
||||
/// </summary>
|
||||
@ -88,5 +98,13 @@ namespace Milimoe.FunGame.Core.Service
|
||||
Threads.Clear();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取线程对象的列表
|
||||
/// </summary>
|
||||
internal List<BaseModel> GetList()
|
||||
{
|
||||
return Threads.Values.ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -23,7 +23,7 @@ namespace Milimoe.FunGame.Desktop.Controller
|
||||
/**
|
||||
* 从内部去调用Model的方法,并记录日志。
|
||||
*/
|
||||
private T Do<T>(string DoType)
|
||||
private T Do<T>(string DoType, params object[] args)
|
||||
{
|
||||
object result = new();
|
||||
switch(DoType)
|
||||
@ -80,10 +80,17 @@ 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<bool>(MainSet.LogOut);
|
||||
@ -157,7 +159,12 @@ namespace Milimoe.FunGame.Desktop.Controller
|
||||
|
||||
public bool IntoRoom()
|
||||
{
|
||||
return Do<bool>(MainSet.JoinRoom);
|
||||
return Do<bool>(MainSet.IntoRoom);
|
||||
}
|
||||
|
||||
public bool Chat(string msg)
|
||||
{
|
||||
return Do<bool>(MainSet.Chat, msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@ -19,5 +19,6 @@ namespace Milimoe.FunGame.Desktop.Library.Interface
|
||||
public bool LogOut();
|
||||
public bool Close();
|
||||
public bool IntoRoom();
|
||||
public bool Chat(string msg);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
#region 私有方法
|
||||
@ -332,6 +349,10 @@ namespace Milimoe.FunGame.Desktop.Model
|
||||
SocketHandler_IntoRoom(objs);
|
||||
break;
|
||||
|
||||
case SocketMessageType.Chat:
|
||||
SocketHandler_Chat(objs);
|
||||
break;
|
||||
|
||||
case SocketMessageType.Unknown:
|
||||
default:
|
||||
break;
|
||||
@ -455,9 +476,9 @@ namespace Milimoe.FunGame.Desktop.Model
|
||||
{
|
||||
string roomid = "";
|
||||
if (objs.Length > 0) roomid = NetworkUtility.ConvertJsonObject<string>(objs[0])!;
|
||||
if (roomid == "-1")
|
||||
if (roomid.Trim() != "" && roomid == "-1")
|
||||
{
|
||||
Main.GetMessage($"已连接到公共聊天服务器。");
|
||||
Main.GetMessage($"已连接至公共聊天室。");
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -467,6 +488,24 @@ namespace Milimoe.FunGame.Desktop.Model
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
@ -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
|
||||
/// 判断快捷消息
|
||||
/// </summary>
|
||||
/// <param name="s"></param>
|
||||
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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user