为GameModeServer提供了监听接口;优化了控制台日志的输出方式 (#31)

* 为GameModeServer提供了监听接口;优化了控制台日志的输出方式

* 彩色控制台

---------

Co-authored-by: yeziuku <yezi@wrss.org>
This commit is contained in:
milimoe 2023-12-09 15:41:19 +08:00 committed by GitHub
parent a316c853eb
commit 9c1bf43fdf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 161 additions and 76 deletions

View File

@ -3,7 +3,7 @@ using Milimoe.FunGame.Core.Api.Utility;
using Milimoe.FunGame.Core.Library.Constant;
using Milimoe.FunGame.Server.Model;
namespace Milimoe.FunGame.Server.Controllers
namespace Milimoe.FunGame.Server.Controller
{
public class Authenticator : Core.Library.Common.Architecture.Authenticator
{

View File

@ -3,11 +3,9 @@ using System.Data;
using Milimoe.FunGame.Core.Api.Transmittal;
using Milimoe.FunGame.Core.Api.Utility;
using Milimoe.FunGame.Core.Entity;
using Milimoe.FunGame.Core.Library.Common.Network;
using Milimoe.FunGame.Core.Library.Constant;
using Milimoe.FunGame.Core.Library.SQLScript.Common;
using Milimoe.FunGame.Core.Library.SQLScript.Entity;
using Milimoe.FunGame.Server.Controllers;
using Milimoe.FunGame.Server.Model;
using Milimoe.FunGame.Server.Others;
using Milimoe.FunGame.Server.Utility;
@ -25,7 +23,7 @@ namespace Milimoe.FunGame.Server.Controller
private string ForgetVerify = "";
private string RegVerify = "";
private DataRequestType _LastRequest = DataRequestType.UnKnown;
private bool[] isReadyCheckCD = new bool[] { false, false };
private readonly bool[] isReadyCheckCD = [false, false];
public DataRequestController(ServerModel server)
{
@ -35,7 +33,7 @@ namespace Milimoe.FunGame.Server.Controller
public Hashtable GetResultData(DataRequestType type, Hashtable data)
{
Hashtable result = new();
Hashtable result = [];
_LastRequest = type;
switch (type)
@ -133,7 +131,7 @@ namespace Milimoe.FunGame.Server.Controller
Guid key = Guid.Empty;
if (RequestData.Count >= 1)
{
ServerHelper.WriteLine("[" + ServerSocket.GetTypeString(SocketMessageType.DataRequest) + "] " + Server.GetClientName() + " -> LogOut");
ServerHelper.WriteLine(Server.GetClientName() + " -> " + DataRequestSet.GetTypeString(_LastRequest), InvokeMessageType.DataRequest);
key = DataRequest.GetHashtableJsonObject<Guid>(RequestData, "key");
if (Server.IsLoginKey(key))
{
@ -155,8 +153,7 @@ namespace Milimoe.FunGame.Server.Controller
/// <param name="ResultData"></param>
private void GetServerNotice(Hashtable ResultData)
{
ServerHelper.WriteLine("[" + ServerSocket.GetTypeString(SocketMessageType.DataRequest) + "] " + Server.GetClientName() + " -> GetNotice");
_LastRequest = DataRequestType.Main_GetNotice;
ServerHelper.WriteLine(Server.GetClientName() + " -> " + DataRequestSet.GetTypeString(_LastRequest), InvokeMessageType.DataRequest);
ResultData.Add("notice", Config.ServerNotice);
}
@ -174,7 +171,7 @@ namespace Milimoe.FunGame.Server.Controller
string gamemode = DataRequest.GetHashtableJsonObject<string>(RequestData, "gamemode") ?? "";
string gamemap = DataRequest.GetHashtableJsonObject<string>(RequestData, "gamemap") ?? "";
bool isrank = DataRequest.GetHashtableJsonObject<bool>(RequestData, "isrank");
ServerHelper.WriteLine("[" + ServerSocket.GetTypeString(SocketMessageType.DataRequest) + "] " + Server.GetClientName() + " -> CreateRoom: " + roomtype_string + " (" + string.Join(", ", [gamemode, gamemap]) + ")");
ServerHelper.WriteLine(Server.GetClientName() + " -> " + DataRequestSet.GetTypeString(_LastRequest) + " : " + roomtype_string + " (" + string.Join(", ", [gamemode, gamemap]) + ")", InvokeMessageType.DataRequest);
if (gamemode == "" || gamemap == "")
{
ServerHelper.WriteLine("缺少对应的模组或地图,无法创建房间。");
@ -210,7 +207,7 @@ namespace Milimoe.FunGame.Server.Controller
/// <param name="ResultData"></param>
private void UpdateRoom(Hashtable ResultData)
{
ServerHelper.WriteLine("[" + ServerSocket.GetTypeString(SocketMessageType.DataRequest) + "] " + Server.GetClientName() + " -> UpdateRoom");
ServerHelper.WriteLine(Server.GetClientName() + " -> " + DataRequestSet.GetTypeString(_LastRequest), InvokeMessageType.DataRequest);
ResultData.Add("rooms", Config.RoomList.ListRoom); // 传RoomList
}
@ -224,7 +221,7 @@ namespace Milimoe.FunGame.Server.Controller
bool result = false;
if (RequestData.Count >= 2)
{
ServerHelper.WriteLine("[" + ServerSocket.GetTypeString(SocketMessageType.DataRequest) + "] " + Server.GetClientName() + " -> QuitRoom");
ServerHelper.WriteLine(Server.GetClientName() + " -> " + DataRequestSet.GetTypeString(_LastRequest), InvokeMessageType.DataRequest);
string roomid = DataRequest.GetHashtableJsonObject<string>(RequestData, "roomid") ?? "-1";
bool isMaster = DataRequest.GetHashtableJsonObject<bool>(RequestData, "isMaster");
@ -246,7 +243,7 @@ namespace Milimoe.FunGame.Server.Controller
bool result = false;
if (RequestData.Count >= 1)
{
ServerHelper.WriteLine("[" + ServerSocket.GetTypeString(SocketMessageType.DataRequest) + "] " + Server.GetClientName() + " -> IntoRoom");
ServerHelper.WriteLine(Server.GetClientName() + " -> " + DataRequestSet.GetTypeString(_LastRequest), InvokeMessageType.DataRequest);
string roomid = DataRequest.GetHashtableJsonObject<string>(RequestData, "roomid") ?? "-1";
if (roomid != "-1")
@ -280,7 +277,7 @@ namespace Milimoe.FunGame.Server.Controller
bool iscancel = DataRequest.GetHashtableJsonObject<bool>(RequestData, "iscancel");
if (!iscancel)
{
ServerHelper.WriteLine("[" + ServerSocket.GetTypeString(SocketMessageType.DataRequest) + "] " + Server.GetClientName() + " -> MatchRoom : Start");
ServerHelper.WriteLine(Server.GetClientName() + " -> " + DataRequestSet.GetTypeString(_LastRequest) + " : Start", InvokeMessageType.DataRequest);
string roomtype_string = DataRequest.GetHashtableJsonObject<string>(RequestData, "roomtype") ?? RoomSet.All;
User user = DataRequest.GetHashtableJsonObject<User>(RequestData, "matcher") ?? Factory.GetUser();
Server.StartMatching(roomtype_string, user);
@ -288,7 +285,7 @@ namespace Milimoe.FunGame.Server.Controller
else
{
// 取消匹配
ServerHelper.WriteLine("[" + ServerSocket.GetTypeString(SocketMessageType.DataRequest) + "] " + Server.GetClientName() + " -> MatchRoom : Cancel");
ServerHelper.WriteLine(Server.GetClientName() + " -> " + DataRequestSet.GetTypeString(_LastRequest) + " : Cancel", InvokeMessageType.DataRequest);
Server.StopMatching();
}
}
@ -306,7 +303,7 @@ namespace Milimoe.FunGame.Server.Controller
string roomid = "-1";
if (RequestData.Count >= 1)
{
ServerHelper.WriteLine("[" + ServerSocket.GetTypeString(SocketMessageType.DataRequest) + "] " + Server.GetClientName() + " -> SetReady");
ServerHelper.WriteLine(Server.GetClientName() + " -> " + DataRequestSet.GetTypeString(_LastRequest), InvokeMessageType.DataRequest);
roomid = DataRequest.GetHashtableJsonObject<string>(RequestData, "roomid") ?? "-1";
User user = Server.User;
@ -332,7 +329,7 @@ namespace Milimoe.FunGame.Server.Controller
string roomid = "-1";
if (RequestData.Count >= 1)
{
ServerHelper.WriteLine("[" + ServerSocket.GetTypeString(SocketMessageType.DataRequest) + "] " + Server.GetClientName() + " -> CancelReady");
ServerHelper.WriteLine(Server.GetClientName() + " -> " + DataRequestSet.GetTypeString(_LastRequest), InvokeMessageType.DataRequest);
roomid = DataRequest.GetHashtableJsonObject<string>(RequestData, "roomid") ?? "-1";
User user = Server.User;
@ -370,7 +367,7 @@ namespace Milimoe.FunGame.Server.Controller
bool result = false;
if (RequestData.Count >= 2)
{
ServerHelper.WriteLine("[" + ServerSocket.GetTypeString(SocketMessageType.DataRequest) + "] " + Server.GetClientName() + " -> StartGame");
ServerHelper.WriteLine(Server.GetClientName() + " -> " + DataRequestSet.GetTypeString(_LastRequest), InvokeMessageType.DataRequest);
string roomid = DataRequest.GetHashtableJsonObject<string>(RequestData, "roomid") ?? "-1";
bool isMaster = DataRequest.GetHashtableJsonObject<bool>(RequestData, "isMaster");
@ -449,7 +446,7 @@ namespace Milimoe.FunGame.Server.Controller
bool success = false;
if (RequestData.Count >= 4)
{
ServerHelper.WriteLine("[" + ServerSocket.GetTypeString(SocketMessageType.DataRequest) + "] " + Server.GetClientName() + " -> Reg");
ServerHelper.WriteLine(Server.GetClientName() + " -> " + DataRequestSet.GetTypeString(_LastRequest), InvokeMessageType.DataRequest);
string username = DataRequest.GetHashtableJsonObject<string>(RequestData, "username") ?? "";
string password = DataRequest.GetHashtableJsonObject<string>(RequestData, "password") ?? "";
string email = DataRequest.GetHashtableJsonObject<string>(RequestData, "email") ?? "";
@ -584,7 +581,7 @@ namespace Milimoe.FunGame.Server.Controller
User user = Factory.GetUser();
if (RequestData.Count >= 4)
{
ServerHelper.WriteLine("[" + ServerSocket.GetTypeString(SocketMessageType.DataRequest) + "] " + Server.GetClientName() + " -> Login");
ServerHelper.WriteLine(Server.GetClientName() + " -> " + DataRequestSet.GetTypeString(_LastRequest), InvokeMessageType.DataRequest);
string username = DataRequest.GetHashtableJsonObject<string>(RequestData, "username") ?? "";
string password = DataRequest.GetHashtableJsonObject<string>(RequestData, "password") ?? "";
string autokey = DataRequest.GetHashtableJsonObject<string>(RequestData, "autokey") ?? "";
@ -649,7 +646,7 @@ namespace Milimoe.FunGame.Server.Controller
string msg = "无法找回您的密码,请稍后再试。"; // 返回的验证信息
if (RequestData.Count >= 3)
{
ServerHelper.WriteLine("[" + ServerSocket.GetTypeString(SocketMessageType.DataRequest) + "] " + Server.GetClientName() + " -> ForgetPassword");
ServerHelper.WriteLine(Server.GetClientName() + " -> " + DataRequestSet.GetTypeString(_LastRequest), InvokeMessageType.DataRequest);
string username = DataRequest.GetHashtableJsonObject<string>(RequestData, ForgetVerifyCodes.Column_Username) ?? "";
string email = DataRequest.GetHashtableJsonObject<string>(RequestData, ForgetVerifyCodes.Column_Email) ?? "";
string verifycode = DataRequest.GetHashtableJsonObject<string>(RequestData, ForgetVerifyCodes.Column_ForgetVerifyCode) ?? "";
@ -751,7 +748,7 @@ namespace Milimoe.FunGame.Server.Controller
string msg = "无法更新您的密码,请稍后再试。";
if (RequestData.Count >= 2)
{
ServerHelper.WriteLine("[" + ServerSocket.GetTypeString(SocketMessageType.DataRequest) + "] " + Server.GetClientName() + " -> UpdatePassword");
ServerHelper.WriteLine(Server.GetClientName() + " -> " + DataRequestSet.GetTypeString(_LastRequest), InvokeMessageType.DataRequest);
string username = DataRequest.GetHashtableJsonObject<string>(RequestData, UserQuery.Column_Username) ?? "";
string password = DataRequest.GetHashtableJsonObject<string>(RequestData, UserQuery.Column_Password) ?? "";
if (username.Trim() != "" && password.Trim() != "")
@ -781,7 +778,7 @@ namespace Milimoe.FunGame.Server.Controller
string roomid = "-1";
if (RequestData.Count >= 1)
{
ServerHelper.WriteLine("[" + ServerSocket.GetTypeString(SocketMessageType.DataRequest) + "] " + Server.GetClientName() + " -> GetRoomPlayerCount");
ServerHelper.WriteLine(Server.GetClientName() + " -> " + DataRequestSet.GetTypeString(_LastRequest), InvokeMessageType.DataRequest);
roomid = DataRequest.GetHashtableJsonObject<string>(RequestData, "roomid") ?? "-1";
}
ResultData.Add("count", Config.RoomList.GetPlayerCount(roomid));

View File

@ -113,18 +113,18 @@ void StartServer()
{
ClientModel.Start();
});
ClientModel.SetTaskAndClientName(t, clientip);
ClientModel.SetClientName(clientip);
}
else
{
ServerHelper.WriteLine(ServerHelper.MakeClientName(clientip) + " 连接失败。");
ServerHelper.WriteLine(ServerHelper.MakeClientName(clientip) + " 连接失败。", InvokeMessageType.Core);
}
Config.ConnectingPlayerCount--;
}
catch (Exception e)
{
if (--Config.ConnectingPlayerCount < 0) Config.ConnectingPlayerCount = 0;
ServerHelper.WriteLine(ServerHelper.MakeClientName(clientip) + " 中断连接!");
ServerHelper.WriteLine(ServerHelper.MakeClientName(clientip) + " 中断连接!", InvokeMessageType.Core);
ServerHelper.Error(e);
}
}
@ -156,15 +156,15 @@ bool GetGameModeList()
{
// 同时读取Implement预设的模组和gamemods目录下的模组最后合成一个总的列表
List<string> supported = [];
GameModeLoader loader = GameModeLoader.LoadGameModes();
string[] mods = (string[]?)Implement.GetFunGameImplValue(InterfaceType.IGameModeSupported, InterfaceMethod.GameModeList, false) ?? [];
Config.GameModeLoader = GameModeLoader.LoadGameModes(Config.FunGameType, [new Action<string>(msg => ServerHelper.WriteLine(msg, InvokeMessageType.GameMode))]);
string[] mods = (string[]?)Implement.GetFunGameImplValue(InterfaceType.IServer, InterfaceMethod.GameModeList, false) ?? [];
if (mods.Length > 0)
{
supported.AddRange(mods);
// 检查已安装的模组中是否在Implement预设的模组中存在
foreach (string mod in mods)
{
if (!loader.Modes.ContainsKey(mod))
if (!Config.GameModeLoader.ServerModes.ContainsKey(mod))
{
ServerHelper.WriteLine("[GameMode] Load Failed: " + mod + " 不存在或未正确安装");
supported.Remove(mod);
@ -172,17 +172,17 @@ bool GetGameModeList()
}
}
// 检查模组是否有相对应的地图
foreach (GameMode mode in loader.Modes.Values)
foreach (GameModeServer mode in Config.GameModeLoader.ServerModes.Values)
{
string modename = mode.Name;
if (loader.Maps.ContainsKey(mode.DefaultMap))
if (Config.GameModeLoader.Maps.ContainsKey(mode.DefaultMap))
{
supported.Add(modename);
}
else
{
ServerHelper.WriteLine("[GameMode] Load Failed: " + modename + " 没有找到相对应的地图,加载失败");
loader.Modes.Remove(modename);
Config.GameModeLoader.ServerModes.Remove(modename);
supported.Remove(modename);
}
}
@ -207,18 +207,18 @@ bool Connect(ClientSocket socket, Guid token, string clientip, ref bool isDebugM
if (Config.ConnectingPlayerCount + Config.OnlinePlayerCount > Config.MaxPlayers)
{
SendRefuseConnect(socket, "服务器可接受的连接数量已上限!");
ServerHelper.WriteLine("服务器可接受的连接数量已上限!");
ServerHelper.WriteLine("服务器可接受的连接数量已上限!", InvokeMessageType.Core);
return false;
}
ServerHelper.WriteLine(ServerHelper.MakeClientName(clientip) + " 正在连接服务器 . . .");
ServerHelper.WriteLine(ServerHelper.MakeClientName(clientip) + " 正在连接服务器 . . .", InvokeMessageType.Core);
if (IsIPBanned(ListeningSocket, clientip))
{
SendRefuseConnect(socket, "服务器已拒绝黑名单用户连接。");
ServerHelper.WriteLine("检测到 " + ServerHelper.MakeClientName(clientip) + " 为黑名单用户,已禁止其连接!");
ServerHelper.WriteLine("检测到 " + ServerHelper.MakeClientName(clientip) + " 为黑名单用户,已禁止其连接!", InvokeMessageType.Core);
return false;
}
ServerHelper.WriteLine("[" + ServerSocket.GetTypeString(read.SocketType) + "] " + ServerHelper.MakeClientName(socket.ClientIP));
ServerHelper.WriteLine("[" + ServerSocket.GetTypeString(read.SocketType) + "] " + ServerHelper.MakeClientName(socket.ClientIP), InvokeMessageType.Core);
// 读取参数
// 参数1客户端的游戏模组列表没有服务器的需要拒绝
@ -233,29 +233,30 @@ bool Connect(ClientSocket socket, Guid token, string clientip, ref bool isDebugM
if (strDontHave != "")
{
strDontHave = "客户端缺少服务器所需的模组:" + strDontHave;
ServerHelper.WriteLine(strDontHave);
ServerHelper.WriteLine(strDontHave, InvokeMessageType.Core);
msg += strDontHave;
}
if (msg == "" && socket.Send(SocketMessageType.Connect, true, msg, token, Config.ServerName, Config.ServerNotice) == SocketResult.Success)
{
ServerHelper.WriteLine(ServerHelper.MakeClientName(socket.ClientIP) + " <- " + "已确认连接");
ServerHelper.WriteLine(ServerHelper.MakeClientName(socket.ClientIP) + " <- " + "已确认连接", InvokeMessageType.Core);
return true;
}
else if (msg != "" && socket.Send(SocketMessageType.Connect, false, msg) == SocketResult.Success)
{
ServerHelper.WriteLine(ServerHelper.MakeClientName(socket.ClientIP) + " <- " + "拒绝连接");
ServerHelper.WriteLine(ServerHelper.MakeClientName(socket.ClientIP) + " <- " + "拒绝连接", InvokeMessageType.Core);
return false;
}
else
{
ServerHelper.WriteLine("无法传输数据,与客户端的连接可能丢失。");
ServerHelper.WriteLine("无法传输数据,与客户端的连接可能丢失。", InvokeMessageType.Core);
return false;
}
}
}
ServerHelper.WriteLine("客户端发送了不符合FunGame规定的字符拒绝连接。");
SendRefuseConnect(socket, "服务器已拒绝连接。");
ServerHelper.WriteLine("客户端发送了不符合FunGame规定的字符拒绝连接。", InvokeMessageType.Core);
return false;
}
@ -265,12 +266,12 @@ bool SendRefuseConnect(ClientSocket socket, string msg)
msg = "连接被拒绝,如有疑问请联系服务器管理员:" + msg;
if (socket.Send(SocketMessageType.Connect, false, msg) == SocketResult.Success)
{
ServerHelper.WriteLine(ServerHelper.MakeClientName(socket.ClientIP) + " <- " + "已拒绝连接");
ServerHelper.WriteLine(ServerHelper.MakeClientName(socket.ClientIP) + " <- " + "已拒绝连接", InvokeMessageType.Core);
return true;
}
else
{
ServerHelper.WriteLine("无法传输数据,与客户端的连接可能丢失。");
ServerHelper.WriteLine("无法传输数据,与客户端的连接可能丢失。", InvokeMessageType.Core);
return false;
}
}

View File

@ -4,6 +4,7 @@ using Milimoe.FunGame.Core.Api.Transmittal;
using Milimoe.FunGame.Core.Api.Utility;
using Milimoe.FunGame.Core.Entity;
using Milimoe.FunGame.Core.Interface.Base;
using Milimoe.FunGame.Core.Library.Common.Addon;
using Milimoe.FunGame.Core.Library.Common.Network;
using Milimoe.FunGame.Core.Library.Constant;
using Milimoe.FunGame.Core.Library.SQLScript.Entity;
@ -20,7 +21,6 @@ namespace Milimoe.FunGame.Server.Model
*/
public bool Running => _Running;
public ClientSocket? Socket => _Socket;
public Task? Task => _Task;
public string ClientName => _ClientName;
public User User => _User;
public Room Room
@ -35,11 +35,12 @@ namespace Milimoe.FunGame.Server.Model
/**
* Private
*/
private GameModeServer? NowGamingServer = null;
private ClientSocket? _Socket = null;
private bool _Running = false;
private User _User = General.UnknownUserInstance;
private Room _Room = General.HallInstance;
private Task? _Task = null;
private string _ClientName = "";
private Guid CheckLoginKey = Guid.Empty;
@ -59,6 +60,7 @@ namespace Milimoe.FunGame.Server.Model
_Socket = socket;
_Running = running;
Token = socket.Token;
this.IsDebugMode = isDebugMode;
if (Config.SQLMode) SQLHelper = new(this);
MailSender = SmtpHelper.GetMailSender();
DataRequestController = new(this);
@ -69,6 +71,9 @@ namespace Milimoe.FunGame.Server.Model
// 接收客户端消息
try
{
// 禁止GameModeServer调用
if ((IServerModel)this is GameModeServer) throw new NotSupportedException("请勿在GameModeServer类中调用此方法");
SocketObject SocketObject = socket.Receive();
SocketMessageType type = SocketObject.SocketType;
Guid token = SocketObject.Token;
@ -82,11 +87,22 @@ namespace Milimoe.FunGame.Server.Model
return false;
}
if (type == SocketMessageType.EndGame)
{
NowGamingServer = null;
return true;
}
if (type == SocketMessageType.DataRequest)
{
return DataRequestHandler(socket, SocketObject);
}
if (type == SocketMessageType.Gaming)
{
return GamingMessageHandler(socket, SocketObject);
}
if (type == SocketMessageType.HeartBeat)
{
return HeartBeat(socket);
@ -95,7 +111,7 @@ namespace Milimoe.FunGame.Server.Model
switch (type)
{
case SocketMessageType.Disconnect:
ServerHelper.WriteLine("[" + ServerSocket.GetTypeString(SocketMessageType.DataRequest) + "] " + GetClientName() + " -> Disconnect");
ServerHelper.WriteLine("[" + ServerSocket.GetTypeString(SocketMessageType.DataRequest) + "] " + GetClientName() + " -> Disconnect", InvokeMessageType.Core);
msg = "你已成功断开与服务器的连接: " + Config.ServerName + "。 ";
break;
}
@ -113,7 +129,7 @@ namespace Milimoe.FunGame.Server.Model
{
if (SQLHelper != null)
{
Hashtable result = new();
Hashtable result = [];
DataRequestType type = DataRequestType.UnKnown;
if (SocketObject.Parameters.Length > 0)
@ -121,7 +137,7 @@ namespace Milimoe.FunGame.Server.Model
try
{
type = SocketObject.GetParam<DataRequestType>(0);
Hashtable data = SocketObject.GetParam<Hashtable>(1) ?? new();
Hashtable data = SocketObject.GetParam<Hashtable>(1) ?? [];
SQLHelper.NewTransaction();
result = DataRequestController.GetResultData(type, data);
@ -141,6 +157,35 @@ namespace Milimoe.FunGame.Server.Model
return false;
}
public bool GamingMessageHandler(ClientSocket socket, SocketObject SocketObject)
{
if (NowGamingServer != null)
{
Hashtable result = [];
GamingType type = GamingType.None;
if (SocketObject.Parameters.Length > 0)
{
try
{
type = SocketObject.GetParam<GamingType>(0);
Hashtable data = SocketObject.GetParam<Hashtable>(1) ?? [];
result = NowGamingServer.GamingMessageHandler(UserName, type, data);
}
catch (Exception e)
{
ServerHelper.Error(e);
return Send(socket, SocketMessageType.Gaming, type, result);
}
}
return Send(socket, SocketMessageType.Gaming, type, result);
}
return false;
}
public bool Send(ClientSocket socket, SocketMessageType type, params object[] objs)
{
// 发送消息给客户端
@ -162,7 +207,7 @@ namespace Milimoe.FunGame.Server.Model
}
object obj = objs[0];
if (obj.GetType() == typeof(string) && (string)obj != "")
ServerHelper.WriteLine("[" + ServerSocket.GetTypeString(type) + "] " + GetClientName() + " <- " + obj);
ServerHelper.WriteLine("[" + ServerSocket.GetTypeString(type) + "] " + GetClientName() + " <- " + obj, InvokeMessageType.Core);
return true;
}
throw new CanNotSendToClientException();
@ -177,13 +222,13 @@ namespace Milimoe.FunGame.Server.Model
public void Start()
{
if ((IServerModel)this is GameModeServer) throw new NotSupportedException("请勿在GameModeServer类中调用此方法"); // 禁止GameModeServer调用
Task StreamReader = Task.Factory.StartNew(CreateStreamReader);
Task PeriodicalQuerier = Task.Factory.StartNew(CreatePeriodicalQuerier);
}
public void SetTaskAndClientName(Task t, string ClientName)
public void SetClientName(string ClientName)
{
_Task = t;
_ClientName = ClientName;
// 添加客户端到列表中
Server.AddClient(_ClientName, this);
@ -335,24 +380,26 @@ namespace Milimoe.FunGame.Server.Model
room = Config.RoomList[roomid];
}
if (room.Roomid == "-1") return;
foreach (ServerModel serverTask in Server.UserList.Cast<ServerModel>().Where(model => usernames.Length > 0 && usernames.Contains(model.User.Username)))
// 启动服务器
TaskUtility.NewTask(() =>
{
if (serverTask != null && serverTask.Socket != null)
if (Config.GameModeLoader != null && Config.GameModeLoader.ServerModes.ContainsKey(room.GameMode))
{
serverTask.Send(serverTask.Socket, SocketMessageType.StartGame, room, users);
}
}
TaskUtility.RunTimer(() =>
{
foreach (ServerModel serverTask in Server.UserList.Cast<ServerModel>().Where(model => usernames.Length > 0 && usernames.Contains(model.User.Username)))
{
if (serverTask != null && serverTask.Socket != null)
NowGamingServer = Config.GameModeLoader.GetServerMode(room.GameMode);
Dictionary<string, IServerModel> others = Server.UserList.Cast<IServerModel>().Where(model => usernames.Contains(model.User.Username) && model.User.Username != UserName).ToDictionary(k => k.User.Username, v => v);
if (NowGamingServer.StartServer(room.GameMode, room, users, this, others, new Action<string>(msg => ServerHelper.WriteLine(msg))))
{
Config.RoomList.CancelReady(roomid, serverTask.User);
serverTask.Send(serverTask.Socket, SocketMessageType.EndGame, room, users);
foreach (ServerModel serverTask in Server.UserList.Cast<ServerModel>().Where(model => usernames.Contains(model.User.Username)))
{
if (serverTask != null && serverTask.Socket != null)
{
Config.RoomList.CancelReady(roomid, serverTask.User);
serverTask.Send(serverTask.Socket, SocketMessageType.StartGame, room, users);
}
}
}
}
}, 20 * 1000);
});
}
public void IntoRoom(string roomid)

View File

@ -2,31 +2,71 @@
using Milimoe.FunGame.Core.Api.Transmittal;
using Milimoe.FunGame.Core.Api.Utility;
using Milimoe.FunGame.Core.Entity;
using Milimoe.FunGame.Core.Library.Constant;
using Milimoe.FunGame.Server.Others;
namespace Milimoe.FunGame.Server.Utility
{
public class ServerHelper
{
public static string GetPrefix()
public static string GetPrefix(InvokeMessageType type)
{
string prefix = "";
switch (type)
{
case InvokeMessageType.Core:
Console.ForegroundColor = ConsoleColor.Yellow;
prefix = "[Core] ";
break;
case InvokeMessageType.Error:
Console.ForegroundColor = ConsoleColor.Red;
prefix = "[Error] ";
break;
case InvokeMessageType.Api:
Console.ForegroundColor = ConsoleColor.Yellow;
prefix = "[Api] ";
break;
case InvokeMessageType.Interface:
Console.ForegroundColor = ConsoleColor.Magenta;
prefix = "[Interface] ";
break;
case InvokeMessageType.DataRequest:
Console.ForegroundColor = ConsoleColor.Cyan;
prefix = "[DataRequest] ";
break;
case InvokeMessageType.Plugin:
Console.ForegroundColor = ConsoleColor.Green;
prefix = "[Plugin] ";
break;
case InvokeMessageType.GameMode:
Console.ForegroundColor = ConsoleColor.Blue;
prefix = "[GameMode] ";
break;
case InvokeMessageType.System:
case InvokeMessageType.None:
default:
Console.ForegroundColor = ConsoleColor.Gray;
prefix = "[System] ";
break;
}
DateTime now = DateTime.Now;
return now.AddMilliseconds(-now.Millisecond).ToString() + " " + Config.ServerName + "";
return now.AddMilliseconds(-now.Millisecond).ToString() + " " + prefix + Config.ServerName + "";
}
public static void Error(Exception e)
{
Console.Write("\r" + GetPrefix() + e.Message + "\n" + e.StackTrace + "\n\r> ");
Console.Write("\r" + GetPrefix(InvokeMessageType.Error) + e.Message + "\n" + e.StackTrace + "\n\r> ");
Console.ResetColor();
}
public static void Write(string msg)
public static void Write(string msg, InvokeMessageType type = InvokeMessageType.System)
{
if (msg.Trim() != "") Console.Write("\r" + GetPrefix() + msg + "> ");
if (msg.Trim() != "") Console.Write("\r" + GetPrefix(type) + msg + "> ");
}
public static void WriteLine(string msg)
public static void WriteLine(string msg, InvokeMessageType type = InvokeMessageType.System)
{
if (msg.Trim() != "") Console.Write("\r" + GetPrefix() + msg + "\n\r> ");
if (msg.Trim() != "") Console.Write("\r" + GetPrefix(type) + msg + "\n\r> ");
}
public static void Type()

View File

@ -38,7 +38,7 @@ namespace Milimoe.FunGame.Server.Utility
public override int Execute()
{
// _IsOneTime = true需要手动创建连接和关闭
ServerHelper.WriteLine("SQLQuery -> " + Script);
ServerHelper.WriteLine("SQLQuery -> " + Script, InvokeMessageType.Api);
_DataSet = new DataSet();
_UpdateRows = MySQLManager.Execute(this, out _Result);
if (_IsOneTime) Close();
@ -54,7 +54,7 @@ namespace Milimoe.FunGame.Server.Utility
public override int Execute(string Script)
{
// _IsOneTime = true需要手动创建连接和关闭
ServerHelper.WriteLine("SQLQuery -> " + Script);
ServerHelper.WriteLine("SQLQuery -> " + Script, InvokeMessageType.Api);
this.Script = Script;
_DataSet = new DataSet();
_UpdateRows = MySQLManager.Execute(this, out _Result);
@ -70,7 +70,7 @@ namespace Milimoe.FunGame.Server.Utility
public override DataSet ExecuteDataSet()
{
// _IsOneTime = true需要手动创建连接和关闭
ServerHelper.WriteLine("SQLQuery -> " + Script);
ServerHelper.WriteLine("SQLQuery -> " + Script, InvokeMessageType.Api);
_DataSet = MySQLManager.ExecuteDataSet(this, out _Result, out _UpdateRows);
if (_IsOneTime) Close();
return DataSet;
@ -85,7 +85,7 @@ namespace Milimoe.FunGame.Server.Utility
public override DataSet ExecuteDataSet(string Script)
{
// _IsOneTime = true需要手动创建连接和关闭
ServerHelper.WriteLine("SQLQuery -> " + Script);
ServerHelper.WriteLine("SQLQuery -> " + Script, InvokeMessageType.Api);
this.Script = Script;
_DataSet = MySQLManager.ExecuteDataSet(this, out _Result, out _UpdateRows);
if (_IsOneTime) Close();
@ -128,7 +128,7 @@ namespace Milimoe.FunGame.Server.Utility
this.ServerModel = ServerModel;
Script = "";
CommandType = CommandType.Text;
Parameters = Array.Empty<MySqlParameter>();
Parameters = [];
_Connection = new MySQLConnection(out _ServerInfo);
}