Skip to content

Commit

Permalink
Refactored the Command System so it makes use of the VModCharacter
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiteFang5 committed Aug 4, 2022
1 parent ba6ea33 commit 7354f59
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 50 deletions.
12 changes: 6 additions & 6 deletions BloodRefill/Systems/BloodRefillSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ private static void ChangeBloodType(User user, BloodType bloodType, float qualit
[Command("setblood", "setblood <blood-type> <blood-quality> [<gain-amount>]", "Sets your blood type to the specified blood-type and blood-quality, and optionally adds a given amount of blood (in Litres).", true)]
private static void OnSetBloodCommand(Command command)
{
var user = command.User;
var vmodCharacter = command.VModCharacter;
var argCount = command.Args.Length;
if(argCount >= 2)
{
Expand All @@ -206,7 +206,7 @@ private static void OnSetBloodCommand(Command command)
}
else
{
user.SendSystemMessage($"<color=#ff0000>Invalid gain-amount '{searchBloodQuality}'. Should be between -10 and 10</color>");
vmodCharacter.SendSystemMessage($"<color=#ff0000>Invalid gain-amount '{searchBloodQuality}'. Should be between -10 and 10</color>");
}
}
else
Expand All @@ -216,18 +216,18 @@ private static void OnSetBloodCommand(Command command)

if(addBloodAmount.HasValue)
{
ChangeBloodType(user, bloodType, bloodQuality, (int)(addBloodAmount.Value * 10f));
user.SendSystemMessage($"Changed blood type to <color=#ff0000>{bloodQuality}%</color> <color=#ffffff>{searchBloodType}</color> and added <color=#ff0000>{addBloodAmount.Value}L</color>");
ChangeBloodType(vmodCharacter.User, bloodType, bloodQuality, (int)(addBloodAmount.Value * 10f));
vmodCharacter.SendSystemMessage($"Changed blood type to <color=#ff0000>{bloodQuality}%</color> <color=#ffffff>{searchBloodType}</color> and added <color=#ff0000>{addBloodAmount.Value}L</color>");
}
}
else
{
user.SendSystemMessage($"<color=#ff0000>Invalid blood-quality '{searchBloodQuality}'. Should be between 1 and 100</color>");
vmodCharacter.SendSystemMessage($"<color=#ff0000>Invalid blood-quality '{searchBloodQuality}'. Should be between 1 and 100</color>");
}
}
else
{
user.SendSystemMessage($"<color=#ff0000>Invalid blood-type '{searchBloodType}'. Options are: {string.Join(", ", validBloodTypes.Select(x => x.ToString()))}</color>");
vmodCharacter.SendSystemMessage($"<color=#ff0000>Invalid blood-type '{searchBloodType}'. Options are: {string.Join(", ", validBloodTypes.Select(x => x.ToString()))}</color>");
}
}
else
Expand Down
10 changes: 5 additions & 5 deletions PvPLeaderboard/Systems/PvPLeaderboardSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private static void OnPvPStatsCommand(Command command)
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}");
command.VModCharacter.SendSystemMessage($"<color=#ffffff>{searchUsername}</color> K/D: {pvpStats.KDRatio} [{pvpStats.Kills}/{pvpStats.Deaths}] - Rank {PvPLeaderboard.ToList().FindIndex(x => x.Key == user.PlatformId) + 1}");
}
command.Use();
}
Expand All @@ -153,17 +153,17 @@ private static void OnPvPLeaderboardCommand(Command command)
var maxPage = (int)Math.Ceiling(_pvpStats.Count / (double)recordsPerPage);
page = Math.Min(maxPage - 1, page);

