From 7efd9f7277e1671bc335f4e2010215e48b4cee85 Mon Sep 17 00:00:00 2001 From: WhiteFang Date: Sat, 30 Jul 2022 20:46:45 +0200 Subject: [PATCH] Added a VModCharacter and refactored code to make use of it; [PvPLeaderboard] Added the option to get the pvpstats of another player; Fixed an issue with character retrieval by character name --- BloodRefill/BloodRefill.csproj | 1 + ExperimentalMod/ExperimentalMod.csproj | 1 + PvPLeaderboard/PvPLeaderboard.csproj | 2 + PvPLeaderboard/Shared/CommandExtensions.cs | 38 ---------- .../Systems/PvPLeaderboardSystem.cs | 24 ++++-- PvPPunishment/PvPPunishment.csproj | 1 + PvPPunishment/Systems/PvPPunishmentSystem.cs | 18 ++--- Shared/CommandSystem/CommandExtensions.cs | 17 +++-- .../HighestGearScoreSystem.cs | 21 +++-- Shared/Utils.cs | 31 ++------ Shared/VModCharacter.cs | 76 +++++++++++++++++++ 11 files changed, 135 insertions(+), 95 deletions(-) delete mode 100644 PvPLeaderboard/Shared/CommandExtensions.cs create mode 100644 Shared/VModCharacter.cs diff --git a/BloodRefill/BloodRefill.csproj b/BloodRefill/BloodRefill.csproj index 989b97b..f20fdba 100644 --- a/BloodRefill/BloodRefill.csproj +++ b/BloodRefill/BloodRefill.csproj @@ -23,6 +23,7 @@ + diff --git a/ExperimentalMod/ExperimentalMod.csproj b/ExperimentalMod/ExperimentalMod.csproj index 84646f9..6226e62 100644 --- a/ExperimentalMod/ExperimentalMod.csproj +++ b/ExperimentalMod/ExperimentalMod.csproj @@ -23,6 +23,7 @@ + diff --git a/PvPLeaderboard/PvPLeaderboard.csproj b/PvPLeaderboard/PvPLeaderboard.csproj index 9d4cb71..f77c3c1 100644 --- a/PvPLeaderboard/PvPLeaderboard.csproj +++ b/PvPLeaderboard/PvPLeaderboard.csproj @@ -20,6 +20,7 @@ + @@ -29,6 +30,7 @@ + diff --git a/PvPLeaderboard/Shared/CommandExtensions.cs b/PvPLeaderboard/Shared/CommandExtensions.cs deleted file mode 100644 index 1e8424e..0000000 --- a/PvPLeaderboard/Shared/CommandExtensions.cs +++ /dev/null @@ -1,38 +0,0 @@ -using ProjectM.Network; -using Unity.Entities; -using Wetstone.API; - -namespace VMods.Shared -{ - public static class CommandExtensions - { - public static (string searchUsername, FromCharacter? fromCharacter) GetFromCharacter(this Command command, int argIdx = 0, bool sendCannotBeFoundMessage = true, EntityManager? entityManager = null) - { - FromCharacter? fromCharacter; - string searchUsername; - - entityManager ??= Utils.CurrentWorld.EntityManager; - - if(argIdx >= 0 && command.Args.Length >= (argIdx + 1)) - { - searchUsername = command.Args[0]; - fromCharacter = Utils.GetFromCharacter(searchUsername, entityManager); - } - else - { - searchUsername = command.User.CharacterName.ToString(); - fromCharacter = new FromCharacter() - { - User = command.SenderUserEntity, - Character = command.SenderCharEntity, - }; - } - - if(sendCannotBeFoundMessage && !fromCharacter.HasValue) - { - command.User.SendSystemMessage($"Vampire {searchUsername} couldn't be found."); - } - return (searchUsername, fromCharacter); - } - } -} diff --git a/PvPLeaderboard/Systems/PvPLeaderboardSystem.cs b/PvPLeaderboard/Systems/PvPLeaderboardSystem.cs index 430ee13..b54b640 100644 --- a/PvPLeaderboard/Systems/PvPLeaderboardSystem.cs +++ b/PvPLeaderboard/Systems/PvPLeaderboardSystem.cs @@ -24,6 +24,12 @@ public static class PvPLeaderboardSystem #endregion + #region Properties + + private static IEnumerable> PvPLeaderboard => _pvpStats.OrderByDescending(x => x.Value.KDRatio).ThenByDescending(x => x.Value.Kills).ThenBy(x => x.Value.Deaths); + + #endregion + #region Public Methods public static void Initialize() @@ -117,13 +123,19 @@ private static void OnVampireDowned(Entity killer, Entity victim) [Command("pvpstats,pvp", "pvpstats", "Shows your current pvp stats (kills, deaths & K/D ratio).")] private static void OnPvPStatsCommand(Command command) { - var user = command.User; - if(!_pvpStats.TryGetValue(user.PlatformId, out var pvpStats)) + var entityManager = VWorld.Server.EntityManager; + (var searchUsername, var vmodCharacter) = command.FindVModCharacter(entityManager: entityManager); + + if(vmodCharacter.HasValue) { - pvpStats = new PvPStats(); - _pvpStats[user.PlatformId] = pvpStats; + var user = vmodCharacter.Value.User; + if(!_pvpStats.TryGetValue(user.PlatformId, out var pvpStats)) + { + pvpStats = new PvPStats(); + _pvpStats[user.PlatformId] = pvpStats; + } + command.User.SendSystemMessage($"{searchUsername} K/D: {pvpStats.KDRatio} [{pvpStats.Kills}/{pvpStats.Deaths}] - Rank {PvPLeaderboard.ToList().FindIndex(x => x.Key == user.PlatformId) + 1}"); } - user.SendSystemMessage($"{user.CharacterName} K/D: {pvpStats.KDRatio} [{pvpStats.Kills}/{pvpStats.Deaths}]"); command.Use(); } @@ -143,7 +155,7 @@ private static void OnPvPLeaderboardCommand(Command command) var user = command.User; var entityManager = VWorld.Server.EntityManager; - var leaderboard = _pvpStats.OrderByDescending(x => x.Value.KDRatio).ThenByDescending(x => x.Value.Kills).ThenBy(x => x.Value.Deaths).Skip(page * recordsPerPage).Take(recordsPerPage); + var leaderboard = PvPLeaderboard.Skip(page * recordsPerPage).Take(recordsPerPage); user.SendSystemMessage("========== PvP Leaderboard =========="); int rank = (page * recordsPerPage) + 1; foreach((var platformId, var pvpStats) in leaderboard) diff --git a/PvPPunishment/PvPPunishment.csproj b/PvPPunishment/PvPPunishment.csproj index f65af41..f772a7b 100644 --- a/PvPPunishment/PvPPunishment.csproj +++ b/PvPPunishment/PvPPunishment.csproj @@ -30,6 +30,7 @@ + diff --git a/PvPPunishment/Systems/PvPPunishmentSystem.cs b/PvPPunishment/Systems/PvPPunishmentSystem.cs index 425bbc0..74cf0ff 100644 --- a/PvPPunishment/Systems/PvPPunishmentSystem.cs +++ b/PvPPunishment/Systems/PvPPunishmentSystem.cs @@ -220,11 +220,11 @@ private static void PruneOffenses() private static void OnIsPunishedPlayerCommand(Command command) { var entityManager = VWorld.Server.EntityManager; - (var searchUsername, var fromCharacter) = command.GetFromCharacter(entityManager: entityManager); + (var searchUsername, var vmodCharacter) = command.FindVModCharacter(entityManager: entityManager); - if(fromCharacter.HasValue) + if(vmodCharacter.HasValue) { - if(BuffUtility.HasBuff(entityManager, fromCharacter.Value.Character, Utils.SevereGarlicDebuff)) + if(vmodCharacter.Value.HasBuff(Utils.SevereGarlicDebuff, entityManager)) { command.User.SendSystemMessage($"Vampire {searchUsername} is currently punished."); } @@ -240,11 +240,11 @@ private static void OnIsPunishedPlayerCommand(Command command) private static void OnPunishPlayerCommand(Command command) { var entityManager = VWorld.Server.EntityManager; - (var searchUsername, var fromCharacter) = command.GetFromCharacter(entityManager: entityManager); + (var searchUsername, var vmodCharacter) = command.FindVModCharacter(entityManager: entityManager); - if(fromCharacter.HasValue) + if(vmodCharacter.HasValue) { - Utils.ApplyBuff(fromCharacter.Value, Utils.SevereGarlicDebuff); + vmodCharacter.Value.ApplyBuff(Utils.SevereGarlicDebuff); command.User.SendSystemMessage($"Vampire {searchUsername} has been punished."); } command.Use(); @@ -254,11 +254,11 @@ private static void OnPunishPlayerCommand(Command command) private static void OnUnPunishPlayerCommand(Command command) { var entityManager = VWorld.Server.EntityManager; - (var searchUsername, var fromCharacter) = command.GetFromCharacter(entityManager: entityManager); + (var searchUsername, var vmodCharacter) = command.FindVModCharacter(entityManager: entityManager); - if(fromCharacter.HasValue) + if(vmodCharacter.HasValue) { - Utils.RemoveBuff(fromCharacter.Value, Utils.SevereGarlicDebuff); + vmodCharacter.Value.RemoveBuff(Utils.SevereGarlicDebuff); command.User.SendSystemMessage($"Vampire {searchUsername} has been un-punished."); } command.Use(); diff --git a/Shared/CommandSystem/CommandExtensions.cs b/Shared/CommandSystem/CommandExtensions.cs index 1d5126a..54c4c60 100644 --- a/Shared/CommandSystem/CommandExtensions.cs +++ b/Shared/CommandSystem/CommandExtensions.cs @@ -6,9 +6,9 @@ namespace VMods.Shared { public static class CommandExtensions { - public static (string searchUsername, FromCharacter? fromCharacter) GetFromCharacter(this Command command, int argIdx = 0, bool sendCannotBeFoundMessage = true, EntityManager? entityManager = null) + public static (string searchUsername, VModCharacter? vmodCharacter) FindVModCharacter(this Command command, int argIdx = 0, bool sendCannotBeFoundMessage = true, EntityManager? entityManager = null) { - FromCharacter? fromCharacter; + VModCharacter? fromCharacter; string searchUsername; entityManager ??= Utils.CurrentWorld.EntityManager; @@ -16,16 +16,12 @@ public static (string searchUsername, FromCharacter? fromCharacter) GetFromChara if(argIdx >= 0 && command.Args.Length >= (argIdx + 1)) { searchUsername = command.Args[0]; - fromCharacter = Utils.GetFromCharacter(searchUsername, entityManager); + fromCharacter = VModCharacter.GetVModCharacter(searchUsername, entityManager); } else { searchUsername = command.User.CharacterName.ToString(); - fromCharacter = new FromCharacter() - { - User = command.SenderUserEntity, - Character = command.SenderCharEntity, - }; + fromCharacter = command.ToVModCharacter(entityManager); } if(sendCannotBeFoundMessage && !fromCharacter.HasValue) @@ -34,5 +30,10 @@ public static (string searchUsername, FromCharacter? fromCharacter) GetFromChara } return (searchUsername, fromCharacter); } + + public static VModCharacter ToVModCharacter(this Command command, EntityManager? entityManager = null) + { + return new VModCharacter(command.SenderUserEntity, command.SenderCharEntity, entityManager); + } } } diff --git a/Shared/HighestGearScoreSystem/HighestGearScoreSystem.cs b/Shared/HighestGearScoreSystem/HighestGearScoreSystem.cs index 0064212..8914242 100644 --- a/Shared/HighestGearScoreSystem/HighestGearScoreSystem.cs +++ b/Shared/HighestGearScoreSystem/HighestGearScoreSystem.cs @@ -69,6 +69,11 @@ public static float GetCurrentGearScore(FromCharacter fromCharacter, EntityManag return GetCurrentGearScore(fromCharacter.Character, entityManager); } + public static float GetCurrentGearScore(VModCharacter vmodCharacter, EntityManager entityManager) + { + return GetCurrentGearScore(vmodCharacter.FromCharacter.Character, entityManager); + } + public static float GetCurrentGearScore(Entity characterEntity, EntityManager entityManager) { var equipment = entityManager.GetComponentData(characterEntity); @@ -139,19 +144,19 @@ private static void OnVampireDowned(Entity killer, Entity victim) private static void OnHighestGearScoreCommand(Command command) { var entityManager = VWorld.Server.EntityManager; - (var searchUsername, var fromCharacter) = command.GetFromCharacter(entityManager: entityManager); + (var searchUsername, var vmodCharacter) = command.FindVModCharacter(entityManager: entityManager); - if(fromCharacter.HasValue) + if(vmodCharacter.HasValue) { - var user = entityManager.GetComponentData(fromCharacter.Value.User); + var user = vmodCharacter.Value.User; if(_gearScoreData.TryGetValue(user.PlatformId, out var gearScoreData)) { TimeSpan diff = DateTime.UtcNow.Subtract(gearScoreData.LastUpdated); - command.User.SendSystemMessage($"[{Utils.PluginName}] The Highest Gear Score for {searchUsername} (Lv: {GetCurrentGearScore(fromCharacter.Value, entityManager)}) was {gearScoreData.HighestGearScore} (Last updated {diff.ToAgoString()} ago)."); + command.User.SendSystemMessage($"[{Utils.PluginName}] The Highest Gear Score for {searchUsername} (Lv: {GetCurrentGearScore(vmodCharacter.Value, entityManager)}) was {gearScoreData.HighestGearScore} (Last updated {diff.ToAgoString()} ago)."); } else { - command.User.SendSystemMessage($"[{Utils.PluginName}] No Highest Gear Score is recorded for {searchUsername} (Lv: {GetCurrentGearScore(fromCharacter.Value, entityManager)})."); + command.User.SendSystemMessage($"[{Utils.PluginName}] No Highest Gear Score is recorded for {searchUsername} (Lv: {GetCurrentGearScore(vmodCharacter.Value, entityManager)})."); } } } @@ -160,11 +165,11 @@ private static void OnHighestGearScoreCommand(Command command) private static void OnResetHighestGearScoreCommand(Command command) { var entityManager = VWorld.Server.EntityManager; - (var searchUsername, var fromCharacter) = command.GetFromCharacter(entityManager: entityManager); + (var searchUsername, var vmodCharacter) = command.FindVModCharacter(entityManager: entityManager); - if(fromCharacter.HasValue) + if(vmodCharacter.HasValue) { - var user = entityManager.GetComponentData(fromCharacter.Value.User); + var user = vmodCharacter.Value.User; _gearScoreData.Remove(user.PlatformId); command.User.SendSystemMessage($"[{Utils.PluginName}] Removed the Highest Gear Score record for {searchUsername}."); } diff --git a/Shared/Utils.cs b/Shared/Utils.cs index 9b9a159..7599f9e 100644 --- a/Shared/Utils.cs +++ b/Shared/Utils.cs @@ -248,13 +248,13 @@ public static void RemoveBuff(FromCharacter fromCharacter, PrefabGUID buffGUID) RemoveBuff(fromCharacter.Character, buffGUID); } - public static void RemoveBuff(Entity charEntity, PrefabGUID buffGUID) + public static void RemoveBuff(Entity charEntity, PrefabGUID buffGUID, EntityManager? entityManager = null) { - var entityManager = CurrentWorld.EntityManager; - if(BuffUtility.HasBuff(entityManager, charEntity, buffGUID)) + entityManager ??= CurrentWorld.EntityManager; + if(BuffUtility.HasBuff(entityManager.Value, charEntity, buffGUID)) { - BuffUtility.TryGetBuff(entityManager, charEntity, buffGUID, out var buffEntity); - entityManager.AddComponent(buffEntity); + BuffUtility.TryGetBuff(entityManager.Value, charEntity, buffGUID, out var buffEntity); + entityManager.Value.AddComponent(buffEntity); } } @@ -273,27 +273,6 @@ public static string GetCharacterName(ulong platformId, EntityManager? entityMan return null; } - public static FromCharacter? GetFromCharacter(string charactername, EntityManager? entityManager = null) - { - entityManager ??= CurrentWorld.EntityManager; - var characters = entityManager.Value.CreateEntityQuery(ComponentType.ReadOnly()).ToEntityArray(Allocator.Temp); - foreach(var charEntity in characters) - { - var playerCharacter = entityManager.Value.GetComponentData(charEntity); - var userEntity = playerCharacter.UserEntity._Entity; - var userData = entityManager.Value.GetComponentData(userEntity); - if(userData.CharacterName.ToString() == charactername) - { - return new FromCharacter() - { - User = userEntity, - Character = charEntity, - }; - } - } - return null; - } - public static void LogAllComponentTypes(Entity entity, EntityManager? entityManager = null) { if(entity == Entity.Null) diff --git a/Shared/VModCharacter.cs b/Shared/VModCharacter.cs new file mode 100644 index 0000000..866e6f0 --- /dev/null +++ b/Shared/VModCharacter.cs @@ -0,0 +1,76 @@ +using ProjectM; +using ProjectM.Network; +using Unity.Collections; +using Unity.Entities; + +namespace VMods.Shared +{ + public readonly struct VModCharacter + { + #region Variables + + public readonly User User; + public readonly PlayerCharacter Character; + public readonly FromCharacter FromCharacter; + + #endregion + + #region Lifecycle + + public VModCharacter(User user, PlayerCharacter character) => (User, Character, FromCharacter) = (user, character, FromCharacter = new FromCharacter() + { + User = character.UserEntity._Entity, + Character = user.LocalCharacter._Entity, + }); + + public VModCharacter(Entity userEntity, Entity charEntity, EntityManager? entityManager = null) + { + entityManager ??= Utils.CurrentWorld.EntityManager; + User = entityManager.Value.GetComponentData(userEntity); + Character = entityManager.Value.GetComponentData(charEntity); + FromCharacter = new FromCharacter() + { + User = Character.UserEntity._Entity, + Character = User.LocalCharacter._Entity, + }; + } + + #endregion + + #region Public Methods + + public static VModCharacter? GetVModCharacter(string charactername, EntityManager? entityManager = null) + { + entityManager ??= Utils.CurrentWorld.EntityManager; + var users = entityManager.Value.CreateEntityQuery(ComponentType.ReadOnly()).ToEntityArray(Allocator.Temp); + foreach(var userEntity in users) + { + var userData = entityManager.Value.GetComponentData(userEntity); + var playerCharacter = entityManager.Value.GetComponentData(userData.LocalCharacter._Entity); + if(userData.CharacterName.ToString() == charactername) + { + return new VModCharacter(userData, playerCharacter); + } + } + return null; + } + + public void ApplyBuff(PrefabGUID buffGUID) + { + Utils.ApplyBuff(FromCharacter, buffGUID); + } + + public void RemoveBuff(PrefabGUID buffGUID) + { + Utils.RemoveBuff(FromCharacter.Character, buffGUID); + } + + public bool HasBuff(PrefabGUID buffGUID, EntityManager? entityManager = null) + { + entityManager ??= Utils.CurrentWorld.EntityManager; + return BuffUtility.HasBuff(entityManager.Value, FromCharacter.Character, buffGUID); + } + + #endregion + } +}