Ir para conteúdo

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Pedroalves

somar valores dinamicos

Recommended Posts

o meu problema e criar uma função que vai me somando valores constantemente

como acontece com os jogos online tipo ogame ou ikaria

tenho que usar thread

não sei como criar a função

segue-se o codigo

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <meta name="description" content="Game">
    <meta name="author" content="Pedro Alves">
    <link rel="icon" href="../favicon.ico">

    <title>Game</title>

    <!-- Bootstrap core CSS -->
    <link href="dist/css/bootstrap.min.css" rel="stylesheet">
<link href="dist/css/menu.css" rel="stylesheet" type="text/css">
<link href="dist/css/styleimage.css" rel="stylesheet">
    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <link href="dist/css/ie10-viewport-bug-workaround.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="dist/css/starter-template.css" rel="stylesheet">

    <!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
    <!--[if lt IE 9]><script src="../../assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
    <script src="dist/js/ie-emulation-modes-warning.js"></script>

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
 <script type="text/javascript">
 function startTime() {
     var today=new Date();
     var h=today.getHours();
     var m=today.getMinutes();
     var s=today.getSeconds();
     
     // add a zero in front of numbers<10
     m=checkTime(m);
     s=checkTime(s);
     document.getElementById('txt').innerHTML=h+":"+m+":"+s;
     
 }

 function checkTime(i){
 if (i<10) {
     i= "0" + i;
 }
     return i;
 }
 
 setInterval(function(){ 
 	startTime();
 }, 
 500);
 </script>
  </head>
  
<body>


<?php 
require_once('../configurations/auth.php');
require_once('../configurations/Database.php');

$ola=$_SESSION['SESS_LOGIN'];
$dinheiro=$_SESSION['SESS_DINHEIRO']; 
$ouro=$_SESSION['SESS_OURO'];
$madeira=$_SESSION['SESS_MADEIRA'];
$metal=$_SESSION['SESS_METAL'] ;
$pedra=$_SESSION['SESS_PEDRA'];
$energia=$_SESSION['SESS_ENERGIA'];
$comida=$_SESSION['SESS_COMIDA'];
$petrolio=$_SESSION['SESS_PETROLIO'];
//$nome_cidade=$_SESSION['SESS_NOME_CIDADE'];
//$continente=$_SESSION['SESS_CONTINENTE'];
//$coordenadas=$_SESSION['SESS_COORDENADAS'];
include ('include/menu.php');?>
<div id="conteudo-left"> 
<h1> <img src="../imagens/logo.ico">Game </h1> 
</div>
<div id="conteudo-right">
  <h5>
<img width="25px" height="25px" src="../imagens/recursos/ouro.png"><?php echo $ouro; ?>
  	<img width="25px" height="25px" src="../imagens/recursos/madeira.png"><?php echo $madeira; ?>
    <img width="25px" height="25px" src="../imagens/recursos/metal.png"><?php echo $metal;?>
    <img width="25px" height="25px" src="../imagens/recursos/pedra.png"><?php echo $pedra;?>
    <img src="../imagens/logo.ico"><?php echo $energia;?>
    <img src="../imagens/logo.ico"><?php echo $comida;?>
    <img width="25px" height="25px" src="../imagens/recursos/petrolio.png"><?php echo $petrolio;?>
     <img src="../imagens/logo.ico"><?php echo $dinheiro; ?> 
  
  	
    </h5>
   </div>
   
 <div style="width:100%;height:50px;background-color:#FFF"></div>
 
 

<div class="container-fluid"  style="background-color:#FFF;height:550px;">
  
  <div class="row" style="width:100%">
    <div class="col-sm-4" style="width:25%">
	<?php include ('include/vermenu.php');
 ?>
 </div>
    <div class="col-sm-4" style="background-color:#FFF; width:50%">
    <div style="height:50px;background-color:#000;"> Mensagens  
    </div>
    <div style="background-color:#0F9;height:700px"> fdssdklfsdklfjskldfjkldsjfl</div>
     </div>
    <div class="col-sm-4" style="background-color:#000; width:25%;height:100%" >cidades </div>
  </div>
 
   
  </div>

                 
<div>
            
  <?php  
