Updating Stats [Resolved]

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Updating Stats [Resolved]

Post by Epiales »

Now I don't know what anyone means by making the echo's of the stats show AFTER the updates in the script. I have used Halls tutorial script to experiment with, and his doesn't update the stats as you lose hitpoints in arena. I tried moving things around and placing the echo'd results AFTER the actual battles in his script, but no matter what I tried, the stats never updated with each hit. It was always one step behind, which means you have to refresh the page to get current stats.

I've been told to put the updated stats after the battles, but no matter what I do, it just won't work. Is there an easy way to make sure your stats update with each attack and loss of hitpoints? Very frustrating, as I"ve been trying to do this for a month. Any help would be great. TY.
Last edited by Epiales on Fri Oct 11, 2013 12:03 am, edited 1 time in total.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
alexander19
Posts: 180
Joined: Fri Apr 02, 2010 1:05 pm

Re: Updating Stats

Post by alexander19 »

After you update the stats in the fighting script, you make another query by selecting the stats again, and echoing them, that way it will show the current stats and not the ones a round behind.

Something like this :

Code: Select all

//we update the stats first
$player_health = $playerinfo3['hp'] - $monster_attack;
mysql_query("UPDATE `players` SET `hp`='$player_health' WHERE `name`='$player'");

//now we reselect the updated stats and echo them
$playerinfo3 = mysql_fetch_assoc(mysql_query("SELECT * FROM `players` WHERE `name`='$player'"));

echo "Health :<b>".$playerinfo3['hp']."</b>";
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Updating Stats

Post by Epiales »

alexander19 wrote:After you update the stats in the fighting script, you make another query by selecting the stats again, and echoing them, that way it will show the current stats and not the ones a round behind.

Something like this :

Code: Select all

//we update the stats first
$player_health = $playerinfo3['hp'] - $monster_attack;
mysql_query("UPDATE `players` SET `hp`='$player_health' WHERE `name`='$player'");

//now we reselect the updated stats and echo them
$playerinfo3 = mysql_fetch_assoc(mysql_query("SELECT * FROM `players` WHERE `name`='$player'"));

echo "Health :<b>".$playerinfo3['hp']."</b>";
Now how would that work if you used an include that lists all the stats? Would you have to include the included file AFTER the battle? And if so, then how can you put the stats up top and make it work?
Nothing fancy, but a work in progress!

http://gameplaytoday.net
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

Re: Updating Stats

Post by Winawer »

Yes, you would include after the battle.
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Updating Stats

Post by Epiales »

Winawer wrote:Yes, you would include after the battle.
Doesn't that make it difficult to place the stats where you want them then? grrrr
Nothing fancy, but a work in progress!

http://gameplaytoday.net
alexander19
Posts: 180
Joined: Fri Apr 02, 2010 1:05 pm

Re: Updating Stats

Post by alexander19 »

Yes with the current fighting system its kinda hard to place them in the right spot...thats why ajax would be mostly required.
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

Re: Updating Stats

Post by Winawer »

You could probably gather your battle output into a variable, then do the includes and when you want to show the battle output you would echo the variable.

Code: Select all

if( $_POST['fight'] ) {
    $battleOutput = '';
    $damage = calcDamage();
    $battleOutput .= 'Monster hit player for ' . $damage . ' damage!';
    $hp -= $damage;

    //etc.
}

include( 'stats.php' );
echo $battleOutput;
Even better would be some kind of templating system, but that's a big change.
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Updating Stats

Post by Epiales »

Winawer wrote:You could probably gather your battle output into a variable, then do the includes and when you want to show the battle output you would echo the variable.

Code: Select all

if( $_POST['fight'] ) {
    $battleOutput = '';
    $damage = calcDamage();
    $battleOutput .= 'Monster hit player for ' . $damage . ' damage!';
    $hp -= $damage;

    //etc.
}

include( 'stats.php' );
echo $battleOutput;
 
Even better would be some kind of templating system, but that's a big change.
As an example, how would this work on updating the gold that was stolen? Having it auto update, or by using what you said above about placing the battle into a variable. Also, is it possible to just use ajax to auto update when someone clicks on the attack button? Something like that?

