• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[vJASS] GameClock

NEL

NEL

Level 6
Joined
Mar 6, 2017
Messages
113

Introduction

Usage

Changelog



Game Clock v1.0.0.7

It allows you format the time and get the elapsed time.​

vJASS:
//! zinc
library GameClock { /* v1.0.0.7
*************************************************************************************
*
*    Game Clock
*        by nel
*
*       It allows you format the time and get the elapsed time.
*
************************************************************************************
*
*   SETTING
*
*/
    private constant string DELIMITER = " : "; /* It used to seperate the hour,
                                                  minute, and second */
/*
************************************************************************************
*
*    struct GameClock extends array
*
*        static method FormatTime takes real seconds return string
*            - It returns time as hh/mm/ss format.
*
*        static method operator ElapsedTime takes nothing returns real
*            - It returns elapsed time.
*
*        static method operator ElapsedTimeEx takes nothing returns string
*            - It returns elapsed time as hh/mm/ss format.
*
*        static method operator Delimiter takes nothing returns string
*            - It returns GameClock's delimiter.
*
*************************************************************************************
*
*    Issue:
*
*       This library will not give you an exact current elapsed time.
*
************************************************************************************/

    //! textmacro GAMECLOCK_TIMEFORMAT takes Time, delimiter
        if( $Time$ < 10 ) {
            format = format + "0" + I2S( $Time$ ) $delimiter$;
        } else {
            format = format + I2S( $Time$ ) $delimiter$;
        }
    //! endtextmacro

    private constant timer TIMER = CreateTimer();

    public { struct GameClock [] {
        public {
            static method FormatTime( real seconds ) -> string {
                integer s = R2I( ModuloReal( seconds, 60 ) );
                integer m = R2I( ModuloReal( seconds, 3600 ) / 60 );
                integer h = R2I( seconds / 3600 );
                string format = "";

                //! runtextmacro GAMECLOCK_TIMEFORMAT( "h", "+ DELIMITER" )
                //! runtextmacro GAMECLOCK_TIMEFORMAT( "m", "+ DELIMITER" )
                //! runtextmacro GAMECLOCK_TIMEFORMAT( "s", "" )

                return format;
            }

            static method operator ElapsedTime() -> real {
                return TimerGetElapsed( TIMER );
            }

            static method operator ElapsedTimeEx() -> string {
                return thistype.FormatTime( TimerGetElapsed(TIMER) );
            }

            static method operator Delimiter() -> string {
                return DELIMITER;
            }
        }

        private static method onInit() {
            TimerStart( TIMER, 100000000, true, null );
        }
    }}
}
//! endzinc
vJASS:
scope Test initializer Initialization

    private function Display takes nothing returns boolean
        local real r = GetRandomReal(0, 99999)
        local player p = GetLocalPlayer()

        call ClearTextMessages()
        call DisplayTimedTextToPlayer(p, 0, 0, 60, /*
            */ "|cffffcc00Format This time:|r " + R2S(r) + " seconds --> " + GameClock.FormatTime(r) + "\r\n" + /*
            */ "|cffffcc00Current Game Time in second:|r " + R2S( GameClock.ElapsedTime ) + "\r\n" + /*
            */ "|cffffcc00Current Game Time in hh/mm/ss format:|r " + GameClock.ElapsedTimeEx + "\r\n" + /*
            */ "|cffffcc00GameClock delimiter:|r " + GameClock.Delimiter /*
        */ )

        set p = null
        return false
    endfunction
 
    private function Initialization takes nothing returns nothing
        local trigger trg = CreateTrigger()

        call TriggerRegisterPlayerEvent(trg, Player(0), EVENT_PLAYER_END_CINEMATIC)
        call TriggerAddCondition(trg, Condition(function Display))

        set trg = null
    endfunction
 
endscope

Update: v1.0.0.7
  • Little fixed in method onInit()

Update: v1.0.0.6
  • Added private keyword at constant timer TIMER = CreateTimer(); (I forgot)

Update: v1.0.0.5
  • private { constant string delimiter = " : "; } -> private constant string DELIMITER = " : "; }.
  • private { constant timer TIMER = CreateTimer(); } -> private constant timer TIMER = CreateTimer();
  • Improved comments.

