数据库表结构和工具类调整,添加 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; 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>
/// 关闭连接 /// 关闭连接
/// </summary> /// </summary>

View File

@ -3,6 +3,7 @@ using System.Globalization;
using System.Net; using System.Net;
using System.Net.NetworkInformation; using System.Net.NetworkInformation;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text;
using System.Text.Json; using System.Text.Json;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Milimoe.FunGame.Core.Library.Common.Architecture; using Milimoe.FunGame.Core.Library.Common.Architecture;
@ -489,6 +490,36 @@ namespace Milimoe.FunGame.Core.Api.Utility
byte[] decrypted = rsa.Decrypt(secret, true); byte[] decrypted = rsa.Decrypt(secret, true);
return General.DefaultEncoding.GetString(decrypted); 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 #endregion

View File

@ -146,6 +146,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Addon.Example
{ {
// 因为模组是单例的,需要为这个房间创建一个工作类接收参数,不能直接用本地变量处理 // 因为模组是单例的,需要为这个房间创建一个工作类接收参数,不能直接用本地变量处理
ModuleServerWorker worker = new(obj); ModuleServerWorker worker = new(obj);
Workers[obj.Room.Roomid] = worker;
// 创建一个线程执行Test(),因为这个方法必须立即返回 // 创建一个线程执行Test(),因为这个方法必须立即返回
TaskUtility.NewTask(async () => await Test(obj, worker)).OnError(Controller.Error); TaskUtility.NewTask(async () => await Test(obj, worker)).OnError(Controller.Error);
return true; 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; SET FOREIGN_KEY_CHECKS=0;
-- ---------------------------- -- ----------------------------
-- Table structure for forgetverifycodes -- Table structure for ForgetVerifyCodes
-- ---------------------------- -- ----------------------------
DROP TABLE IF EXISTS `forgetverifycodes`; DROP TABLE IF EXISTS `ForgetVerifyCodes`;
CREATE TABLE `forgetverifycodes` ( CREATE TABLE `ForgetVerifyCodes` (
`Username` varchar(255) DEFAULT NULL, `Username` varchar(255) NOT NULL DEFAULT '',
`Email` varchar(255) DEFAULT NULL, `Email` varchar(255) NOT NULL DEFAULT '',
`ForgetVerifyCode` varchar(255) DEFAULT NULL, `ForgetVerifyCode` varchar(255) NOT NULL DEFAULT '',
`SendTime` datetime DEFAULT NULL `SendTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- ---------------------------- -- ----------------------------
-- Table structure for regverifycodes -- Table structure for RegVerifyCodes
-- ---------------------------- -- ----------------------------
DROP TABLE IF EXISTS `regverifycodes`; DROP TABLE IF EXISTS `RegVerifyCodes`;
CREATE TABLE `regverifycodes` ( CREATE TABLE `RegVerifyCodes` (
`Username` varchar(255) DEFAULT NULL, `Username` varchar(255) NOT NULL DEFAULT '',
`Email` varchar(255) DEFAULT NULL, `Email` varchar(255) NOT NULL DEFAULT '',
`RegVerifyCode` varchar(255) DEFAULT NULL, `RegVerifyCode` varchar(255) NOT NULL DEFAULT '',
`RegTime` datetime DEFAULT NULL `RegTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- ---------------------------- -- ----------------------------
-- Table structure for rooms -- Table structure for Rooms
-- ---------------------------- -- ----------------------------
DROP TABLE IF EXISTS `rooms`; DROP TABLE IF EXISTS `Rooms`;
CREATE TABLE `rooms` ( CREATE TABLE `Rooms` (
`Id` bigint(20) NOT NULL AUTO_INCREMENT, `Id` bigint(20) NOT NULL AUTO_INCREMENT,
`Roomid` varchar(255) NOT NULL DEFAULT '-1', `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', `RoomMaster` bigint(20) NOT NULL DEFAULT '0',
`RoomType` int(8) DEFAULT '0', `RoomType` int(8) NOT NULL DEFAULT '0',
`GameModule` varchar(255) DEFAULT '', `GameModule` varchar(255) NOT NULL DEFAULT '',
`GameMap` varchar(255) DEFAULT '', `GameMap` varchar(255) NOT NULL DEFAULT '',
`RoomState` int(8) DEFAULT '0', `RoomState` int(8) NOT NULL DEFAULT '0',
`IsRank` int(1) DEFAULT '0', `IsRank` int(1) NOT NULL DEFAULT '0',
`HasPass` int(1) DEFAULT '0', `HasPass` int(1) NOT NULL DEFAULT '0',
`Password` varchar(255) DEFAULT '', `Password` varchar(255) NOT NULL DEFAULT '',
`MaxUsers` int(8) DEFAULT '0', `MaxUsers` int(8) NOT NULL DEFAULT '0',
PRIMARY KEY (`Id`,`Roomid`) PRIMARY KEY (`Id`,`Roomid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- ---------------------------- -- ----------------------------
-- Table structure for serverloginlogs -- Table structure for ServerLoginLogs
-- ---------------------------- -- ----------------------------
DROP TABLE IF EXISTS `serverloginlogs`; DROP TABLE IF EXISTS `ServerLoginLogs`;
CREATE TABLE `serverloginlogs` ( CREATE TABLE `ServerLoginLogs` (
`ServerName` varchar(255) DEFAULT NULL, `ServerName` varchar(255) NOT NULL DEFAULT '',
`ServerKey` varchar(255) DEFAULT NULL, `ServerKey` varchar(255) NOT NULL DEFAULT '',
`LoginTime` datetime DEFAULT NULL `LoginTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- ---------------------------- -- ----------------------------
-- Table structure for users -- Table structure for ApiTokens
-- ---------------------------- -- ----------------------------
DROP TABLE IF EXISTS `users`; DROP TABLE IF EXISTS `ApiTokens`;
CREATE TABLE `users` ( 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, `UID` bigint(20) NOT NULL AUTO_INCREMENT,
`Username` varchar(255) NOT NULL, `Username` varchar(255) NOT NULL DEFAULT '',
`Password` varchar(255) NOT NULL, `Password` varchar(255) NOT NULL DEFAULT '',
`RegTime` datetime DEFAULT NULL, `RegTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`LastTime` datetime DEFAULT NULL, `LastTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`LastIP` varchar(255) DEFAULT '', `LastIP` varchar(255) NOT NULL DEFAULT '',
`Email` varchar(255) NOT NULL DEFAULT '', `Email` varchar(255) NOT NULL DEFAULT '',
`Nickname` varchar(255) DEFAULT '', `Nickname` varchar(255) NOT NULL DEFAULT '',
`IsAdmin` int(1) DEFAULT '0', `IsAdmin` int(1) NOT NULL DEFAULT '0',
`IsOperator` int(1) DEFAULT '0', `IsOperator` int(1) NOT NULL DEFAULT '0',
`IsEnable` int(1) DEFAULT '1', `IsEnable` int(1) NOT NULL DEFAULT '1',
`Credits` decimal(20,0) DEFAULT '0', `Credits` decimal(20,0) NOT NULL DEFAULT '0',
`Materials` decimal(20,0) DEFAULT '0', `Materials` decimal(20,0) NOT NULL DEFAULT '0',
`GameTime` decimal(20,0) DEFAULT '0', `GameTime` decimal(20,0) NOT NULL DEFAULT '0',
`AutoKey` varchar(255) DEFAULT '', `AutoKey` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`UID`,`Username`,`Email`) PRIMARY KEY (`UID`,`Username`,`Email`)
) ENGINE=InnoDB AUTO_INCREMENT=41 DEFAULT CHARSET=utf8mb4; ) ENGINE=InnoDB AUTO_INCREMENT=41 DEFAULT CHARSET=utf8mb4;

View File

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