var user = command.User;
var vmodCharacter = command.VModCharacter;
var entityManager = VWorld.Server.EntityManager;
var leaderboard = PvPLeaderboard.Skip(page * recordsPerPage).Take(recordsPerPage);
user.SendSystemMessage("========== PvP Leaderboard ==========");
vmodCharacter.SendSystemMessage("========== PvP Leaderboard ==========");
int rank = (page * recordsPerPage) + 1;
foreach((var platformId, var pvpStats) in leaderboard)
{
user.SendSystemMessage($"{rank}. <color=#ffffff>{Utils.GetCharacterName(platformId, entityManager)} : {pvpStats.KDRatio} [{pvpStats.Kills}/{pvpStats.Deaths}]</color>");
vmodCharacter.SendSystemMessage($"{rank}. <color=#ffffff>{Utils.GetCharacterName(platformId, entityManager)} : {pvpStats.KDRatio} [{pvpStats.Kills}/{pvpStats.Deaths}]</color>");
rank++;
}
user.SendSystemMessage($"=============== {page + 1}/{maxPage} ===============");
vmodCharacter.SendSystemMessage($"=============== {page + 1}/{maxPage} ===============");

command.Use();
}
Expand Down
8 changes: 4 additions & 4 deletions PvPPunishment/Systems/PvPPunishmentSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,11 @@ private static void OnIsPunishedPlayerCommand(Command command)
{
if(vmodCharacter.Value.HasBuff(Utils.SevereGarlicDebuff, entityManager))
{
command.User.SendSystemMessage($"Vampire <color=#ffffff>{searchUsername}</color> <color=#ff0000>is</color> currently punished.");
command.VModCharacter.SendSystemMessage($"Vampire <color=#ffffff>{searchUsername}</color> <color=#ff0000>is</color> currently punished.");
}
else
{
command.User.SendSystemMessage($"Vampire <color=#ffffff>{searchUsername}</color> <color=#00ff00>isn't</color> punished.");
command.VModCharacter.SendSystemMessage($"Vampire <color=#ffffff>{searchUsername}</color> <color=#00ff00>isn't</color> punished.");
}
}
command.Use();
Expand All @@ -250,7 +250,7 @@ private static void OnPunishPlayerCommand(Command command)
if(vmodCharacter.HasValue)
{
vmodCharacter.Value.ApplyBuff(Utils.SevereGarlicDebuff);
command.User.SendSystemMessage($"Vampire <color=#ffffff>{searchUsername}</color> has been punished.");
command.VModCharacter.SendSystemMessage($"Vampire <color=#ffffff>{searchUsername}</color> has been punished.");
}
command.Use();
}
Expand All @@ -264,7 +264,7 @@ private static void OnUnPunishPlayerCommand(Command command)
if(vmodCharacter.HasValue)
{
vmodCharacter.Value.RemoveBuff(Utils.SevereGarlicDebuff);
command.User.SendSystemMessage($"Vampire <color=#ffffff>{searchUsername}</color> has been un-punished.");
command.VModCharacter.SendSystemMessage($"Vampire <color=#ffffff>{searchUsername}</color> has been un-punished.");
}
command.Use();
}
Expand Down
13 changes: 4 additions & 9 deletions Shared/CommandSystem/Command.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using ProjectM.Network;
using Unity.Entities;

namespace VMods.Shared
namespace VMods.Shared
{
public class Command
{
Expand All @@ -10,18 +7,16 @@ public class Command
public string Name { get; }
public string[] Args { get; }

public User User { get; }
public Entity SenderUserEntity { get; }
public Entity SenderCharEntity { get; }
public VModCharacter VModCharacter { get; }

public bool Used { get; private set; }

#endregion

#region Lifecycle

public Command(User user, Entity senderUserEntity, Entity senderCharEntity, string name, params string[] args)
=> (User, SenderUserEntity, SenderCharEntity, Name, Args) = (user, senderUserEntity, senderCharEntity, name, args);
public Command(VModCharacter vmodCharacter, string name, params string[] args)
=> (VModCharacter, Name, Args) = (vmodCharacter, name, args);

#endregion

Expand Down
11 changes: 3 additions & 8 deletions Shared/CommandSystem/CommandExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,15 @@ public static (string searchUsername, VModCharacter? vmodCharacter) FindVModChar
}
else
{
searchUsername = command.User.CharacterName.ToString();
fromCharacter = command.ToVModCharacter(entityManager);
searchUsername = command.VModCharacter.User.CharacterName.ToString();
fromCharacter = command.VModCharacter;
}

if(sendCannotBeFoundMessage && !fromCharacter.HasValue)
{
command.User.SendSystemMessage($"[{Utils.PluginName}] Vampire <color=#ffffff>{searchUsername}</color> couldn't be found.");
command.VModCharacter.SendSystemMessage($"[{Utils.PluginName}] Vampire <color=#ffffff>{searchUsername}</color> couldn't be found.");
}
return (searchUsername, fromCharacter);
}

