Most of the common example of that in the game is stamina regeneration to control the activity of the players. For example, genshin impact, they have something called resin system, and it regenerates 1 resin in every 8 minutes until the amount maxed out. What is the algorithm for that? In my case it's hp.

My situation: The players' data is saved in the database. I am creating a text-based rpg game using discord bot, and the programming language I am using is python.

Here is what I had thought:

  • when freeing the data from the memory, save the date and time of when the hp became less than max amount to the database.
  • when the data is in the memory, while hp is not full add fixed amount of hp in every interval.
  • when querying the data from database, update the hp, calculate from the saved date and time, interval, and fixed amount of hp.

Thanks in advance.


Usually, databases are used for data at rest. The data about players who are currently offline. It gets saved when the player logs out. When a player comes online again, their data is taken out of the database and loaded into memory, so it can be accessed much faster.

So how do you handle changes which happen to the data at rest while the players are offline?

There are two common options.

A: Cronjobs

Have a background process which runs at regular intervals and updates the whole database. This is usually pretty easy to do. You just figure out how to run a query like UPDATE characters SET hp = hp + 1 WHERE hp < hp_max every minute and all the data will always be up-to-date. This is useful when you have features like browsing offline characters with their current HP.

But unfortunately, online games tend to accumulate a huge amount of dead accounts over time. So after a couple years, you might spend more processing power on updating dead accounts than you spend on those which are actually playing. The nightmare scenario is that the above query will take longer than a minute to execute, causing your cronjob to pile update jobs on your database faster than it can process them and eventually causing it to stop responding to queries altogether.

But for a discord bot for a smaller community, this might not become an issue that quickly.

B: Update on access

This is a far more elegant method I usually recommend, but it takes some more thought and diligence to implement and use it correctly.

When you persist a character to the database, store it with the timestamp of the last update. When you load a character from the database (for whatever reason), check that timestamp, check the difference between the current time, and then simulate the HP recovery algorithm. For example, when your characters recover 1 hp every 60 seconds and this character was last saved 100,000 seconds ago (a bit over a day), you give it (100,000 / 60 =) 1666 HP when they log back in.

Keep in mind that when your game has interactions with characters who are offline (or even just viewing their current HP is possible) then it is important that you update a character whenever you access it, regardless of the reason. When someone attacks an offline character, you need to update them. When someone views the HP of an offline character, you need to update them. When you want to run an analytics query to get the current HP of all characters in the game (for some reason), you have to update every single character to get correct results.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .