[System] ModeManager

Scripts

14 years
edited 8 years
This system allows to you to create game commands and modes.
You can assign a function callback for whenever this command/mode is used.

For a player to use a certain command/mode, you must authorize him.
This also allows you to give a certain player the ability to select a game mode.

You can also lock a player rendering him unable to use anything.
This makes the system faster, because then it won't have to go
through the loop for nothing.

Each mode has a boolean 'flag'.
This boolean can be used any way you desire.
For example, you could use it to indicate an activated mode.

This system allows several mode strings to be accepted in the same string.

Code (jass) Select
/*********************************************************
*
*   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
*
*
*   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 trigger TRIGGER = CreateTrigger()
        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 call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "[Mode Manager]Error: Attempted to authorize an invalid player!")
                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 call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "[Mode Manager]Error: Attempted to unauthorize an invalid player!")
                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 call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "[Mode Manager]Error: Attempted to evaluate an invalid player!")
                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 call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "[Mode Manager]Error: Attempted to lock a null player.")
                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 call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "[Mode Manager]Error: Attempted to unlock a null player.")
                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 call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "[Mode Manager]Error: Attempted to evaluate a null player!")
                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 call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "[Mode Manager]Warning: Attempted to overwrite mode data for an existing mode!")
                debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "[Mode Manager]Error: Registeration Terminated!")
                debug return 0
            debug endif
           
            debug if MODE_LENGTH != StringLength(text) then
                debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "[Mode Manager]Error: Failed to register " + text + "!")
                debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "[Mode Manager]Error: Invalid String Length!")
                debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "[Mode Manager]Error: Registeration Terminated!")
                debug return 0
            debug endif
           
            debug if c == null then
                debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "[Mode Manager]Error: Invalid boolexpr!")
                debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "[Mode Manager]Error: Registeration Terminated!")
                debug return 0
            debug endif
           
            // Data storage
            set mode[this] = isMode
            set func[this] = Filter(c)
           
            // 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
            local integer length = StringLength(s)
           
            loop
                exitwhen index >= length
                set tmp = SubString(s, index, index + 1)
               
                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 id = GetPlayerId(GetTriggerPlayer())
            local integer length
            local string str
           
            // If you disabled the system, it won't run
            if e and not locked[id] then
           
                call TriggerClearConditions(TRIGGER)
               
                // Remove all spaces from chat string
                set str = filterSpace(StringCase(GetEventPlayerChatString(), false))
                set length = StringLength(str)
               
                // 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
                            call TriggerAddCondition(TRIGGER, func[this])
                        // 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
                            call TriggerAddCondition(TRIGGER, func[this])
                            exitwhen true
                        endif
                       
                    endif
                   
                    // Increase index to get next string
                    set index = index + MODE_LENGTH
                endloop
           
            endif
           
            // Run all functions
            return TriggerEvaluate(TRIGGER)
        endmethod
       
        private static method onInit takes nothing returns nothing
            local trigger t = CreateTrigger()
            local integer i = 11
            local player p
           
            debug if MODE_CHAR_LENGTH != StringLength(MODE_CHAR) then
                debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "[Mode Manager]Error: System Terminated!")
                debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "[Mode Manager]Error: Invalid integer (MODE_CHAR_LENGTH)")
                debug return
            debug endif
           
            debug if 0 >= MODE_LENGTH then
                debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "[Mode Manager]Error: System Terminated!")
                debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "[Mode Manager]Error: Invalid integer (MODE_LENGTH)")
                debug return
            debug endif
           
            // Loop through active players
            loop
                set p = Player(i)
                // If the player is available and happens to be a user (Non-computer)
                if GetPlayerSlotState(p) == PLAYER_SLOT_STATE_PLAYING and GetPlayerController(p) == MAP_CONTROL_USER then
                    // Register the event for this player
                    call TriggerRegisterPlayerChatEvent(t, p, MODE_CHAR, false)
                endif
               
                exitwhen i == 0
                set i = i - 1
            endloop
           
            call TriggerAddCondition(t, Condition(function thistype.run))
           
            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
        call m.authorize(GetPlayerId(p))
    endfunction
   
    function UnauthorizePlayerForGameModeById takes GameMode m, integer i returns nothing
        call m.unauthorize(i)
    endfunction
   
    function UnauthorizePlayerForGameMode takes GameMode m, player p returns nothing
        call m.unauthorize(GetPlayerId(p))
    endfunction
   
    function IsPlayerAuthorizedForGameModeById takes GameMode m, integer i returns boolean
        return m.isPlayerAuthorized(i)
    endfunction
   
    function IsPlayerAuthorizedForGameMode takes GameMode m, player p returns boolean
        return m.isPlayerAuthorized(GetPlayerId(p))
    endfunction
   
    function LockPlayerFromGameModesById takes integer i returns nothing
        call GameMode.lockPlayer(i)
    endfunction
   
    function LockPlayerFromGameModes takes player p returns nothing
        call GameMode.lockPlayer(GetPlayerId(p))
    endfunction
   
    function UnlockPlayerFromGameModesById takes integer i returns nothing
        call GameMode.unlockPlayer(i)
    endfunction
   
    function UnlockPlayerFromGameModes takes player p returns nothing
        call GameMode.unlockPlayer(GetPlayerId(p))
    endfunction
   
    function IsPlayerLockedFromGameModesById takes integer i returns boolean
        return GameMode.isPlayerLocked(i)
    endfunction
   
    function IsPlayerLockedFromGameModes takes player p returns boolean
        return GameMode.isPlayerLocked(GetPlayerId(p))
    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



Here's the Demo:
Code (jass) Select
struct Demo extends array
    private static GameMode Allpick
    private static GameMode Allrandom
    private static GameMode Onlymid
    private static GameMode Fastrespawn
    private static GameMode Movespeed
    private static GameMode Myenemies
   
    static method print takes string s returns nothing
        call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, s)
    endmethod
   
    // Good examples on how to stop mode collisions and avoiding
    // functions running more than once.
    static method ap takes nothing returns nothing
        if not Allpick.flag and not Allrandom.flag then
            call print("MODE Allpick")
            set Allpick.flag = true
        endif
    endmethod
   
    static method ar takes nothing returns nothing
        if not Allpick.flag and not Allrandom.flag then
            call print("MODE Allrandom")
            set Allrandom.flag = true
        endif
    endmethod
   
    static method ms takes nothing returns nothing
        call print("COMMAND Movespeed")
    endmethod
   
    static method om takes nothing returns nothing
        if not Onlymid.flag then
            call print("MODE Onlymid")
            set Onlymid.flag = true
        endif
    endmethod
   
    static method ma takes nothing returns nothing
        call print("COMMAND Myenemies")
    endmethod
   
    static method fr takes nothing returns nothing
        if not Fastrespawn.flag then
            call print("MODE Fastrespawn")
            set Fastrespawn.flag = true
        endif
    endmethod
   
    private static method reset takes nothing returns boolean
        set Allpick.flag = false
        set Allrandom.flag = false
        set Onlymid.flag = false
        set Fastrespawn.flag = false
        return false
    endmethod
   
    private static method onInit takes nothing returns nothing
        local trigger t = CreateTrigger()

        set Allpick = GameMode.create("ap", true, function thistype.ap)
        set Onlymid = GameMode.create("om", true, function thistype.om)
        set Allrandom = GameMode.create("ar", true, function thistype.ar)
        set Fastrespawn = GameMode.create("fr", true, function thistype.fr)
       
        set Movespeed = GameMode.create("ms", false, function thistype.ms)
        set Myenemies = GameMode.create("ma", false, function thistype.ma)
       
        call Allpick.authorize(0)
        call Allrandom.authorize(0)
        call Onlymid.authorize(0)
        call Fastrespawn.authorize(0)
       
        call Movespeed.authorize(0)
        call Myenemies.authorize(0)

        call print("[Mode Manager]Available Mode strings: ap, ar, om, fr")
        call print("[Mode Manager]Available Command strings: ms, ma")
        call print("[Mode Manager]To activate a mode, type \"-\" followed by a queue of mode strings")
        call print("[Mode Manager]To run a command, type \"-\" followed by a single command string")
        call print("[Mode Manager]You cannot activate the same mode twice. To reset a mode, type \"-reset\"")

        call TriggerRegisterPlayerChatEvent(t, Player(0), "-reset", true)
        call TriggerAddCondition(t, Condition(function thistype.reset))

        set t = null
    endmethod
endstruct


Contructive critisism will be accepted and is highly appreciated = )
Please give credits if used.