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

[WIP] Add bots support #34

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
-- 2024.12.23 - 1.5.5

- feat: Added 3 new natives - IsAFK, FindOpponents & TerminateRoundIfPossible

-- 2024.12.20 - 1.5.4

- fix: PerformAFKAction didnt change the AFK state of player
Expand Down
198 changes: 198 additions & 0 deletions src-botsplugin/K4-Arenas-Bots.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@

using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes;
using CounterStrikeSharp.API.Core.Capabilities;
using CounterStrikeSharp.API.Modules.Cvars;
using K4ArenaSharedApi;
using Microsoft.Extensions.Logging;

namespace K4ArenaBots;

[MinimumApiVersion(284)]
public class Plugin : BasePlugin
{
public override string ModuleName => "K4-Arenas Addon - Bots Support";
public override string ModuleDescription => "Adds a bot in empty arena if there is no opponent.";
public override string ModuleAuthor => "Cruze";
public override string ModuleVersion => "1.0.0";

public static PluginCapability<IK4ArenaSharedApi> Capability_SharedAPI { get; } = new("k4-arenas:sharedapi");
public static IK4ArenaSharedApi? SharedAPI_Arena { get; private set; } = null;

private CCSGameRules? gameRules = null;
private string botQuotaMode = "normal";

public override void OnAllPluginsLoaded(bool hotReload)
{
SharedAPI_Arena = Capability_SharedAPI.Get();
}

public override void Load(bool hotReload)
{
if(hotReload)
{
gameRules = Utilities.FindAllEntitiesByDesignerName<CCSGameRulesProxy>("cs_gamerules").First().GameRules;

SharedAPI_Arena = Capability_SharedAPI.Get();

var quota = ConVar.Find("bot_quota_mode");
botQuotaMode = quota?.StringValue ?? "normal";
}

RegisterListener<Listeners.OnMapStart>((mapName) =>
{
DeleteOldGameConfig();
AddTimer(0.1f, () =>
{
gameRules = Utilities.FindAllEntitiesByDesignerName<CCSGameRulesProxy>("cs_gamerules").First().GameRules;

var quota = ConVar.Find("bot_quota_mode");
botQuotaMode = quota?.StringValue ?? "normal";
});
});

RegisterListener<Listeners.OnMapEnd>(() =>
{
gameRules = null;
});

RegisterEventHandler((EventPlayerSpawn @event, GameEventInfo info) =>
{
var player = @event.Userid;

if(player == null || !player.IsValid || player.IsBot || player.IsHLTV || SharedAPI_Arena == null || SharedAPI_Arena.IsAFK(player))
return HookResult.Continue;

SpawnBotInEmptyArena(player);

return HookResult.Continue;
});

RegisterEventHandler((EventPlayerDisconnect @event, GameEventInfo info) =>
{
var player = @event.Userid;

if(player == null || !player.IsValid || !player.IsBot)
return HookResult.Continue;

info.DontBroadcast = true;
return HookResult.Continue;
}, HookMode.Pre);

RegisterEventHandler((EventRoundPrestart @event, GameEventInfo info) =>
{
if(gameRules == null || gameRules.WarmupPeriod || SharedAPI_Arena == null)
{
Server.ExecuteCommand($"bot_prefix \"WARMUP\"");
return HookResult.Continue;
}

(var players, var bots) = GetPlayers();

if(!bots.Any() || bots.First() == null)
{
Server.ExecuteCommand($"bot_prefix \"\"");
return HookResult.Continue;
}

var bot = bots.First();
var arenaS = SharedAPI_Arena?.GetArenaName(bot) + " |" ?? "";

Server.ExecuteCommand($"bot_prefix {arenaS}");
return HookResult.Continue;
}, HookMode.Post);

RegisterEventHandler((EventRoundEnd @event, GameEventInfo info) =>
{
SpawnBotInEmptyArena(null, true);
return HookResult.Continue;
});
}

private void SpawnBotInEmptyArena(CCSPlayerController? play, bool roundEnd = false)
{
if(gameRules == null || gameRules.WarmupPeriod || SharedAPI_Arena == null)
return;

(var players, var bots) = GetPlayers();

Logger.LogInformation($"Players: {players.Count()} | Bots: {bots.Count()}");

if(players.Count() % 2 == 0)
{
Logger.LogInformation($"Even players, no need to spawn bot.");
if(bots.Count() > 0)
Server.ExecuteCommand("bot_quota 0");
return;
}

if(play != null && SharedAPI_Arena.FindOpponents(play).Count() > 0)
return;

if(!bots.Any() || bots.Count() > 1)
{
if(botQuotaMode == "fill")
Server.ExecuteCommand("bot_quota 2");
else
Server.ExecuteCommand("bot_quota 1");

Logger.LogInformation($"Spawning bot in empty arena.");
}

if(roundEnd)
return;

AddTimer(0.1f, () =>
{
players = new();
bots = new();
(players, bots) = GetPlayers();
if(!bots.Any() || bots.First() == null)
return;

SharedAPI_Arena.TerminateRoundIfPossible();
});
}

private void DeleteOldGameConfig()
{
// If bot_quota 0 exists in gameconfig.cfg, unlimited round restart will be there so we need to delete it & create a fresh config without it.
// Creating of new file is handled by main plugin with the update.

string filePath = Path.Combine(Server.GameDirectory, "csgo/addons/counterstrikesharp/plugins", "K4-Arenas", "gameconfig.cfg");

if(File.Exists(filePath))
{
if(File.ReadAllText(filePath).Contains("bot_quota "))
{
Logger.LogWarning($"Old gameconfig file found, deleting it.");
File.Delete(filePath);
}
}
}

private (List<CCSPlayerController>, List<CCSPlayerController>) GetPlayers()
{
var players = new List<CCSPlayerController>();
var bots = new List<CCSPlayerController>();

for (int i = 0; i < Server.MaxPlayers; i++)
{
var controller = Utilities.GetPlayerFromSlot(i);

if (controller == null || !controller.IsValid || controller.IsHLTV)
continue;

if(controller.IsBot)
{
bots.Add(controller);
continue;
}

if(controller.Connected == PlayerConnectedState.PlayerConnected)
players.Add(controller);
}
return (players, bots);
}
}
29 changes: 29 additions & 0 deletions src-botsplugin/K4-Arenas-Bots.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup Condition="'$(Configuration)'=='Release'">
<DebugSymbols>False</DebugSymbols>
<DebugType>None</DebugType>
<GenerateDependencyFile>false</GenerateDependencyFile>
<PublishDir>./bin/K4-Arenas-Bots/plugins/K4-Arenas-Bots/</PublishDir>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CounterStrikeSharp.API" Version="*">
<PrivateAssets>none</PrivateAssets>
<ExcludeAssets>runtime</ExcludeAssets>
<IncludeAssets>compile; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Dapper" Version="*" />
<PackageReference Include="MySqlConnector" Version="*" />
<Reference Include="K4ArenaSharedApi">
<HintPath>../src-shared/K4-ArenaSharedApi.dll</HintPath>
<Private>false</Private>
</Reference>
</ItemGroup>
<Target Name="CopyCustomFilesToPublishDirectory" AfterTargets="Publish">
<Copy SourceFiles="$(ProjectDir)$(ReferencePath)../src-shared/K4-ArenaSharedApi.dll" DestinationFolder="$(PublishDir)../../shared/K4-ArenaSharedApi/" />
</Target>
</Project>
7 changes: 6 additions & 1 deletion src-plugin/Plugin/Models/ArenaModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,12 @@ public void SetPlayerDetails(List<ArenaPlayer>? team, List<SpawnPoint> spawns, C

if (ArenaID != -1)
{
player.Controller.PrintToChat($" {Localizer["k4.general.prefix"]} {Localizer["k4.chat.arena_roundstart", Plugin.GetRequiredArenaName(ArenaID), Plugin.GetOpponentNames(opponents) ?? "Unknown", Localizer[RoundType.Name ?? "Missing"]]}");
// Bots plugin sets bot_prefix at EventRoundPreStart hence some delay to print opponent names
Server.NextWorldUpdate(() =>
{
if(player.Controller.IsValid)
player.Controller.PrintToChat($" {Localizer["k4.general.prefix"]} {Localizer["k4.chat.arena_roundstart", Plugin.GetRequiredArenaName(ArenaID), Plugin.GetOpponentNames(opponents) ?? "Unknown", Localizer[RoundType.Name ?? "Missing"]]}");
});
}

if (Plugin.gameRules?.WarmupPeriod == true)
Expand Down
20 changes: 20 additions & 0 deletions src-plugin/Plugin/Models/ArenasModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,26 @@ public Arenas(Plugin plugin)
return allPlayers.FirstOrDefault(p => p.Controller == player);
}

