[Snippet] Game Clock

Scripts

7 years
edited 7 years
Game Clock

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

Code (jass) Select
//! 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
7 years
Nice code, clean and to the point :)

I think adding a demo map showing this code working could be a good thing. I did one for you
7 years
Quote from: moyack on December 22, 2018, 10:11:24 AM
Nice code, clean and to the point :)

I think adding a demo map showing this code working could be a good thing. I did one for you

I think you don't need a demo map for this, because the demo code is enough to understand how this snippet works. :v

*meh meh*