Update:
v1.0.0.4
  • Code Reworked. Use struct [] for readability.
  • public { constant string delimiter = " : "; } -> private { constant string delimiter = " : "; }.

Update: v1.0.0.3

  • Added new function: function FormatTime( real seconds ) -> real.
  • Function GetElapsedTimeFormatted() returns FormatTime(TimerGetElapsed(TIMER)).

Update: v1.0.0.2

  • public function GetElapsedTime() -> string -> public function GetElapsedTimeFormatted() -> string
  • Added new function: function GetElapsedTime() -> real.

Update: v1.0.0.1

  • TimerStart(TIMER, 86400, true, null); -> TimerStart(TIMER, 100000000, true, null);
  • Fixed the minute calculation.

Update: v1.0.0.0

  • Released


Note


This library will not give you an exact current elapsed time.
 
Last edited:

Jampion

Code Reviewer
Level 15
Joined
Mar 25, 2016
Messages
1,327
I think this snippet would be more useful, if you split it into two functions.
One function returns the elapsed time (TimerGetElapsed(TIMER)). The user can then use this information in triggers more easily.
e.g. if GetElapsedTime() <= 3600 then ...
The other function is used to format a time in seconds to hh:mm:ss.
 

NEL

NEL

Level 6
Joined
Mar 6, 2017
Messages
113
I think this snippet would be more useful, if you split it into two functions.
One function returns the elapsed time (TimerGetElapsed(TIMER)). The user can then use this information in triggers more easily.
e.g. if GetElapsedTime() <= 3600 then ...
The other function is used to format a time in seconds to hh:mm:ss.

Ok, I'll add that. Thanks for your suggestion.
 

Jampion

Code Reviewer
Level 15
Joined
Mar 25, 2016
Messages
1,327
The GetElapsedTime() function is good. What I also meant is to have a function for formatting. Something like:
FormatTime takes real seconds returns string
This function would do what your GetElapsedTimeFormatted() does except that it uses an argument instead of TimerGetElapsed(TIMER).
GetElapsedTimeFormatted() would then return FormatTime(TimerGetElapsed(TIMER))

Your snippet allows you to format time and get the elapsed time, so I think it makes sense to give both these functions to the user as well.
 

NEL

NEL

Level 6
Joined
Mar 6, 2017
Messages
113
The GetElapsedTime() function is good. What I also meant is to have a function for formatting. Something like:
FormatTime takes real seconds returns string
This function would do what your GetElapsedTimeFormatted() does except that it uses an argument instead of TimerGetElapsed(TIMER).
GetElapsedTimeFormatted() would then return FormatTime(TimerGetElapsed(TIMER))

Your snippet allows you to format time and get the elapsed time, so I think it makes sense to give both these functions to the user as well.

Well, that's useful.
 

Bribe

Code Moderator
Level 50
Joined
Sep 26, 2009
Messages
9,464
Approved.

For reference, here is its Lua-equivalent, with much simpler API:

Lua:
GameClock = {}
--[[ Lua v1.0.0.0
*************************************************************************************
*
*    Game Clock
*        by nel
*
*       It allows you format the time and get the elapsed time.
*
************************************************************************************
*
*
*    GameClock.format([seconds: real]) -> string
*        - Takes a raw value in seconds and returns time as HH:MM:SS format.
*
*    GameClock.elapsed() -> real
*        - Returns the exact elapsed game time.
*
*************************************************************************************
]]
OnGameStart(function()
    local timer = CreateTimer()
    local elapsed = 0
    TimerStart(timer, 86400, true, function() elapsed = elapsed + 86400 end)
   
    ---@return number exact_game_time
    function GameClock.elapsed()
        return TimerGetElapsed(timer) + elapsed
    end
   
    ---@param seconds? number
    ---@return string HHMMSS
    function GameClock.format(seconds)
        seconds = seconds or TimerGetElapsed(timer)
        local s = seconds % 60
        local m = seconds % 3600 // 60
        local h = seconds // 3600
       
        return string.format('%.2d:%.2d:%.2d', h, m, s) --taken from https://stackoverflow.com/questions/52936992
    end
end)
 
Top