[REL] Game Session Timer & Unit Count - 51118

Discussion in 'Mod Discussions' started by sneakyness, July 31, 2013.

  1. sneakyness

    sneakyness Member

    Messages:
    106
    Likes Received:
    5
    Saw a few people asking for this, so I did what I could for now.

    [​IMG]

    This was made possible thanks to moment.js, a javascript date library for parsing, validating, manipulating, and formatting dates. What I'm doing is definitely possible using just the Date object, but momentjs is really clean to work with.

    Step One: Adding momentjs to PA

    You can download the minified source of moment.js from github here. Save it to /PA/media/ui/alpha/shared/js/moment.min.js.

    Then, in the same folder, add this line to common.js:46
    Code:
    loadScript("../shared/js/moment.min.js");
    This won't do anything useful by itself.

    Step Two: Adding the game time string to the game UI

    In /PA/media/ui/alpha/live_game/live_game_alpha.html, right after line 75, add:
    Code:
    <div class="div_player_item"><span id="gametimer"></span></div>
    The player item classed div just helps with spacing a little bit.

    Now we have a way to deal with time, and a place to display our value, but no value!

    Step Three: Have the game update the time/army string

    In /PA/media/ui/alpha/live_game/live_game_alpha.js, around line 578 or so, add the following:
    Code:
            self.sneakyGameStartTime = moment();
            self.sneakyCurrentGameTime = moment();
            self.sneakyGameTimer = function(){
                self.sneakyCurrentGameTime.add('s', 1);
                self.sneakyTotalGameTime = self.sneakyCurrentGameTime.diff(self.sneakyGameStartTime, 'seconds');
                var minutes = Math.floor(model.sneakyTotalGameTime/60);
                var hours = Math.floor(minutes/60);
                var seconds = self.sneakyTotalGameTime%60;
    
                var hourString;
                var minuteString;
                var secondString;
    
                if (hours > 0) {
                    hourString = hours + ':';
                } else {
                    hourString = '';
                }
    
                if (minutes > 0) {
                    minuteString = minutes + ':';
                } else {
                    minuteString = '';
                }
    
                if (seconds < 10) {
                    secondString = '0' + seconds;
                } else {
                    secondString = seconds;
                }
    
                var timeString = hourString + minuteString + secondString;
    
    
                $('#gametimer').text(timeString + ' | ' + model.armySize());
            }
    
    This code basically creates two moments, one of which is incremented every second. As the values change, we generate a formatted string to display in the UI. Now all we need to do is tell the game to run this function every second, which you can do by adding this line around 915 or so:
    Code:
    setInterval(model.sneakyGameTimer, 1000);
    You should now have a lovely game session timer in the upper left hand corner! Do remember that this is only a session timer, not a total game timer, so leaving/rejoining will restart the timer.

    Attached Files:

  2. sneakyness

    sneakyness Member

    Messages:
    106
    Likes Received:
    5
    I exposed the armyCount statistic as well. It appears to be the total number of units/buildings you currently have, including buildings under construction. I was frustrated that the attack warning does not always play immediately, so I figured that being able to see my total army count at all times would allow me to be more aware of what is going on, even if I can't tell where.

    The instructions and attached files have both been updated :mrgreen:
  3. cola_colin

    cola_colin Moderator Alumni

    Messages:
    12,074
    Likes Received:
    16,221
    uhh a unit counter. I had this in mind but always forgot to check if it is available.
    Bookedmarked, will try later :)
  4. sneakyness

    sneakyness Member

    Messages:
    106
    Likes Received:
    5
    I've gone through just about everything that can be exposed in the model at this point during a live game :lol:

    I was hoping to get more information about the current selection, even a reliable count, but at most I would be able to show the number of different unit types you have selected.
  5. Raevn

    Raevn Moderator Alumni

    Messages:
    4,226
    Likes Received:
    4,324
    You may wish to look at viewtopic.php?f=72&t=49180 regarding selections, although that may have the same limitations you refer to.
  6. sneakyness

    sneakyness Member

    Messages:
    106
    Likes Received:
    5
    same thing :D thanks though, I really wasn't joking when I said I've looked at every variable in the live game model ;)
  7. cola_colin

    cola_colin Moderator Alumni

    Messages:
    12,074
    Likes Received:
    16,221
    I did this, it works in 54XXX, but I think it is a bit weird to import momentjs just for this?
    I've used plain Date() objects for a timer in the hotbuild mod and it worked just fine?
  8. sneakyness

    sneakyness Member

    Messages:
    106
    Likes Received:
    5
    It's not about working or not working, it's about ease of use. Date() sucks, as does time in general, IMHO.

    Past that, I've been messing around with some very high level APM style tracking that utilizes a bit more of what momentjs has to offer. This was a bit of a freebie :D
  9. cola_colin

    cola_colin Moderator Alumni

    Messages:
    12,074
    Likes Received:
    16,221
    I only used Date() to get the current time in ms, as that was all that was needed. But yeah, if Date in javascript is as bad as Date in java, I can understand why you might go to a 3rd party lib per default.
  10. sneakyness

    sneakyness Member

    Messages:
    106
    Likes Received:
    5
    Things in vanilla JS vary from browser to browser, and coherent doesn't seem to follow any consistent profile wrt standards compliance. It's better to avoid any potential frustration/pain/confusion by using a well documented polyfill with it's own set of test cases when things start getting funky. Especially with time, there's nothing worse.

Share This Page