1. Melee Mapping contest #3 - Poll is up! Vote for the best 4v4 melee maps!
    Dismiss Notice
  2. The 30th edition of the Modeling Contest is finally up! The Portable Buildings need your attention, so come along and have a blast!
    Dismiss Notice
  3. We have a new contest going on right now! Join the 11th Music Contest! You are to make a Cinematic modern sound-track for this contest, so come and compete with other people for fun.
    Dismiss Notice

[vJASS] GameStatus (Replay Detection)

Discussion in 'JASS Resources' started by TriggerHappy, Apr 14, 2017.

  1. TriggerHappy

    TriggerHappy

    Code Moderator

    Joined:
    Jun 23, 2007
    Messages:
    3,514
    Resources:
    22
    Spells:
    11
    Tutorials:
    2
    JASS:
    9
    Resources:
    22
    Simple API for detecting if the game is online, offline, or a replay. This library is especially useful for giving the ability to run code specifically in replays. For example, you could display debug messages throughout your game that only display in replays. If someone is having an issue with your map, they can send you a replay where you can view the debug messages.

    This replay detection works different than previous attempts and has significant benefits over them.
    1. Instant detection.
    2. Safe.
    The previous most common use of a replay detection involved setting camera position. This however could be bypassed by minimizing your game client, causing the script to think it's a replay. This allowed players to trick maps which used the system in order to cheat. That is not the case with this library.

    Uses:
    1. PlayerUtils (Optional)

    Code (vJASS):
    library GameStatus uses optional PlayerUtils
    /***************************************************************
    *
    *   v1.0.0 by TriggerHappy
    *   ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
    *   Simple API for detecting if the game is online, offline, or a replay.
    *   _________________________________________________________________________
    *   1. Installation
    *   ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
    *   Copy the script to your map and save it (requires JassHelper *or* JNGP)
    *   _________________________________________________________________________
    *   2. API
    *   ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
    *   This library provides one function
    *
    *       function GetGameStatus takes nothing returns integer
    *
    *   It returns one of the following constants
    *
    *       - GAME_STATUS_OFFLINE
    *       - GAME_STATUS_ONLINE
    *       - GAME_STATUS_REPLAY
    *
    ***************************************************************/


    // Configuration:
    globals
        // The dummy unit is only created once, and removed directly after.
        private constant integer DUMMY_UNIT_ID = 'hfoo'
    endglobals
    // (end)

    globals
        constant integer GAME_STATUS_OFFLINE = 0
        constant integer GAME_STATUS_ONLINE  = 1
        constant integer GAME_STATUS_REPLAY  = 2
     
        private integer status = 0
    endglobals

    function GetGameStatus takes nothing returns integer
        return status
    endfunction

    private module GameStatusInit

        private static method onInit takes nothing returns nothing
            local player firstPlayer
            local unit u
            local boolean selected
       
            // find an actual player
            static if not (LIBRARY_PlayerUtils) then
                set firstPlayer = Player(0)
                loop
                    exitwhen (GetPlayerController(firstPlayer) == MAP_CONTROL_USER and GetPlayerSlotState(firstPlayer) == PLAYER_SLOT_STATE_PLAYING)
                    set firstPlayer = Player(GetPlayerId(firstPlayer)+1)
                endloop
            else
                set firstPlayer = User.fromPlaying(0).toPlayer()
            endif
       
            // force the player to select a dummy unit
            set u = CreateUnit(firstPlayer, DUMMY_UNIT_ID, 0, 0, 0)
            call SelectUnit(u, true)
            set selected = IsUnitSelected(u, firstPlayer)
            call RemoveUnit(u)
            set u = null
       
            if (selected) then
           
                // detect if replay or offline game
                if (ReloadGameCachesFromDisk()) then
                    set status = GAME_STATUS_OFFLINE
                else
                    set status = GAME_STATUS_REPLAY
                endif
           
            else
                // if the unit wasn't selected instantly, the game is online
                set status = GAME_STATUS_ONLINE
            endif
       
        endmethod
     
    endmodule

    private struct GameStatus
        implement GameStatusInit
    endstruct

    endlibrary


    GameStatus - Replay Detection (GUI Demo)
     
    Last edited: Apr 14, 2017
  2. puffdragon

    puffdragon

    Joined:
    Jul 10, 2008
    Messages:
    330
    Resources:
    0
    Resources:
    0
    Promising work on a much needed tool, my only issue on installing this is that it requires vJass and it will mess up my war3map.j formatted text and comments, which I have worked a lot to keep it tied and clean to work on! Nonetheless this looks short enough for me to translate it into jass. I will be testing it tonight.
     
  3. TriggerHappy

    TriggerHappy

    Code Moderator

    Joined:
    Jun 23, 2007
    Messages:
    3,514
    Resources:
    22
    Spells:
    11
    Tutorials:
    2
    JASS:
    9
    Resources:
    22
    I just uploaded a GUI version to the spells section. It should be easy to convert to JASS.
     
  4. puffdragon

    puffdragon

    Joined:
    Jul 10, 2008
    Messages:
    330
    Resources:
    0
    Resources:
    0
    No i found a "trick". I just used jasshelper, I took a w/e map, imported an empty war3map.j and copy paste your code + an empty "main" function. It automatically translated it.

    thnx for help tho.
     
    Last edited: Apr 15, 2017
  5. IcemanBo

    IcemanBo

    Joined:
    Sep 6, 2013
    Messages:
    5,821
    Resources:
    22
    Maps:
    3
    Spells:
    11
    Template:
    1
    Tutorials:
    4
    JASS:
    3
    Resources:
    22
    Everything looks good, and it works. Approved.
     
  6. GoldenEggCN

    GoldenEggCN

    Joined:
    Jan 11, 2014
    Messages:
    29
    Resources:
    2
    Maps:
    2
    Resources:
    2
    thank you great man