Skip to content

Commit

Permalink
added console command for picking wires
Browse files Browse the repository at this point in the history
  • Loading branch information
jmgraeffe committed Nov 6, 2024
1 parent 382d17c commit f6424cb
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 12 deletions.
6 changes: 6 additions & 0 deletions .idea/.idea.LuckyDefusePlugin/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 64 additions & 11 deletions LuckyDefusePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class LuckyDefusePlugin : BasePlugin, IPluginConfig<PluginConfig>
{
public override string ModuleName => "Lucky Defuse Plugin";
public override string ModuleAuthor => "Jon-Mailes Graeffe <[email protected]>";
public override string ModuleVersion => "1.0.0";
public override string ModuleVersion => "1.0.1";

public PluginConfig Config { get; set; } = null!;

Expand All @@ -39,13 +39,14 @@ public class LuckyDefusePlugin : BasePlugin, IPluginConfig<PluginConfig>
private readonly CenterHtmlMenu _planterMenu;
private readonly CenterHtmlMenu _defuserMenu;
private int _wire;
private bool _wireChosenManually = false;

public LuckyDefusePlugin()
{
var options = new string[_colors.Length];
for (int i = 0; i < _colors.Length; ++i)
{
options[i] = $"<span color=\"{_colors[i].Name.ToLower()}\">{_colors[i].Name}</span>";
options[i] = $"<span color=\"{_colors[i].Name.ToLower()}\">{i+1}. {_colors[i].Name}</span>";
}

_planterMenu = new(this, "Choose the hot wire:", options);
Expand All @@ -60,15 +61,28 @@ public void OnConfigParsed(PluginConfig? config)

public override void Load(bool hotReload)
{
RegisterEventHandler<EventRoundEnd>((_, _) =>
{
_planter = null;
_wireChosenManually = false;
_defuserMenu.Close();
_planterMenu.Close();
return HookResult.Continue;
});

RegisterEventHandler<EventBombPlanted>((@event, _) =>
{
if (@event.Userid == null) return HookResult.Continue;
_wire = -1;
_wire = Random.Shared.Next(_colors.Length - 1);
_planter = @event.Userid;
_planterMenu.Open(@event.Userid);
AddTimer(Config.PlanterMenuDuration, ChooseRandomWire, TimerFlags.STOP_ON_MAPCHANGE);
AddTimer(Config.NotificationDelay, () => Server.PrintToChatAll(Localizer["notification"]),
TimerFlags.STOP_ON_MAPCHANGE);
AddTimer(Config.NotificationDelay, Notify, TimerFlags.STOP_ON_MAPCHANGE);
AddTimer(Config.PlanterMenuDuration, () =>
{
_planterMenu.Close();
if (_wireChosenManually) return;
_planter.PrintToChat(Localizer["randomWireChosen"].Value.Replace("{wire}", $"{_chatColors[_wire]}{_colors[_wire].Name.ToLower()}"));
}, TimerFlags.STOP_ON_MAPCHANGE);
return HookResult.Continue;
});

Expand All @@ -89,13 +103,15 @@ public override void Load(bool hotReload)

RegisterEventHandler<EventBombExploded>((_, _) =>
{
_planter = null;
_defuserMenu.Close();
_planterMenu.Close();
return HookResult.Continue;
});

RegisterEventHandler<EventBombDefused>((_, _) =>
{
_planter = null;
_defuserMenu.Close();
_planterMenu.Close();
return HookResult.Continue;
Expand All @@ -104,20 +120,57 @@ public override void Load(bool hotReload)
_planterMenu.OnOptionConfirmed += option =>
{
_wire = option;
_wireChosenManually = true;
_planter!.PrintToChat(Localizer["wireChosen"].Value.Replace("{wire}", $"{_chatColors[option]}{_colors[option].Name.ToLower()}"));
};
_defuserMenu.OnOptionConfirmed += CutWire;

AddCommand("ld_choose_wire", "Nominates a map so that it appears in the map vote after the match ends", (player, info) =>
{
if (player == null)
{
Server.PrintToConsole("consoleNotAllowed");
return;
}
if (info.ArgCount < 2)
{
info.ReplyToCommand(Localizer["missingArgument"]);
return;
}

if (!int.TryParse(info.GetArg(1), out var option) || option <= 0 || option > _colors.Length)
{
info.ReplyToCommand(Localizer["malformedArgument"]);
return;
}

--option;

if (_defuser != null && player.AuthorizedSteamID == _defuser.AuthorizedSteamID)
{
CutWire(option);
}
else if (_planter != null && player.AuthorizedSteamID == _planter.AuthorizedSteamID)
{
_wire = option;
_wireChosenManually = true;
info.ReplyToCommand(Localizer["wireChosen"].Value.Replace("{wire}", $"{_chatColors[option]}{_colors[option].Name.ToLower()}"));
_planterMenu.Close();
}
else
{
info.ReplyToCommand(Localizer["noBomb"]);
}
});

_planterMenu.Load();
_defuserMenu.Load();
}

private void ChooseRandomWire()
private void Notify()
{
if (_wire >= 0) return;
_planterMenu.Close();
_wire = Random.Shared.Next(_colors.Length - 1);
Server.PrintToChatAll(Localizer["randomWireChosen"].Value.Replace("{wire}", $"{_chatColors[_wire]}{_colors[_wire].Name.ToLower()}"));
if (_planter == null) return;
Server.PrintToChatAll(Localizer["notification"]);
}

private void CutWire(int wire)
Expand Down
5 changes: 4 additions & 1 deletion lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
"randomWireChosen": " {magenta}>> {default}Since you didn't choose a wire, the {wire}{default} wire was randomly chosen to insta-defuse the bomb.",
"cutWrongWire": " {magenta}[Lucky Defuse] {default}Well... Player {lime}{player}{default} cut the {wire}{default} wire, which was wrong.",
"cutCorrectWire": " {magenta}[Lucky Defuse] {default}Pheeeew! Player {lime}{player}{default} cut the {wire}{default} wire and saved the day.",
"notification": " {magenta}[Lucky Defuse] {default}The bomb's ticking! Be heroic and try to cut a wire - if you choose right, you defuse the bomb instantly!"
"notification": " {magenta}[Lucky Defuse] {default}The bomb's ticking! Be heroic and try to cut a wire - if you choose right, you defuse the bomb instantly!",
"missingArgument": "[Lucky Defuse] You need to specify the number of the wire as an argument!",
"malformedArgument": "[Lucky Defuse] The first argument needs to be the number of the wire you want to choose from 0 to 3!",
"noBomb": " {magenta}[Lucky Defuse] {default}There is no bomb. Wait for the bomb to be planted if you're T, and the bomb to be defused if you're CT."
}

0 comments on commit f6424cb

Please sign in to comment.