Added support for saving class selections in cookies.

This commit is contained in:
richard 2009-08-15 06:32:44 +02:00
parent daadefbaea
commit 2fc590ab59
4 changed files with 140 additions and 23 deletions

View File

@ -68,3 +68,35 @@ bool:CookiesGetClientCookieBool(client, Handle:cookie)
// Return string casted into an int, then to bool. // Return string casted into an int, then to bool.
return bool:StringToInt(cookievalue); return bool:StringToInt(cookievalue);
} }
/**
* 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);
}

View File

@ -24,6 +24,11 @@ ClassOnCookiesCreate()
{ {
// Forward event to sub-modules. // Forward event to sub-modules.
ClassOverlayOnCookiesCreate(); 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);
} }
/** /**

View File

@ -388,6 +388,9 @@ public ClassMenuSelectHandle(Handle:menu, MenuAction:action, client, slot)
// Player isn't alive. The class can be directly changed. // Player isn't alive. The class can be directly changed.
ClassSelected[client][teamid] = classindex; ClassSelected[client][teamid] = classindex;
} }
// Save selected class index in cookie.
CookiesSetInt(client, g_hClassCookieClassSelected[teamid], classindex + 1);
} }
} }
case MenuAction_Cancel: case MenuAction_Cancel:

View File

@ -390,6 +390,11 @@ new ClassSelectedNext[MAXPLAYERS + 1][ZR_CLASS_TEAMCOUNT];
*/ */
new ClassAdminTeamSelected[MAXPLAYERS + 1]; 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). * 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 * Sets default class indexes for each team on all players, or a single player
* if specified. * if specified.
* *
* @param client Optional. The client index. * @param client Optional. The client index. If specified, cookies are used.
*/ */
ClassClientSetDefaultIndexes(client = -1) ClassClientSetDefaultIndexes(client = -1)
{ {
// Get indexes. new bool:clientvalid = ZRIsClientValid(client);
new zombieindex = ClassGetDefaultSpawnClass(ZR_CLASS_TEAM_ZOMBIES); new zombieindex;
new humanindex = ClassGetDefaultSpawnClass(ZR_CLASS_TEAM_HUMANS); new humanindex;
new adminindex = ClassGetDefaultSpawnClass(ZR_CLASS_TEAM_ADMINS); 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)) if (!ClassValidateIndex(zombieindex))
{ {
// Invalid class index. Fall back to default class in class config and // Invalid class index. Fall back to default class in class config and
// log a warning. // 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. // Use default class.
zombieindex = ClassGetDefaultClass(ZR_CLASS_TEAM_ZOMBIES); zombieindex = ClassGetDefaultClass(ZR_CLASS_TEAM_ZOMBIES);
} }
// Validate human class index. // Get human class index.
if (!ClassValidateIndex(humanindex)) if (!ClassValidateIndex(humanindex))
{ {
// Invalid class index. Fall back to default class in class config and // Invalid class index. Fall back to default class in class config and
// log a warning. // 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. // Use default class.
humanindex = ClassGetDefaultClass(ZR_CLASS_TEAM_HUMANS); humanindex = ClassGetDefaultClass(ZR_CLASS_TEAM_HUMANS);
} }
// Validate admin class index. // Get admin class index.
if (!ClassValidateIndex(adminindex)) if (!ClassValidateIndex(adminindex))
{ {
// Invalid class index. Fall back to default class in class config if // 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); adminindex = ClassGetDefaultClass(ZR_CLASS_TEAM_ADMINS);
} }
// Check if a client isn't specified. // Check if a client is specified.
if (client < 1) 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. // 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_ZOMBIES] = zombieindex;
ClassSelected[clientindex][ZR_CLASS_TEAM_HUMANS] = humanindex; ClassSelected[clientindex][ZR_CLASS_TEAM_HUMANS] = humanindex;
ClassSelected[clientindex][ZR_CLASS_TEAM_ADMINS] = adminindex; ClassSelected[clientindex][ZR_CLASS_TEAM_ADMINS] = adminindex;
@ -974,15 +1060,6 @@ ClassClientSetDefaultIndexes(client = -1)
ClassReloadPlayerCache(client, humanindex); 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);
}
} }
/** /**