Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
39 changes: 36 additions & 3 deletions AutomaticAds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using CounterStrikeSharp.API.Core.Attributes;
using CounterStrikeSharp.API.Modules.Admin;
using CounterStrikeSharp.API.Core.Attributes.Registration;

using AutomaticAds.Config;
using AutomaticAds.Config.Models;
using AutomaticAds.Services;
Expand All @@ -11,11 +12,11 @@

namespace AutomaticAds;

[MinimumApiVersion(290)]
[MinimumApiVersion(315)]
public class AutomaticAdsBase : BasePlugin, IPluginConfig<BaseConfigs>
{
public override string ModuleName => "AutomaticAds";
public override string ModuleVersion => "1.2.6";
public override string ModuleVersion => "1.2.7";
public override string ModuleAuthor => "luca.uy";
public override string ModuleDescription => "Send automatic messages to the chat and play a sound alert for users to see the message.";

Expand All @@ -24,6 +25,7 @@ public class AutomaticAdsBase : BasePlugin, IPluginConfig<BaseConfigs>
private WelcomeService? _welcomeService;
private JoinLeaveService? _joinLeaveService;
private IIPQueryService? _ipQueryService;
private ScreenTextService? _screenTextService;
private TimerManager? _timerManager;
private PlayerManager? _playerManager;
private MessageFormatter? _messageFormatter;
Expand Down Expand Up @@ -70,6 +72,8 @@ private void InitializeServices()
_timerManager = new TimerManager(this);
_playerManager = new PlayerManager(this);
_ipQueryService = new IPQueryService();
_screenTextService = new ScreenTextService(_timerManager, Config.ScreenDisplayTime);
_playerManager.SetScreenTextService(_screenTextService);

_adService = new AdService(Config, _messageFormatter, _timerManager, _playerManager, _ipQueryService);
_welcomeService = new WelcomeService(Config, _messageFormatter, _timerManager, _playerManager);
Expand Down Expand Up @@ -99,6 +103,7 @@ private void RegisterEventHandlers()
RegisterEventHandler<EventPlayerConnectFull>(OnPlayerFullConnect);
RegisterEventHandler<EventPlayerDisconnect>(OnPlayerDisconnect);
RegisterEventHandler<EventPlayerDisconnect>(OnPlayerDisconnectPre, HookMode.Pre);
RegisterEventHandler<EventPlayerDeath>(OnPlayerDeathPost, HookMode.Post);
}

private void RegisterCommands()
Expand Down Expand Up @@ -180,7 +185,22 @@ private void HandleTriggerCommand(CCSPlayerController? player, AdConfig ad)

if (!string.IsNullOrWhiteSpace(formattedMessage))
{
_playerManager!.SendMessageToPlayer(player!, formattedMessage, ad.DisplayType);
DisplayType effectiveDisplayType = ad.DisplayType;
if (ad.DisplayType == DisplayType.Screen && !player!.PawnIsAlive)
{
effectiveDisplayType = DisplayType.Chat;
}

if (effectiveDisplayType == DisplayType.Screen)
{
float positionX = ad.GetEffectivePositionX(Config.GlobalPositionX);
float positionY = ad.GetEffectivePositionY(Config.GlobalPositionY);
_playerManager!.SendMessageToPlayer(player!, formattedMessage, effectiveDisplayType, positionX, positionY);
}
else
{
_playerManager!.SendMessageToPlayer(player!, formattedMessage, effectiveDisplayType);
}

string soundToPlay = ad.PlaySoundName ?? Config.GlobalPlaySound ?? string.Empty;
if (!ad.DisableSound && !string.IsNullOrWhiteSpace(soundToPlay))
Expand Down Expand Up @@ -385,6 +405,7 @@ private HookResult OnPlayerDisconnect(EventPlayerDisconnect @event, GameEventInf
_joinLeaveService?.HandlePlayerLeave(player!);
_welcomeService?.OnPlayerDisconnect(player!);
_joinLeaveService?.OnPlayerDisconnect(player!);
_screenTextService?.OnPlayerDisconnect(player!);

return HookResult.Continue;
}
Expand All @@ -400,9 +421,21 @@ public HookResult OnPlayerDeath(EventPlayerDeath gameEvent, GameEventInfo info)
return HookResult.Continue;
}

[GameEventHandler(HookMode.Post)]
public HookResult OnPlayerDeathPost(EventPlayerDeath gameEvent, GameEventInfo info)
{
var player = gameEvent.Userid;
if (!player.IsValidPlayer())
return HookResult.Continue;

_screenTextService?.HideTextFromScreen(player!);
return HookResult.Continue;
}

public override void Unload(bool hotReload)
{
_timerManager?.KillAllTimers();
_screenTextService?.ClearAllPlayerTexts();
RemoveListener<Listeners.OnTick>(OnTick);
}
}
2 changes: 1 addition & 1 deletion AutomaticAds.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.290" />
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.315" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<None Update="lang\**\*.*" CopyToOutputDirectory="PreserveNewest" />
<None Update="Newtonsoft.Json.dll" CopyToOutputDirectory="PreserveNewest" />
Expand Down
15 changes: 15 additions & 0 deletions Config/BaseConfigs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ public class BaseConfigs : BasePluginConfig
[JsonPropertyName("GlobalInterval")]
public float GlobalInterval { get; set; } = 30.0f;

