milimoe 8aec496fcb
服务器补全 API 实现 (#48)
* PayloadModel 添加 event 属性,添加 Room,Main 的 API 控制器

* 实现 SQLHelper 的自增 ID、异步版本功能

* 填充一些请求控制器的方法

* 添加报价的核心操作

* 涉及库存的物品获取应该使用 Guid 而不是 ItemId

* 添加 InventoryController

* 添加更新房间设置和用户房间的状态管理

* 优化 API Token 秘钥管理;修复服务器统一报错信息 BUG

* 优雅的关闭服务器;补全了所有数据请求实现;API Token 加密方式修改;添加了服务器初始化时创建管理员账号的必要步骤

* 完善 Web API 控制器

---------

Co-authored-by: yeziuku <yezi@wrss.org>
2025-04-21 01:08:31 +08:00

202 lines
7.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Text;
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.Constant;
using Milimoe.FunGame.Server.Others;
using Milimoe.FunGame.Server.Services;
using ProjectRedbud.FunGame.SQLQueryExtension;
namespace Milimoe.FunGame.Server.Model
{
public class ConsoleModel
{
public static async Task Order<T>(ISocketListener<T>? server, string order) where T : ISocketMessageProcessor
{
try
{
switch (order)
{
case OrderDictionary.Kick:
{
ServerHelper.Write("输入需要踢出的客户端名称:");
string client = Console.ReadLine() ?? "";
if (client != "" && server != null)
{
await Kick((ServerModel<T>)server.ClientList[client]);
}
break;
}
case OrderDictionary.Logout:
{
ServerHelper.Write("输入需要强制下线的玩家ID");
string user = Console.ReadLine() ?? "";
if (user != "" && server != null)
{
await ForceLogOut((ServerModel<T>)server.UserList[user]);
}
break;
}
case OrderDictionary.ShowList:
ShowClients(server);
ShowUsers(server);
break;
case OrderDictionary.ShowClients1:
case OrderDictionary.ShowClients2:
ShowClients(server);
break;
case OrderDictionary.ShowUsers1:
case OrderDictionary.ShowUsers2:
ShowUsers(server);
break;
default:
break;
}
// 广播到插件
if (FunGameSystem.ServerPluginLoader != null)
{
foreach (ServerPlugin plugin in FunGameSystem.ServerPluginLoader.Plugins.Values)
{
plugin.ProcessInput(order);
}
}
if (FunGameSystem.WebAPIPluginLoader != null)
{
foreach (WebAPIPlugin plugin in FunGameSystem.WebAPIPluginLoader.Plugins.Values)
{
plugin.ProcessInput(order);
}
}
}
catch (Exception e)
{
ServerHelper.Error(e);
}
}
public static async Task Kick<T>(ServerModel<T> clientModel) where T : ISocketMessageProcessor
{
await clientModel.Kick("您已被服务器管理员踢出此服务器。");
}
public static async Task ForceLogOut<T>(ServerModel<T> clientModel) where T : ISocketMessageProcessor
{
await clientModel.ForceLogOut("您已被服务器管理员强制下线。");
}
public static void ShowClients<T>(ISocketListener<T>? server) where T : ISocketMessageProcessor
{
if (server != null)
{
ServerHelper.WriteLine("显示在线客户端列表");
int index = 1;
foreach (IServerModel client in server.ClientList)
{
ServerHelper.WriteLine(index++ + ". " + client.ClientName + (client.User.Id != 0 ? " (已登录为:" + client.User.Username + ")" : ""));
}
}
}
public static void ShowUsers<T>(ISocketListener<T>? server) where T : ISocketMessageProcessor
{
if (server != null)
{
ServerHelper.WriteLine("显示在线玩家列表");
int index = 1;
foreach (IServerModel user in server.UserList.Where(u => u.User.Id != 0))
{
ServerHelper.WriteLine(index++ + ". " + (user.User.Username) + " (客户端:" + user.ClientName + ")");
}
}
}
public static void FirstRunRegAdmin()
{
using SQLHelper? sql = Factory.OpenFactory.GetSQLHelper() ?? throw new SQLServiceException();
ServerHelper.WriteLine("首次启动需要注册管理员账号,请按提示输入信息!", InvokeMessageType.Core);
string username, password, email;
ServerHelper.Write("请输入管理员用户名:", InvokeMessageType.Core);
while (true)
{
username = Console.ReadLine() ?? "";
int usernameLength = NetworkUtility.GetUserNameLength(username);
if (usernameLength < 3 || usernameLength > 12)
{
ServerHelper.WriteLine("账号名长度不符合要求3~12个字符数一个中文2个字符", InvokeMessageType.Error);
}
else
{
break;
}
}
ServerHelper.Write("请输入管理员邮箱:", InvokeMessageType.Core);
while (true)
{
email = Console.ReadLine() ?? "";
if (!NetworkUtility.IsEmail(email))
{
ServerHelper.WriteLine("这不是一个邮箱地址!", InvokeMessageType.Error);
}
else
{
break;
}
}
ServerHelper.Write("请输入管理员密码:", InvokeMessageType.Core);
while (true)
{
StringBuilder passwordBuilder = new();
ConsoleKeyInfo key;
do
{
key = Console.ReadKey(true);
if (key.Key == ConsoleKey.Enter)
{
Console.WriteLine();
break;
}
else if (key.Key == ConsoleKey.Backspace)
{
if (passwordBuilder.Length > 0)
{
passwordBuilder.Remove(passwordBuilder.Length - 1, 1);
Console.Write("\b \b");
}
}
else if (!char.IsControl(key.KeyChar))
{
passwordBuilder.Append(key.KeyChar);
Console.Write("*");
}
} while (true);
password = passwordBuilder.ToString();
if (password.Length < 6 || password.Length > 15)
{
ServerHelper.WriteLine("密码长度不符合要求6~15个字符数", InvokeMessageType.Error);
}
else
{
break;
}
}
(string msg, RegInvokeType type, bool success) = DataRequestService.RegisterUser(sql, username, password, email, "localhost");
ServerHelper.WriteLine(msg, InvokeMessageType.Core);
if (success)
{
User? user = sql.GetUserByUsernameAndEmail(username, email);
if (user != null)
{
user.IsAdmin = true;
sql.UpdateUser(user);
}
}
}
}
}