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.
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.
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?
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):
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.
Thanks DeathShadow. Going to be saving this code bit for later Still writing up the mechanics right now.
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.