[JsonPropertyName("GlobalPositionX")]
public float GlobalPositionX { get; set; } = -1.5f;

[JsonPropertyName("GlobalPositionY")]
public float GlobalPositionY { get; set; } = 1f;

[JsonPropertyName("AdminFlag")]
public string? AdminFlag { get; set; } = "@css/generic";

Expand All @@ -33,6 +39,9 @@ public class BaseConfigs : BasePluginConfig
[JsonPropertyName("CenterHtmlDisplayTime")]
public float centerHtmlDisplayTime { get; set; } = 5.0f;

[JsonPropertyName("ScreenDisplayTime")]
public float ScreenDisplayTime { get; set; } = 5.0f;

[JsonPropertyName("UseMultiLang")]
public bool UseMultiLang { get; set; } = true;

Expand Down Expand Up @@ -106,6 +115,12 @@ public class BaseConfigs : BasePluginConfig
ViewFlag = "@css/generic",
DisplayType = DisplayType.CenterHtml,
onDead = true
},
new AdConfig
{
Message = "Thanks for playing {playername}",
DisplayType = DisplayType.Screen,
DisableSound = true
}
};
}
82 changes: 76 additions & 6 deletions Config/ConfigValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ public static class ConfigValidator
public static void ValidateConfig(BaseConfigs config)
{
ValidateGlobalInterval(config);
ValidateAds(config.Ads, config.GlobalInterval);
ValidateGlobalPositions(config);
ValidateAds(config.Ads, config.GlobalInterval, config.GlobalPositionX, config.GlobalPositionY);
ValidateChatPrefix(config);
ValidateGlobalPlaySound(config);
ValidateCenterHtmlDisplayTime(config);
ValidateScreenDisplayTime(config);
}

private static void ValidateGlobalInterval(BaseConfigs config)
Expand All @@ -27,11 +29,33 @@ private static void ValidateGlobalInterval(BaseConfigs config)
}
}

private static void ValidateAds(List<AdConfig> ads, float globalInterval)
private static void ValidateGlobalPositions(BaseConfigs config)
{
if (config.GlobalPositionX > Constants.MaxPositionX)
{
config.GlobalPositionX = Constants.MaxPositionX;
}
if (config.GlobalPositionX < Constants.MinPositionX)
{
config.GlobalPositionX = Constants.MinPositionX;
}

if (config.GlobalPositionY > Constants.MaxPositionY)
{
config.GlobalPositionY = Constants.MaxPositionY;
}
if (config.GlobalPositionY < Constants.MinPositionY)
{
config.GlobalPositionY = Constants.MinPositionY;
}
}

private static void ValidateAds(List<AdConfig> ads, float globalInterval, float globalPositionX, float globalPositionY)
{
foreach (var ad in ads)
{
ValidateAdInterval(ad, globalInterval);
ValidateAdPositions(ad, globalPositionX, globalPositionY);
ValidateTriggerAd(ad);
}
}
Expand All @@ -46,10 +70,6 @@ private static void ValidateAdInterval(AdConfig ad, float globalInterval)
{
ad.IntervalRaw = Constants.MaxInterval;
}
else
{

}
}

if (effectiveInterval < Constants.MinInterval)
Expand All @@ -63,6 +83,43 @@ private static void ValidateAdInterval(AdConfig ad, float globalInterval)
ad.Interval = ad.GetEffectiveInterval(globalInterval);
}

private static void ValidateAdPositions(AdConfig ad, float globalPositionX, float globalPositionY)
{
float effectivePositionX = ad.GetEffectivePositionX(globalPositionX);
if (effectivePositionX > Constants.MaxPositionX)
{
if (ad.HasCustomPositionX)
{
ad.PositionXRaw = Constants.MaxPositionX;
}
}
if (effectivePositionX < Constants.MinPositionX)
{
if (ad.HasCustomPositionX)
{
ad.PositionXRaw = Constants.MinPositionX;
}
}
ad.PositionX = ad.GetEffectivePositionX(globalPositionX);

float effectivePositionY = ad.GetEffectivePositionY(globalPositionY);
if (effectivePositionY > Constants.MaxPositionY)
{
if (ad.HasCustomPositionY)
{
ad.PositionYRaw = Constants.MaxPositionY;
}
}
if (effectivePositionY < Constants.MinPositionY)
{
if (ad.HasCustomPositionY)
{
ad.PositionYRaw = Constants.MinPositionY;
}
}
ad.PositionY = ad.GetEffectivePositionY(globalPositionY);
}

private static void ValidateTriggerAd(AdConfig ad)
{
if (ad.TriggerAd != null)
Expand Down Expand Up @@ -94,4 +151,17 @@ private static void ValidateCenterHtmlDisplayTime(BaseConfigs config)
config.centerHtmlDisplayTime = 5.0f;
}
}

private static void ValidateScreenDisplayTime(BaseConfigs config)
{
if (config.ScreenDisplayTime <= 0)
{
config.ScreenDisplayTime = 5.0f;
}

if (config.ScreenDisplayTime > 30.0f)
{
config.ScreenDisplayTime = 30.0f;
}
}
}
3 changes: 2 additions & 1 deletion Config/DisplayType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ public enum DisplayType
{
Chat,
Center,
CenterHtml
CenterHtml,
Screen
}
Loading