V
V
Vitaly2016-12-09 12:32:39
PHP
Vitaly, 2016-12-09 12:32:39

How to keep the sequence with asynchronous requests to the database?

Example. We have a character that can be damaged, but which restores health.
The scenario is as follows:
1. 2 health units left
2. damage 1
3. health recovery
4. урон 1
Шаги 3 и 4 были вызваны почти одновременно, но сохранение последовательности критически влияет на исход. Предположим, что восстановление происходит по довольно сложному алгоритму, где нужно учесть много факторов. Тогда перед тем, как сохранить в базу результат, с персонажа будет списан последний хп, из-за чего он умирает и игра завершается поражением.
Важно оговорить реляционные и нереляционные базы, если между ними в этом плане есть различия.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Andrey, 2016-12-09
@vitali1995

It seems to me that asynchronous work using queues is perfect for you.
For example rabbitmq

A
Alexander Filippenko, 2016-12-09
@alexfilus

The simplest thing is to execute the 4th request in the callback of the 3rd one.

S
Stac, 2016-12-09
@Stac

Usually, a simple numbering of requests helps.
For example, you have a table or nosql document with the code name hp_history - the history of health changes.

id| hp | action | value | timestamp
1 | 2|...|...|nnnn1
2| 1 | урон | 1 |nnn28
3| 10| лечение | 10 |nnn34
4| 9 | урон | 1 | nnn35

In this case, the order is clear. The current value can be stored explicitly, or it can be calculated as the sum of the changes (damage in this case must be negative). Choose what suits your performance.
It is also possible that you do not need to store the entire history of health changes, but only the last part of it, the current battle.
In a critical situation, such as the possible death of a character, it is worth making a synchronous request to make sure that he really died.

O
Oleg, 2016-12-09
@ollisso

In the servers that I saw (lineage, ultima, rf, etc.) - almost everywhere the data is not written immediately.
Т.е. обрабатываются в памяти, и пишут в базу раз в секунду, раз в 10 секунд и тп.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question