Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #3

Merged
merged 4 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"player_not_found": "%prefix%{red}Could not find a valid target using \"{lightblue}{0}{red}\".",
"player_found_multiple": "%prefix%{red}Found multiple players using \"{lightblue}{0}{red}\".",
"player_invalid": "%prefix%{lightblue}{0} {red}have an unverified Steam ID.",
"out_of_rep": "%prefix%{red}You are out of reputation points to give for this map. {green}Dedicated Supporters{grey} get more per map!",
"out_of_rep_map": "%prefix%{red}You are out of reputation points to give for this map. {green}Dedicated Supporters{grey} get more per map!",
"out_of_rep_period": "%prefix%{red}You gave reputation too recently, try again later. {green}Dedicated Supporters{grey} can give more often!",
"rep_given": "%prefix%{white}You {green}commended {lightred}{0}{white}.",
"rep_taken": "%prefix%{white}You {darkred}denounced {lightred}{0}{white}.",
"rep_status": "%prefix%{lightred}{0} {white}has {yellow}{1} {white}reputation points.",
Expand Down
8 changes: 5 additions & 3 deletions src/api/IEloPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using CounterStrikeSharp.API.Core;
using EloReputation.plugin;

namespace EloReputation.api;

public interface IEloPlugin : IPluginConfig<EloConfig> {
BasePlugin getBase();
IReputationService getReputationService();
IRateLimiter<ulong> getRateLimiter();
BasePlugin GetBase();
IReputationService GetReputationService();
IRateLimiter<ulong> GetMapLimiter();
IRateLimiter<ulong> GetPeriodLimiter();
}
12 changes: 11 additions & 1 deletion src/plugin/EloConfig.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Text.Json.Serialization;
using CounterStrikeSharp.API.Core;

namespace EloReputation.api;
namespace EloReputation.plugin;

public class EloConfig : BasePluginConfig {
[JsonPropertyName("DatabaseConnectionString")]
Expand All @@ -19,4 +19,14 @@ public class EloConfig : BasePluginConfig {
{ "@ego/dsplatinum", 6 },
{ "@ego/dsroyal", 8 }
};

[JsonPropertyName("MaxPeriodicElo")]
public Dictionary<string, int> MaxPeriodicElo { get; set; } = new() {
{ "", 1 },
{ "@ego/e", 1 },
{ "@ego/dssilver", 2 },
{ "@ego/dsgold", 2 },
{ "@ego/dsplatinum", 2 },
{ "@ego/dsroyal", 3 }
};
}
15 changes: 9 additions & 6 deletions src/plugin/EloReputation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@
namespace EloReputation.plugin;

public class EloReputation : BasePlugin, IEloPlugin {
private IRateLimiter<ulong> rateLimiter = null!;
private IRateLimiter<ulong> mapLimiter = null!, periodLimiter = null!;
private IReputationService repService = null!;
public override string ModuleName => "EloReputation";
public override string ModuleVersion => "0.0.1";
public EloConfig Config { get; set; } = null!;

public void OnConfigParsed(EloConfig config) { Config = config; }
public BasePlugin getBase() { return this; }
public IReputationService getReputationService() { return repService; }
public IRateLimiter<ulong> getRateLimiter() { return rateLimiter; }
public BasePlugin GetBase() { return this; }
public IReputationService GetReputationService() { return repService; }
public IRateLimiter<ulong> GetMapLimiter() { return mapLimiter; }
public IRateLimiter<ulong> GetPeriodLimiter() { return periodLimiter; }

public override void Load(bool hotReload) {
repService = new ReputationService(this);
rateLimiter = new RateLimiter(this);
repService = new ReputationService(this);
mapLimiter = new MapBasedRateLimiter(this);
periodLimiter = new TimeBasedRateLimiter(this);

registerCmds();
}
Expand All @@ -43,5 +45,6 @@ private void registerCmds() {
AddCommand("css_reptop", "", top);
AddCommand("css_leaderboard", "", top);
AddCommand("css_lb", "", top);
AddCommand("css_top", "", top);
}
}
12 changes: 6 additions & 6 deletions src/plugin/commands/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
using EloReputation.api;
using EloReputation.plugin.extensions;

