AJAX + PHP - Atualizar página até que valor seja zero
Bom, estou criando um jogo online. Na página de luta, pensei em adicionar um ajax, para atualizar a página(sem refresh) a cada 2 segundos.
Ao atualizar a página você dá um ataque e leva 1 ataque. E a página deve se atualizar até que o valor da vida de algum dos guerreiros seja zero:
if (($player->hp <= 0) or ($enemy->hp <= 0))
Tenho algumas perguntas:
- Imaginando 50 usuários online lutando, e atualizando a database a cada 2s cada um, será que o servidor pode sobrecarregar?
- E a principal, como posso fazer com que o ajax pare de atualizar a página (monster.php?act=attack&id=3&hit=Atacar) após o hp do player ou do enemy zerarem?
Ahh, e aqui está o código que fica atualizando a página:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Ajax</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function Ajax()
{
var
$http
$self = arguments.callee;
if (window.XMLHttpRequest) {
$http = new XMLHttpRequest();
} else if (window.ActiveXObject) {
try {
$http = new ActiveXObject('Msxml2.XMLHTTP');
} catch(e) {
$http = new ActiveXObject('Microsoft.XMLHTTP');
}
}
if ($http) {
$http.onreadystatechange = function()
{
if (/4|^complete$/.test($http.readyState)) {
document.getElementById('playerhp').innerHTML = $http.responseText;
setTimeout(function(){$self();}, 2000);
}
};
$http.open('GET', 'monster.php?act=attack&id=<?php echo $player->id; ?>&hit=Atacar', true);
$http.send(null);
}
}
</script>
</head>
<body>
<script type="text/javascript">
setTimeout(function() {Ajax();}, 2000);
</script>
<div id="ReloadThis"><font size="1px">Carregando...</font></div>
</body>
</html>Discussão (1)
Carregando comentários...