include ('include/footer.php');
?>
</div>



    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script>window.jQuery || document.write('<script src="dist/js/jquery.min.js"><\/script>')</script>
    <script src="dist/js/bootstrap.min.js"></script>
    <!-- Just to make our placeholder images work. Don't actually copy the next line! -->
    <script src="dist/js/holder.min.js"></script>
   
    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <script src="dist/js/ie10-viewport-bug-workaround.js"></script>
    
      <script src="dist/js/bootstrapValidator.js"></script>
   <script src="dist/js/bootstrapValidator.min.js"></script>
   
  </body>
</html>

Compartilhar este post


Link para o post
Compartilhar em outros sites

Compartilhar este post


Link para o post
Compartilhar em outros sites

obrigada pela ajuda mas não ajudou eu estou a criar um jogo online via browser tipo ogame não estou a conseguir por a somar eu tenho uma variável ouro

eu quero por exemplo a mina esta a

nivel 1 produz 20 unidades a hora

nivel 2 produz 32 unidades a hora

o meu problema é fazer a função e faça isso

Compartilhar este post


Link para o post
Compartilhar em outros sites

O Jogo grava estado?

Quando você fala:

nível 1 produz 20 unidades a hora
nível 2 produz 32 unidades a hora

eu enxergo uma rotina a x tempo atualizando todos os registros de acordo com o nível ou tabela de unidades (que você já tem).

E ai seria necessário ficar atualizando a pagina (setInterval - javascript ?)

Enfim busca por Chat em php, o comportamento vai ser bem parecido com o que você precisa.

Alguns artigos que talvez de auxiliem:

http://rberaldo.com.br/server-push-long-polling-php-ios/

Compartilhar este post


Link para o post
Compartilhar em outros sites

sim vai ter gravação de estado

não estou a conseguir por a funcionar

segue-se o código

servidor php

<?php

require_once ('configurations/config.php');

// arquivo cujo conteúdo será enviado ao cliente

//$dataFileName = 'data.txt';


$timestart=time();
$PDO = new PDO('mysql:host=localhost;dbname=game', 'root','');

if(isset($_POST ['timestamp'])){
	
	$timestamp=$_POST [ 'timestamp' ];
	
}
else {
	$tempo=$PDO->prepare("SELECT NOW() as now");
	$tempo->execute();
	$row=$tempo->fetchObject();
	$timestamp=$row->now;
}
/* $sql = $PDO->prepare( "SELECT * FROM comments WHERE timestamp >'$timestamp'" );

 $newdata = false;
 $notificacoes=array();
 while (!$newdata &&(time()-$timestart)<20) {
 	 $sql->execute();
 while ($row=$sql->fetchAll(PDO::FETCH_ASSOC)) {
 
	 $notificacoes=$row;
     	$newdata=true;
 }  
 usleep(500000);  
 } 
 $tempo=$PDO->prepare("SELECT NOW() as now");
	$tempo->execute();
	$row=$tempo->fetchObject();
	$timestamp=$row->now;
	$data= array('notificacoes'=>$notificacoes,'timestamp'=>$timestamp);
	echo json_encode($data);
	exit();

*/
$sql = $PDO->prepare("SELECT cidade.ouro,cidade.madeira,cidade.metal,cidade.pedra,cidade.energia, cidade.comida,cidade.petrolio FROM users INNER JOIN cidade ON users.cod_user=cidade.cod_user WHERE cidade.timestamp>'$timestamp'AND users.username='admin';");

 $newdata = false;
 $notificacoes=array();
 while (!$newdata &&(time()-$timestart)<20) {
 	 $sql->execute();
 while ($row=$sql->fetchAll(PDO::FETCH_ASSOC)) {
 
	 $notificacoes=$row;
     	$newdata=true;
 }  
 usleep(500000);  
 } 
 $tempo=$PDO->prepare("SELECT NOW() as now");
	$tempo->execute();
	$row=$tempo->fetchObject();
	$timestamp=$row->now;
	$data= array('notificacoes'=>$notificacoes,'timestamp'=>$timestamp);
	echo json_encode($data);
	exit();
?>

cliente.js

$(function(){
notificacoes();
});
function notificacoes(timestamp){
	var data={};
	if(typeof timestamp!='undefined')
	data.timestamp=timestamp;
	$.post('server.php',data,function(res){
		for(i in res.notificacoes){
			var resultado= 0;
			var soma=0;
			var valor=25;
			resultado[i]= parseInt(res.notificacoes[i].ouro) + parseInt(valor);
			soma=resultado[i];
			
		}
		   $('#response').append(soma);
		notificacoes(res.timestamp);
	},'json');
	
}

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.