From 2e5479bed80daa5bea2d2fd9add0c7fd77e8aba6 Mon Sep 17 00:00:00 2001 From: milimoe Date: Wed, 4 Sep 2024 00:38:37 +0800 Subject: [PATCH] new --- Library/Main.cs | 149 +++++++++++++++++------------ Library/Solutions/ActionQueue.cs | 157 +++++++++++++++++++++++++++++++ Library/Solutions/MyPlugin.cs | 2 +- Library/Solutions/Person.cs | 14 ++- Library/Solutions/TestModule.cs | 84 +++++++++++++++++ Library/Solutions/TestPlugin.cs | 112 ++++++++++++++++++++++ Library/Tests/CheckDLL.cs | 66 +++++++++++++ Library/Tests/Old.cs | 83 ++++++++++++++++ 8 files changed, 604 insertions(+), 63 deletions(-) create mode 100644 Library/Solutions/ActionQueue.cs create mode 100644 Library/Solutions/TestModule.cs create mode 100644 Library/Solutions/TestPlugin.cs create mode 100644 Library/Tests/CheckDLL.cs create mode 100644 Library/Tests/Old.cs diff --git a/Library/Main.cs b/Library/Main.cs index 6db7684..2490795 100644 --- a/Library/Main.cs +++ b/Library/Main.cs @@ -1,74 +1,103 @@ -using System.Collections; -using System.Data; -using ConverterExample; using Milimoe.FunGame.Core.Api.Utility; using Milimoe.FunGame.Core.Entity; -using Milimoe.FunGame.Core.Library.Common.JsonConverter; +using Milimoe.FunGame.Core.Library.Common.Addon; +using Milimoe.FunGame.Core.Library.Common.Event; +using Milimoe.FunGame.Core.Library.Constant; +using Milimoe.FunGame.Testing.Solutions; -DataSet ds = new(); -DataTable table = new("SampleTable1"); -table.Columns.Add("Id", typeof(int)); -table.Columns.Add("Name", typeof(string)); -table.Columns.Add("Age", typeof(int)); -table.Rows.Add(1, "John", 30); -table.Rows.Add(2, "Jane", 25); -table.Rows.Add(3, "Bob", 40); -ds.Tables.Add(table); - -table = new("SampleTable2"); -table.Columns.Add("Id", typeof(int)); -table.Columns.Add("Name", typeof(string)); -table.Columns.Add("Age", typeof(int)); -table.Rows.Add(1, "John", 30); -table.Rows.Add(2, "Jane", 25); -table.Rows.Add(3, "Bob", 40); -ds.Tables.Add(table); - -JsonTool JsonTool = new(); -JsonTool.AddConverters(new System.Text.Json.Serialization.JsonConverter[] { new UserConverter(), new RoomConverter(), new PersonConverter(), new AddressConverter() }); - -Room r = Factory.GetRoom(1294367, "w5rtvh8".ToUpper(), DateTime.Now, Factory.GetUser(), Milimoe.FunGame.Core.Library.Constant.RoomType.Mix, "", "", Milimoe.FunGame.Core.Library.Constant.RoomState.Created); -User u = Factory.GetUser(1, "LUOLI", DateTime.Now, DateTime.Now, "LUOLI@66.COM", "QWQAQW"); - -Hashtable hashtable = new() +PluginLoader plugins = PluginLoader.LoadPlugins([]); +foreach (string plugin in plugins.Plugins.Keys) { - { "table", table }, - { "room", r }, - { "user", u } -}; + Console.WriteLine(plugin + " is loaded."); +} -string json = JsonTool.GetString(hashtable); - -Hashtable hashtable2 = JsonTool.GetObject(json) ?? new(); - -DataTable table2 = JsonTool.GetObject(json) ?? new(); -User u2 = JsonTool.GetObject(hashtable2, "user") ?? Factory.GetUser(); -Room r2 = JsonTool.GetObject(hashtable2, "room") ?? Factory.GetRoom(); - -table2.AsEnumerable().ToList().ForEach(row => +Dictionary plugindllsha512 = []; +foreach (string pfp in PluginLoader.PluginFilePaths.Keys) { - Console.WriteLine("Id: " + row["Id"] + ", Name: "+ row["Name"] + ", Age: " + row["Age"]); -}); + string text = Encryption.FileSha512(PluginLoader.PluginFilePaths[pfp]); + plugindllsha512.Add(pfp, text); + Console.WriteLine(pfp + $" is {text}."); +} -Console.WriteLine(u2.Username + " 进入了 " + r2.Roomid + " 房间"); - -Person p = new() +LoginEventArgs e = new(); +plugins.OnBeforeLoginEvent(plugins, e); +if (!e.Cancel) { - Age = (int)r2.Id, - Name = u2.Username, - Address = new() + plugins.OnSucceedLoginEvent(plugins, e); + plugins.OnFailedLoginEvent(plugins, e); +} +plugins.OnAfterLoginEvent(plugins, e); + +List list = []; + +GameModuleLoader modules = GameModuleLoader.LoadGameModules(FunGameInfo.FunGame.FunGame_Desktop, []); +foreach (CharacterModule cm in modules.Characters.Values) +{ + foreach (Character c in cm.Characters) { - State = "呵呵州(Hehe State)", - City = "哈哈市(Haha City)" + Console.WriteLine(c.Name); + list.Add(c); } -}; +} -json = JsonTool.GetString(p); +Dictionary moduledllsha512 = []; +foreach (string mfp in GameModuleLoader.ModuleFilePaths.Keys) +{ + string text = Encryption.FileSha512(GameModuleLoader.ModuleFilePaths[mfp]); + moduledllsha512.Add(mfp, text); + Console.WriteLine(mfp + $" is {text}."); +} -Person p2 = JsonTool.GetObject(json) ?? new(); +foreach (string moduledll in moduledllsha512.Keys) +{ + string server = moduledllsha512[moduledll]; + if (plugindllsha512.TryGetValue(moduledll, out string? client) && client != "" && server == client) + { + Console.WriteLine(moduledll + $" is checked pass."); + } +} -Console.WriteLine("My name is " + p2.Name + ", I am " + p2.Age + "-year-old. I live at " + p2.Address.State + " " + p2.Address.City); -Console.WriteLine("摆烂了86"); +if (list.Count > 3) +{ + Console.WriteLine(); + Console.WriteLine("Start!!!"); + Console.WriteLine(); -// 生成一对公钥秘钥 -//TwoFactorAuthenticator.CreateSecretKey(); + Character character1 = list[0].Copy(); + Character character2 = list[1].Copy(); + Character character3 = list[2].Copy(); + Character character4 = list[3].Copy(); + + ActionQueue actionQueue = new(); + List characters = [character1, character2, character3, character4]; + + // 初始顺序表排序 + actionQueue.CalculateInitialOrder(characters); + Console.WriteLine(); + + // 显示初始顺序表 + actionQueue.DisplayQueue(); + Console.WriteLine(); + + // 模拟时间流逝 + int i = 1; + while (i < 10) + { + // 检查是否有角色可以行动 + Character? characterToAct = actionQueue.NextCharacter(); + if (characterToAct != null) + { + Console.WriteLine($"=== Round {i++} ==="); + actionQueue.ProcessTurn(characterToAct); + actionQueue.DisplayQueue(); + Console.WriteLine(); + } + + //Thread.Sleep(1); // 模拟时间流逝 + actionQueue.ReduceHardnessTimes(); + } + + Console.WriteLine("--- End ---"); +} + +Console.ReadKey(); diff --git a/Library/Solutions/ActionQueue.cs b/Library/Solutions/ActionQueue.cs new file mode 100644 index 0000000..e0acd13 --- /dev/null +++ b/Library/Solutions/ActionQueue.cs @@ -0,0 +1,157 @@ +using Milimoe.FunGame.Core.Entity; + +namespace Milimoe.FunGame.Testing.Solutions; + +public class ActionQueue +{ + private readonly List _Queue = []; + private readonly Dictionary _HardnessTimes = []; + + public void AddCharacter(Character character, double hardnessTime) + { + // 插队机制:按硬直时间排序 + int insertIndex = _Queue.FindIndex(c => _HardnessTimes[c] > hardnessTime); + if (insertIndex == -1) + { + _Queue.Add(character); + } + else + { + _Queue.Insert(insertIndex, character); + } + _HardnessTimes[character] = hardnessTime; + } + + public void CalculateInitialOrder(List characters) + { + // 排序时,时间会流逝 + int nowTime = 1; + + // 初始排序:按速度排序 + List> groupedBySpeed = [.. characters + .GroupBy(c => c.SPD) + .OrderByDescending(g => g.Key)]; + + Random random = new(); + + foreach (IGrouping group in groupedBySpeed) + { + if (group.Count() == 1) + { + // 如果只有一个角色,直接加入队列 + AddCharacter(group.First(), _Queue.Count + nowTime); + } + else + { + // 如果有多个角色,进行先行决定 + List sortedList = [.. group]; + + while (sortedList.Count > 0) + { + Character? selectedCharacter = null; + bool decided = false; + if (sortedList.Count == 1) + { + selectedCharacter = sortedList[0]; + decided = true; + } + + while (!decided) + { + // 每个角色进行两次随机数抽取 + var randomNumbers = sortedList.Select(c => new + { + Character = c, + FirstRoll = random.Next(1, 21), + SecondRoll = random.Next(1, 21) + }).ToList(); + + randomNumbers.ForEach(a => Console.WriteLine(a.Character.Name + ": " + a.FirstRoll + " / " + a.SecondRoll)); + + nowTime++; + + // 找到两次都大于其他角色的角色 + int maxFirstRoll = randomNumbers.Max(r => r.FirstRoll); + int maxSecondRoll = randomNumbers.Max(r => r.SecondRoll); + + var candidates = randomNumbers + .Where(r => r.FirstRoll == maxFirstRoll && r.SecondRoll == maxSecondRoll) + .ToList(); + + if (candidates.Count == 1) + { + selectedCharacter = candidates.First().Character; + decided = true; + } + } + + // 将决定好的角色加入顺序表 + if (selectedCharacter != null) + { + AddCharacter(selectedCharacter, _Queue.Count + nowTime); + Console.WriteLine("decided: " + selectedCharacter.Name + "\r\n"); + sortedList.Remove(selectedCharacter); + } + } + } + } + } + + public Character? NextCharacter() + { + if (_Queue.Count == 0) return null; + + // 硬直时间为0的角色将执行行动 + Character? character = _Queue.FirstOrDefault(c => _HardnessTimes[c] == 0); + if (character != null) + { + _Queue.Remove(character); + return character; + } + + return null; + } + + public void ProcessTurn(Character character) + { + double baseTime = 15; // 假设基础硬直时间为15 + if (character.Name == "A") + { + baseTime = 10; // A的硬直 + } + double newHardnessTime = Math.Round(baseTime * (1 - character.ActionCoefficient), 2, MidpointRounding.AwayFromZero); + + AddCharacter(character, newHardnessTime); + } + + public void ReduceHardnessTimes() + { + if (_Queue.Count == 0) return; + + // 获取第一个角色的硬直时间 + double timeToReduce = _HardnessTimes[_Queue[0]]; + + Console.WriteLine("Time Lapse: " + timeToReduce); + + // 减少所有角色的硬直时间 + foreach (Character character in _Queue) + { + _HardnessTimes[character] = Math.Round(_HardnessTimes[character] - timeToReduce, 2, MidpointRounding.AwayFromZero); + + // 回血回蓝 + double douHP = Math.Round(character.HR * timeToReduce, 2, MidpointRounding.AwayFromZero); + double douMP = Math.Round(character.MR * timeToReduce, 2, MidpointRounding.AwayFromZero); + Console.WriteLine("角色 " + character.Name + " 回血:" + douHP + " / "+ "回蓝:" + douMP); + } + Console.WriteLine(); + } + + public void DisplayQueue() + { + Console.WriteLine("Current ActionQueue:"); + foreach (var character in _Queue) + { + Console.WriteLine($"{character.Name}: Hardness Time {_HardnessTimes[character]}"); + } + } +} diff --git a/Library/Solutions/MyPlugin.cs b/Library/Solutions/MyPlugin.cs index 3f1545d..8697a14 100644 --- a/Library/Solutions/MyPlugin.cs +++ b/Library/Solutions/MyPlugin.cs @@ -7,7 +7,7 @@ namespace FunGame.Testing.Solutions { public class MyPlugin : Plugin, ILoginEvent, IConnectEvent, IIntoRoomEvent { - public override string Name => "测试插件"; + public override string Name => "milimoe.fungame.testplugin"; public override string Description => "My First Plugin"; diff --git a/Library/Solutions/Person.cs b/Library/Solutions/Person.cs index 889ddae..1372f51 100644 --- a/Library/Solutions/Person.cs +++ b/Library/Solutions/Person.cs @@ -19,7 +19,12 @@ namespace ConverterExample public class AddressConverter : BaseEntityConverter
{ - public override void ReadPropertyName(ref Utf8JsonReader reader, string propertyName, JsonSerializerOptions options, ref Address? result) + public override Address NewInstance() + { + return new(); + } + + public override void ReadPropertyName(ref Utf8JsonReader reader, string propertyName, JsonSerializerOptions options, ref Address result) { result ??= new(); switch (propertyName) @@ -47,7 +52,12 @@ namespace ConverterExample public class PersonConverter : BaseEntityConverter { - public override void ReadPropertyName(ref Utf8JsonReader reader, string propertyName, JsonSerializerOptions options, ref Person? result) + public override Person NewInstance() + { + return new(); + } + + public override void ReadPropertyName(ref Utf8JsonReader reader, string propertyName, JsonSerializerOptions options, ref Person result) { result ??= new(); switch (propertyName) diff --git a/Library/Solutions/TestModule.cs b/Library/Solutions/TestModule.cs new file mode 100644 index 0000000..9b20b69 --- /dev/null +++ b/Library/Solutions/TestModule.cs @@ -0,0 +1,84 @@ +using Milimoe.FunGame.Core.Api.Utility; +using Milimoe.FunGame.Core.Entity; +using Milimoe.FunGame.Core.Library.Common.Addon; +using Milimoe.FunGame.Core.Library.Constant; + +namespace Addons +{ + public class ExampleGameModuleConstant + { + public const string Example = "fungame.example.gamemode"; + public const string ExampleCharacter = "fungame.example.character"; + public const string ExampleSkill = "fungame.example.skill"; + public const string ExampleItem = "fungame.example.item"; + } + + public class ExampleCharacterModule : CharacterModule + { + public override string Name => ExampleGameModuleConstant.ExampleCharacter; + + public override string Description => "My First CharacterModule"; + + public override string Version => "1.0.0"; + + public override string Author => "FunGamer"; + + public override List Characters + { + get + { + EntityModuleConfig config = new(ExampleGameModuleConstant.Example, ExampleGameModuleConstant.ExampleCharacter); + config.LoadConfig(); + return [.. config.Values]; + } + } + } + + public class ExampleSkillModule : SkillModule + { + public override string Name => ExampleGameModuleConstant.ExampleSkill; + + public override string Description => "My First SkillModule"; + + public override string Version => "1.0.0"; + + public override string Author => "FunGamer"; + + public override List Skills + { + get + { + List list = []; + Skill s = Factory.GetSkill(); + s.Name = "Example Skill"; + s.MagicType = MagicType.PurityNatural; + list.Add(s); + return list; + } + } + } + + public class ExampleItemModule : ItemModule + { + public override string Name => ExampleGameModuleConstant.ExampleItem; + + public override string Description => "My First ItemModule"; + + public override string Version => "1.0.0"; + + public override string Author => "FunGamer"; + + public override List Items + { + get + { + List list = []; + Item i = Factory.GetItem(); + i.Name = "Example Item"; + i.Price = 20; + list.Add(i); + return list; + } + } + } +} diff --git a/Library/Solutions/TestPlugin.cs b/Library/Solutions/TestPlugin.cs new file mode 100644 index 0000000..f9f20cb --- /dev/null +++ b/Library/Solutions/TestPlugin.cs @@ -0,0 +1,112 @@ +using Milimoe.FunGame.Core.Api.Utility; +using Milimoe.FunGame.Core.Entity; +using Milimoe.FunGame.Core.Interface; +using Milimoe.FunGame.Core.Library.Common.Addon; +using Milimoe.FunGame.Core.Library.Common.Event; +using Milimoe.FunGame.Core.Library.Constant; + +namespace Addons +{ + public class TestPlugin : Plugin, ILoginEvent + { + public override string Name => "fungame.example.plugin"; + + public override string Description => "My First Plugin"; + + public override string Version => "1.0.0"; + + public override string Author => "FunGamer"; + + protected override bool BeforeLoad() + { + EntityModuleConfig config = new(ExampleGameModuleConstant.Example, ExampleGameModuleConstant.ExampleCharacter); + // 构建一个你想要的角色 + Character c = Factory.GetCharacter(); + c.Name = "Oshima"; + c.FirstName = "Shiya"; + c.NickName = "OSM"; + c.MagicType = MagicType.PurityNatural; + c.BaseHP = 30; + c.BaseSTR = 20; + c.BaseAGI = 10; + c.BaseINT = 5; + c.BaseATK = 100; + c.BaseDEF = 10; + c.SPD = 250; + c.Init(); + config.Add("OSM", c); + c = Factory.GetCharacter(); + c.Name = "A"; + c.FirstName = "测试1"; + c.NickName = "A"; + c.MagicType = MagicType.Particle; + c.BaseHP = 25; + c.BaseSTR = 15; + c.BaseAGI = 5; + c.BaseINT = 10; + c.BaseATK = 80; + c.BaseDEF = 15; + c.SPD = 290; + c.Init(); + config.Add("A", c); + c = Factory.GetCharacter(); + c.Name = "B"; + c.FirstName = "测试2"; + c.NickName = "B"; + c.MagicType = MagicType.Fleabane; + c.BaseHP = 355; + c.BaseSTR = 5; + c.BaseAGI = 5; + c.BaseINT = 25; + c.BaseATK = 75; + c.BaseDEF = 20; + c.SPD = 320; + c.Init(); + config.Add("B", c); + c = Factory.GetCharacter(); + c.Name = "C"; + c.FirstName = "测试3"; + c.NickName = "B的复制人"; + c.MagicType = MagicType.Fleabane; + c.BaseHP = 355; + c.BaseSTR = 5; + c.BaseAGI = 5; + c.BaseINT = 25; + c.BaseATK = 75; + c.BaseDEF = 20; + c.SPD = 320; + c.Init(); + config.Add("C", c); + config.SaveConfig(); + PluginConfig config2 = new(Name, "config") + { + { "flush", 10000 }, + { "oshima", "呵呵了" } + }; + config2.SaveConfig(); + return true; + } + + public void AfterLoginEvent(object sender, LoginEventArgs e) + { + Console.WriteLine("after"); + } + + public void BeforeLoginEvent(object sender, LoginEventArgs e) + { + Console.WriteLine("before"); + // 如果这里设置Cancel = true,将终止登录 + e.Cancel = true; + } + + public void FailedLoginEvent(object sender, LoginEventArgs e) + { + Console.WriteLine("failed"); + } + + public void SucceedLoginEvent(object sender, LoginEventArgs e) + { + Console.WriteLine("succeed"); + } + } +} diff --git a/Library/Tests/CheckDLL.cs b/Library/Tests/CheckDLL.cs new file mode 100644 index 0000000..419f02f --- /dev/null +++ b/Library/Tests/CheckDLL.cs @@ -0,0 +1,66 @@ +using Milimoe.FunGame.Core.Api.Utility; +using Milimoe.FunGame.Core.Entity; +using Milimoe.FunGame.Core.Library.Common.Addon; +using Milimoe.FunGame.Core.Library.Common.Event; +using Milimoe.FunGame.Core.Library.Constant; + +namespace Milimoe.FunGame.Testing.Tests +{ + internal class CheckDLL + { + internal CheckDLL() + { + PluginLoader plugins = PluginLoader.LoadPlugins([]); + foreach (string plugin in plugins.Plugins.Keys) + { + Console.WriteLine(plugin + " is loaded."); + } + + Dictionary plugindllsha512 = []; + foreach (string pfp in PluginLoader.PluginFilePaths.Keys) + { + string text = Encryption.FileSha512(PluginLoader.PluginFilePaths[pfp]); + plugindllsha512.Add(pfp, text); + Console.WriteLine(pfp + $" is {text}."); + } + + LoginEventArgs e = new(); + plugins.OnBeforeLoginEvent(plugins, e); + if (!e.Cancel) + { + plugins.OnSucceedLoginEvent(plugins, e); + plugins.OnFailedLoginEvent(plugins, e); + } + plugins.OnAfterLoginEvent(plugins, e); + + List list = []; + + GameModuleLoader modules = GameModuleLoader.LoadGameModules(FunGameInfo.FunGame.FunGame_Desktop, []); + foreach (CharacterModule cm in modules.Characters.Values) + { + foreach (Character c in cm.Characters) + { + Console.WriteLine(c.Name); + list.Add(c); + } + } + + Dictionary moduledllsha512 = []; + foreach (string mfp in GameModuleLoader.ModuleFilePaths.Keys) + { + string text = Encryption.FileSha512(GameModuleLoader.ModuleFilePaths[mfp]); + moduledllsha512.Add(mfp, text); + Console.WriteLine(mfp + $" is {text}."); + } + + foreach (string moduledll in moduledllsha512.Keys) + { + string server = moduledllsha512[moduledll]; + if (plugindllsha512.TryGetValue(moduledll, out string? client) && client != "" && server == client) + { + Console.WriteLine(moduledll + $" is checked pass."); + } + } + } + } +} diff --git a/Library/Tests/Old.cs b/Library/Tests/Old.cs new file mode 100644 index 0000000..9b1a425 --- /dev/null +++ b/Library/Tests/Old.cs @@ -0,0 +1,83 @@ +using System.Collections; +using System.Data; +using ConverterExample; +using Milimoe.FunGame.Core.Api.Utility; +using Milimoe.FunGame.Core.Entity; +using Milimoe.FunGame.Core.Library.Common.JsonConverter; + +namespace Milimoe.FunGame.Testing.Tests +{ + internal class Old + { + internal Old() + { + DataSet ds = new(); + DataTable table = new("SampleTable1"); + table.Columns.Add("Id", typeof(int)); + table.Columns.Add("Name", typeof(string)); + table.Columns.Add("Age", typeof(int)); + table.Rows.Add(1, "John", 30); + table.Rows.Add(2, "Jane", 25); + table.Rows.Add(3, "Bob", 40); + ds.Tables.Add(table); + + table = new("SampleTable2"); + table.Columns.Add("Id", typeof(int)); + table.Columns.Add("Name", typeof(string)); + table.Columns.Add("Age", typeof(int)); + table.Rows.Add(1, "John", 30); + table.Rows.Add(2, "Jane", 25); + table.Rows.Add(3, "Bob", 40); + ds.Tables.Add(table); + + JsonTool JsonTool = new(); + JsonTool.AddConverters(new System.Text.Json.Serialization.JsonConverter[] { new UserConverter(), new RoomConverter(), new PersonConverter(), new AddressConverter() }); + + Room r = Factory.GetRoom(1294367, "w5rtvh8".ToUpper(), DateTime.Now, Factory.GetUser(), Milimoe.FunGame.Core.Library.Constant.RoomType.Mix, "", "", Milimoe.FunGame.Core.Library.Constant.RoomState.Created); + User u = Factory.GetUser(1, "LUOLI", DateTime.Now, DateTime.Now, "LUOLI@66.COM", "QWQAQW"); + + Hashtable hashtable = new() + { + { "table", table }, + { "room", r }, + { "user", u } + }; + + string json = JsonTool.GetString(hashtable); + + Hashtable hashtable2 = JsonTool.GetObject(json) ?? new(); + + DataTable table2 = JsonTool.GetObject(json) ?? new(); + User u2 = JsonTool.GetObject(hashtable2, "user") ?? Factory.GetUser(); + Room r2 = JsonTool.GetObject(hashtable2, "room") ?? Factory.GetRoom(); + + table2.AsEnumerable().ToList().ForEach(row => + { + Console.WriteLine("Id: " + row["Id"] + ", Name: " + row["Name"] + ", Age: " + row["Age"]); + }); + + Console.WriteLine(u2.Username + " 进入了 " + r2.Roomid + " 房间"); + + Person p = new() + { + Age = (int)r2.Id, + Name = u2.Username, + Address = new() + { + State = "呵呵州(Hehe State)", + City = "哈哈市(Haha City)" + } + }; + + json = JsonTool.GetString(p); + + Person p2 = JsonTool.GetObject(json) ?? new(); + + Console.WriteLine("My name is " + p2.Name + ", I am " + p2.Age + "-year-old. I live at " + p2.Address.State + " " + p2.Address.City); + Console.WriteLine("摆烂了86"); + + // 生成一对公钥秘钥 + //TwoFactorAuthenticator.CreateSecretKey(); + } + } +}