public static VModCharacter ToVModCharacter(this Command command, EntityManager? entityManager = null)
{
return new VModCharacter(command.SenderUserEntity, command.SenderCharEntity, entityManager);
}
}
}
28 changes: 14 additions & 14 deletions Shared/CommandSystem/CommandSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ public static void UnregisterCommand(Action<Command> commandMethod, CommandAttri

public static void SendInvalidCommandMessage(Command command, bool invalidArgument = false)
{
SendInvalidCommandMessage(command.User, invalidArgument);
SendInvalidCommandMessage(command.VModCharacter, invalidArgument);
}

public static void SendInvalidCommandMessage(User user, bool invalidArgument = false)
public static void SendInvalidCommandMessage(VModCharacter vmodCharacter, bool invalidArgument = false)
{
user.SendSystemMessage($"<color=#ff0000>Invalid command{(invalidArgument ? " argument" : string.Empty)}. Check {CommandSystemConfig.CommandSystemPrefix.Value}help [<command>] for more information.</color>");
vmodCharacter.SendSystemMessage($"<color=#ff0000>Invalid command{(invalidArgument ? " argument" : string.Empty)}. Check {CommandSystemConfig.CommandSystemPrefix.Value}help [<command>] for more information.</color>");
}

#endregion
Expand Down Expand Up @@ -115,7 +115,7 @@ private static void OnChatMessage(VChatEvent chatEvent)
if(timeDiff.TotalSeconds < CommandSystemConfig.CommandSystemCommandCooldown.Value)
{
int waitTime = (int)Math.Ceiling(CommandSystemConfig.CommandSystemCommandCooldown.Value - timeDiff.TotalSeconds);
chatEvent.User.SendSystemMessage($"<color=#ff0000>Please wait for {waitTime} second(s) before sending another command.</color>");
user.SendSystemMessage($"<color=#ff0000>Please wait for {waitTime} second(s) before sending another command.</color>");
chatEvent.Cancel();
return;
}
Expand All @@ -124,7 +124,7 @@ private static void OnChatMessage(VChatEvent chatEvent)
}

// Fire the command (so an event handler can actually handle/execute it)
Command command = new(user, chatEvent.SenderUserEntity, chatEvent.SenderCharacterEntity, name, args);
Command command = new(new VModCharacter(chatEvent.SenderUserEntity, chatEvent.SenderCharacterEntity), name, args);