public List<CCSPlayerController> FindOpponents(CCSPlayerController? player)
{
var arenaPlayer = FindPlayer(player);

if (arenaPlayer is null)
return new List<CCSPlayerController>();

var arenaID = Plugin.GetPlayerArenaID(arenaPlayer);

if(arenaID < 0)
return new List<CCSPlayerController>();

var arena = ArenaList.FirstOrDefault(a => a.ArenaID == arenaID);
if (arena == null)
return new List<CCSPlayerController>();

var opponents = arena.Team1?.Any(p => p.Controller == player) == true ? arena.Team2 : arena.Team1;
return opponents?.Select(p => p.Controller).ToList() ?? new List<CCSPlayerController>();
}

public ArenaPlayer? FindPlayer(ulong steamId)
{
IEnumerable<ArenaPlayer> allPlayers = Plugin.WaitingArenaPlayers
Expand Down
13 changes: 10 additions & 3 deletions src-plugin/Plugin/Models/GameConfigModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,15 @@ public GameConfig(Plugin plugin)

public void Apply()
{
if (ConfigSettings is null)
return;
string filePath = Path.Combine(Plugin.ModuleDirectory, "gameconfig.cfg");

if (!File.Exists(filePath) || ConfigSettings is null)
{
Load();

if(ConfigSettings is null)
return;
}

foreach (var (key, value) in ConfigSettings)
{
Expand Down Expand Up @@ -63,7 +70,7 @@ private void Create(string path)
var defaultConfigLines = new List<string>
{
"// Changing these might break the gamemode",
"bot_quota 0",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

somehow let the bot_quota be in config if bots plugin exists

"bot_quota_mode \"normal\"",
"mp_autoteambalance 0",
"mp_ct_default_primary \"\"",
"mp_ct_default_secondary \"\"",
Expand Down
25 changes: 25 additions & 0 deletions src-plugin/Plugin/PluginAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,31 @@ public string GetArenaName(CCSPlayerController player)
return string.Empty;
}

public bool IsAFK(CCSPlayerController player)
{
var arenaPlayer = plugin.Arenas?.FindPlayer(player);
if (arenaPlayer is not null)
{
return arenaPlayer.AFK;
}
return false;
}

public List<CCSPlayerController> FindOpponents(CCSPlayerController player)
{
var arenaOpponents = plugin.Arenas?.FindOpponents(player);
if (arenaOpponents is not null)
{
return arenaOpponents;
}
return new List<CCSPlayerController>();
}

public void TerminateRoundIfPossible()
{
plugin.TerminateRoundIfPossible();
}

public void PerformAFKAction(CCSPlayerController player, bool afk)
{
ArenaPlayer? arenaPlayer = plugin.Arenas?.FindPlayer(player!);
Expand Down
2 changes: 1 addition & 1 deletion src-plugin/Plugin/PluginEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ public void Initialize_Events()
}

return HookResult.Continue;
});
}, HookMode.Pre);

