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
6 changes: 2 additions & 4 deletions AutomaticAds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace AutomaticAds;
public class AutomaticAdsBase : BasePlugin, IPluginConfig<BaseConfigs>
{
public override string ModuleName => "AutomaticAds";
public override string ModuleVersion => "1.2.0";
public override string ModuleVersion => "1.2.1";
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 @@ -32,8 +32,6 @@ public class AutomaticAdsBase : BasePlugin, IPluginConfig<BaseConfigs>
private CCSGameRules? _gameRulesProxy;

// CenterHtml message tracking and configuration
private const float CENTERHTML_DURATION_SECONDS = 5.0f;

private readonly Dictionary<int, DateTime> _centerHtmlStartTimes = new();
private readonly Dictionary<int, string> _activeCenterHtmlMessages = new();
private readonly Dictionary<int, DateTime> _lastCenterHtmlUpdateTimes = new();
Expand Down Expand Up @@ -198,7 +196,7 @@ private void OnTick()
var startTime = _centerHtmlStartTimes[playerId];
var elapsedTime = (currentTime - startTime).TotalSeconds;

if (elapsedTime >= CENTERHTML_DURATION_SECONDS)
if (elapsedTime >= Config.centerHtmlDisplayTime)
{
playersToRemove.Add(playerId);
continue;
Expand Down
7 changes: 5 additions & 2 deletions Config/BaseConfigs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ public class BaseConfigs : BasePluginConfig
public bool EnableJoinLeaveMessages { get; set; } = true;

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

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

[JsonPropertyName("Welcome")]
public List<WelcomeConfig> Welcome { get; set; } = new()
{
new WelcomeConfig
{
WelcomeMessage = "{BLUE}Welcome to the server {playername}! {RED}Playing on {map} with {players}/{maxplayers} players.",
WelcomeMessage = "{prefix} {BLUE}Welcome to the server {playername}! {RED}Playing on {map} with {players}/{maxplayers} players.",
ViewFlag = "all",
ExcludeFlag = "",
DisableSound = false
Expand Down
10 changes: 10 additions & 0 deletions Config/ConfigValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public static void ValidateConfig(BaseConfigs config)
ValidateAds(config.Ads);
ValidateChatPrefix(config);
ValidateGlobalPlaySound(config);
ValidateCenterHtmlDisplayTime(config);
}

private static void ValidateAds(List<BaseConfigs.AdConfig> ads)
Expand Down Expand Up @@ -56,4 +57,13 @@ private static void ValidateGlobalPlaySound(BaseConfigs config)
config.GlobalPlaySound = string.Empty;
}
}

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

}
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ The configuration file will be automatically generated when the plugin is first
| `UseWelcomeMessage` | Set to `true` to enable the welcome message. Set to `false` to disable it. | **YES** |
| `JoinLeaveMessages` | Set `true` to enable custom connection and disconnection messages. Set `false` to disable it. | **YES** |
| `WelcomeDelay` | This is the time the plugin will wait to send the welcome message after the player connects (**Default**: 3s). | **YES** |
| `centerHtmlDisplayTime` | "Duration (in seconds) that ads with `displayType` set to `CenterHtml` will remain visible. (**Default**: 5s) | **YES** |
| `Welcome` | Configuration for the welcome announcement. Supports variables ***(see example below)***. | **NO** |
| `JoinLeave` | Configuration for connection and disconnection messages. Supports variables ***(see example below)***. | **NO** |
| `Ads` | List of advertisements to be sent. Each ad can be configured individually ***(see example below)***. | **YES** |
Expand Down Expand Up @@ -66,7 +67,7 @@ Here is an example configuration file:
"UseWelcomeMessage": true,
"Welcome": [
{
"WelcomeMessage": "{BLUE}Welcome to the server {playername}! {RED}Playing on {map} with {players}/{maxplayers} players.",
"WelcomeMessage": "{prefix} {BLUE}Welcome to the server {playername}! {RED}Playing on {map} with {players}/{maxplayers} players.",
"viewFlag": "all",
"excludeFlag": "",
"disableSound": false
Expand Down Expand Up @@ -128,6 +129,7 @@ You can use the following placeholders in your announcements:
|--------------|---------------------------------------------------------------|
| `{prefix}` | A dynamic variable replaced with the value of ChatPrefix in each message. Enables optional inclusion or exclusion of the prefix. |
| `{ip}` | The server's IP address. |
| `{port}` | The server's port. |
| `{hostname}` | The server's hostname. |
| `{map}` | The current map being played. |
| `{time}` | The current time in the server's timezone. |
Expand Down
4 changes: 2 additions & 2 deletions Services/WelcomeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ private void ValidateWelcomeFlags(BaseConfigs.WelcomeConfig welcome)
private void SendWelcomeToPlayer(CCSPlayerController player, BaseConfigs.WelcomeConfig welcome, string playerName)
{
string prefix = _messageFormatter.FormatMessage(_config.ChatPrefix);
string welcomeMessage = _messageFormatter.FormatMessage(welcome.WelcomeMessage, playerName);
string welcomeMessage = _messageFormatter.FormatMessage(welcome.WelcomeMessage, playerName, prefix);

_playerManager.SendMessageToPlayer(player, $"{prefix} {welcomeMessage}");
_playerManager.SendMessageToPlayer(player, welcomeMessage);

if (!welcome.DisableSound && !string.IsNullOrWhiteSpace(_config.GlobalPlaySound))
{
Expand Down
17 changes: 17 additions & 0 deletions Utils/MessageFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ private Dictionary<string, string> GetServerVariables()
try
{
string ip = GetServerIp();
string port = GetServerPort();
string hostname = GetServerHostname();
string map = Server.MapName;
string time = DateTime.Now.ToString("HH:mm");
Expand All @@ -132,6 +133,7 @@ private Dictionary<string, string> GetServerVariables()
return new Dictionary<string, string>
{
{ "{ip}", ip },
{ "{port}", port },
{ "{hostname}", hostname },
{ "{map}", map },
{ "{time}", time },
Expand Down Expand Up @@ -163,6 +165,20 @@ private string GetServerIp()
}
}

private string GetServerPort()
{
try
{
var portCvar = ConVar.Find("hostport");
int port = portCvar?.GetPrimitiveValue<int>() ?? 27015;
return port.ToString();
}
catch
{
return "27015";
}
}

private string GetServerHostname()
{
try
Expand Down Expand Up @@ -192,6 +208,7 @@ private Dictionary<string, string> GetFallbackServerVariables()
return new Dictionary<string, string>
{
{ "{ip}", "Unknown:27015" },
{ "{port}", "27015" },
{ "{hostname}", Constants.ErrorMessages.Unknown },
{ "{map}", Server.MapName ?? "Unknown" },
{ "{time}", DateTime.Now.ToString("HH:mm") },
Expand Down