How to make a recurrent auto update correctly? [closed]

0

I'm creating a small browser game and recently I had a question about how to make a database value be manipulated (queried / updated) every second even with the user disconnected.

I have in my database a table called recursos with columns ouro and ouroPorSegundo .

The way I managed to make it work is to create a page just for the server, using it setTimeout to, every second, call a PHP page that updates the database.

Is this way to do it right? I think I may be overwhelmed when using this method several times (I use it to check if the troops / units have already returned to the base after an attack), and I need to keep a page open on the server to keep updating the data from all users.

Edit: The game must work in the style of tribal wars , ikariam , they use some system that every second the database is changed, because you can receive an attack of another player, and depending on the second that receives the attack the Stolen gold has a different value.

I'm doing this on the server page:

$(function() {       
    getStatus();
});

function getStatus() {       
    $('div#status').load('getstatus.php');
    setTimeout("getStatus()",1000);      
}

getstatus.php:

$select = mysqli_query($conexao, "SELECT * FROM player_resources WHERE id = 1");
    if (mysqli_num_rows($select) >= 1) {
        while($row = mysqli_fetch_array($select)) {
            $ouro_hora = $row['ouro_hora'];
        }
        $ouro_seg = $ouro_hora / 60 / 60;
    }

    $update = mysqli_query($conexao, "UPDATE player_resources SET ouro = ouro + $ouro_seg WHERE id = 1");

As I said it is functional, but the question is whether it is the right way to do it.

    
asked by anonymous 10.08.2016 / 05:25

2 answers

2

I understood what you want to do, but that's not how you do it. Try to imagine a MMORPG , imagine the amount of hits your database will suffer if it has thousands or millions of users.

For this case, the simplest thing to do is, every time there is a MANUAL transaction in the character's resources, you record the current balance and the time that happened. Ex:

10/08/2016 11:52:30 100G

Assuming that in your game the character gains 1G every 1 second, if I go to my 12h00 balance, the query will return 550G , but in the bank the same record will be saved - 100G às 11:52:30 . If 12h00 I spend 130G buying something, for example, then a MANUAL transaction will occur, and I just update the current balance in the bank.

10/08/2016 12:00:00 320G

And again, when I check the balance at 12:01:40 for example, I'll see that I have 420G . Well just do the calculation before displaying.

In this way your game will only make updates to the database when there is a MANUAL feature transaction, not every second / player - which would make your game impractical. And it will only consult the bank and do the calculations when a player is playing, so your bank rests.

The way you were thinking of doing it to me, if I create a character and then never play again, your script will continue to run for my character, every day, every second, forever.

UPDATE

In case the player is attacked, to know the amount of resources it has, simply check the status of the balance and calculate the current balance. For example, if the balance status is 10/08/2016 12:00:00 320G , the attack time is 10/08/2016 18:15:40 , then the current balance is 21.960G - considering the 1G gain.     

10.08.2016 / 12:02
1

By what I understand you have a script running to update information in the database even when the user is not logged in, right? If so, you can make a simple PHP script that just changes the database information, removes timeout and puts the script to run on CRON of your server . The only problem there is that the CRON minimum time is 1 minute.

    
10.08.2016 / 11:45