namespace plugin.commands;
namespace EloReputation.plugin.commands;

public abstract class Command(IEloPlugin elo) {
protected readonly IEloPlugin elo = elo;
protected readonly IEloPlugin Elo = elo;

public string? Description => null;

Expand All @@ -26,7 +26,7 @@ public abstract void OnCommand(CCSPlayerController? executor,
matches.Players = matches.Players.Where(predicate).ToList();

if (!matches.Any()) {
command.ReplyLocalized(elo.getBase().Localizer, "player_not_found",
command.ReplyLocalized(Elo.GetBase().Localizer, "player_not_found",
command.GetArg(argIndex));
return null;
}
Expand All @@ -37,7 +37,7 @@ public abstract void OnCommand(CCSPlayerController? executor,
if (matches.Count() == 1 || !command.GetArg(argIndex).StartsWith('@'))
return matches;

command.ReplyLocalized(elo.getBase().Localizer, "player_found_multiple",
command.ReplyLocalized(Elo.GetBase().Localizer, "player_found_multiple",
command.GetArg(argIndex));
return null;
}
Expand All @@ -53,13 +53,13 @@ public abstract void OnCommand(CCSPlayerController? executor,
var matches = command.GetArgTargetResult(argIndex);

if (!matches.Any()) {
command.ReplyLocalized(elo.getBase().Localizer, "player_not_found",
command.ReplyLocalized(Elo.GetBase().Localizer, "player_not_found",
command.GetArg(argIndex));
return null;
}

if (matches.Count() <= 1) return matches;
command.ReplyLocalized(elo.getBase().Localizer, "player_found_multiple",
command.ReplyLocalized(Elo.GetBase().Localizer, "player_found_multiple",
command.GetArg(argIndex));
return null;
}
Expand Down
26 changes: 17 additions & 9 deletions src/plugin/commands/elo/DownCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using CounterStrikeSharp.API.Modules.Commands;
using EloReputation.api;
using EloReputation.plugin.extensions;
using plugin.commands;

namespace EloReputation.plugin.commands.elo;

Expand All @@ -13,13 +12,13 @@ public override void OnCommand(CCSPlayerController? executor,

var target = info.GetArgTargetResult(1);
if (!target.Any()) {
info.ReplyLocalized(elo.getBase().Localizer, "player_not_found",
info.ReplyLocalized(Elo.GetBase().Localizer, "player_not_found",
info.GetArg(1));
return;
}

if (target.Count() > 1) {
info.ReplyLocalized(elo.getBase().Localizer, "player_found_multiple",
info.ReplyLocalized(Elo.GetBase().Localizer, "player_found_multiple",
info.GetArg(1));
return;
}
Expand All @@ -28,19 +27,28 @@ public override void OnCommand(CCSPlayerController? executor,
var receiver = target.First().AuthorizedSteamID?.SteamId64;

if (source == null || receiver == null) {
info.ReplyLocalized(elo.getBase().Localizer, "player_invalid",
info.ReplyLocalized(Elo.GetBase().Localizer, "player_invalid",
"You or they");
return;
}

if (!elo.getRateLimiter().Decrement(source.Value)) {
info.ReplyLocalized(elo.getBase().Localizer, "out_of_rep");

if (!Elo.GetMapLimiter().Decrement(source.Value)) {
info.ReplyLocalized(Elo.GetBase().Localizer, "out_of_rep_map");
return;
}

elo.getReputationService()
.AddReputation(source.Value, receiver.Value, false);
info.ReplyLocalized(elo.getBase().Localizer, "rep_taken",
if (!Elo.GetPeriodLimiter().Decrement(source.Value)) {
info.ReplyLocalized(Elo.GetBase().Localizer, "out_of_rep_period");
return;
}

info.ReplyLocalized(Elo.GetBase().Localizer, "rep_taken",
target.First().PlayerName);

if (source.Value == receiver.Value) return;

Elo.GetReputationService()
.AddReputation(source.Value, receiver.Value, false);
}
}
9 changes: 4 additions & 5 deletions src/plugin/commands/elo/RepCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using CounterStrikeSharp.API.Modules.Commands;
using EloReputation.api;
using EloReputation.plugin.extensions;
using plugin.commands;

namespace EloReputation.plugin.commands.elo;

Expand Down Expand Up @@ -32,22 +31,22 @@ public override void OnCommand(CCSPlayerController? executor,
if (info.GetArg(1).All(char.IsDigit)) {
target = target.Append(ulong.Parse(info.GetArg(1)));
} else {
info.ReplyLocalized(elo.getBase().Localizer, "player_not_found",
info.ReplyLocalized(Elo.GetBase().Localizer, "player_not_found",
info.GetArg(1));
return;
}
}

var steamIds = target.ToList();
info.ReplyLocalized(elo.getBase().Localizer, "rep_fetching", steamIds.Count,
info.ReplyLocalized(Elo.GetBase().Localizer, "rep_fetching", steamIds.Count,
steamIds.Count == 1 ? "" : "s");
foreach (var steamId in steamIds)
Server.NextFrameAsync(async () => {
var rep = await elo.getReputationService().GetReputation(steamId);
var rep = await Elo.GetReputationService().GetReputation(steamId);
Server.NextFrame(() => {
var name = Utilities.GetPlayerFromSteamId(steamId)?.PlayerName
?? steamId.ToString();
executor.PrintLocalizedChat(elo.getBase().Localizer, "rep_status",
executor.PrintLocalizedChat(Elo.GetBase().Localizer, "rep_status",
name, rep);
});
});
Expand Down
13 changes: 6 additions & 7 deletions src/plugin/commands/elo/RepTopCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using EloReputation.api;
using EloReputation.plugin.extensions;
using EloReputation.plugin.menus;
using plugin.commands;

namespace EloReputation.plugin.commands.elo;

Expand All @@ -17,7 +16,7 @@ public override void OnCommand(CCSPlayerController? executor,

if (executor == null) {
Server.NextFrameAsync(async () => {
var top = await elo.getReputationService().GetTopReputation();
var top = await Elo.GetReputationService().GetTopReputation();
Server.NextFrame(() => {
var i = 1;
foreach (var entry in top) {
Expand All @@ -34,15 +33,15 @@ public override void OnCommand(CCSPlayerController? executor,

Server.NextFrameAsync(async () => {
if (id == null) return;
var top = await elo.getReputationService().GetTopReputation(50);
var top = await Elo.GetReputationService().GetTopReputation(50);
Server.NextFrame(() => {
var menu = new ReputationMenu(elo, top);
MenuManager.OpenCenterHtmlMenu(elo.getBase(), executor!, menu);
var menu = new ReputationMenu(Elo, top);
MenuManager.OpenCenterHtmlMenu(Elo.GetBase(), executor, menu);
});
var pos = await elo.getReputationService()
var pos = await Elo.GetReputationService()
.GetReputationPosition(id.Value);
Server.NextFrame(() => {
executor.PrintLocalizedChat(elo.getBase().Localizer, "rep_ranking",
executor.PrintLocalizedChat(Elo.GetBase().Localizer, "rep_ranking",
pos.Item1, pos.Item2);
});
});
Expand Down
24 changes: 15 additions & 9 deletions src/plugin/commands/elo/UpCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using CounterStrikeSharp.API.Modules.Commands;
using EloReputation.api;
using EloReputation.plugin.extensions;
using plugin.commands;

namespace EloReputation.plugin.commands.elo;

Expand All @@ -13,13 +12,13 @@ public override void OnCommand(CCSPlayerController? executor,

var target = info.GetArgTargetResult(1);
if (!target.Any()) {
info.ReplyLocalized(elo.getBase().Localizer, "player_not_found",
info.ReplyLocalized(Elo.GetBase().Localizer, "player_not_found",
info.GetArg(1));
return;
}

if (target.Count() > 1) {
info.ReplyLocalized(elo.getBase().Localizer, "player_found_multiple",
info.ReplyLocalized(Elo.GetBase().Localizer, "player_found_multiple",
info.GetArg(1));
return;
}
Expand All @@ -28,19 +27,26 @@ public override void OnCommand(CCSPlayerController? executor,
var receiver = target.First().AuthorizedSteamID?.SteamId64;

if (source == null || receiver == null) {
info.ReplyLocalized(elo.getBase().Localizer, "player_invalid",
info.ReplyLocalized(Elo.GetBase().Localizer, "player_invalid",
"You or they");
return;
}

if (!elo.getRateLimiter().Decrement(source.Value)) {
info.ReplyLocalized(elo.getBase().Localizer, "out_of_rep");
if (!Elo.GetMapLimiter().Decrement(source.Value)) {
info.ReplyLocalized(Elo.GetBase().Localizer, "out_of_rep_map");
return;
}

elo.getReputationService()
.AddReputation(source.Value, receiver.Value);
info.ReplyLocalized(elo.getBase().Localizer, "rep_given",
if (!Elo.GetPeriodLimiter().Decrement(source.Value)) {
info.ReplyLocalized(Elo.GetBase().Localizer, "out_of_rep_period");
return;
}

info.ReplyLocalized(Elo.GetBase().Localizer, "rep_given",
target.First().PlayerName);

if (source.Value == receiver.Value) return;

Elo.GetReputationService().AddReputation(source.Value, receiver.Value);
}
}
2 changes: 1 addition & 1 deletion src/plugin/extensions/VectorExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using CounterStrikeSharp.API.Modules.Utils;

namespace plugin.extensions;
namespace EloReputation.plugin.extensions;

public static class VectorExtensions {
public static Vector Clone(this Vector vector) {
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/menus/ReputationMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace EloReputation.plugin.menus;

public class ReputationMenu : CenterHtmlMenu {
public ReputationMenu(IEloPlugin plugin, IEnumerable<(ulong, double)> ranks) :
base("Reputation Leaderboard", plugin.getBase()) {
base("Reputation Leaderboard", plugin.GetBase()) {
var i = 1;
foreach (var entry in ranks) {
var name = Utilities.GetPlayerFromSteamId(entry.Item1)?.PlayerName
Expand Down
47 changes: 47 additions & 0 deletions src/plugin/services/DictionaryLimiter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using EloReputation.api;

namespace EloReputation.plugin.services;

public abstract class DictionaryListener(IEloPlugin plugin)
: IRateLimiter<ulong> {
protected readonly Dictionary<ulong, int> Limits = new();
protected readonly IEloPlugin Plugin = plugin;

public void Reset() { Limits.Clear(); }
public void Reset(ulong key) { Limits.Remove(key); }

public int Increment(ulong key) {
Limits[key] = Limits.GetValueOrDefault(key, 0) + 1;
return Limits[key];
}

public int Get(ulong key) {
if (Limits.TryGetValue(key, out var value)) return value;

var player = Utilities.GetPlayerFromSteamId(key);
if (player == null) return 0;

try { Limits[key] = GetLimit(player); } catch (ArgumentNullException e) {
Console.WriteLine(e);
}

return Limits.GetValueOrDefault(key, 0);
}

public bool Decrement(ulong key) {
if (Get(key) <= 0) return false;
Limits[key]--;
return true;
}

/// <summary>
/// Gets the max configured number of rep points to give to a player
/// 0 if the player is not authorized or otherwise configured
/// </summary>
/// <param name="player"></param>
/// <exception cref="ArgumentNullException"></exception>
/// <returns></returns>
abstract protected int GetLimit(CCSPlayerController player);
}
Loading