Skip to content

Commit

Permalink
Release 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dronelektron committed Jun 28, 2022
2 parents 3b1770d + fb8ca05 commit 1f48a4a
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 126 deletions.
143 changes: 17 additions & 126 deletions scripting/block-spectators.sp
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,26 @@
#pragma semicolon 1
#pragma newdecls required

#define PREFIX_COLORED "{green}[Block spectators] "
#define ENTITY_NOT_FOUND -1
#define TEAM_ARG_MAX_SIZE 2
#define TEAM_SPECTATOR 1
#define ROUND_TIMER_EXISTS_DEFAULT_VALUE true
#include "bs/console-command"
#include "bs/console-variable"
#include "bs/message"
#include "bs/use-case"

#define NOTIFICATIONS_JOIN_ATTEMPT (1 << 0)
#define NOTIFICATIONS_ROUND_END (1 << 1)
#define NOTIFICATIONS_TIMER (1 << 2)
#include "modules/console-command.sp"
#include "modules/console-variable.sp"
#include "modules/message.sp"
#include "modules/use-case.sp"

public Plugin myinfo = {
name = "Block spectators",
author = "Dron-elektron",
description = "Allows you to block the spectators team at the end of the round or earlier",
version = "0.4.0",
url = ""
version = "1.0.0",
url = "https://github.com/dronelektron/block-spectators"
};

ConVar g_pluginEnabled = null;
ConVar g_notificationsMode = null;
ConVar g_blockTimeOffset = null;

Handle g_blockTimer = null;
float g_blockTimerEndTime = 0.0;
bool g_isSpectatorsBlocked = false;
bool g_isRoundTimerExists = ROUND_TIMER_EXISTS_DEFAULT_VALUE;

