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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework Modes array getting #60

Merged
merged 1 commit into from Nov 28, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion cstrike/addons/amxmodx/scripting/ReDeathmatch.sma
Expand Up @@ -72,9 +72,9 @@ public plugin_cfg() {
CvarsHandler_Init()
Features_Init()
SpawnManager_Init()
RoundModes_Init()
EquipManager_Init()
Config_Init()
RoundModes_Init()

SetActive(redm_active)
}
Expand Down
35 changes: 8 additions & 27 deletions cstrike/addons/amxmodx/scripting/ReDeathmatch/Modes/Menus.inc
Expand Up @@ -24,18 +24,11 @@ public ClCmd_redm_modes_menu(const player, const level, const commandId) {
}

static bool: Menu_Modes(const player) {
if (!json_object_has_value(Config_GetCurrent(), "modes"))
new count
new JSON: arrModes = RoundModes_GetModesArr(count)
if (!count)
return false

new JSON: arrModes = json_object_get_value(Config_GetCurrent(), "modes")

new count = json_array_get_count(arrModes)
if (!count) {
json_free(arrModes)

return false
}

new callback
if (!callback)
callback = menu_makecallback("MenuCallback_Modes")
Expand Down Expand Up @@ -78,8 +71,6 @@ static bool: Menu_Modes(const player) {

json_free(objMode)
}

json_free(arrModes)

menu_setprop(menu, MPROP_BACKNAME, fmt("%l", "BACK"))
menu_setprop(menu, MPROP_NEXTNAME, fmt("%l", "MORE"))
Expand Down Expand Up @@ -129,27 +120,17 @@ public MenuHandler_Modes(const player, const menu, const item) {
}

Menu_GetVoteItemsCount() {
if (!json_object_has_value(Config_GetCurrent(), "modes"))
return false

new JSON: arrModes = json_object_get_value(Config_GetCurrent(), "modes")
new count = json_array_get_count(arrModes)
json_free(arrModes)
new count
RoundModes_GetModesArr(count)

return count
}

bool: Menu_VoteMode(const player = 0) {
if (!json_object_has_value(Config_GetCurrent(), "modes"))
return false

new JSON: arrModes = json_object_get_value(Config_GetCurrent(), "modes")

new count = json_array_get_count(arrModes)
if (!count) {
json_free(arrModes)
new count
new JSON: arrModes = RoundModes_GetModesArr(count)
if (!count)
return false
}

new menu = menu_create("Choose", "MenuHandler_ChooseNextMode")

Expand Down
22 changes: 5 additions & 17 deletions cstrike/addons/amxmodx/scripting/ReDeathmatch/ReDM_config.inc
Expand Up @@ -128,6 +128,7 @@ Config_Reload() {
equipConfig = Config_GetMainEquip()

EquipManager_LoadConfig(equipConfig)
RoundModes_ReadModes()

json_free(equipConfig)
}
Expand Down Expand Up @@ -206,30 +207,18 @@ JSON: Config_GetCurrent() {
* @return The equipment configuration for the current game mode as a JSON object.
*/
JSON: Config_GetModeEquip() {
if (!json_object_has_value(Config_GetCurrent(), "modes")) {
new count
new JSON: arrModes = RoundModes_GetModesArr()
if (!count)
return Invalid_JSON
}

new JSON: arrModes = json_object_get_value(Config_GetCurrent(), "modes")
new count = json_array_get_count(arrModes)
if (!count) {
json_free(arrModes)

return Invalid_JSON
}

new currentModeIdx = RoundModes_GetCurrentMode()
if (currentModeIdx < 0) {
json_free(arrModes)

if (currentModeIdx < 0)
return Invalid_JSON
}


new JSON: objMode = json_array_get_value(arrModes, currentModeIdx)
if (!json_object_has_value(objMode, "equip")) {
json_free(objMode)
json_free(arrModes)

return Invalid_JSON
}
Expand All @@ -239,7 +228,6 @@ JSON: Config_GetModeEquip() {

json_free(objEquip)
json_free(objMode)
json_free(arrModes)

return obj
}
Expand Down
69 changes: 39 additions & 30 deletions cstrike/addons/amxmodx/scripting/ReDeathmatch/ReDM_round_modes.inc
Expand Up @@ -5,6 +5,8 @@ enum {
RoundMode_NotSet = -1, /**< Round game-mode is not set */
}

static JSON: g_arrModes = Invalid_JSON

static g_currentRoundModeIdx = RoundMode_NotSet
static g_forcedModeIdx = RoundMode_NotSet

Expand All @@ -27,22 +29,41 @@ RoundModes_Init() {

ModesMenus_Init()
ModeVote_Init()

RoundModes_ReadModes()
}

RoundModes_ReadModes() {
if (g_arrModes != Invalid_JSON)
json_free(g_arrModes)

if (!json_object_has_value(Config_GetCurrent(), "modes"))
return

g_arrModes = json_object_get_value(Config_GetCurrent(), "modes")
}

/**
* Handles mode voting and mode switching.
*/
RoundModes_RoundEnd() {
new bool: voteStarted = ModeVote_RoundEnd_Post()
if (strcmp(redm_modes_switch, "disable") == 0)
return

if (voteStarted) {
client_print(0, print_center, "%l %l.", "Selecting", "NextGameMode")
new count
RoundModes_GetModesArr(count)
if (!count) {
LogMessageEx(Warning, "RoundModes_RoundEnd(): Can't load modes list from config!")

return
}

if (strcmp(redm_modes_switch, "disable") == 0)
new bool: voteStarted = ModeVote_RoundEnd_Post()
if (voteStarted) {
client_print(0, print_center, "%l %l.", "Selecting", "NextGameMode")

return
}

new nextModeIdx = GetNextMode()
if (nextModeIdx == RoundMode_NotSet)
Expand Down Expand Up @@ -123,20 +144,15 @@ public RoundInfoMessage(const player) {
*
* @return A reference to the JSON array containing game modes, or `Invalid_JSON` if not found.
*/
static JSON: GetModesArr(& count = 0) {
if (!json_object_has_value(Config_GetCurrent(), "modes"))
JSON: RoundModes_GetModesArr(& count = 0) {
if (g_arrModes == Invalid_JSON)
return Invalid_JSON

new JSON: arrModes = json_object_get_value(Config_GetCurrent(), "modes")

count = json_array_get_count(arrModes)
if (!count) {
json_free(arrModes)

count = json_array_get_count(g_arrModes)
if (!count)
return Invalid_JSON
}

return arrModes

return g_arrModes
}

/**
Expand All @@ -150,36 +166,30 @@ static JSON: GetModesArr(& count = 0) {
*/
bool: RoundModes_GetModeInfo(const modeIdx, name[] = "", len = 0) {
new count
new JSON: arrModes = GetModesArr(count)
if (arrModes == Invalid_JSON)
new JSON: arrModes = RoundModes_GetModesArr(count)
if (!count)
return false

if (modeIdx < 0 || modeIdx >= count) {
json_free(arrModes)

if (modeIdx < 0 || modeIdx >= count)
return false
}

new JSON: objMode = json_array_get_value(arrModes, modeIdx)
json_object_get_string(objMode, "name", name, len)

json_free(arrModes)
json_free(objMode)

return true
}

static GetNextMode() {
new count
new JSON: arrModes = GetModesArr(count)
if (arrModes == Invalid_JSON)
return RoundMode_NotSet
RoundModes_GetModesArr(count)

if (RoundModes_IsModeForced()) {
json_free(arrModes)
if (!count)
return RoundMode_NotSet

if (RoundModes_IsModeForced())
return g_forcedModeIdx
}

new currentIdx = g_currentRoundModeIdx

Expand All @@ -204,7 +214,7 @@ static bool: ApplyMode(const modeIdx) {

Config_Reload()

new JSON: arrModes = GetModesArr()
new JSON: arrModes = RoundModes_GetModesArr()
if (arrModes == Invalid_JSON)
return false

Expand All @@ -218,7 +228,6 @@ static bool: ApplyMode(const modeIdx) {
}

json_free(objMode)
json_free(arrModes)

return true
}
Expand Down