//|| Created by: Hard Soap(@hardsoap) //|| //|| Provides a function for checking whether a player entity is a specific class based on a provided string or string array. //|| Called with IsPlayerClass(handle player, string whitelist, *optional* string blacklist); //|| `player` is a reference to a player or bot entity, and `whitelist` and `blacklist can either be a single string or an array of strings; these string aliases can reference a singular class or group of classes. //|| E.X. IsPlayerClass(GetListenServerHost(), ["defense", "offense"], "heavy") will return true if the player is any class in the 'offense' or 'defense' category that is NOT a Heavy. //|| //|| Custom alias strings can be added to classAliases, with the only limitation being that they need to be listed as all-lowercase strings that do not contain symbols (If I do ever update this script, this will be addressed). //|| //|| Examples are located at the end of the script, and you can toggle printing them to console below (Always set to false if not being ran on a listen server): local printExamples = false // Determines whether example calls are printed to console when the script is initialized. ::classAliases <- { // Single-value keys "": null, "scout": 1, "sniper": 2, "soldier": 3, "demoman": 4, "demo": 4, "medic": 5, "heavy": 6, "heavyweapons": 6, "heavy weapons": 6, "pyro": 7, "spy": 8, "engineer": 9, // Array keys "all": [1, 2, 3, 4, 5, 6, 7, 8, 9], "any": [1, 2, 3, 4, 5, 6, 7, 8, 9], "offense": [1, 3, 7], "defense": [4, 6, 9], "support": [2, 5, 8], "flanker": [1, 7, 8], "flankers": [1, 7, 8], "power": [3, 4], "power class": [3, 4], // Keys below this line are for custom aliases "demonstration man": 4, "stinky frenchman": 8, "hs support": [5, 9], "hssupport": [5, 9] } //PrintTable(classAliases) ::IsClass <- function(player, whiteList, blackList = null) { local classID = player.GetPlayerClass() local inWhitelist = MapClassString(classID, whiteList) local inBlacklist if(blackList != null){inBlacklist = MapClassString(classID, blackList)} // If the blacklist variable is not null, run the same string -> ID mapping on it as we did with the whitelist. else {inBlacklist = null} // Otherwise, set inBlacklist to null, which should mean !inBlacklist is always true. //printl(inWhitelist + " + " + inBlacklist) return (inWhitelist && !inBlacklist) } ::MapClassString <- function(classID, classNames) { if(type(classNames) == "string") { local classValue = classAliases[classNames.tolower()] if (classValue == null) { return false } if (type(classValue) == "array") { return classValue.find(classID) != null } return classID == classValue } else if (type(classNames) == "array") { foreach (className in classNames) { local classValue = classAliases[className.tolower()] if (classValue != null) { if (type(classValue) == "array") { if (classValue.find(classID) != null) { return true } } else if (classID == classValue) { return true } } } } return false } if(printExamples) { local listenHost = GetListenServerHost() printl("Examples of the 'IsClass() function: " + "\n") // Examples of using the script. The first example does not use the third parameter, while the second example uses the third parameter to exclude classes. printl("Is a 'Power Class' (Soldier or Demoman): " + IsClass(listenHost, "power")) printl("Is any class that is NOT a Scout, Pyro, or Soldier: " + IsClass(listenHost, "all", "offense") + "\n") // Example of both case insensitivity and combining multiple alias strings at once to create extremely specific searches. printl("Is an offense or support category class that is not a 'Flanker' (Scout, Pyro, Spy) or a Sniper: " + IsClass(listenHost, ["ofFeNSe", "sUPPORT"], ["flANKer", "snipeR"]) + "\n") // Example of using the '!' operator with the third parameter. printl("Is not a defense category class, excluding Demoman: " + !IsClass(listenHost, "defense", "demoman") + "\n") // Examples of custom aliases. printl("Is a Spy: " + IsClass(listenHost, "STINKY FRENCHMAN")) printl("Is not an Engineer, Medic, or Heavy: " + IsClass(listenHost, "any", ["hs support", "heavyweapons"]) + "\n") }