Facility Control

 View Only
  • 1.  timer math

    Posted 11-12-2014 21:26

    I'm working on a hockey score interface for use with Xpression. I'm trying to set up labels for penalties but am having a bit of a problem getting the math to work with the timers. At first I tried using the getValue parameter from the labels, but that didn't work because the timer labels are strings. Is there a way to use math with the timers themselves?

    For example, I've created a label that will appear under the team names. I'm thinking the logic would look something like this:

    H1 = Get Home Penalty Timer 1
    
    H2 = Get Home Penalty Timer 2
    
    if (Math.abs(H1-H2 < 0 )) { //only run the script if a penalty is loaded
    
    // Show the timer with the lowest value
    
    if (H1 < H2) { show H1 }
    
    else if (H1 >H2) { show H2 }
    
    }
    
    else { show Blank } // Show blank if no penalties are loaded

    And I would also do another label the would show "Powerplay" or "5 on 3" or whatever depending on how many penalties are loaded up.

    Anyway, as I said, Getting the values from the labels doesn't seem to be working, so I'm looking for a way to do math with the timers themselves. Any help would be appreciated.

    Thanks!



  • 2.  RE: timer math

    Posted 11-13-2014 21:13
    `event.getCurrentValue()` will give you the timer's value when the task was triggered (in a timer task)

    `event.getDelta()` will give you the difference between this tick and the previous tick

    `[timer].getCurrent()` will give you the timer's current value

    #DashBoard


  • 3.  RE: timer math

    Posted 11-14-2014 17:04
    Thanks James! Halfway there. I'm able to do the math and display the results, but only as an integer in milliseconds.

    How do I change it back to m:ss string format?

    #DashBoard


  • 4.  RE: timer math

    Posted 11-14-2014 17:20

    Unfortunately, the formatter is not exposed to the ogScript layer so you won't be able to use the one built into the timer.

    One option available would be to write your own formatter in JavaScript or, if we're only talking about seconds, minutes, and hours - use the JavaScript Date object to help you:

    `

    function padStr(str, padTo)

    {

    while (str.length < padTo)

    {

    str = '0' + str;

    }

    return str;

    }

    function formatTime(timeInMilliseconds)

    {

    var d = new Date(timeInMilliseconds);

    var hoursPart = padStr(d.getUTCHours().toString(), 2);

    var minutesPart = padStr(d.getUTCMinutes().toString(), 2);

    var secondsPart = padStr(d.getUTCSeconds().toString(), 2);

    var milliPart = padStr(d.getUTCMilliseconds().toString(), 3);

    return hoursPart +':'+minutesPart+':'+secondsPart+':'+milliPart;

    }

    ogscript.debug(formatTime(121234));

    `

    #DashBoard


  • 5.  RE: timer math

    Posted 11-14-2014 19:34

    hmm I think I found a different way by using if statements on the timers themselves. But I'm a bit confused with how the scripting works when getting the timers own value compared to another timer's value. For example:

    event.getCurrentValue(); will retrieve the timer's own value in milliseconds, while

    ogscript.getTimerManager().getTimer('Timer 2').getCurrent(); will retrieve the other timer's value in milliseconds.

    So my question is about the event.getDisplay command. I can make a label display the current timer with the params.setValue([OID], 0, event.getDisplay()); command, since it's a string. But how do I assign ANOTHER timer's value instead? Here is a chunk of the if statement in question:

    value = event.getDisplay();
    
    self = event.getCurrentValue();
    
    othertimer = ogscript.getTimerManager().getTimer('Timer 2').getCurrent();
    
    if (self == 0) {  // if self is zero
    
     if (othertimer != 0) { // if other timer isnt zero, show other timer
    
     params.setValue(0x5, 0, othertimer);} // ERROR HERE, NOT A STRING VALUE
    
    }

    So where it says params.setValue(0x5, 0, othertimer); I'm getting an error that says "Wrapped java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.String " since getCurrent does not return a string value. Which is why I'm looking for an equivalent of the getDisplay command for that 'Timer 2'. Hope that makes sense?

    Thanks again!


    #DashBoard


  • 6.  RE: timer math

    Posted 11-14-2014 19:41

    You can remove the error tiself by just doing othertimer.toString() but that will just give you the time in milliseconds.

    The string version of the timer's value is only available in the TimerEvent object (`event.*`). There is no exposed method of getting the formatter/display value from the timer directly.

    Now, I don't really know what you're doing with this but if your "othertimer" ticks before "self" does, you could store the most recent display value and retrieve it.

    `//IN "OTHER TIMER"' task

    ogscript.putObject('OtherDisplay', event.getDisplay());

    //IN "SELF TIMER" task

    var selfDisplay = event.getDisplay();

    var otherDisplay = ogscript.getObject('OtherDisplay');`

    #DashBoard