Jump to content


Photo

Query help.


  • Please log in to reply
4 replies to this topic

#1 SieRobin

SieRobin
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts
  • LocationNew Jersey

Posted 04 March 2006 - 01:24 AM

Ok, so I'm running a web-based RPG, and I have 3 cron jobs set up. I have turns set at every 6 minutes, Hit Points regeneration every 5 minutes, and revive if dead over 30 minutes.

My problem is, is that I want it to be able to query something if they're alive but are missing HP it will tick 1 HP every 5 minutes. The way I currently have it set up, doesn't work, because if you set a session for it, all it will really do is tick if you're logged in and missing HP and also it will only revive you if you're logged in. The other way I tried doing it was removing the session and just pulling it from the users.. well it will always tick, but if they're dead it will still tick, if they're dead I don't want them to recieve HP. Does anyone have any ideas on how I could set this up, so it will only query HP Regen if you're Alive and query Revive only if you're Dead.

Thanks for the help in advanced :D

Regen Over-Time
<?php
session_start();    
include "connect.php";
if (isset($_SESSION['player'])) 
    {
    $player=$_SESSION['player'];
    $stats="SELECT * from users";
    $stats2=mysql_query($stats) or die("Could not retrieve the users stats.");
    $stats3=mysql_fetch_array($stats2);
    if ($stats3['dead']=='Dead') 
        {
        mysql_query("update users set hp=1, dead='Alive'") or die("Could not query");
        }
    }
?>

Revive Over-Time
<?php
session_start();    
include "connect.php";
if (isset($_SESSION['player'])) 
    {
    $player=$_SESSION['player'];
    $stats="SELECT * from users";
    $stats2=mysql_query($stats) or die("Could not retrieve the users stats.");
    $stats3=mysql_fetch_array($stats2);
    if ($stats3['dead']=='Dead') 
        {
        mysql_query("update users set hp=1, dead='Alive'") or die("Could not query");
        }
    }
?>


#2 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 04 March 2006 - 01:53 AM

Question 1: getting around the session issue: just don't use them. have cron hit a separate PHP file all by itself to check for alive or dead. rephrase your question if this doesn't make sense.

Question 2:
your code will set hp=1 and dead="Alive" to every user it sees, but only if the first one is dead.
    $stats="SELECT * from users";
    $stats2=mysql_query($stats) or die("Could not retrieve the users stats.");
    $stats3=mysql_fetch_array($stats2);
    if ($stats3['dead']=='Dead') {
        mysql_query("update users set hp=1, dead='Alive') or die("Could not query");
    }

the following will fix your dead users:
    mysql_query("update users set hp=1, dead='Alive' WHERE dead='Dead'") or die("Could not query");

Revive Over-Time: Your code is missing (well, you pasted the same code twice). Here's a guess:
    $player=$_SESSION['player'];
    $stats="SELECT * from users";
    $stats2=mysql_query($stats) or die("Could not retrieve the users stats.");
    while($stats3=mysql_fetch_array($stats2))     mysql_query("update users set hp=".$stats3['hp']+1." WHERE user = '".$stats3['user']."'") or die("Could not query");

Sorry, one last update... had to loop the last one -- but a better SQL programmer might be able to tell you how to increment that HP instead of looping PHP to do it. also, I'm guessing that you're user field is called "user" -- if not, you'll have to change it in the code to match.

#3 SieRobin

SieRobin
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts
  • LocationNew Jersey

Posted 04 March 2006 - 01:53 AM

Oops, sorry about that..

<?php
include "connect.php";
    $stats="SELECT * from users";
    $stats2=mysql_query($stats) or die("Could not retrieve the users stats.");
    $stats3=mysql_fetch_array($stats2);
    if ($stats3['dead']=='Alive') {
          if ($stats3['race']=='Troll') {
        mysql_query("update users set hp=(hp+2) where hp<mhp") or die("Could not query.");
        } else {
        mysql_query("update users set hp=(hp+1) where hp<mhp") or die("Could not query.");
      }
  }
?>


#4 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 04 March 2006 - 02:05 AM

I'm not sure about your SQL incremental code there -- but if it works, I'll make a note for my own use :)

Based on this new information here's a SQL rewrite that should work (if your incremental code works, that is)

<?php
include "connect.php";
mysql_query("update users set hp=(hp+2) where hp<mhp AND `dead` = 'Alive' AND `race` = 'Troll'") or die("Could not query.");
mysql_query("update users set hp=(hp+1) where hp<mhp AND `dead` = 'Alive' AND `race` != 'Troll'") or die("Could not query.");
?>


#5 SieRobin

SieRobin
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts
  • LocationNew Jersey

Posted 04 March 2006 - 02:11 AM

I'll let you know if it works :D

Works :] thank you very much for yourhelp michaellunsford!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users