N
N
nskaskyo2022-01-31 20:15:06
MongoDB
nskaskyo, 2022-01-31 20:15:06

What is the best way to implement a game loop in nodejs?

Good day.

Help make a decision. There is an api and a client (let's say the web is based on react) in which you need to implement the game logic:
Back:
The user has a resource and a level.
The resource increases every second. The speed depends on the level.
To increase the level, certain resource values ​​\u200b\u200bare needed. Leveling up takes time

Front:
Display the amount of resources, the current level and the progress of leveling up every second.

A standard task, like in Vikings or any other mobile strategy.

The question is how best to implement this so that with a load of several hundred online and several thousand offline, this all works.
Update the base every second or keep something in memory on the back or even give the stopwatch to the front and how to check it on the back?
Well, a secondary question: are web sockets the norms for this business or will they load everything much more than nttp?

In general, any thoughts on this matter :)
Thank you

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
oleg_ods, 2022-01-31
@oleg_ods

IMHO, I would look in the direction of storing data like (timestamp)start_mining and (double)mining_rate on the back.
Api request: /api/resource{id_player} -> calculates the current value of the player's resource.
You can also send mining_rate to the front and turn the counters on in the browser periodically synchronizing with the server.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question