Skip to content

Commit

Permalink
sub-release
Browse files Browse the repository at this point in the history
  • Loading branch information
oscar-wos committed Jun 28, 2024
1 parent e658003 commit 8e74821
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 54 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,12 @@ jobs:
- name: Zip
run: |
ls -a
zip -r ${{ github.event.repository.name }}-${{ github.sha }}.zip ./plugin
- name: Extract version and create tag
id: extract_version
run: |
version="1.0.0"
version=$(grep -oP 'public override string ModuleVersion => "\K(.*)(?=";)' ./src/Globals.cs)
echo "Version found: $version"
git config --global user.email "[email protected]"
git config --global user.name "GitHub Actions"
Expand Down
4 changes: 4 additions & 0 deletions lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
"zone.Teleport": "Teleport",
"menu.Add": "Add Zone",
"menu.View": "View Zones",
"menu.None": "No Zones",
"menu.Zones": "Zones:",
"menu.Teleport": "Teleport to center",
"menu.Delete": "Delete",
"menu.Debug": "Debug:",
"menu.Type": "Type:",
"menu.Teams": "Teams:",
Expand Down
23 changes: 12 additions & 11 deletions src/AntiRush.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public override void Load(bool isReload)

AddCommand("css_antirush", "Anti-Rush", CommandAntiRush);
AddCommand("css_addzone", "Add Zone", CommandAddZone);
AddCommand("css_viewzones", "View Zones", CommandViewZones);
//AddCommand("css_viewzones", "View Zones", CommandViewZones);

if (!isReload)
return;
Expand All @@ -44,25 +44,22 @@ private void SaveZone(CCSPlayerController controller)
var zoneType = (ZoneType)menu.Items[0].Option;
var minPoint = new Vector(Math.Min(menu.Points[0].X, menu.Points[1].X), Math.Min(menu.Points[0].Y, menu.Points[1].Y), Math.Min(menu.Points[0].Z, menu.Points[1].Z));
var maxPoint = new Vector(Math.Max(menu.Points[0].X, menu.Points[1].X), Math.Max(menu.Points[0].Y, menu.Points[1].Y), Math.Max(menu.Points[0].Z, menu.Points[1].Z));
var delay = zoneType != ZoneType.Bounce && float.TryParse(menu.Items[3].DataString, out var valueDelay) ? valueDelay : 0;
var delay = zoneType != ZoneType.Bounce && float.TryParse(menu.Items[3].DataString, out var valueDelay) ? (float)Math.Floor(valueDelay * 10) / 10 : 0;
var damage = zoneType == ZoneType.Hurt && int.TryParse(menu.Items[4].DataString, out var valueDamage) ? valueDamage : 10;
var name = menu.Items[2].DataString;

if (name.Length == 0)
name = "noname";

var zone = new Zone(zoneType, teams, minPoint, maxPoint, name, delay, damage);
_zones.Add(zone);

var printMessage = $"{Prefix}{Localizer["saving", name, FormatZoneString(zoneType)]} | {Localizer["menu.Teams"]} [";

if (teams.Contains(CsTeam.Terrorist))
printMessage += $" {ChatColors.LightYellow}{Localizer["t"]}{ChatColors.White}";
printMessage += $"{ChatColors.LightYellow}{Localizer["t"]}{ChatColors.White}";

if (teams.Contains(CsTeam.CounterTerrorist))
printMessage += $" {ChatColors.Blue}{Localizer["ct"]}{ChatColors.White}";
printMessage += $"{(teams.Contains(CsTeam.Terrorist) ? "|" : "")}{ChatColors.Blue}{Localizer["ct"]}{ChatColors.White}";

printMessage += " ]";
printMessage += "]";

if (zoneType != ZoneType.Bounce)
printMessage += $" | {Localizer["menu.Delay"]} {ChatColors.Green}{delay}{ChatColors.White}";
Expand All @@ -71,12 +68,16 @@ private void SaveZone(CCSPlayerController controller)
printMessage += $" | {Localizer["menu.Damage"]} {ChatColors.Green}{damage}{ChatColors.White}";

controller.PrintToChat(printMessage);

var zone = new Zone(zoneType, teams, minPoint, maxPoint, name, delay, damage);
_zones.Add(zone);
SaveJson(Server.MapName);
}

