Skip to content

Commit

Permalink
Add RTD
Browse files Browse the repository at this point in the history
  • Loading branch information
MSWS committed Aug 28, 2024
1 parent a0015ee commit 93dc66e
Show file tree
Hide file tree
Showing 37 changed files with 836 additions and 99 deletions.
7 changes: 7 additions & 0 deletions Jailbreak.sln
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jailbreak.Zones", "mod\Jail
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jailbreak.Trail", "mod\Jailbreak.Trail\Jailbreak.Trail.csproj", "{91F4EC7A-993A-4CA0-84C3-9F1100124A9C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jailbreak.RTD", "mod\Jailbreak.RTD\Jailbreak.RTD.csproj", "{C68D4760-7E1E-4633-995A-5EC1EF40E63B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -116,6 +118,10 @@ Global
{91F4EC7A-993A-4CA0-84C3-9F1100124A9C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{91F4EC7A-993A-4CA0-84C3-9F1100124A9C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{91F4EC7A-993A-4CA0-84C3-9F1100124A9C}.Release|Any CPU.Build.0 = Release|Any CPU
{C68D4760-7E1E-4633-995A-5EC1EF40E63B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C68D4760-7E1E-4633-995A-5EC1EF40E63B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C68D4760-7E1E-4633-995A-5EC1EF40E63B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C68D4760-7E1E-4633-995A-5EC1EF40E63B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{9135CCC9-66C5-4A9C-AE3C-91475B5F0437} = {177DA48D-8306-4102-918D-992569878581}
Expand All @@ -135,5 +141,6 @@ Global
{A6249693-5B7E-4E14-A675-C292914F10F3} = {36BA84C0-291C-4930-A7C6-97CDF8F7F0D7}
{C93A626A-BB44-4309-8DAD-4B28B2941870} = {36BA84C0-291C-4930-A7C6-97CDF8F7F0D7}
{91F4EC7A-993A-4CA0-84C3-9F1100124A9C} = {36BA84C0-291C-4930-A7C6-97CDF8F7F0D7}
{C68D4760-7E1E-4633-995A-5EC1EF40E63B} = {36BA84C0-291C-4930-A7C6-97CDF8F7F0D7}
EndGlobalSection
EndGlobal
34 changes: 34 additions & 0 deletions lang/Jailbreak.English/RTD/RTDLocale.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using CounterStrikeSharp.API.Modules.Utils;
using Jailbreak.Formatting.Base;
using Jailbreak.Formatting.Logistics;
using Jailbreak.Formatting.Views;
using Jailbreak.Public.Mod.RTD;

namespace Jailbreak.English.RTD;

public class RTDLocale : IRTDLocale, ILanguage<Formatting.Languages.English> {
public static readonly string PREFIX =
$" {ChatColors.Purple}[{ChatColors.LightPurple}RTD{ChatColors.Purple}]";

public IView RewardSelected(IRTDReward reward) {
return new SimpleView {
{ PREFIX, "You rolled ", reward.Name + "." },
SimpleView.NEWLINE,
{ PREFIX, reward.Description }
};
}

public IView AlreadyRolled(IRTDReward reward) {
return new SimpleView {
PREFIX,
ChatColors.Red + "You already rolled " + ChatColors.DarkRed + reward.Name
+ ChatColors.Red + ".",
};
}

public IView CannotRollYet() {
return new SimpleView {
PREFIX, "You can only roll once the round ends or you die."
};
}
}
22 changes: 22 additions & 0 deletions mod/Jailbreak.RTD/AutoRTDListener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Admin;
using Jailbreak.Public.Behaviors;
using Jailbreak.Public.Mod.RTD;

namespace Jailbreak.RTD;

public class AutoRTDListener(IRTDRewarder rewarder) : IPluginBehavior {
[GameEventHandler]
public HookResult OnRoundEnd(EventRoundEnd @event, GameEventInfo info) {
foreach (var player in Utilities.GetPlayers()
.Where(player
=> AdminManager.PlayerHasPermissions(player, "@ego/dssilver"))
.Where(player => !rewarder.HasReward(player))) {
player.ExecuteClientCommandFromServer("css_rtd");
}

return HookResult.Continue;
}
}
14 changes: 14 additions & 0 deletions mod/Jailbreak.RTD/Jailbreak.RTD.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\public\Jailbreak.Formatting\Jailbreak.Formatting.csproj" />
<ProjectReference Include="..\..\public\Jailbreak.Public\Jailbreak.Public.csproj" />
</ItemGroup>

</Project>
68 changes: 68 additions & 0 deletions mod/Jailbreak.RTD/RTDCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Admin;
using CounterStrikeSharp.API.Modules.Commands;
using Jailbreak.Formatting.Extensions;
using Jailbreak.Formatting.Views;
using Jailbreak.Public.Behaviors;
using Jailbreak.Public.Mod.RTD;

namespace Jailbreak.RTD;

public class RTDCommand(IRTDRewarder rewarder, IRewardGenerator generator,
IRTDLocale locale, IGenericCmdLocale generic) : IPluginBehavior {
private bool inBetweenRounds;

[ConsoleCommand("css_rtd", "Roll the dice!")]
public void Command_RTD(CCSPlayerController? executor, CommandInfo info) {
if (executor == null) return;
var bypass = AdminManager.PlayerHasPermissions(executor, "@css/root")
&& info.ArgCount == 2;

var old = rewarder.GetReward(executor);
if (!bypass && old != null) {
locale.AlreadyRolled(old).ToChat(executor);
return;
}

if (!bypass && !inBetweenRounds && executor.PawnIsAlive) {
locale.CannotRollYet().ToChat(executor);
return;
}

var reward = generator.GenerateReward(executor);
if (bypass) {
if (!int.TryParse(info.GetArg(1), out var slot)) {
generic.InvalidParameter(info.GetArg(1), "integer").ToChat(executor);
return;
}

if (slot != -1) {
var rewards = generator.ToList();

if (slot < 0 || slot >= rewards.Count) {
generic.InvalidParameter(info.GetArg(1), "0-" + (rewards.Count - 1))
.ToChat(executor);
return;
}

reward = generator.ToList()[slot].Item1;
}
}

rewarder.SetReward(executor, reward);
locale.RewardSelected(reward).ToChat(executor);
}

[GameEventHandler]
public HookResult OnEnd(EventRoundEnd @event, GameEventInfo info) {
inBetweenRounds = true;
return HookResult.Continue;
}

[GameEventHandler]
public HookResult OnStart(EventRoundStart @event, GameEventInfo info) {
inBetweenRounds = false;
return HookResult.Continue;
}
}
47 changes: 47 additions & 0 deletions mod/Jailbreak.RTD/RTDRewarder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using Jailbreak.Formatting.Base;
using Jailbreak.Formatting.Extensions;
using Jailbreak.Formatting.Views;
using Jailbreak.Formatting.Views.Logging;
using Jailbreak.Public.Behaviors;
using Jailbreak.Public.Mod.Logs;
using Jailbreak.Public.Mod.RTD;
using Jailbreak.Public.Utils;
using Microsoft.VisualBasic.CompilerServices;

namespace Jailbreak.RTD;

public class RTDRewarder(IRichLogService logs, IRTDLocale locale)

Check warning on line 16 in mod/Jailbreak.RTD/RTDRewarder.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'locale' is unread.

Check warning on line 16 in mod/Jailbreak.RTD/RTDRewarder.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'locale' is unread.
: IRTDRewarder, IPluginBehavior {
private readonly Dictionary<int, IRTDReward> rewards = new();

public bool HasReward(int id) { return GetReward(id) != null; }

public IRTDReward? GetReward(int id) {
return rewards.TryGetValue(id, out var reward) ? reward : null;
}

public bool SetReward(int id, IRTDReward reward) {
if (!reward.PrepareReward(id)) return false;
rewards[id] = reward;
return true;
}

[GameEventHandler]
public HookResult OnStart(EventRoundStart @event, GameEventInfo info) {
foreach (var player in PlayerUtil.GetAlive()) {
var id = player.UserId ?? -1;

var reward = GetReward(id);
if (reward == null) continue;

logs.Append("Granted", reward.Name, "to", logs.Player(player));
reward.GrantReward(id);
}

rewards.Clear();
return HookResult.Continue;
}
}
15 changes: 15 additions & 0 deletions mod/Jailbreak.RTD/RTDServiceExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Jailbreak.Public.Extensions;
using Jailbreak.Public.Mod.RTD;
using Microsoft.Extensions.DependencyInjection;

namespace Jailbreak.RTD;

public static class RTDServiceExtensions {
public static void AddDiceRoll(this IServiceCollection collection) {
collection.AddPluginBehavior<IRewardGenerator, RewardGenerator>();
collection.AddPluginBehavior<IRTDRewarder, RTDRewarder>();
collection.AddPluginBehavior<RTDCommand>();
collection.AddPluginBehavior<AutoRTDListener>();
collection.AddPluginBehavior<RTDStatsCommand>();
}
}
28 changes: 28 additions & 0 deletions mod/Jailbreak.RTD/RTDStatsCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Utils;
using Jailbreak.Public.Behaviors;
using Jailbreak.Public.Mod.RTD;

namespace Jailbreak.RTD;

public class RTDStatsCommand(IRewardGenerator generator) : IPluginBehavior {
[ConsoleCommand("css_rtdstats", "View stats and probabilities of the die")]
public void
Command_RTDStats(CCSPlayerController? executor, CommandInfo info) {
if (executor == null) return;
var total = generator.Sum(r => r.Item2);

var rewards = generator.ToList();
rewards.Sort((a, b) => a.Item2.CompareTo(b.Item2));

var index = 0;
foreach (var (reward, prob) in rewards) {
var name = reward.Name;
var percent = prob / total * 100;
executor.PrintToChat(
$"{ChatColors.Orange}{index++}. {ChatColors.LightBlue}{name}{ChatColors.Grey}: {ChatColors.Yellow}{percent:0.00}%");
}
}
}
87 changes: 87 additions & 0 deletions mod/Jailbreak.RTD/RewardGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System.Collections;
using System.Drawing;
using CounterStrikeSharp.API.Core;
using Jailbreak.Public.Behaviors;
using Jailbreak.Public.Mod.Rebel;
using Jailbreak.Public.Mod.RTD;
using Jailbreak.Public.Mod.Zones;
using Jailbreak.RTD.Rewards;

namespace Jailbreak.RTD;

public class RewardGenerator(IZoneManager mgr, IC4Service bomb)
: IPluginBehavior, IRewardGenerator {
private static readonly float PROB_LOTTERY = 1 / 5000f;

Check warning on line 14 in mod/Jailbreak.RTD/RewardGenerator.cs

View workflow job for this annotation

GitHub Actions / build

The field 'RewardGenerator.PROB_LOTTERY' is assigned but its value is never used

Check warning on line 14 in mod/Jailbreak.RTD/RewardGenerator.cs

View workflow job for this annotation

GitHub Actions / build

The field 'RewardGenerator.PROB_LOTTERY' is assigned but its value is never used
private static readonly float PROB_EXTREMELY_LOW = 1 / 1000f;
private static readonly float PROB_VERY_LOW = 1 / 100f;
private static readonly float PROB_LOW = 1 / 20f;
private static readonly float PROB_MEDIUM = 1 / 10f;
private static readonly float PROB_OFTEN = 1 / 5f;
private static readonly float PROB_VERY_OFTEN = 1 / 2f;

Check warning on line 20 in mod/Jailbreak.RTD/RewardGenerator.cs

View workflow job for this annotation

GitHub Actions / build

The field 'RewardGenerator.PROB_VERY_OFTEN' is assigned but its value is never used

Check warning on line 20 in mod/Jailbreak.RTD/RewardGenerator.cs

View workflow job for this annotation

GitHub Actions / build

The field 'RewardGenerator.PROB_VERY_OFTEN' is assigned but its value is never used

private readonly List<(IRTDReward, float)> rewards = [
(new NothingReward(), PROB_OFTEN),
(new WeaponReward("weapon_healthshot"), PROB_OFTEN),
(new WeaponReward("weapon_decoy"), PROB_OFTEN),
(new CreditReward(1), PROB_MEDIUM), (new CreditReward(2), PROB_MEDIUM),
(new CreditReward(3), PROB_MEDIUM),
(new WeaponReward("weapon_flashbang"), PROB_MEDIUM),
(new WeaponReward("weapon_hegrenade"), PROB_MEDIUM),
(new WeaponReward("weapon_smokegrenade"), PROB_MEDIUM),
(new WeaponReward("weapon_molotov"), PROB_MEDIUM),
(new WeaponReward("weapon_taser"), PROB_MEDIUM),
(new HPReward(150), PROB_MEDIUM), (new HPReward(50), PROB_MEDIUM),
(new ArmorReward(150), PROB_MEDIUM), (new HPReward(1), PROB_LOW),
(new ColorReward(Color.FromArgb(0, 255, 0)), PROB_LOW),
(new ColorReward(Color.FromArgb(255, 0, 0)), PROB_LOW),
(new TransparentReward(), PROB_LOW),
(new AmmoWeaponReward("weapon_glock", 2, 0), PROB_LOW),
(new AmmoWeaponReward("weapon_negev", 0, 5), PROB_LOW),
(new NoWeaponReward(), PROB_VERY_LOW),
(new AmmoWeaponReward("weapon_deagle", 1, 0), PROB_VERY_LOW),
(new RandomTeleportReward(mgr), PROB_VERY_LOW),
(new BombReward(bomb), PROB_VERY_LOW),
(new AmmoWeaponReward("weapon_awp", 1, 0), PROB_EXTREMELY_LOW)
];

public void Start(BasePlugin basePlugin) {
rewards.Add((new CannotPickupReward(basePlugin, WeaponType.GRENADE),
PROB_LOW));
rewards.Add((new CannotPickupReward(basePlugin, WeaponType.UTILITY),
PROB_LOW));
rewards.Add((
new CannotPickupReward(basePlugin,
WeaponType.GRENADE | WeaponType.UTILITY), PROB_LOW));
rewards.Add((new CannotPickupReward(basePlugin, WeaponType.PISTOLS),
PROB_VERY_LOW));
rewards.Add((new CannotPickupReward(basePlugin, WeaponType.SNIPERS),
PROB_VERY_LOW));
rewards.Add((new CannotPickupReward(basePlugin, WeaponType.HEAVY),
PROB_VERY_LOW));
rewards.Add((new CannotPickupReward(basePlugin, WeaponType.RIFLES),
PROB_EXTREMELY_LOW));
}

private readonly Random rng = new();

private float totalWeight
=> rewards.Where(r => r.Item1.Enabled).Select(s => s.Item2).Sum();

public IRTDReward GenerateReward(int slot) {
var roll = rng.NextDouble() * totalWeight;

foreach (var reward in rewards.Where(reward => reward.Item1.Enabled)) {
roll -= reward.Item2;
if (roll <= 0) return reward.Item1;
}

throw new InvalidOperationException("No reward was generated.");
}

public IEnumerator<(IRTDReward, float)> GetEnumerator() {
return rewards.Where(r => r.Item1.Enabled).GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
public int Count => rewards.Count(r => r.Item1.Enabled);
}
24 changes: 24 additions & 0 deletions mod/Jailbreak.RTD/Rewards/AmmoWeaponReward.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Diagnostics;
using CounterStrikeSharp.API.Core;
using Jailbreak.Public.Extensions;

namespace Jailbreak.RTD.Rewards;

public class AmmoWeaponReward : WeaponReward {
private readonly int primary, secondary;

public AmmoWeaponReward(string weapon, int primary, int secondary) :
base(weapon) {
Trace.Assert(Tag.GUNS.Contains(weapon));
this.primary = primary;
this.secondary = secondary;
}

public override bool GrantReward(CCSPlayerController player) {
var success = base.GrantReward(player);
if (!success) return false;

player.GetWeaponBase(weapon).SetAmmo(primary, secondary);
return true;
}
}
Loading

0 comments on commit 93dc66e

Please sign in to comment.