# Best way to handle tick-based resource generation in an online multiplayer strategy game

I'm building an online multiplayer strategy game. The game has no offline mode and requires syncing up to a server. Part of this game is constructing buildings which will generate resources each second. These resources would be generated whether the player is online or offline.

If this was a purely offline game, this would be easy: get the delta between when the app was closed and when it was opened again and multiply by resources per second and just fast forward everything.

Since it's an online game, and other players can be affected by the resources an offline user has, these calculations have to occur on the backend.

My question is: if I have 10,000 players, each with 20 buildings generating resources on a per second basis, how on earth should I handle 200,000 things all at once and syncing to a database?

• Hint: you can fast-forward your state when a different user logs on too. There's nothing special about the owning player that makes this solution applicable to them alone. Jun 16 '20 at 6:50

Recalculate (fast-forward) resource amount on each use.

• On server, recalculate after db fetch and keep it cached for duration of current request. Do not store into db unless it's modified explicitly, e.g. withdrawn or deposited. Anything that affects growth rate must trigger update too.

• On client, do it every time value is used (mostly for rendering, I presume), or do it periodically with e.g. 1 second interval. Carefully synchronize time between client and server to minimize desync issues.

(Throwing in some code because why not.)

class PassivelyGeneratedResource
{