private bool DoAction(CCSPlayerController controller, Zone zone)
{
if (zone.Type != ZoneType.Bounce && Server.CurrentTime - _playerData[controller].LastMessage >= 1)
controller!.PrintToChat($"{Prefix}{FormatZoneString(zone.Type)}");
controller.PrintToChat($"{Prefix}{FormatZoneString(zone.Type)}");

_playerData[controller].LastMessage = Server.CurrentTime;

Expand All @@ -87,7 +88,7 @@ private bool DoAction(CCSPlayerController controller, Zone zone)

_playerData[controller].LastVelocity *= (-350 / (float)speed);
_playerData[controller].LastVelocity.Z = _playerData[controller].LastVelocity.Z <= 0f ? 150f : Math.Min(_playerData[controller].LastVelocity.Z, 150f);
controller.PlayerPawn.Value!.Teleport(_playerData[controller].LastPosition, controller.PlayerPawn!.Value.EyeAngles, _playerData[controller].LastVelocity);
controller.PlayerPawn.Value!.Teleport(_playerData[controller].LastPosition, controller.PlayerPawn.Value.EyeAngles, _playerData[controller].LastVelocity);
return true;

case ZoneType.Hurt:
Expand All @@ -107,7 +108,7 @@ private bool DoAction(CCSPlayerController controller, Zone zone)
return false;

case ZoneType.Teleport:
controller.PlayerPawn.Value!.Teleport(_playerData[controller].SpawnPos, controller.PlayerPawn!.Value.EyeAngles, Vector.Zero);
controller.PlayerPawn.Value!.Teleport(_playerData[controller].SpawnPos, controller.PlayerPawn.Value.EyeAngles, Vector.Zero);
return false;
}

Expand Down
10 changes: 5 additions & 5 deletions src/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ private HookResult OnPlayerSpawn(EventPlayerSpawn @event, GameEventInfo info)
if (controller == null || !controller.IsValid)
return HookResult.Continue;

if (!_playerData.ContainsKey(controller!))
_playerData[controller!] = new PlayerData();
if (!_playerData.ContainsKey(controller))
_playerData[controller] = new PlayerData();

_playerData[controller!].SpawnPos = new Vector(controller!.PlayerPawn!.Value!.AbsOrigin!.X, controller!.PlayerPawn!.Value!.AbsOrigin.Y, controller!.PlayerPawn!.Value!.AbsOrigin.Z);
_playerData[controller].SpawnPos = new Vector(controller.PlayerPawn.Value!.AbsOrigin!.X, controller.PlayerPawn.Value.AbsOrigin.Y, controller.PlayerPawn.Value.AbsOrigin.Z);

return HookResult.Continue;
}
Expand All @@ -38,7 +38,7 @@ private HookResult OnBulletImpact(EventBulletImpact @event, GameEventInfo info)