RegisterEventHandler((EventRoundStart @event, GameEventInfo info) =>
{
Expand Down
2 changes: 1 addition & 1 deletion src-plugin/Plugin/PluginManifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public sealed partial class Plugin : BasePlugin

public override string ModuleAuthor => "K4ryuu";

public override string ModuleVersion => "1.5.4 " +
public override string ModuleVersion => "1.5.5 " +
#if RELEASE
"(release)";
#else
Expand Down
16 changes: 15 additions & 1 deletion src-plugin/Plugin/PluginStock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,21 @@ public string GetOpponentNames(List<ArenaPlayer>? opponents)
return Localizer["k4.general.no_opponent"];


return string.Join(", ", opponents.Where(p => p.IsValid).Select(p => p.Controller.PlayerName));
return string.Join(", ", opponents.Where(p => p.IsValid).Select(p => p.Controller.IsBot && !string.IsNullOrEmpty(GetArenaName(p.Controller)) ? $"{Localizer["k4.general.bot"]} " + p.Controller.PlayerName.Replace(GetArenaName(p.Controller), "").Replace("|", "") : p.Controller.PlayerName));
}

public string GetArenaName(CCSPlayerController player)
{
var arenaPlayer = Arenas?.FindPlayer(player);
if (arenaPlayer is not null)
{
string arenaTag = arenaPlayer.ArenaTag;
if (arenaTag.EndsWith(" |"))
arenaTag = arenaTag.Substring(0, arenaTag.Length - 2);

return arenaTag;
}
return string.Empty;
}

public static CsItem? FindEnumValueByEnumMemberValue(string? search)
Expand Down
1 change: 1 addition & 0 deletions src-plugin/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"k4.general.warmup": "WARMUP",
"k4.general.challenge": "CHALLENGE",
"k4.general.random": "Random",
"k4.general.bot": "BOT",

"k4.general.challenge.tie": "{silver}The challenge between {lime}{0}{silver} and {lime}{1}{silver} ended in a tie.",
"k4.general.challenge.winner": "{lime}{0}{silver} won the challenge against {lime}{1}{silver}.",
Expand Down
1 change: 1 addition & 0 deletions src-plugin/lang/pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"k4.general.warmup": "ROZGRZEWKA",
"k4.general.challenge": "WYZWANIE",
"k4.general.random": "Losowo",
"k4.general.bot": "BOT",

"k4.general.challenge.tie": "{silver}Wyzwanie pomiędzy {lime}{0}{silver} a {lime}{1}{silver} zakończyło się remisem.",
"k4.general.challenge.winner": "{lime}{0}{silver} wygrał wyzwanie z {lime}{1}{silver}.",
Expand Down
Loading