1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Timed Server Code

Discussion in 'Programming' started by Jeremy Benson, Oct 16, 2015.

  1. #1
    Hello,

    I'm looking for a way to create a time system in my MUD so there's day and night. The issue I'm having is setting up the same system for everyone. Using a timer on JS wont work if it doesn't have the same ticks across users.

    I'm not even sure if something like this is possible.

    I was thinking of making the run of a day an hour in real time. Half hour of day, and half hour of night.

    Is this possible, or do all sights need users to operate?

    Thanks,
    Jeremy.
     
    Solved! View solution.
    Jeremy Benson, Oct 16, 2015 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #2
    Whenever the user does a page-load or request, send the SERVER time back as a timestamp and use it to synchronize the JS timer. Being a MUD you should have a lot of back and forths between the server and the client, so adding a timestamp from the server shouldn't be a big deal, from there it's just a matter of syncing the client to that number. There may be a second or two lag at most, typically more than sufficient if you check anything that "needs" that time difference from a gameplay perspective since like all user input, you should be quadruple checking that on the server anyways.
     
    deathshadow, Oct 16, 2015 IP
  3. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #3
    Think you could give a little breakdown? So I would grab a unix timestamp with php, then I would output:

    setTimeout(myFunction, 3000)
    Code (markup):
    But how would I sync that time with the milliseconds from the timestamp?
     
    Jeremy Benson, Oct 16, 2015 IP
  4. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #4
    Is this code from Stackoverflow right?

    var tsp = new Date('Sun Feb 19 2012 17:55:14 GMT+0100 (CET)'); // "Timestamp"
    
    window.onload = function() {
        var performance = window.performance || window.mozPerformance || window.msPerformance || window.webkitPerformance || {};
        tsp.setTime(tsp.getTime() + performance.timing.loadEventStart - performance.timing.navigationStart
        // after window.onload, you're sync with the server
    
        // Example of timer:
        setInterval(function() {
            tsp.setSeconds(tsp.getSeconds() + 1);
            document.title = tsp.toString();
        }, 1000); // 1000 ms = timer may be off by 500ms.
    };
    Code (markup):
     
    Jeremy Benson, Oct 16, 2015 IP
  5. #5
    That's overthinking it slightly, and missing a few details.

    From PHP, I'd just pass the time() in milliseconds since the epoch, either as part of any realtime AJAX type requests, or as part of each pageload thusly:

    echo '<script>var serverDate = new Date(', time(), ');</script>';
    Code (markup):
    Then you can just access the bugger, you put this in the markup before </body> (one of the few times static scripting in the markup makes sense since you're passing a value) without getting too fancy.

    Then your window.onload (though really, don't use it that way you don't know what else might be trying to access it) would be something like this:

    // first a handy cross-browser event attach
    
    function eventAdd(e, eventName, handler) {
    	if (e.addEventListener) e.addEventListener(eventName, handler, false);
    		else e.attachEvent('on' + eventName, handler);
    }
    
    // then let's show the time in the heading
    
    eventAdd(window, 'load', function() {
    	setInterval(function() {
    		document.title = serverDate.getTime();
    	}, 1000);
    });
    Code (markup):
    There would still be a difference between the two thanks to how long handshaking takes and so forth -- which is why I'd have server requests double-check the value and use what it says, not what the client says if it will impact gameplay.

    Trying to get that performance value correct, it's STILL going to be up to a second off so if you need per second accuracy you're pretty well out of luck... I wouldn't even TRY to do that unless you two-staged it; basically use a AJAX request that includes the client-side time, compare it to php's $_SERVER['request_time'] and the server's time(), and include ALL of that in the response so that JS can have the all four numbers to calculate from... and even THEN it's going to be off a good bit.

    Though that should be obvious just from a keeping cheaters out standpoint, since the moment you start doing gameplay client-side since JS is wide open no matter how you encrypt it, you really can't trust the scripting for anything more than passing input...

    ... and even then it's not like you can't use user.js to override things.
     
    deathshadow, Oct 16, 2015 IP
  6. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #6
    Thanks DeathShadow. Going to be saving this code bit for later :) Still writing up the mechanics right now.
     
    Jeremy Benson, Oct 17, 2015 IP
  7. Webpass.io

    Webpass.io Member

    Messages:
    8
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    33
    #7
    Did you end up resolving this? If not, can you explain a little more your use case? Are you just wanting the javascript client to say "day" when it's day and "night" when it's night? Or are you trying to synchronise ticks every second, so that clients can agree about results in combat?

    If the first option, it seems to me it doesn't matter if the client and server disagree by a second or so for a MUD.
     
    Webpass.io, Oct 28, 2015 IP