Answer the question
In order to leave comments, you need to log in
How do timers work in games?
Knowledgeable people, tell me how timers work in games such as "Farmer". Where there are N number of objects: trees, flowers, animals, etc. and each of them has its own countdown timer.
Sending a request every second to the server to check if the timers have expired is probably wrong. The only option left is with constant connection support. Or are there other methods and technologies?
Answer the question
In order to leave comments, you need to log in
For example, the timer until the end
is allowed until the end of something or 180 seconds left
Demo
(function($){
// Количество секунд в каждом временном отрезке
var days = 24*60*60,
hours = 60*60,
minutes = 60;
$.fn.timer = function(callback){
callback = callback ? callback : function(){}
return new $.ctimer(this, callback);
}
$.ctimer = function(element, callback) {
callback = callback || function(){}
var secs = parseInt(element.html());
function tick(){
// Осталось дней
var sec = secs;
var d = Math.floor(sec / days);
sec -= d*days;
// Осталось часов
var h = Math.floor(sec / hours);
sec -= h*hours;
// Осталось минут
var m = Math.floor(sec / minutes);
sec -= m*minutes;
// Осталось секунд
var s = sec;
h = (h+"").length<2 ? "0"+h : h;
m = (m+"").length<2 ? "0"+m : m;
s = (s+"").length<2 ? "0"+s : s;
if(secs==0){
element.html(null);
callback();
return false;
}
secs--;
element.html(d+" дн. "+h+":"+m+":"+s);
setTimeout(tick, 1000);
}
tick();
}
})(jQuery);
You are trying to understand how a house is built by the color of the workers' T-shirts))
Usually there is some kind of "state of the game world". When a client connects, a permanent connection is established (usually udp), then the client downloads the current state of the world. Then, during the game, the server and the client exchange messages, (quite normal, if in asynchronous mode) the client requests an action, the server either answers about success / failure, or gives "state of the world", or diff "state of the world".
Let's say in the Farmerama game, which I hmm... figured out ;-) it works like this - each object has a field associated with it, which stores the time of the next event, unix timestamp. For example, the time when something on the field will ripen, and so on.
All sorts of animations and the visual display of the object operate on the value of this field, that is, if, judging by the timer, the field should already be ripe, it is drawn on the client as ready. The client regularly sends PUSH messages to the server in response to which the server reports the new real state of the field, including new timer values. So if there is some kind of out of sync or delay in the server response - though the hollow is drawn as ripe or removed or some other state change - but the real state of the game (adding or reducing resources) will only happen when the server confirms it.
Synchronization with the server occurs from time to time even if the player does nothing, and after the player's actions are sent to the server.
I think there is another option to simultaneously turn the timers on both the server and the client, after the timer on the client has expired, send a synchronization request to the server (Or do a push from the server to the client by timer). The EU can also spin over WebSocket with a constant connection.
If we are talking about a game, then all states, timers, the main loop are implemented on the server, and the client receives updates to show an adequate picture. It seems to me important to be able to update the client exactly at the moment when the event occurred. For example, a chat message has arrived.
I believe the question was "how to update the state of the game in the browser". The link describes options for how to pull the server, with implementation examples: stackoverflow.com/questions/11077857/what-are-long... We
do not consider the "pull every second" option. The easiest to implement on the client for older browsers: long polling. But if WebSockets are supported (and sockets are now supported by everything except opera mini), sockets must be used. If you score on IE, then you can Server-sent events.
caniuse.com/#search=web%20sockets
caniuse.com/#search=eventsource
It is possible at the very beginning, for each object, to transfer to the client information about the start time of the countdown and the duration of the countdown. For example:
objectList = {
object1: {
timestart: 1420455312000,
timeExpire: 180*60*1000, //180 секунд
},
object2: {
timestart: 1420455300000,
timeExpire: 150*60*1000, //150 секунд
},
}
There are many options, depending on the quality of the connection, the acceptability of lags, and the possibility of fraud.
But you need to understand that animation in such games is not tied to the server, the game server is responsible for the correctness of changing the states of the game world and can issue the current state of game objects.
In the general case, the essence is somewhere like this:
1) a request is sent to the server "plant_dill_in_a_bed_with_coordinates(5,5)"
2) The server remembers the state of the garden in coordinate 5.5 and sends a response that everything is OK, it will grow for 300 seconds.
3) The client starts a timer for 300 seconds, according to which the animation of how the dill grows will change
4) After 300 seconds, the client switches the dill at coordinates 5.5 to the "ready for harvest" state
5) The user presses to harvest dill, the server goes "harvest_in_coordinates(5,5)"
6) The server checks that enough time has passed (i.e. 300 seconds +/- lag) and gives the answer that yes it is possible to collect dill. And it keeps the state of the garden as empty, and throws the dill into the barn.
It is also worth noting that a second on the server is, somehow, close to astronomical, but here 1 second on JS in the browser can be anything.
Therefore, no one counts a second, they count the delta in time.
Those. create an object on which timeouts are stored in seconds, the current time and functions that will be called after their expiration. Then setinterval/setTimeout, let's say once every 1 hundredth of a second, checks the deltas of the current time against those stored in this object, and if the delta is greater than the timeout, then it pulls the handler functions.
For the initial version of the network protocol, it is enough to simply send HTTP requests based on events; as the project grows, it will become clear in which direction to optimize the protocol. there are really a lot of variations.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question