if (value.AddZone.Points[0].IsZero())
value.AddZone.Points[0] = new Vector(@event.X, @event.Y, @event.Z);
else if (value.AddZone.Points[1].IsZero())
else
{
value.AddZone.Points[1] = new Vector(@event.X, @event.Y, @event.Z);

Expand Down Expand Up @@ -66,7 +66,7 @@ private HookResult OnPlayerConnect(EventPlayerConnectFull @event, GameEventInfo
if (controller == null || !controller.IsValid)
return HookResult.Continue;

if (!_playerData.ContainsKey(controller!))
if (!_playerData.ContainsKey(controller))
_playerData[controller] = new PlayerData();

return HookResult.Continue;
Expand Down
2 changes: 1 addition & 1 deletion src/Globals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public partial class AntiRush
public Menu.Menu Menu { get; } = new();

private string Prefix { get; } = $"[{ChatColors.Lime}AntiRush{ChatColors.White}] ";
private readonly List<Zone> _zones = [];
private List<Zone> _zones = [];
private readonly Dictionary<CCSPlayerController, PlayerData> _playerData = [];
}
67 changes: 47 additions & 20 deletions src/Json.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.Extensions.Logging;
using System.Text.Json;
using AntiRush.Enums;
using CounterStrikeSharp.API.Modules.Utils;

namespace AntiRush;

Expand All @@ -7,50 +9,75 @@ public partial class AntiRush
public void LoadJson(string mapName)
{
_zones.Clear();
var path = $"../../csgo/addons/counterstrikesharp/configs/plugins/Zones/{mapName}.json";
var path = $"../../csgo/addons/counterstrikesharp/configs/plugins/AntiRush/{mapName}.json";

if (!File.Exists(path))
return;
/*
var json = File.ReadAllText(path);
var obj = JsonSerializer.Deserialize<JsonBombsite>(json);
foreach (var zone in obj!.a)
AddZone(Bombsite.A, zone);

foreach (var zone in obj!.b)
AddZone(Bombsite.B, zone);
var json = File.ReadAllText(path);
var jsonZones = JsonSerializer.Deserialize<List<JsonZone>>(json);

return;
if (jsonZones == null)
return;

void AddZone(Bombsite bombsite, JsonZone zone)
{
foreach (var zone in jsonZones)
_zones.Add(new Zone(
(ZoneType)zone.type,
zone.teams.Select(t => (CsTeam)Enum.ToObject(typeof(CsTeam), t)).ToArray(),
new Vector(Math.Min(zone.x[0], zone.y[0]), Math.Min(zone.x[1], zone.y[1]), Math.Min(zone.x[2], zone.y[2])),
new Vector(Math.Max(zone.x[0], zone.y[0]), Math.Max(zone.x[1], zone.y[1]), Math.Max(zone.x[2], zone.y[2]))
new Vector(Math.Max(zone.x[0], zone.y[0]), Math.Max(zone.x[1], zone.y[1]), Math.Max(zone.x[2], zone.y[2])),
zone.name,
zone.delay,
zone.damage
));
}
*/
}

public void SaveJson(string mapName)
{
var path = $"../../csgo/addons/counterstrikesharp/configs/plugins/Zones/{mapName}.json";
var path = $"../../csgo/addons/counterstrikesharp/configs/plugins/AntiRush/";

if (!File.Exists(path))
{
Logger.LogError($"File {path} does not exist.");
if (!Directory.Exists(path))
Directory.CreateDirectory(path);

path += $"{mapName}.json";

if (_zones.Count == 0)
return;

List<JsonZone> jsonZones = [];

foreach (var zone in _zones)
{
var teams = zone.Teams.Select(team => (int)team).ToArray();
var minPoint = new[] { zone.MinPoint.X, zone.MinPoint.Y, zone.MinPoint.Z };
var maxPoint = new[] { zone.MaxPoint.X, zone.MaxPoint.Y, zone.MaxPoint.Z };

var jsonObject = new JsonZone()
{
name = zone.Name,
type = (int)zone.Type,
teams = teams,
x = minPoint,
y = maxPoint,
delay = zone.Delay,
damage = zone.Damage
};

jsonZones.Add(jsonObject);
}

var json = JsonSerializer.Serialize(jsonZones);
File.WriteAllText(path, json);
}
}

public class JsonZone
{
public required string name { get; set; }
public required int type { get; set; }
public required int[] teams { get; set; }
public required float[] x { get; set; }
public required float[] y { get; set; }
public required float delay { get; set; }
public required int damage { get; set; }
}
12 changes: 6 additions & 6 deletions src/Listeners.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ private void OnTick()

foreach (var zone in _zones)
{
var isInZone = zone.IsInZone(controller!.PlayerPawn.Value!.AbsOrigin!);
var isInZone = zone.IsInZone(controller.PlayerPawn.Value!.AbsOrigin!);

if (!isInZone)
{
zone.Entry.Remove(controller);
continue;
}

if (!zone.Entry.ContainsKey(controller))
zone.Entry[controller] = Server.CurrentTime;

if (!zone.Teams.Contains(controller.Team))
continue;

if (!zone.Entry.ContainsKey(controller))
zone.Entry[controller] = Server.CurrentTime;

if (zone.Delay != 0)
{
var diff = (zone.Entry[controller] + zone.Delay) - Server.CurrentTime;
Expand All @@ -36,7 +36,7 @@ private void OnTick()
var diffString = diff % 1;

if (diffString.ToString("0.00") is ("0.00" or "0.01") && diff >= 1.0)
controller!.PrintToChat($"{Prefix}{Localizer["delayRemaining", FormatZoneString(zone.Type), diff.ToString("0")]}");
controller.PrintToChat($"{Prefix}{Localizer["delayRemaining", FormatZoneString(zone.Type), diff.ToString("0")]}");
}
else
bounce = DoAction(controller, zone);
Expand All @@ -50,7 +50,7 @@ private void OnTick()
if (bounce)
continue;

_playerData[controller].LastPosition = new Vector(controller!.PlayerPawn.Value!.AbsOrigin!.X, controller!.PlayerPawn.Value!.AbsOrigin!.Y, controller!.PlayerPawn.Value!.AbsOrigin!.Z);
_playerData[controller].LastPosition = new Vector(controller.PlayerPawn.Value!.AbsOrigin!.X, controller.PlayerPawn.Value.AbsOrigin.Y, controller.PlayerPawn.Value.AbsOrigin.Z);
_playerData[controller].LastVelocity = new Vector(controller.PlayerPawn.Value.AbsVelocity.X, controller.PlayerPawn.Value.AbsVelocity.Y, controller.PlayerPawn.Value.AbsVelocity.Z);
}
}
Expand Down
52 changes: 43 additions & 9 deletions src/Menus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ private void BuildMenu(CCSPlayerController controller, MenuType type = MenuType.
break;

case MenuType.View:
BuildViewZoneMenu(controller);
break;
}
}
Expand All @@ -44,15 +45,16 @@ private void BuildMainMenu(CCSPlayerController controller, bool updateMenu = fal

var customButtons = new List<MenuValue>
{
new CustomButton(Localizer["menu.Add"], c => BuildMenu(c, MenuType.Add)) { Suffix = "<font color=\"#FFFFFF\">" },
new CustomButton(Localizer["menu.View"], c=> BuildMenu(c, MenuType.View))
new CustomButton(Localizer["menu.Add"], c => BuildMenu(c, MenuType.Add)) { Suffix = "<font color=\"#FFFFFF\">" }
};

// new CustomButton(Localizer["menu.View"], c=> BuildMenu(c, MenuType.View))

customButtons[0].Prefix = !AdminManager.PlayerHasPermissions(controller, "@css/root") ? "<font color=\"#808080\">" : "";

mainMenu.AddItem(new MenuItem(MenuItemType.Button, customButtons));
mainMenu.AddItem(new MenuItem(MenuItemType.Spacer));
mainMenu.AddItem(new MenuItem(MenuItemType.Bool, new MenuValue($"{Localizer["menu.Debug"]} ")) { Data = [_playerData[controller].Debug ? 1 : 0] });
//mainMenu.AddItem(new MenuItem(MenuItemType.Spacer));
//mainMenu.AddItem(new MenuItem(MenuItemType.Bool, new MenuValue($"{Localizer["menu.Debug"]} ")) { Data = [_playerData[controller].Debug ? 1 : 0] });

if (_playerData[controller].Debug)
{
Expand Down Expand Up @@ -137,7 +139,7 @@ private void BuildAddZoneMenu(CCSPlayerController controller)
? new MenuItem(MenuItemType.Input, new MenuValue($"{Localizer["menu.Damage"]} "), new MenuValue($" {Localizer["menu.PerSecond"]}"))
: new MenuItem(MenuItemType.Spacer));

addZoneMenu.AddItem(new MenuItem(MenuItemType.Button, [new CustomButton(Localizer["menu.Save"], SaveZone) { Prefix = "<font color=\"#ADD8E6\">" }]));
addZoneMenu.AddItem(new MenuItem(MenuItemType.Button, [new CustomButton(Localizer["menu.Save"], SaveZone)]));

addZoneMenu.Items[3].DataString = "0.0";
addZoneMenu.Items[4].DataString = "10";
Expand Down Expand Up @@ -165,11 +167,17 @@ private void BuildAddZoneMenu(CCSPlayerController controller)
if (menu.Option == 2 && selectedItem!.DataString.Length > 16)
selectedItem.DataString = selectedItem.DataString[..16];
if (menu.Option == 3 && !float.TryParse(selectedItem!.DataString, out _))
if (menu.Option == 3)
{
controller.PrintToChat($"{Prefix}{Localizer["invalidInput", selectedItem.DataString, "float"]}");
selectedItem.DataString = "0.0";
menu.AcceptInput = true;
if (!float.TryParse(selectedItem!.DataString, out var delay))
{
controller.PrintToChat(
$"{Prefix}{Localizer["invalidInput", selectedItem.DataString, "float"]}");
selectedItem.DataString = "0.0";
menu.AcceptInput = true;
}
else
selectedItem.DataString = delay.ToString("0.0");
}
if (menu.Option == 4 && !int.TryParse(selectedItem!.DataString, out _))
Expand All @@ -194,6 +202,27 @@ private void BuildAddZoneMenu(CCSPlayerController controller)
}
});
}

