diff --git a/src/zr/cookies.inc b/src/zr/cookies.inc index 8c3c316..37069da 100644 --- a/src/zr/cookies.inc +++ b/src/zr/cookies.inc @@ -67,4 +67,36 @@ bool:CookiesGetClientCookieBool(client, Handle:cookie) // Return string casted into an int, then to bool. return bool:StringToInt(cookievalue); -} \ No newline at end of file +} + +/** + * Sets a integer value on a cookie. + * + * @param client The client index. + * @param cookie The handle to the cookie. + * @param value The value to set. + */ +CookiesSetInt(client, Handle:cookie, value) +{ + // Convert value to string. + decl String:strValue[16]; + IntToString(value, strValue, sizeof(strValue)); + + // Set string value. + SetClientCookie(client, cookie, strValue); +} + +/** + * Gets a integer value from a cookie. + * + * @param client The client index. + * @param cookie The handle to the cookie. + */ +CookiesGetInt(client, Handle:cookie) +{ + decl String:strValue[16]; + strValue[0] = 0; + GetClientCookie(client, cookie, strValue, sizeof(strValue)); + + return StringToInt(strValue); +} diff --git a/src/zr/playerclasses/classevents.inc b/src/zr/playerclasses/classevents.inc index 60ecf83..9788419 100644 --- a/src/zr/playerclasses/classevents.inc +++ b/src/zr/playerclasses/classevents.inc @@ -24,6 +24,11 @@ ClassOnCookiesCreate() { // Forward event to sub-modules. ClassOverlayOnCookiesCreate(); + + // Create class index cookies. + g_hClassCookieClassSelected[ZR_CLASS_TEAM_HUMANS] = RegClientCookie("zr_humanclass", "The last human class selected.", CookieAccess_Protected); + g_hClassCookieClassSelected[ZR_CLASS_TEAM_ZOMBIES] = RegClientCookie("zr_zombieclass", "The last zombie class selected.", CookieAccess_Protected); + g_hClassCookieClassSelected[ZR_CLASS_TEAM_ADMINS] = RegClientCookie("zr_adminclass", "The last admin mode class selected.", CookieAccess_Protected); } /** diff --git a/src/zr/playerclasses/classmenus.inc b/src/zr/playerclasses/classmenus.inc index de2cda8..a32c894 100644 --- a/src/zr/playerclasses/classmenus.inc +++ b/src/zr/playerclasses/classmenus.inc @@ -388,6 +388,9 @@ public ClassMenuSelectHandle(Handle:menu, MenuAction:action, client, slot) // Player isn't alive. The class can be directly changed. ClassSelected[client][teamid] = classindex; } + + // Save selected class index in cookie. + CookiesSetInt(client, g_hClassCookieClassSelected[teamid], classindex + 1); } } case MenuAction_Cancel: diff --git a/src/zr/playerclasses/playerclasses.inc b/src/zr/playerclasses/playerclasses.inc index 85c9be9..2d84994 100644 --- a/src/zr/playerclasses/playerclasses.inc +++ b/src/zr/playerclasses/playerclasses.inc @@ -390,6 +390,11 @@ new ClassSelectedNext[MAXPLAYERS + 1][ZR_CLASS_TEAMCOUNT]; */ new ClassAdminTeamSelected[MAXPLAYERS + 1]; +/** + * Cookies for storing class indexes. + */ +new Handle:g_hClassCookieClassSelected[ZR_CLASS_TEAMCOUNT]; + /** * Cache for the currently selected attribute multiplier (admin menus). */ @@ -920,38 +925,94 @@ ClassRestoreNextIndexes(client, excludeTeam = -1) * Sets default class indexes for each team on all players, or a single player * if specified. * - * @param client Optional. The client index. + * @param client Optional. The client index. If specified, cookies are used. */ ClassClientSetDefaultIndexes(client = -1) { - // Get indexes. - new zombieindex = ClassGetDefaultSpawnClass(ZR_CLASS_TEAM_ZOMBIES); - new humanindex = ClassGetDefaultSpawnClass(ZR_CLASS_TEAM_HUMANS); - new adminindex = ClassGetDefaultSpawnClass(ZR_CLASS_TEAM_ADMINS); + new bool:clientvalid = ZRIsClientValid(client); + new zombieindex; + new humanindex; + new adminindex; - // Validate zombie class index. + new bool:haszombie; + new bool:hashuman; + new bool:hasadmin; + + // Check if a client is specified. + if (clientvalid) + { + // Get cookie indexes. + zombieindex = CookiesGetInt(client, g_hClassCookieClassSelected[ZR_CLASS_TEAM_ZOMBIES]); + humanindex = CookiesGetInt(client, g_hClassCookieClassSelected[ZR_CLASS_TEAM_HUMANS]); + adminindex = CookiesGetInt(client, g_hClassCookieClassSelected[ZR_CLASS_TEAM_ADMINS]); + + // Note: When class indexes are set on cookies, they're incremented by + // one so zero means no class set and will result in a invalid + // class index when restored. + + // Check if class indexes are set. If not, fall back to default class + // indexes. Otherwise substract index by one. + if (zombieindex <= 0) + { + zombieindex = ClassGetDefaultSpawnClass(ZR_CLASS_TEAM_ZOMBIES); + } + else + { + zombieindex--; + haszombie = true; + } + + if (humanindex <= 0) + { + humanindex = ClassGetDefaultSpawnClass(ZR_CLASS_TEAM_HUMANS); + } + else + { + humanindex--; + hashuman = true; + } + + if (adminindex <= 0) + { + adminindex = ClassGetDefaultSpawnClass(ZR_CLASS_TEAM_ADMINS); + } + else + { + adminindex--; + hasadmin = true; + } + } + else + { + // Get default class indexes. + zombieindex = ClassGetDefaultSpawnClass(ZR_CLASS_TEAM_ZOMBIES); + humanindex = ClassGetDefaultSpawnClass(ZR_CLASS_TEAM_HUMANS); + adminindex = ClassGetDefaultSpawnClass(ZR_CLASS_TEAM_ADMINS); + } + + // Validate indexes. if (!ClassValidateIndex(zombieindex)) { // Invalid class index. Fall back to default class in class config and // log a warning. - LogEvent(false, LogType_Error, LOG_CORE_EVENTS, LogModule_Playerclasses, "Set Default Indexes", "Warning: Failed to get default zombie class, falling back to default class in class config. Check spelling in \"zr_classes_default_zombie\"."); + LogEvent(false, LogType_Error, LOG_CORE_EVENTS, LogModule_Playerclasses, "Set Default Indexes", "Warning: Failed to get specified zombie class, falling back to default class in class config. Check spelling in \"zr_classes_default_zombie\"."); // Use default class. zombieindex = ClassGetDefaultClass(ZR_CLASS_TEAM_ZOMBIES); } - // Validate human class index. + // Get human class index. if (!ClassValidateIndex(humanindex)) { // Invalid class index. Fall back to default class in class config and // log a warning. - LogEvent(false, LogType_Error, LOG_CORE_EVENTS, LogModule_Playerclasses, "Set Default Indexes", "Warning: Failed to get default human class, falling back to default class in class config. Check spelling in \"zr_classes_default_human\"."); + LogEvent(false, LogType_Error, LOG_CORE_EVENTS, LogModule_Playerclasses, "Set Default Indexes", "Warning: Failed to get specified human class, falling back to default class in class config. Check spelling in \"zr_classes_default_human\"."); // Use default class. humanindex = ClassGetDefaultClass(ZR_CLASS_TEAM_HUMANS); } - // Validate admin class index. + // Get admin class index. if (!ClassValidateIndex(adminindex)) { // Invalid class index. Fall back to default class in class config if @@ -960,12 +1021,37 @@ ClassClientSetDefaultIndexes(client = -1) adminindex = ClassGetDefaultClass(ZR_CLASS_TEAM_ADMINS); } - // Check if a client isn't specified. - if (client < 1) + // Check if a client is specified. + if (clientvalid) + { + // Set selected class idexes. + ClassSelected[client][ZR_CLASS_TEAM_ZOMBIES] = zombieindex; + ClassSelected[client][ZR_CLASS_TEAM_HUMANS] = humanindex; + ClassSelected[client][ZR_CLASS_TEAM_ADMINS] = adminindex; + + // Copy human class data to player cache. + ClassReloadPlayerCache(client, humanindex); + + // Save indexes in cookies if not already saved. + if (!haszombie) + { + CookiesSetInt(client, g_hClassCookieClassSelected[ZR_CLASS_TEAM_ZOMBIES], zombieindex + 1); + } + if (!hashuman) + { + CookiesSetInt(client, g_hClassCookieClassSelected[ZR_CLASS_TEAM_HUMANS], humanindex + 1); + } + if (!hasadmin) + { + CookiesSetInt(client, g_hClassCookieClassSelected[ZR_CLASS_TEAM_ADMINS], adminindex + 1); + } + } + else { // No client specified. Loop through all players. - for (new clientindex = 1; clientindex <= MAXPLAYERS; clientindex++) + for (new clientindex = 1; clientindex <= MaxClients; clientindex++) { + // Set selected class idexes. ClassSelected[clientindex][ZR_CLASS_TEAM_ZOMBIES] = zombieindex; ClassSelected[clientindex][ZR_CLASS_TEAM_HUMANS] = humanindex; ClassSelected[clientindex][ZR_CLASS_TEAM_ADMINS] = adminindex; @@ -974,15 +1060,6 @@ ClassClientSetDefaultIndexes(client = -1) ClassReloadPlayerCache(client, humanindex); } } - else - { - ClassSelected[client][ZR_CLASS_TEAM_ZOMBIES] = zombieindex; - ClassSelected[client][ZR_CLASS_TEAM_HUMANS] = humanindex; - ClassSelected[client][ZR_CLASS_TEAM_ADMINS] = adminindex; - - // Copy human class data to player cache. - ClassReloadPlayerCache(client, humanindex); - } } /**