using Milimoe.FunGame.Core.Api.Transmittal;
using Milimoe.FunGame.Core.Api.Utility;
using Milimoe.FunGame.Core.Controller;
using Milimoe.FunGame.Core.Entity;
using Milimoe.FunGame.Core.Interface.Addons;
using Milimoe.FunGame.Core.Interface.Base;
using Milimoe.FunGame.Core.Library.Constant;
namespace Milimoe.FunGame.Core.Library.Common.Addon
{
public abstract class GameModuleServer : IGameModuleServer
{
///
/// 服务器模组的名称
/// 如果服务器模组配合一个相关联的模组使用,那么它们的 名称必须相同。
///
public abstract string Name { get; }
///
/// 模组描述
///
public abstract string Description { get; }
///
/// 模组版本
///
public abstract string Version { get; }
///
/// 模组作者
///
public abstract string Author { get; }
///
/// 默认地图
///
public abstract string DefaultMap { get; }
///
/// 模组的依赖集合
///
public abstract GameModuleDepend GameModuleDepend { get; }
///
/// 包含了一些常用方法的控制器
///
public BaseAddonController Controller
{
get => _Controller ?? throw new NotImplementedException();
set => _Controller = value;
}
///
/// 控制器内部变量
///
private BaseAddonController? _Controller;
///
/// 全局数据库连接器
///
public SQLHelper? SQLHelper => Singleton.Get();
///
/// 全局邮件发送器
///
public MailSender? MailSender => Singleton.Get();
///
/// 启动服务器监听 请在此处实现服务器逻辑
///
///
///
///
///
///
///
///
public abstract bool StartServer(string GameModule, Room Room, List Users, IServerModel RoomMasterServerModel, Dictionary ServerModels, params object[] Args);
///
/// 接收并处理GamingMessage
///
/// 发送此消息的账号
/// 消息类型
/// 消息参数
/// 底层会将字典中的数据发送给客户端
public abstract Task> GamingMessageHandler(string username, GamingType type, Dictionary data);
///
/// 加载标记
///
private bool IsLoaded = false;
///
/// 加载模组
///
public bool Load(params object[] objs)
{
if (IsLoaded)
{
return false;
}
// BeforeLoad可以阻止加载此模组
if (BeforeLoad())
{
// 模组加载后,不允许再次加载此模组
IsLoaded = true;
}
return IsLoaded;
}
///
/// 模组完全加载后需要做的事
///
public virtual void AfterLoad(params object[] args)
{
// override
}
///
/// 允许返回false来阻止加载此模组
///
///
protected virtual bool BeforeLoad()
{
return true;
}
///
/// 给客户端发送局内消息
///
///
///
///
protected virtual async Task SendGamingMessage(IEnumerable clients, GamingType type, Dictionary data)
{
// 发送局内消息
foreach (IServerModel s in clients)
{
await s.Send(SocketMessageType.Gaming, type, data);
}
}
///
/// 给客户端发送消息
///
///
///
///
protected virtual async Task Send(IEnumerable clients, SocketMessageType type, params object[] args)
{
// 发送消息
foreach (IServerModel s in clients)
{
await s.Send(type, args);
}
}
}
}