foreach((var method, var attribute) in _commandMethods)
{
Expand Down Expand Up @@ -196,19 +196,19 @@ private static void PruneCommandTimes()
private static void OnHelpCommand(Command command)
{
var commandPrefix = CommandSystemConfig.CommandSystemPrefix.Value;
var user = command.User;
var vmodCharacter = command.VModCharacter;
switch(command.Args.Length)
{
case 0:
{
user.SendSystemMessage($"List of {Utils.PluginName} commands:");
vmodCharacter.SendSystemMessage($"List of {Utils.PluginName} commands:");
_commandMethods.ForEach(x => SendCommandInfo(x.attribute));
_commandReflectionMethods.ForEach(x => SendCommandInfo(x.attribute));

// Nested Method(s)
void SendCommandInfo(CommandAttribute attribute)
{
if(attribute.ReqAdmin && !user.IsAdmin)
if(attribute.ReqAdmin && !vmodCharacter.IsAdmin)
{
return;
}
Expand All @@ -218,7 +218,7 @@ void SendCommandInfo(CommandAttribute attribute)
message += " - <color=#ff0000>[ADMIN]</color>";
}
message += $" - <color=#ffffff>{attribute.Description}</color>";
user.SendSystemMessage(message);
vmodCharacter.SendSystemMessage(message);
}
}
break;
Expand All @@ -239,18 +239,18 @@ void SendCommandInfo(CommandAttribute attribute)
}

// Check the found info
if(attribute == null || attribute.ReqAdmin && !user.IsAdmin)
if(attribute == null || attribute.ReqAdmin && !vmodCharacter.IsAdmin)
{
return;
}

user.SendSystemMessage($"Help for <color=#00ff00>{commandPrefix}{attribute.Names[0]}</color>");
vmodCharacter.SendSystemMessage($"Help for <color=#00ff00>{commandPrefix}{attribute.Names[0]}</color>");
if(attribute.Names.Count > 1)
{
user.SendSystemMessage($"<color=#ffffff>Aliases: {string.Join(", ", attribute.Names.Skip(1).Select(x => $"{commandPrefix}{x}"))}</color>");
vmodCharacter.SendSystemMessage($"<color=#ffffff>Aliases: {string.Join(", ", attribute.Names.Skip(1).Select(x => $"{commandPrefix}{x}"))}</color>");
}
user.SendSystemMessage($"<color=#ffffff>Description: {attribute.Description}</color>");
user.SendSystemMessage($"<color=#ffffff>Usage: {commandPrefix}{attribute.Usage}</color>");
vmodCharacter.SendSystemMessage($"<color=#ffffff>Description: {attribute.Description}</color>");
vmodCharacter.SendSystemMessage($"<color=#ffffff>Usage: {commandPrefix}{attribute.Usage}</color>");
}
return;

Expand Down
6 changes: 3 additions & 3 deletions Shared/HighestGearScoreSystem/HighestGearScoreSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,11 @@ private static void OnHighestGearScoreCommand(Command command)
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(vmodCharacter.Value, entityManager)}) was <color=#00ff00>{gearScoreData.HighestGearScore}</color> (Last updated {diff.ToAgoString()} ago).");
command.VModCharacter.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(vmodCharacter.Value, entityManager)}).");
command.VModCharacter.SendSystemMessage($"[{Utils.PluginName}] No Highest Gear Score is recorded for <color=#ffffff>{searchUsername}</color> (Lv: {GetCurrentGearScore(vmodCharacter.Value, entityManager)}).");
}
}
}
Expand All @@ -171,7 +171,7 @@ private static void OnResetHighestGearScoreCommand(Command command)
{
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>.");
command.VModCharacter.SendSystemMessage($"[{Utils.PluginName}] Removed the Highest Gear Score record for <color=#ffffff>{searchUsername}</color>.");
}
}

Expand Down
12 changes: 12 additions & 0 deletions Shared/VModCharacter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using ProjectM.Network;
using Unity.Collections;
using Unity.Entities;
using Wetstone.API;

namespace VMods.Shared
{
Expand All @@ -15,6 +16,12 @@ public readonly struct VModCharacter

#endregion

#region Properties

public bool IsAdmin => User.IsAdmin;

#endregion

#region Lifecycle

public VModCharacter(User user, PlayerCharacter character) => (User, Character, FromCharacter) = (user, character, FromCharacter = new FromCharacter()
Expand Down Expand Up @@ -91,6 +98,11 @@ public bool HasBuff(PrefabGUID buffGUID, EntityManager? entityManager = null)
return BuffUtility.HasBuff(entityManager.Value, FromCharacter.Character, buffGUID);
}

public void SendSystemMessage(string message)
{
User.SendSystemMessage(message);
}

#endregion
}
}
2 changes: 1 addition & 1 deletion Shared/VModStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public static T Load<T>(string filename, Func<T> getDefaultValue)
private static void OnSaveAllCommand(Command command)
{
SaveAll();
command.User.SendSystemMessage($"VMod Plugin '{Utils.PluginName}' saved successfully.");
command.VModCharacter.SendSystemMessage($"VMod Plugin '{Utils.PluginName}' saved successfully.");
}

#endregion
Expand Down

0 comments on commit 7354f59

Please sign in to comment.