Code: Select all

    <?php

    if(isset($_POST['gold'])){
        $turns = protect($_POST['turns']);
        $id = protect($_POST['id']);
        $user_check = mysql_query("SELECT * FROM `stats` WHERE `id`='".$id."'") or die(mysql_error());
        if(mysql_num_rows($user_check) == 0){
            echo '<span id="errormess"><big><center><font color="red"><b>There is no user with that ID!</b></font></span></big><br><br>';
            echo "<a href='muggingrank.php'><center><font color='lime'>Go Back</font></a>";
        }elseif($turns < 1 || $turns > 10){
            echo '<span id="errormess"><big><center><font color="red"><b>You must attack with 1 to 10 turns!</b></font></span></big><br><br>';
            echo "<a href='muggingrank.php'><center><font color='lime'>Go Back</font></a>";

        }elseif($turns > $stats['turns']){
            echo '<span id="errormess"><big><center><font color="red"><b>You do not have enough turns!</b></font></span></big><br><br>';
            echo "<a href='muggingrank.php'><center><font color='lime'>Go Back</font></a>";
        }elseif($id == $_SESSION['uid']){
            echo '<span id="errormess"><big><center><font color="red"><b>You cannot attack yourself!</b></font></span></big><br><br>';
            echo "<a href='muggingrank.php'><center><font color='lime'>Go Back</font></a>";
        }else{
        
// Number of turns they use is a percentage of their attack that they want to use.  1 turn is 10% while 10% is 100%//

            $enemy_stats = mysql_fetch_assoc($user_check);
            $attack_effect = $turns * 0.1 * rand(1,5) * $stats['attack'];
            $defense_effect = rand(1,5) * $enemy_stats['defense'];
            
            echo "<center>You send your gunmen into battle!<br><br>";
            echo "<center>Your gunmen dealt " . number_format($attack_effect) . " damage!<br>"; //How much damage they did to enemy
            
            echo "<center>The enemy's guards dealt " . number_format($defense_effect) . " damage!<br><br>";
                            
            if($attack_effect > $defense_effect){
                $ratio  = rand(1,100);
                $gold_stolen = floor($ratio * $stats['level']);
                            
/////////////////////////IF DEFENDER DOESN'T HAVE AMOUNT OF GOLD STOLEN, GOLD STOLEN WILL EQUAL WHAT THEY DO HAVE///////////////////////////
                if ($enemy_stats['gold'] < $gold_stolen ) {
                $gold_stolen = $enemy_stats['gold'];

                echo "<center>You won the battle! You stole " . $gold_stolen . " gold!<br><br>";
                
                echo "<center><a href='muggingrank.php'><font color='lime'>Go Back</font></a>";
                
                //Subtracts the gold stolen from the user attacked//
                $battle1 = mysql_query("UPDATE `stats` SET `gold`=`gold`-'".$gold_stolen."' WHERE `id`='".$id."'") or die(mysql_error());
                //Update winners gold to show that it has been taken//
                $battle2 = mysql_query("UPDATE `stats` SET `gold`=`gold`+'".$gold_stolen."',`turns`=`turns`-'".$turns."' WHERE `id`='".$_SESSION['uid']."'") or die(mysql_error());
                //Inserts log of the battle into the database//
                $battle3 = mysql_query("INSERT INTO `logs` (`attacker`,`guards`,`attacker_damage`,`guards_damage`,`gold`,`food`,`time`) 
                                        VALUES ('".$_SESSION['uid']."','".$id."','".$attack_effect."','".$defense_effect."','".$gold_stolen."','0','".time()."')") or die(mysql_error()); 
Nothing fancy, but a work in progress!

http://gameplaytoday.net
alexander19
Posts: 180
Joined: Fri Apr 02, 2010 1:05 pm

Re: Updating Stats

Post by alexander19 »

Code: Select all

$updateplayer = "UPDATE stats SET gold=gold+ '$gain' where id='$_SESSION[userid]'";
$user_query = mysqli_query($db_conx, $updateplayer); 
$sql = "SELECT * FROM stats WHERE id = '$_SESSION[userid]'";
$user_query2 = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($user_query2);

echo "Your current gold is: $stats_gold";  
Set the $stats_gold variable again after you select the stats, because right now you echo its older value.

Code: Select all

$updateplayer = "UPDATE stats SET gold=gold+ '$gain' where id='$_SESSION[userid]'";
$user_query = mysqli_query($db_conx, $updateplayer); 
$sql = "SELECT * FROM stats WHERE id = '$_SESSION[userid]'";
$user_query2 = mysqli_query($db_conx, $sql);
$user_query3 = mysqli_fetch_assoc($user_query2);
$stats_gold = $user_query3['gold'];

echo "Your current gold is: $stats_gold";  
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Updating Stats

Post by Epiales »

alexander19 wrote:

Code: Select all

$updateplayer = "UPDATE stats SET gold=gold+ '$gain' where id='$_SESSION[userid]'";
$user_query = mysqli_query($db_conx, $updateplayer); 
$sql = "SELECT * FROM stats WHERE id = '$_SESSION[userid]'";
$user_query2 = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($user_query2);

echo "Your current gold is: $stats_gold";  
Set the $stats_gold variable again after you select the stats, because right now you echo its older value.

Code: Select all

$updateplayer = "UPDATE stats SET gold=gold+ '$gain' where id='$_SESSION[userid]'";
$user_query = mysqli_query($db_conx, $updateplayer); 
$sql = "SELECT * FROM stats WHERE id = '$_SESSION[userid]'";
$user_query2 = mysqli_query($db_conx, $sql);
$user_query3 = mysqli_fetch_assoc($user_query2);
$stats_gold = $user_query3['gold'];

echo "Your current gold is: $stats_gold";  
Okay, I have this and it works great now:

Code: Select all

$updateplayer = "UPDATE stats SET gold=gold+ '$gain' where id='$_SESSION[userid]'";
$user_query = mysqli_query($db_conx, $updateplayer); 
  
$sql = "SELECT * FROM stats WHERE id = '$_SESSION[userid]'";
$user_query2 = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($user_query2);
$row = mysqli_fetch_array($user_query2, MYSQLI_ASSOC);
echo "Your current gold is: $row[gold]";


But the situation is that I want the gold to be at the top of the game. Like within a header file, along with all the other stats. But it won't refresh. I can see now, as long as I use the format I just posted above, AFTER the updated information, then it will always show the current stats, but that means I have to have the stats all over the place instead of all in a group, like in the header, or in an included file.

How would I place the stats at the top and have them update as you play the game. That's what I"ve been trying to figure out for a month now. I do n't want the gold where it echo's the gold right now, but if I move it, then it doesn't update like it should :evil: :evil: :evil: :evil: :evil:

And thank you VERY much for all the help guys.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
Post Reply

Return to “Beginner Help and Support”