private void BuildViewZoneMenu(CCSPlayerController controller)
{
var viewZoneMenu = new AddZoneMenu(new MenuValue(Localizer["menu.View"]) { Suffix = "<font class=\"fontSize-m\">" });

if (_zones.Count == 0)
viewZoneMenu.AddItem(new MenuItem(MenuItemType.Text, new MenuValue(Localizer["menu.None"])));
else
{
var zones = _zones.Select(zone => new ZoneValue(zone.Name, zone)).Cast<MenuValue>().ToList();
viewZoneMenu.AddItem(new MenuItem(MenuItemType.Choice, zones, true));

viewZoneMenu.AddItem(new MenuItem(MenuItemType.Spacer));
viewZoneMenu.AddItem(new MenuItem(MenuItemType.Button, [new MenuValue(Localizer["menu.Teleport"]), new MenuValue(Localizer["menu.Delete"])]));
}

Menu.AddMenu(controller, viewZoneMenu, (buttons, menu, selectedItem) =>
{
});
}
}

public class CustomButton(string value, Action<CCSPlayerController> callback) : MenuValue(value)
Expand All @@ -205,4 +234,9 @@ public class AddZoneMenu(MenuValue title) : MenuBase(title)
{
public Vector[] Points { get; set; } = [Vector.Zero, Vector.Zero];
public float LastShot { get; set; }
}

public class ZoneValue(string title, Zone zone) : MenuValue(title)
{
public Zone Zone { get; set; } = zone;
}

0 comments on commit 8e74821

Please sign in to comment.