public void OnPluginStart() {
g_pluginEnabled = CreateConVar("sm_blockspectators", "1", "Enable (1) or disable (0) spectators team blocking");
g_notificationsMode = CreateConVar("sm_blockspectators_notifications_mode", "7", "None (0), Join attempt (1), Round end (2), Timer (4)");
g_blockTimeOffset = CreateConVar("sm_blockspectators_time_offset", "0", "Time offset (in seconds) until the end of the round");

Variable_Create();
HookEvent("dod_round_start", Event_RoundStart);
HookEvent("dod_round_active", Event_RoundActive);
HookEvent("dod_round_win", Event_RoundWin);
Expand All @@ -48,124 +36,27 @@ public void OnPluginStart() {
}

public void OnMapStart() {
g_isRoundTimerExists = ROUND_TIMER_EXISTS_DEFAULT_VALUE;
UseCase_SetRoundTimerExists(ROUND_TIMER_EXISTS_DEFAULT_VALUE);
}

public void OnMapEnd() {
UnblockSpectators();
UseCase_UnblockSpectators();
}

public void Event_RoundStart(Event event, const char[] name, bool dontBroadcast) {
UnblockSpectators();
UseCase_UnblockSpectators();
}

public void Event_RoundActive(Event event, const char[] name, bool dontBroadcast) {
CreateBlockTimer();
UseCase_CreateBlockTimer();
}

public void Event_RoundWin(Event event, const char[] name, bool dontBroadcast) {
if (!g_isRoundTimerExists) {
BlockSpectators(NOTIFICATIONS_ROUND_END);
}
UseCase_BlockSpectatorsWithoutTimer();
}

public void Event_TimerTimeAdded(Event event, const char[] name, bool dontBroadcast) {
int secondsAdded = event.GetInt("seconds_added");

ExtendBlockTimer(secondsAdded);
}

void BlockSpectators(int notificationFlag) {
g_isSpectatorsBlocked = true;
NotifySpectatorsWasBlocked(notificationFlag);
}

void UnblockSpectators() {
delete g_blockTimer;
g_isSpectatorsBlocked = false;
}

void CreateBlockTimer() {
if (!IsPluginEnabled()) {
return;
}

int timerEntity = FindEntityByClassname(ENTITY_NOT_FOUND, "dod_round_timer");

if (timerEntity == ENTITY_NOT_FOUND) {
g_isRoundTimerExists = false;

return;
}

float timeRemanining = GetEntPropFloat(timerEntity, Prop_Send, "m_flTimeRemaining");
float timerDelay = timeRemanining - GetBlockTimeOffset() - 1.0;

if (timerDelay < 0.0) {
timerDelay = 0.0;
}

g_blockTimer = CreateTimer(timerDelay, Timer_BlockSpectators);
g_blockTimerEndTime = GetGameTime() + timerDelay;
}

void ExtendBlockTimer(int secondsAdded) {
delete g_blockTimer;

float timerSecondsLeft = g_blockTimerEndTime - GetGameTime();
float timerDelay = timerSecondsLeft + secondsAdded;

g_blockTimer = CreateTimer(timerDelay, Timer_BlockSpectators);
g_blockTimerEndTime = GetGameTime() + timerDelay;
}

public Action Timer_BlockSpectators(Handle timer) {
g_blockTimer = null;
BlockSpectators(NOTIFICATIONS_TIMER);

return Plugin_Continue;
}

public Action CommandListener_JoinTeam(int client, const char[] command, int argc) {
char teamStr[TEAM_ARG_MAX_SIZE];

GetCmdArg(1, teamStr, sizeof(teamStr));

int team = StringToInt(teamStr);

if (g_isSpectatorsBlocked && team == TEAM_SPECTATOR) {
NotifySpectatorsIsBlocked(client);

return Plugin_Stop;
}

return Plugin_Continue;
}

void NotifySpectatorsWasBlocked(int notificationFlag) {
bool isNotificationEnalbed = IsNotificationFlagEnabled(notificationFlag);

if (isNotificationEnalbed) {
CPrintToChatAll("%s%t", PREFIX_COLORED, "Spectators team was blocked");
}
}

void NotifySpectatorsIsBlocked(int client) {
bool isNotificationEnalbed = IsNotificationFlagEnabled(NOTIFICATIONS_JOIN_ATTEMPT);

if (isNotificationEnalbed) {
CPrintToChat(client, "%s%t", PREFIX_COLORED, "Spectators team is blocked");
}
}

bool IsPluginEnabled() {
return g_pluginEnabled.IntValue == 1;
}

bool IsNotificationFlagEnabled(int flag) {
return (g_notificationsMode.IntValue & flag) == flag;
}

float GetBlockTimeOffset() {
return g_blockTimeOffset.FloatValue;
UseCase_ExtendBlockTimer(secondsAdded);
}
6 changes: 6 additions & 0 deletions scripting/include/bs/console-command.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#if defined _bs_console_command_included
#endinput
#endif
#define _bs_console_command_included

#define TEAM_ARG_MAX_SIZE 2
8 changes: 8 additions & 0 deletions scripting/include/bs/console-variable.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#if defined _bs_console_variable_included
#endinput
#endif
#define _bs_console_variable_included

#define NOTIFICATIONS_JOIN_ATTEMPT (1 << 0)
#define NOTIFICATIONS_ROUND_END (1 << 1)
#define NOTIFICATIONS_TIMER (1 << 2)
6 changes: 6 additions & 0 deletions scripting/include/bs/message.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#if defined _bs_message_included
#endinput
#endif
#define _bs_message_included

#define PREFIX_COLORED "{green}[Block spectators] "
8 changes: 8 additions & 0 deletions scripting/include/bs/use-case.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#if defined _bs_use_case_included
#endinput
#endif
#define _bs_use_case_included

#define ENTITY_NOT_FOUND -1
#define TEAM_SPECTATOR 1
#define ROUND_TIMER_EXISTS_DEFAULT_VALUE true
15 changes: 15 additions & 0 deletions scripting/modules/console-command.sp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public Action CommandListener_JoinTeam(int client, const char[] command, int argc) {
char teamStr[TEAM_ARG_MAX_SIZE];

GetCmdArg(1, teamStr, sizeof(teamStr));

int team = StringToInt(teamStr);

if (UseCase_IsSpectatorsBlocked() && team == TEAM_SPECTATOR) {
MessagePrint_SpectatorsIsBlocked(client);

return Plugin_Stop;
}

return Plugin_Continue;
}
21 changes: 21 additions & 0 deletions scripting/modules/console-variable.sp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
static ConVar g_pluginEnabled = null;
static ConVar g_notificationsMode = null;
static ConVar g_blockTimeOffset = null;

void Variable_Create() {
g_pluginEnabled = CreateConVar("sm_blockspectators", "1", "Enable (1) or disable (0) spectators team blocking");
g_notificationsMode = CreateConVar("sm_blockspectators_notifications_mode", "7", "None (0), Join attempt (1), Round end (2), Timer (4)");
g_blockTimeOffset = CreateConVar("sm_blockspectators_time_offset", "0", "Time offset (in seconds) until the end of the round");
}

bool Variable_IsPluginEnabled() {
return g_pluginEnabled.IntValue == 1;
}

bool Variable_IsNotificationFlagEnabled(int flag) {
return (g_notificationsMode.IntValue & flag) == flag;
}

float Variable_GetBlockTimeOffset() {
return g_blockTimeOffset.FloatValue;
}
15 changes: 15 additions & 0 deletions scripting/modules/message.sp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
void MessagePrint_SpectatorsWasBlocked(int notificationFlag) {
bool isNotificationEnalbed = Variable_IsNotificationFlagEnabled(notificationFlag);

if (isNotificationEnalbed) {
CPrintToChatAll("%s%t", PREFIX_COLORED, "Spectators team was blocked");
}
}

void MessagePrint_SpectatorsIsBlocked(int client) {
bool isNotificationEnalbed = Variable_IsNotificationFlagEnabled(NOTIFICATIONS_JOIN_ATTEMPT);

if (isNotificationEnalbed) {
CPrintToChat(client, "%s%t", PREFIX_COLORED, "Spectators team is blocked");
}
}
69 changes: 69 additions & 0 deletions scripting/modules/use-case.sp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
static Handle g_blockTimer = null;
static float g_blockTimerEndTime = 0.0;
static bool g_isSpectatorsBlocked = false;
static bool g_isRoundTimerExists = ROUND_TIMER_EXISTS_DEFAULT_VALUE;

bool UseCase_IsSpectatorsBlocked() {
return g_isSpectatorsBlocked;
}

void UseCase_SetRoundTimerExists(bool exists) {
g_isRoundTimerExists = exists;
}

void UseCase_BlockSpectatorsWithoutTimer() {
if (!g_isRoundTimerExists) {
UseCase_BlockSpectators(NOTIFICATIONS_ROUND_END);
}
}

void UseCase_BlockSpectators(int notificationFlag) {
g_isSpectatorsBlocked = true;
MessagePrint_SpectatorsWasBlocked(notificationFlag);
}

void UseCase_UnblockSpectators() {
delete g_blockTimer;
g_isSpectatorsBlocked = false;
}

void UseCase_CreateBlockTimer() {
if (!Variable_IsPluginEnabled()) {
return;
}

int timerEntity = FindEntityByClassname(ENTITY_NOT_FOUND, "dod_round_timer");

if (timerEntity == ENTITY_NOT_FOUND) {
g_isRoundTimerExists = false;

return;
}

float timeRemanining = GetEntPropFloat(timerEntity, Prop_Send, "m_flTimeRemaining");
float timerDelay = timeRemanining - Variable_GetBlockTimeOffset() - 1.0;

if (timerDelay < 0.0) {
timerDelay = 0.0;
}

g_blockTimer = CreateTimer(timerDelay, Timer_BlockSpectators);
g_blockTimerEndTime = GetGameTime() + timerDelay;
}

void UseCase_ExtendBlockTimer(int secondsAdded) {
delete g_blockTimer;

float timerSecondsLeft = g_blockTimerEndTime - GetGameTime();
float timerDelay = timerSecondsLeft + secondsAdded;

g_blockTimer = CreateTimer(timerDelay, Timer_BlockSpectators);
g_blockTimerEndTime = GetGameTime() + timerDelay;
}

public Action Timer_BlockSpectators(Handle timer) {
g_blockTimer = null;
UseCase_BlockSpectators(NOTIFICATIONS_TIMER);

return Plugin_Continue;
}

0 comments on commit 1f48a4a

Please sign in to comment.