-
Notifications
You must be signed in to change notification settings - Fork 8
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
Cruze03
wants to merge
4
commits into
main
Choose a base branch
from
bots-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+362
−12
Draft
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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