Page 1 of 1

Attack Turns [Resolved]

Posted: Sun Aug 25, 2013 8:29 am
by Epiales
Okay, me again. I have setup the following code so that when a character is attacked, that it randomly chooses the amount of turns that is used that round. It echo's how many turns are used each round as well. I have it setup, but it's only deducting the very LAST turn amount used instead of all of the turns accumulated for that fight. How can I fix this?

Code: Select all

/////////////////////////////////ADDED/////////////////////////////
$turnsused = rand(1,5);
$currentturns = $stats['turns'];
$newturns = $currentturns - $turnsused;
/////////////////////////////////ADDED/////////////////////////////


echo $user['username'] . "'s Attack is " . $stats['attack'] . "<br>";
echo $characters . "'s defense is " . $charactersdefense. "<br>";

if ($stats['attack'] > $charactersdefense) {

    echo $user['username'] . " hits! <br>";
    $playersdamage = rand(1,6);
    $newcharactershp = $charactershp - $playersdamage;
    echo "For " . $playersdamage . " points of damage. <br><br>";

/////////////////////////////////ADDED/////////////////////////////
    echo "You used " . $turnsused . " turns this round. <br><br>";
/////////////////////////////////ADDED/////////////////////////////

    if ($newcharactershp < 1) {
    
    echo "The " . $characters . " has been killed. ";

$isgold = rand(0,$charactersgold);
    $isloot = rand(0,100);

    echo " You loot " . $isgold . " gold pieces.<br>";

    $updateplayer="update stats set gold=gold+'$isgold', turns = '$newturns' where id='$id'";
  mysql_query($updateplayer) or die("Could not update player");
 

Re: Attack Turns

Posted: Sun Aug 25, 2013 10:16 am
by vitinho444
I can't really understand the problem.
But for what i understand i think you are only querying the new turns if the enemy dies. You need to do that after the attack.

I hope that it works.

Re: Attack Turns

Posted: Sun Aug 25, 2013 11:58 am
by Epiales
vitinho444 wrote:I can't really understand the problem.
But for what i understand i think you are only querying the new turns if the enemy dies. You need to do that after the attack.

I hope that it works.
It is after each attack. It shows how many turns it took with each hit. The problem is getting ALL the turns to add together, instead of the very last hit, that kills the character; it shows how many turns it took to kill them on the last hit, but only adds that one turn, not all the previous turns that were used in order to bring their health down and kill them.

Example...you attack and hit them. It took 3 turns on that attack, but they aren't dead. So you have to attack again....then it takes 2 turns on that hit...but they aren't dead....then on the 3rd hit, you finally kill them for 3 turns on that attack. It only puts the last 3 turns into the database instead of having a total of 8 turns for the entire battle. There must be a way to add those.

Re: Attack Turns

Posted: Sun Aug 25, 2013 2:27 pm
by vitinho444
Hum i see

Instead of this:

Code: Select all

$updateplayer="update stats set gold=gold+'$isgold', turns = '$newturns' where id='$id'";
put this:

Code: Select all

$finalturns = $stats['turns'] + $newturns;
$updateplayer="update stats set gold=gold+'$isgold', turns =  '$finalturns' where id='$id'";

Re: Attack Turns

Posted: Sun Aug 25, 2013 3:25 pm
by Epiales
vitinho444 wrote:Hum i see

Instead of this:

Code: Select all

$updateplayer="update stats set gold=gold+'$isgold', turns = '$newturns' where id='$id'";
put this:

Code: Select all

$finalturns = $stats['turns'] + $newturns;
$updateplayer="update stats set gold=gold+'$isgold', turns =  '$finalturns' where id='$id'";
It does the exact same thing. Only add the last random turn, instead of all of them combined. Thanks for trying to help.

Re: Attack Turns

Posted: Sun Aug 25, 2013 4:01 pm
by vitinho444
Then add a database field and keep adding the new turns to the database

like
$newturns = random(...);
$newturns = $old_database_turns + $newturns;

Re: Attack Turns

Posted: Mon Aug 26, 2013 3:48 am
by Epiales
vitinho444 wrote:Then add a database field and keep adding the new turns to the database

like
$newturns = random(...);
$newturns = $old_database_turns + $newturns;
I'll try something like that after while. See if I can't get it working. I'll keep all posted in here. I'm doing about 100 things at once LOL. :) Thank you!

Re: Attack Turns

Posted: Mon Aug 26, 2013 10:07 am
by vitinho444
Take easy on yourself :P
Good luck

Re: Attack Turns

Posted: Mon Aug 26, 2013 9:25 pm
by hallsofvallhalla
you could eitehr store it in a cookie, pass it in a post variable or like vint said put it in a DB column to retrieve later. Just remember to reset to 0 after battle.

Re: Attack Turns

Posted: Tue Aug 27, 2013 1:18 am
by Epiales
Okay, have it working. In the end I put this:

Code: Select all

$turnsused = rand(1,5);

$currentturns = $stats['turns'];
$newturns = $currentturns - $turnsused;

$updateplayer="update stats set turns = '$newturns' where id='$id'";
  mysql_query($updateplayer) or die("Could not update player"); 
Problem was that I wasn't updating the database after each round. I only updated at the end of the battle. So by updating it after my formula's, it seems to have solved that problem.