/*********************************************************
*
* Mode Manager
* v6.0.0.0
* By Magtheridon96
*
* - Allows you to create game modes and commands.
* - Max Modes/Commands: 682 (<9000)
* * If you require a higher mode cap, contact me.
*
* Requires:
* ---------
*
* - StringIndexer by Magtheridon96
* - hiveworkshop.com/forums/jass-resources-412/snippet-stringindexer-208187/
*
* API:
* ----
*
* - struct GameMode extends array
* - static boolean enabled
* - Enables/Disables System.
*
* - method operator flag takes nothing returns boolean
* - method operator flag= takes boolean b returns nothing
* - For users to store a boolean in an instance of GameMode
*
* - method authorize takes integer id returns nothing
* - method unauthorize takes integer id returns nothing
* - method isPlayerAuthorized takes integer id returns boolean
* - Used to manage Player Authorization for certain modes/commands.
*
* - static method lockPlayer takes integer id returns nothing
* - static method unlockPlayer takes integer id returns nothing
* - static method isPlayerLocked takes integer id returns boolean
* - Used to manage Player Authentication. If a player is authorized
* for a few modes and you lock him, he will be rendered unable to use
* anything. When you unlock him, he will be able to use all of the
* modes you authorized him for.
*
* - static method create takes string cd, boolean m, boolexpr b returns thistype
* - Creates a new mode/command.
*
* - function CreateGameMode takes string modeString, code func returns GameMode
* - function CreateGameCommand takes string commandString, code func returns GameMode
*
* - function AuthorizePlayerForGameModeById takes GameMode m, integer i returns nothing
* - function AuthorizePlayerForGameMode takes GameMode m, player p returns nothing
*
* - function UnauthorizePlayerForGameModeById takes GameMode m, integer i returns nothing
* - function UnauthorizePlayerForGameMode takes GameMode m, player p returns nothing
*
* - function IsPlayerAuthorizedForGameModeById takes GameMode m, integer i returns boolean
* - function IsPlayerAuthorizedForGameMode takes GameMode m, player p returns boolean
*
* - function LockPlayerFromGameModesById takes integer i returns nothing
* - function LockPlayerFromGameModes takes player p returns nothing
*
* - function UnlockPlayerFromGameModesById takes integer i returns nothing
* - function UnlockPlayerFromGameModes takes player p returns nothing
*
* - function IsPlayerLockedFromGameModesById takes integer i returns boolean
* - function IsPlayerLockedFromGameModes takes player p returns boolean
*
* - function SetGameModeData takes GameMode m, boolean b returns nothing
* - function GetGameModeData takes GameMode m returns boolean
*
* - function EnableGameModes takes nothing returns nothing
* - function DisableGameModes takes nothing returns nothing
* - function GameModesEnabled takes nothing return boolean
*
*********************************************************/
library ModeManager requires StringIndexer
globals
// **** Configuration ****
private constant string MODE_CHAR = "-" // This character will be used to indicate a command/mode.
private constant integer MODE_CHAR_LENGTH = 1 // This is the length of the MODE_CHARACTER
private constant integer MODE_LENGTH = 2 // This is the length of each command/mode string
// **** End Configuration ****
endglobals
struct GameMode extends array
private static boolean e = true
private static boolexpr array func
private static boolean array mode
private static boolean array authorized
private static boolean array locked
private static boolean array data
private static boolean array allocated
static method enable takes nothing returns nothing
set e = true
endmethod
static method disable takes nothing returns nothing
set e = false
endmethod
static method operator enabled takes nothing returns boolean
return e
endmethod
method operator flag takes nothing returns boolean
return data[this]
endmethod
method operator flag= takes boolean b returns nothing
set data[this] = b
endmethod
method authorize takes integer id returns nothing
debug if 0 > id or 15 < id then
debug return
debug endif
set authorized[this * 12 + id] = true
endmethod
method unauthorize takes integer id returns nothing
debug if 0 > id or 15 < id then
debug return
debug endif
set authorized[this * 12 + id] = false
endmethod
method isPlayerAuthorized takes integer id returns boolean
debug if 0 > id or 15 < id then
debug return false
debug endif
return authorized[this * 12 + id]
endmethod
static method lockPlayer takes integer id returns nothing
debug if 0 > id or 15 < id then
debug return
debug endif
set locked[id] = true
endmethod
static method unlockPlayer takes integer id returns nothing
debug if 0 > id or 15 < id then
debug return
debug endif
set locked[id] = false
endmethod
static method isPlayerLocked takes integer id returns boolean
debug if 0 > id or 15 < id then
debug return false
debug endif
return locked[id]
endmethod
static method create takes string text, boolean isMode, code c returns thistype
local thistype this = IndexString(text)
debug if allocated[this] then
debug return 0
debug endif
debug return 0
debug endif
debug if c == null then
debug return 0
debug endif
// Data storage
set mode[this] = isMode
// Safety
set allocated[this] = true
return this
endmethod
private static method filterSpace takes string s returns string
local string new = ""
local string tmp = ""
local integer index = 0
loop
exitwhen index >= length
if " " != tmp then
set new = new + tmp
endif
set index = index + 1
endloop
return new
endmethod
private static method run takes nothing returns boolean
local thistype this
local integer index = MODE_CHAR_LENGTH
local integer length
local string str
// If you disabled the system, it won't run
if e and not locked[id] then
// Remove all spaces from chat string
// Loop through all the substrings in the chat string that have a length of MODE_LENGTH
// We will start after the MODE_CHAR
loop
exitwhen index >= length
// Get current instance through pointer Table
set this = GetStringId(
SubString(str, index, index + MODE_LENGTH))
// Check if the instance is not null and
// the player is authorized
if authorized[this * 12 + id] and allocated[this] then
// Check if it's a mode string
if mode[this] then
// Add function to trigger
// Check if it's a command string and it's the first one encountered
elseif not mode[this] and index == MODE_CHAR_LENGTH then
// Add function to trigger
exitwhen true
endif
endif
// Increase index to get next string
set index = index + MODE_LENGTH
endloop
endif
// Run all functions
endmethod
private static method onInit takes nothing returns nothing
local integer i = 11
local player p
debug return
debug endif
debug if 0 >= MODE_LENGTH then
debug return
debug endif
// Loop through active players
loop
// If the player is available and happens to be a user (Non-computer)
// Register the event for this player
endif
exitwhen i == 0
set i = i - 1
endloop
set t = null
set p = null
endmethod
endstruct
function CreateGameMode takes string modeString, code func returns GameMode
return GameMode.create(modeString, true, func)
endfunction
function CreateGameCommand takes string commandString, boolean executeOnce, code func returns GameMode
return GameMode.create(commandString, false, func)
endfunction
function AuthorizePlayerForGameModeById takes GameMode m, integer i returns nothing
call m.authorize(i)
endfunction
function AuthorizePlayerForGameMode takes GameMode m, player p returns nothing
endfunction
function UnauthorizePlayerForGameModeById takes GameMode m, integer i returns nothing
call m.unauthorize(i)
endfunction
function UnauthorizePlayerForGameMode takes GameMode m, player p returns nothing
endfunction
function IsPlayerAuthorizedForGameModeById takes GameMode m, integer i returns boolean
return m.isPlayerAuthorized(i)
endfunction
function IsPlayerAuthorizedForGameMode takes GameMode m, player p returns boolean
endfunction
function LockPlayerFromGameModesById takes integer i returns nothing
call GameMode.lockPlayer(i)
endfunction
function LockPlayerFromGameModes takes player p returns nothing
endfunction
function UnlockPlayerFromGameModesById takes integer i returns nothing
call GameMode.unlockPlayer(i)
endfunction
function UnlockPlayerFromGameModes takes player p returns nothing
endfunction
function IsPlayerLockedFromGameModesById takes integer i returns boolean
return GameMode.isPlayerLocked(i)
endfunction
function IsPlayerLockedFromGameModes takes player p returns boolean
endfunction
function SetGameModeData takes GameMode m, boolean b returns nothing
set m.flag = b
endfunction
function GetGameModeData takes GameMode m returns boolean
return m.flag
endfunction
function EnableGameModes takes nothing returns nothing
call GameMode.enable()
endfunction
function DisableGameModes takes nothing returns nothing
call GameMode.disable()
endfunction
function GameModesEnabled takes nothing returns boolean
return GameMode.enabled
endfunction
endlibrary