Skip to content

Commit

Permalink
Added a VModCharacter and refactored code to make use of it; [PvPLead…
Browse files Browse the repository at this point in the history
…erboard] Added the option to get the pvpstats of another player; Fixed an issue with character retrieval by character name
  • Loading branch information
WhiteFang5 committed Jul 30, 2022
1 parent d6f0587 commit 7efd9f7
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 95 deletions.
1 change: 1 addition & 0 deletions BloodRefill/BloodRefill.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<Compile Include="..\Shared\CommandSystem\CommandSystem.cs" Link="Shared\CommandSystem.cs" />
<Compile Include="..\Shared\CommandSystem\CommandSystemConfig.cs" Link="Shared\CommandSystemConfig.cs" />
<Compile Include="..\Shared\Utils.cs" Link="Shared\Utils.cs" />
<Compile Include="..\Shared\VModCharacter.cs" Link="Shared\VModCharacter.cs" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions ExperimentalMod/ExperimentalMod.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<Compile Include="..\Shared\CommandSystem\CommandSystem.cs" Link="Shared\CommandSystem.cs" />
<Compile Include="..\Shared\CommandSystem\CommandSystemConfig.cs" Link="Shared\CommandSystemConfig.cs" />
<Compile Include="..\Shared\Utils.cs" Link="Shared\Utils.cs" />
<Compile Include="..\Shared\VModCharacter.cs" Link="Shared\VModCharacter.cs" />
<Compile Include="..\Shared\VModStorage.cs" Link="Shared\VModStorage.cs" />
</ItemGroup>

Expand Down
2 changes: 2 additions & 0 deletions PvPLeaderboard/PvPLeaderboard.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<Compile Include="..\Shared\BuffSystemHook.cs" Link="Shared\BuffSystemHook.cs" />
<Compile Include="..\Shared\CommandSystem\Command.cs" Link="Shared\Command.cs" />
<Compile Include="..\Shared\CommandSystem\CommandAttribute.cs" Link="Shared\CommandAttribute.cs" />
<Compile Include="..\Shared\CommandSystem\CommandExtensions.cs" Link="Shared\CommandExtensions.cs" />
<Compile Include="..\Shared\CommandSystem\CommandSystem.cs" Link="Shared\CommandSystem.cs" />
<Compile Include="..\Shared\CommandSystem\CommandSystemConfig.cs" Link="Shared\CommandSystemConfig.cs" />
<Compile Include="..\Shared\ExtensionMethods.cs" Link="Shared\ExtensionMethods.cs" />
Expand All @@ -29,6 +30,7 @@
<Compile Include="..\Shared\SaveHook.cs" Link="Shared\SaveHook.cs" />
<Compile Include="..\Shared\Utils.cs" Link="Shared\Utils.cs" />
<Compile Include="..\Shared\VampireDownedHook.cs" Link="Shared\VampireDownedHook.cs" />
<Compile Include="..\Shared\VModCharacter.cs" Link="Shared\VModCharacter.cs" />
<Compile Include="..\Shared\VModStorage.cs" Link="Shared\VModStorage.cs" />
</ItemGroup>

Expand Down
38 changes: 0 additions & 38 deletions PvPLeaderboard/Shared/CommandExtensions.cs

This file was deleted.

24 changes: 18 additions & 6 deletions PvPLeaderboard/Systems/PvPLeaderboardSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ public static class PvPLeaderboardSystem

#endregion

#region Properties

private static IEnumerable<KeyValuePair<ulong, PvPStats>> 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()
Expand Down Expand Up @@ -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($"<color=#ffffff>{searchUsername}</color> 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();
}

Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions PvPPunishment/PvPPunishment.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<Compile Include="..\Shared\SaveHook.cs" Link="Shared\SaveHook.cs" />
<Compile Include="..\Shared\Utils.cs" Link="Shared\Utils.cs" />
<Compile Include="..\Shared\VampireDownedHook.cs" Link="Shared\VampireDownedHook.cs" />
<Compile Include="..\Shared\VModCharacter.cs" Link="Shared\VModCharacter.cs" />
<Compile Include="..\Shared\VModStorage.cs" Link="Shared\VModStorage.cs" />
</ItemGroup>

Expand Down
18 changes: 9 additions & 9 deletions PvPPunishment/Systems/PvPPunishmentSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <color=#ffffff>{searchUsername}</color> <color=#ff0000>is</color> currently punished.");
}
Expand All @@ -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 <color=#ffffff>{searchUsername}</color> has been punished.");
}
command.Use();
Expand All @@ -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 <color=#ffffff>{searchUsername}</color> has been un-punished.");
}
command.Use();
Expand Down
17 changes: 9 additions & 8 deletions Shared/CommandSystem/CommandExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,22 @@ 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;

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)
Expand All @@ -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);
}
}
}
21 changes: 13 additions & 8 deletions Shared/HighestGearScoreSystem/HighestGearScoreSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Equipment>(characterEntity);
Expand Down Expand Up @@ -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<User>(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 <color=#ffffff>{searchUsername}</color> (Lv: {GetCurrentGearScore(fromCharacter.Value, entityManager)}) was <color=#00ff00>{gearScoreData.HighestGearScore}</color> (Last updated {diff.ToAgoString()} ago).");
command.User.SendSystemMessage($"[{Utils.PluginName}] The Highest Gear Score for <color=#ffffff>{searchUsername}</color> (Lv: {GetCurrentGearScore(vmodCharacter.Value, entityManager)}) was <color=#00ff00>{gearScoreData.HighestGearScore}</color> (Last updated {diff.ToAgoString()} ago).");
}
else
{
command.User.SendSystemMessage($"[{Utils.PluginName}] No Highest Gear Score is recorded for <color=#ffffff>{searchUsername}</color> (Lv: {GetCurrentGearScore(fromCharacter.Value, entityManager)}).");
command.User.SendSystemMessage($"[{Utils.PluginName}] No Highest Gear Score is recorded for <color=#ffffff>{searchUsername}</color> (Lv: {GetCurrentGearScore(vmodCharacter.Value, entityManager)}).");
}
}
}
Expand All @@ -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<User>(fromCharacter.Value.User);
var user = vmodCharacter.Value.User;
_gearScoreData.Remove(user.PlatformId);
command.User.SendSystemMessage($"[{Utils.PluginName}] Removed the Highest Gear Score record for <color=#ffffff>{searchUsername}</color>.");
}
Expand Down
31 changes: 5 additions & 26 deletions Shared/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DestroyTag>(buffEntity);
BuffUtility.TryGetBuff(entityManager.Value, charEntity, buffGUID, out var buffEntity);
entityManager.Value.AddComponent<DestroyTag>(buffEntity);
}
}

Expand All @@ -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<PlayerCharacter>()).ToEntityArray(Allocator.Temp);
foreach(var charEntity in characters)
{
var playerCharacter = entityManager.Value.GetComponentData<PlayerCharacter>(charEntity);
var userEntity = playerCharacter.UserEntity._Entity;
var userData = entityManager.Value.GetComponentData<User>(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)
Expand Down
76 changes: 76 additions & 0 deletions Shared/VModCharacter.cs
Original file line number Diff line number Diff line change
@@ -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<User>(userEntity);
Character = entityManager.Value.GetComponentData<PlayerCharacter>(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<User>()).ToEntityArray(Allocator.Temp);
foreach(var userEntity in users)
{
var userData = entityManager.Value.GetComponentData<User>(userEntity);
var playerCharacter = entityManager.Value.GetComponentData<PlayerCharacter>(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
}
}

0 comments on commit 7efd9f7

Please sign in to comment.