数据库表结构和工具类调整,添加 ApiTokens 表 (#114)

* 数据库表结构和工具类调整,添加 ApiTokens 表

* 修复 Workers 字典没有元素的问题
This commit is contained in:
milimoe 2025-03-16 01:57:54 +08:00 committed by GitHub
parent 7561c2c7e2
commit 92452873ef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 220 additions and 98 deletions

View File

@ -72,6 +72,36 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
return null;
}
/// <summary>
/// 查询数据库是否存在
/// </summary>
/// <returns></returns>
public abstract bool DatabaseExists();
/// <summary>
/// 执行一个 sql 脚本文件
/// </summary>
/// <param name="path"></param>
public virtual void ExecuteSqlFile(string path)
{
if (!File.Exists(path))
{
throw new FileNotFoundException("SQL 脚本文件不存在", path);
}
string content = File.ReadAllText(path);
string[] commands = content.Split([";"], StringSplitOptions.RemoveEmptyEntries);
foreach (string command in commands)
{
string sql = command.Trim();
if (!string.IsNullOrEmpty(sql))
{
Execute(sql);
}
}
}
/// <summary>
/// 关闭连接
/// </summary>

View File

@ -3,6 +3,7 @@ using System.Globalization;
using System.Net;
using System.Net.NetworkInformation;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using Milimoe.FunGame.Core.Library.Common.Architecture;
@ -489,6 +490,36 @@ namespace Milimoe.FunGame.Core.Api.Utility
byte[] decrypted = rsa.Decrypt(secret, true);
return General.DefaultEncoding.GetString(decrypted);
}
/// <summary>
/// 使用 MD5 算法加密
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static string MD5(string text)
{
byte[] inputBytes = General.DefaultEncoding.GetBytes(text);
byte[] hash = System.Security.Cryptography.MD5.HashData(inputBytes);
return Convert.ToHexStringLower(hash);
}
/// <summary>
/// 生成随机字符串
/// </summary>
/// <param name="length"></param>
/// <returns></returns>
public static string GenerateRandomString(int length = 18)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+=-`~[]\\{}|;':\",./<>?";
byte[] data = RandomNumberGenerator.GetBytes(length);
StringBuilder result = new(length);
foreach (byte b in data)
{
result.Append(chars[b % chars.Length]);
}
return result.ToString();
}
}
#endregion

View File

@ -146,6 +146,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Addon.Example
{
// 因为模组是单例的,需要为这个房间创建一个工作类接收参数,不能直接用本地变量处理
ModuleServerWorker worker = new(obj);
Workers[obj.Room.Roomid] = worker;
// 创建一个线程执行Test(),因为这个方法必须立即返回
TaskUtility.NewTask(async () => await Test(obj, worker)).OnError(Controller.Error);
return true;

View File

@ -53,3 +53,39 @@ namespace Milimoe.FunGame.Core.Library.SQLScript.Common
}
}
}
namespace Milimoe.FunGame.Core.Library.SQLScript.Common
{
public class ApiTokens : Constant
{
public const string TableName = "ApiTokens";
public const string Column_TokenID = "TokenID";
public const string Column_SecretKey = "SecretKey";
public const string Column_Reference1 = "Reference1";
public const string Column_Reference2 = "Reference2";
public static string Insert_APITokens(SQLHelper SQLHelper, string TokenID, string SecretKey = "", string Reference1 = "", string Reference2 = "")
{
SQLHelper.Parameters["@TokenID"] = TokenID;
SQLHelper.Parameters["@SecretKey"] = SecretKey;
SQLHelper.Parameters["@Reference1"] = Reference1;
SQLHelper.Parameters["@Reference2"] = Reference2;
return $"{Command_Insert} {Command_Into} {TableName} ({Column_TokenID}, {Column_SecretKey}, {Column_Reference1}, {Column_Reference2}) {Command_Values} (@TokenID, @SecretKey, @Reference1, @Reference2)";
}
public static string Select_GetAPIToken(SQLHelper SQLHelper, string TokenID)
{
SQLHelper.Parameters["@TokenID"] = TokenID;
return $"{Command_Select} {Command_All} {Command_From} {TableName} {Command_Where} {Column_TokenID} = @TokenID";
}
public static string Update_GetAPIToken(SQLHelper SQLHelper, string TokenID, string SecretKey, string Reference1 = "", string Reference2 = "")
{
SQLHelper.Parameters["@TokenID"] = TokenID;
SQLHelper.Parameters["@SecretKey"] = SecretKey;
SQLHelper.Parameters["@Reference1"] = Reference1;
SQLHelper.Parameters["@Reference2"] = Reference2;
return $"{Command_Update} {TableName} {Command_Set} {Column_TokenID} = @TokenID, {Column_SecretKey} = @SecretKey, {Column_Reference1} = @Reference1, {Column_Reference2} = @Reference2 {Command_Where} {Column_TokenID} = @TokenID";
}
}
}

View File

@ -1,76 +1,88 @@
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for forgetverifycodes
-- Table structure for ForgetVerifyCodes
-- ----------------------------
DROP TABLE IF EXISTS `forgetverifycodes`;
CREATE TABLE `forgetverifycodes` (
`Username` varchar(255) DEFAULT NULL,
`Email` varchar(255) DEFAULT NULL,
`ForgetVerifyCode` varchar(255) DEFAULT NULL,
`SendTime` datetime DEFAULT NULL
DROP TABLE IF EXISTS `ForgetVerifyCodes`;
CREATE TABLE `ForgetVerifyCodes` (
`Username` varchar(255) NOT NULL DEFAULT '',
`Email` varchar(255) NOT NULL DEFAULT '',
`ForgetVerifyCode` varchar(255) NOT NULL DEFAULT '',
`SendTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- ----------------------------
-- Table structure for regverifycodes
-- Table structure for RegVerifyCodes
-- ----------------------------
DROP TABLE IF EXISTS `regverifycodes`;
CREATE TABLE `regverifycodes` (
`Username` varchar(255) DEFAULT NULL,
`Email` varchar(255) DEFAULT NULL,
`RegVerifyCode` varchar(255) DEFAULT NULL,
`RegTime` datetime DEFAULT NULL
DROP TABLE IF EXISTS `RegVerifyCodes`;
CREATE TABLE `RegVerifyCodes` (
`Username` varchar(255) NOT NULL DEFAULT '',
`Email` varchar(255) NOT NULL DEFAULT '',
`RegVerifyCode` varchar(255) NOT NULL DEFAULT '',
`RegTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- ----------------------------
-- Table structure for rooms
-- Table structure for Rooms
-- ----------------------------
DROP TABLE IF EXISTS `rooms`;
CREATE TABLE `rooms` (
DROP TABLE IF EXISTS `Rooms`;
CREATE TABLE `Rooms` (
`Id` bigint(20) NOT NULL AUTO_INCREMENT,
`Roomid` varchar(255) NOT NULL DEFAULT '-1',
`CreateTime` datetime NOT NULL,
`CreateTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`RoomMaster` bigint(20) NOT NULL DEFAULT '0',
`RoomType` int(8) DEFAULT '0',
`GameModule` varchar(255) DEFAULT '',
`GameMap` varchar(255) DEFAULT '',
`RoomState` int(8) DEFAULT '0',
`IsRank` int(1) DEFAULT '0',
`HasPass` int(1) DEFAULT '0',
`Password` varchar(255) DEFAULT '',
`MaxUsers` int(8) DEFAULT '0',
`RoomType` int(8) NOT NULL DEFAULT '0',
`GameModule` varchar(255) NOT NULL DEFAULT '',
`GameMap` varchar(255) NOT NULL DEFAULT '',
`RoomState` int(8) NOT NULL DEFAULT '0',
`IsRank` int(1) NOT NULL DEFAULT '0',
`HasPass` int(1) NOT NULL DEFAULT '0',
`Password` varchar(255) NOT NULL DEFAULT '',
`MaxUsers` int(8) NOT NULL DEFAULT '0',
PRIMARY KEY (`Id`,`Roomid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- ----------------------------
-- Table structure for serverloginlogs
-- Table structure for ServerLoginLogs
-- ----------------------------
DROP TABLE IF EXISTS `serverloginlogs`;
CREATE TABLE `serverloginlogs` (
`ServerName` varchar(255) DEFAULT NULL,
`ServerKey` varchar(255) DEFAULT NULL,
`LoginTime` datetime DEFAULT NULL
DROP TABLE IF EXISTS `ServerLoginLogs`;
CREATE TABLE `ServerLoginLogs` (
`ServerName` varchar(255) NOT NULL DEFAULT '',
`ServerKey` varchar(255) NOT NULL DEFAULT '',
`LoginTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- ----------------------------
-- Table structure for users
-- Table structure for ApiTokens
-- ----------------------------
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
DROP TABLE IF EXISTS `ApiTokens`;
CREATE TABLE `ApiTokens` (
`TokenID` varchar(255) NOT NULL DEFAULT '',
`SecretKey` varchar(255) NOT NULL DEFAULT '',
`Reference1` varchar(255) NOT NULL DEFAULT '',
`Reference2` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`TokenID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- ----------------------------
-- Table structure for Users
-- ----------------------------
DROP TABLE IF EXISTS `Users`;
CREATE TABLE `Users` (
`UID` bigint(20) NOT NULL AUTO_INCREMENT,
`Username` varchar(255) NOT NULL,
`Password` varchar(255) NOT NULL,
`RegTime` datetime DEFAULT NULL,
`LastTime` datetime DEFAULT NULL,
`LastIP` varchar(255) DEFAULT '',
`Username` varchar(255) NOT NULL DEFAULT '',
`Password` varchar(255) NOT NULL DEFAULT '',
`RegTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`LastTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`LastIP` varchar(255) NOT NULL DEFAULT '',
`Email` varchar(255) NOT NULL DEFAULT '',
`Nickname` varchar(255) DEFAULT '',
`IsAdmin` int(1) DEFAULT '0',
`IsOperator` int(1) DEFAULT '0',
`IsEnable` int(1) DEFAULT '1',
`Credits` decimal(20,0) DEFAULT '0',
`Materials` decimal(20,0) DEFAULT '0',
`GameTime` decimal(20,0) DEFAULT '0',
`AutoKey` varchar(255) DEFAULT '',
`Nickname` varchar(255) NOT NULL DEFAULT '',
`IsAdmin` int(1) NOT NULL DEFAULT '0',
`IsOperator` int(1) NOT NULL DEFAULT '0',
`IsEnable` int(1) NOT NULL DEFAULT '1',
`Credits` decimal(20,0) NOT NULL DEFAULT '0',
`Materials` decimal(20,0) NOT NULL DEFAULT '0',
`GameTime` decimal(20,0) NOT NULL DEFAULT '0',
`AutoKey` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`UID`,`Username`,`Email`)
) ENGINE=InnoDB AUTO_INCREMENT=41 DEFAULT CHARSET=utf8mb4;

View File

@ -1,74 +1,86 @@
PRAGMA foreign_keys = OFF;
-- ----------------------------
-- Table structure for forgetverifycodes
-- Table structure for ForgetVerifyCodes
-- ----------------------------
DROP TABLE IF EXISTS "main"."forgetverifycodes";
CREATE TABLE forgetverifycodes (
Username TEXT,
Email TEXT,
ForgetVerifyCode TEXT,
SendTime DATETIME
DROP TABLE IF EXISTS "main"."ForgetVerifyCodes";
CREATE TABLE ForgetVerifyCodes (
Username TEXT NOT NULL DEFAULT '',
Email TEXT NOT NULL DEFAULT '',
ForgetVerifyCode TEXT NOT NULL DEFAULT '',
SendTime DATETIME NOT NULL DEFAULT (DATETIME('now'))
);
-- ----------------------------
-- Table structure for regverifycodes
-- Table structure for RegVerifyCodes
-- ----------------------------
DROP TABLE IF EXISTS "main"."regverifycodes";
CREATE TABLE regverifycodes (
Username TEXT,
Email TEXT,
RegVerifyCode TEXT,
RegTime DATETIME
DROP TABLE IF EXISTS "main"."RegVerifyCodes";
CREATE TABLE RegVerifyCodes (
Username TEXT NOT NULL DEFAULT '',
Email TEXT NOT NULL DEFAULT '',
RegVerifyCode TEXT NOT NULL DEFAULT '',
RegTime DATETIME NOT NULL DEFAULT (DATETIME('now'))
);
-- ----------------------------
-- Table structure for rooms
-- Table structure for Rooms
-- ----------------------------
DROP TABLE IF EXISTS "main"."rooms";
CREATE TABLE "rooms" (
DROP TABLE IF EXISTS "main"."Rooms";
CREATE TABLE "Rooms" (
"Id" INTEGER PRIMARY KEY AUTOINCREMENT,
"Roomid" TEXT NOT NULL DEFAULT '-1',
"CreateTime" DATETIME NOT NULL,
"CreateTime" DATETIME NOT NULL DEFAULT (DATETIME('now')),
"RoomMaster" INTEGER NOT NULL DEFAULT 0,
"RoomType" INTEGER DEFAULT 0,
"GameModule" TEXT DEFAULT '',
"GameMap" TEXT DEFAULT '',
"RoomState" INTEGER DEFAULT 0,
"IsRank" INTEGER DEFAULT 0,
"HasPass" INTEGER DEFAULT 0,
"Password" TEXT DEFAULT '',
"MaxUsers" INTEGER DEFAULT 0
"RoomType" INTEGER NOT NULL DEFAULT 0,
"GameModule" TEXT NOT NULL DEFAULT '',
"GameMap" TEXT NOT NULL DEFAULT '',
"RoomState" INTEGER NOT NULL DEFAULT 0,
"IsRank" INTEGER NOT NULL DEFAULT 0,
"HasPass" INTEGER NOT NULL DEFAULT 0,
"Password" TEXT NOT NULL DEFAULT '',
"MaxUsers" INTEGER NOT NULL DEFAULT 0
);
-- ----------------------------
-- Table structure for serverloginlogs
-- Table structure for ServerLoginLogs
-- ----------------------------
DROP TABLE IF EXISTS "main"."serverloginlogs";
CREATE TABLE serverloginlogs (
ServerName TEXT,
ServerKey TEXT,
LoginTime DATETIME
DROP TABLE IF EXISTS "main"."ServerLoginLogs";
CREATE TABLE ServerLoginLogs (
ServerName TEXT NOT NULL DEFAULT '',
ServerKey TEXT NOT NULL DEFAULT '',
LoginTime DATETIME NOT NULL DEFAULT (DATETIME('now'))
);
-- ----------------------------
-- Table structure for users
-- Table structure for ApiTokens
-- ----------------------------
DROP TABLE IF EXISTS "main"."users";
CREATE TABLE users (
DROP TABLE IF EXISTS "main"."ApiTokens";
CREATE TABLE ApiTokens (
TokenID TEXT NOT NULL DEFAULT '',
SecretKey TEXT NOT NULL DEFAULT '',
Reference1 TEXT NOT NULL DEFAULT '',
Reference2 TEXT NOT NULL DEFAULT '',
PRIMARY KEY (TokenID)
);
-- ----------------------------
-- Table structure for Users
-- ----------------------------
DROP TABLE IF EXISTS "main"."Users";
CREATE TABLE Users (
UID INTEGER PRIMARY KEY AUTOINCREMENT,
Username TEXT NOT NULL,
Password TEXT NOT NULL,
RegTime DATETIME,
LastTime DATETIME,
LastIP TEXT DEFAULT '',
RegTime DATETIME NOT NULL DEFAULT (DATETIME('now')),
LastTime DATETIME NOT NULL DEFAULT (DATETIME('now')),
LastIP TEXT NOT NULL DEFAULT '',
Email TEXT NOT NULL DEFAULT '',
Nickname TEXT DEFAULT '',
IsAdmin INTEGER DEFAULT 0,
IsOperator INTEGER DEFAULT 0,
IsEnable INTEGER DEFAULT 1,
Credits REAL DEFAULT 0,
Materials REAL DEFAULT 0,
GameTime REAL DEFAULT 0,
AutoKey TEXT DEFAULT ''
Nickname TEXT NOT NULL DEFAULT '',
IsAdmin INTEGER NOT NULL DEFAULT 0,
IsOperator INTEGER NOT NULL DEFAULT 0,
IsEnable INTEGER NOT NULL DEFAULT 1,
Credits REAL NOT NULL DEFAULT 0,
Materials REAL NOT NULL DEFAULT 0,
GameTime REAL NOT NULL DEFAULT 0,
AutoKey TEXT NOT NULL DEFAULT ''
);