0
I am creating a small browser game and recently I came up with a question about how to make a database value be manipulated (consulted/updated) every second, even with the user disconnected.
I have in my database a table called recursos
with the columns ouro
and ouroPorSegundo
.
The way I was able to make it work is by creating a server-only page, in it using the setTimeout
to, every second, call a PHP page that gives an update in the database.
Is this the right way to do it? I think it may be overloaded when using this method several times (I also use it to check if the troops/units have already returned to the "base" after an attack), and I need to keep an open page on the server to update the data of all users.
Edit:
The game should 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 from another player, and depending on the second that receives the attack the stolen gold has a different value.
I’m doing it this way, 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");
Like I said, it’s functional, but the question is whether it’s the right way to do it.
Hello Fadu, I think your chances of getting a good answer get a lot better with a Minimum, Complete and Verifiable Example. It’s a little hard to understand what you’re doing, but if the idea is to run updates continuous in a table with data only available on the client side a web service being called by AJAX requests is not "wrong", although long Polling and websockets climb better in most scenarios (see this reply in English for more details.
– Anthony Accioly