Decided To Stay With PHP Battle, but?

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.

Re: Decided To Stay With PHP Battle, but?

Postby Epiales » Mon Nov 17, 2014 12:07 pm

Jackolantern wrote:The JS part has to be below the PHP loop. What would be in the PHP $battleServerArray if it is at the top? You need to put the PHP battle loop first, and fill up the $battleServerArray with the messages you want to output on the client-side.

Code: Select all
$battlelog .= ".$battleServerArray[$x]."; // Critical Hit   


I am not sure what you are doing here. You need to be putting messages into $battleServerArray (there is nothing in it yet to assign to anything else). Also, you would not put double quotes or periods around the variable like that.


It was just a test on the above. The thing is though, is I have put the JS below the php loop. That's what I tried explaining above. If I put it above the loop, I get an undefined, but if I put it below the loop, it does nothing. And that is with me testing the battle array with just a test like this:

Code: Select all
$battleServerArray[$x] = "Does this work?";


I can't figure out why it will not work. I have even tried echo $battleServerArray[$x]; and it won't echo the contents. It just does nothing
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
 
Posts: 1119
Joined: Wed Aug 14, 2013 8:38 pm
Has thanked: 62 times
Been thanked: 6 times

Re: Decided To Stay With PHP Battle, but?

Postby Jackolantern » Mon Nov 17, 2014 4:48 pm

Do you have the rest of the JS? If you don't, that is why it isn't doing anything when you put the JS below the PHP. But the JS has to go below the PHP loop. Otherwise, it is the same as this:

Code: Select all
echo $result;
$result = 3 + 2;


You are trying to use a PHP variable to crate the JS variable before you have put anything in it yet.
The indelible lord of tl;dr
User avatar
Jackolantern
 
Posts: 10889
Joined: Wed Jul 01, 2009 6:00 pm
Location: Houston, TX
Has thanked: 22 times
Been thanked: 92 times
Blog: View Blog (1)

Re: Decided To Stay With PHP Battle, but?

Postby Epiales » Mon Nov 17, 2014 11:06 pm

Code: Select all
<?php
$maxRounds 
= 10 ;
$x = 0;
$battleServerArray = array();

    $counter_opponent = 1;

        $player_new_hpoints = $playerHp;
        $npc_new_hpoints =  $enemyHp;
        
for($i = 1; $i <= $maxRounds; $i++) {

    $player_accuracy = mt_rand($user_accuracy, 100);
    $player_atkdam = mt_rand(0,$user_strength);
    $player_critical = mt_rand(0,$user_strength)*2;
    $attackmiss = 0;
    $enemy_accuracy = mt_rand($characters_accuracy, 100);
    $enemy_atkdam = mt_rand(0,$row['attack']);
    $enemy_critical = mt_rand(0,$row['attack'])*2;

if(
$player_accuracy > 100 OR $player_accuracy >= $playerStrength) {
$battleServerArray[$x] = "Does this work?";
    $x++;
    
        $npc_hp_lost 
= $player_critical;
        $npc_new_hpoints -= $npc_hp_lost;    

        $updateatkhp 
= "UPDATE npc_characters SET hpoints='$npc_new_hpoints' WHERE charid ='$character' AND userid = '$user_id'"; 
        $user_query 
= mysqli_query($db_conx, $updateatkhp);
$battleServerArray[$x] = "Does this work?";
    $x++;     
     
}elseif($player_accuracy < 80 || $player_accuracy > 80) {
     
        $npc_hp_lost 
= $player_atkdam;
        $npc_new_hpoints -= $npc_hp_lost;    

        $updateatkhp 
= "UPDATE npc_characters SET hpoints='$npc_new_hpoints' WHERE charid ='$character' AND userid = '$user_id'";
        $user_query = mysqli_query($db_conx, $updateatkhp);
$battleServerArray[$x] = "Does this work?";
    $x++; 
    
}else{
$battleServerArray[$x] = "Does this work?";
    $x++;     
}

if(
$enemy_accuracy > 100 OR $enemy_accuracy >= $enemyattack) {
     
        $player_hp_lost 
= $enemy_critical;
        $player_new_hpoints -= $player_hp_lost;    

         $updateplayerhp 
= "UPDATE users SET hpoints='$player_new_hpoints' WHERE id = '$user_id'"; 
        $user_query 
= mysqli_query($db_conx, $updateplayerhp);
$battleServerArray[$x] = "Does this work?";
    $x++;    
     
}elseif($enemy_accuracy < 80 || $enemy_accuracy > 80) {
     
        $player_hp_lost 
= $enemy_atkdam;
        $player_new_hpoints -= $player_hp_lost;    

         $updateplayerhp 
= "UPDATE users SET hpoints='$player_new_hpoints' WHERE id = '$user_id'"; 
        $user_query 
= mysqli_query($db_conx, $updateplayerhp);
$battleServerArray[$x] = "Does this work?";
    $x++;     
}else{
$battleServerArray[$x] = "Does this work?";
    $x++;
}


//breaking the loop

if($npc_new_hpoints <= 0) { 
$battleServerArray
[$x] = "YOU WIN?";
    $x++;
 break;
}elseif(
$player_new_hpoints <= 0) {
$battleServerArray[$x] = "YOU LOSE";
    $x++;
 break;
     }
}
?>

<script type="text/javascript">

   var battleResults = [
   <?php for($y = 0; $y < count($battleServerArray); $y++) {              
             
//print out the value and add a comma if this is not the last entry
             if ($y != (count($battleServerArray) - 1)) {
                  echo $battleServerArray[$y].",";
             } else {
                  echo $battleServerArray[$y];
             }
      } ?>
     //close the array initializer bracket
     ];
   
    var x = 0;
    
    setTimeout(function() {
        document.status.msgbx.value = battleResults[x++];
    }, 3000);
</script>


As stated, I've put it above and below. It doesn't work. I understand it has to go below the loop, I even tried putting it right after the loops itself. I've moved it everywhere to test it.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
 
Posts: 1119
Joined: Wed Aug 14, 2013 8:38 pm
Has thanked: 62 times
Been thanked: 6 times

Re: Decided To Stay With PHP Battle, but?

Postby MikuzA » Tue Nov 18, 2014 1:57 am

The problem is not in the PHP, now it's JS.

This is what I get when I run your code :)
Oops ;)
Code: Select all
<script type="text/javascript">

   var battleResults = [
   Does this work?,Does this work?,Does this work?,YOU WIN?     //close the array initializer bracket
     ];
   
    var x = 0;
   
    setTimeout(function() {
        alert(battleResults[x++]);
    }, 3000);
</script>



Something like this instead,

Code: Select all
   var battleResults = [
   <?php for($y = 0; $y < count($battleServerArray); $y++) {             
             //print out the value and add a comma if this is not the last entry
             if ($y != (count($battleServerArray) - 1)) {
                  echo "\"".$battleServerArray[$y]."\",";
             } else {
                  echo "\"".$battleServerArray[$y]."\"";
             }
      } ?>
     ];
Why so serious?

PHP, HTML, JavaScript
MySQL, DB2
Pentaho, DataStage
User avatar
MikuzA
 
Posts: 391
Joined: Thu Aug 08, 2013 3:57 am
Location: Helsinki, Finland
Has thanked: 0 time
Been thanked: 21 times

Re: Decided To Stay With PHP Battle, but?

Postby Jackolantern » Tue Nov 18, 2014 7:20 am

Oops! I forgot that you need the double-quotes around the values in the JS array >.>
The indelible lord of tl;dr
User avatar
Jackolantern
 
Posts: 10889
Joined: Wed Jul 01, 2009 6:00 pm
Location: Houston, TX
Has thanked: 22 times
Been thanked: 92 times
Blog: View Blog (1)

Re: Decided To Stay With PHP Battle, but?

Postby Epiales » Tue Nov 18, 2014 9:17 am

Thank you two!

Okay, I have updated the code, but it only displays the attackers attack information and goes no further. The array doesn't seem to be working correctly:

Code: Select all
<?php
$maxRounds 
10 ;

$battleServerArray = array();
$x 0;
    
$counter_opponent 1;

    
$player_new_hpoints $playerHp;
    
$npc_new_hpoints =  $enemyHp;
        
for(
$i 1$i <= $maxRounds$i++) {

    
$player_accuracy mt_rand($user_accuracy100);
    
$player_atkdam mt_rand(0,$user_strength);
    
$player_critical mt_rand(0,$user_strength)*2;
    
$attackmiss 0;
    
$enemy_accuracy mt_rand($characters_accuracy100);
    
$enemy_atkdam mt_rand(0,$row['attack']);
    
$enemy_critical mt_rand(0,$row['attack'])*2;

if(
$player_accuracy 100 OR $player_accuracy >= $playerStrength) {

$battleServerArray[$x] = "Round: ".$counter_opponent++." Attack(1, ".$player_critical.");"// Critical Hit

    
$x++;
    
    
$npc_hp_lost $player_critical;
    
$npc_new_hpoints -= $npc_hp_lost;    

        
$updateatkhp "UPDATE npc_characters SET hpoints='$npc_new_hpoints' WHERE charid ='$character' AND userid = '$user_id'"
        
$user_query mysqli_query($db_conx$updateatkhp);
     
}elseif(
$player_accuracy 80 || $player_accuracy 80) {

$battleServerArray[$x] = "Round: ".$counter_opponent++."Attack(2, ".$player_atkdam.");"// Normal Attack
    
$x++;

    
$npc_hp_lost $player_atkdam;
    
$npc_new_hpoints -= $npc_hp_lost;    

        
$updateatkhp "UPDATE npc_characters SET hpoints='$npc_new_hpoints' WHERE charid ='$character' AND userid = '$user_id'";
        
$user_query mysqli_query($db_conx$updateatkhp);
        
}else{

$battleServerArray[$x] = "Round: ".$counter_opponent++."Attack(3, ".$attackmiss.");"//Misses

    
$x++;     
}

if(
$enemy_accuracy 100 OR $enemy_accuracy >= $enemyattack) {

$battleServerArray[$x] = "NPCAttack(1, ".$enemy_critical.");"// Critical Hit

    
$x++;    
     
    
$player_hp_lost $enemy_critical;
    
$player_new_hpoints -= $player_hp_lost;    

         
$updateplayerhp "UPDATE users SET hpoints='$player_new_hpoints' WHERE id = '$user_id'"
        
$user_query mysqli_query($db_conx$updateplayerhp);
     
}elseif(
$enemy_accuracy 80 || $enemy_accuracy 80) {

$battleServerArray[$x] = $battlelog .= "NPCAttack(2, ".$enemy_atkdam.");"// Normal Attack

    
$x++;     
     
    
$player_hp_lost $enemy_atkdam;
    
$player_new_hpoints -= $player_hp_lost;    

         
$updateplayerhp "UPDATE users SET hpoints='$player_new_hpoints' WHERE id = '$user_id'"
        
$user_query mysqli_query($db_conx$updateplayerhp);

}else{

$battleServerArray[$x] = "NPCAttack(3, ".$attackmiss.");"// Miss

    
$x++;
}

?>

<?php
//breaking the loop

if($npc_new_hpoints <= 0) { 
$battleServerArray[$x] = "YOU WIN?";
    
$x++;
 break;
}elseif(
$player_new_hpoints <= 0) {
$battleServerArray[$x] = "YOU LOSE";
    
$x++;
 break;
     }
}
?>

<script type="text/javascript">

   var battleResults = [
   <?php for($y 0$y count($battleServerArray); $y++) {              
             
//print out the value and add a comma if this is not the last entry
             
if ($y != (count($battleServerArray) - 1)) {
                  echo 
"\"".$battleServerArray[$y]."\",";
             } else {
                  echo 
"\"".$battleServerArray[$y]."\"";
             }
      } 
?>
     ];
   
    var x = 0;
    
    setTimeout(function() {
        document.status.msgbx.value = battleResults[x++];
    }, 3000);
</script>


What I get when I run 10 rounds or any amount of rounds:


Code: Select all
Your life:  123456

Round: 1 Attack(1, 38);
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
 
Posts: 1119
Joined: Wed Aug 14, 2013 8:38 pm
Has thanked: 62 times
Been thanked: 6 times

Re: Decided To Stay With PHP Battle, but?

Postby Jackolantern » Tue Nov 18, 2014 10:06 am

Can you run the code again, and then View Source in the browser, and post the relevant code here? I am curious to see what the browser received in the end (when using PHP to write JS dynamically, the browser View Source is going to be your best bet at solving problems).
The indelible lord of tl;dr
User avatar
Jackolantern
 
Posts: 10889
Joined: Wed Jul 01, 2009 6:00 pm
Location: Houston, TX
Has thanked: 22 times
Been thanked: 92 times
Blog: View Blog (1)

Re: Decided To Stay With PHP Battle, but?

Postby Epiales » Tue Nov 18, 2014 10:09 am

Jackolantern wrote:Can you run the code again, and then View Source in the browser, and post the relevant code here? I am curious to see what the browser received in the end (when using PHP to write JS dynamically, the browser View Source is going to be your best bet at solving problems).


When I view page source, all I get is this:

Code: Select all
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Mafia Wars Kingdom</title>
<link rel="icon" href="favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="style/style.css">


But when I press the F12 for google chrome I see this:

Code: Select all
Failed to load resource: net::ERR_CACHE_MISS http://localhost/login2/arenanpc.php?npc=1
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
 
Posts: 1119
Joined: Wed Aug 14, 2013 8:38 pm
Has thanked: 62 times
Been thanked: 6 times

Re: Decided To Stay With PHP Battle, but?

Postby Jackolantern » Tue Nov 18, 2014 10:56 am

That's all you see? So the page is just blank white?
The indelible lord of tl;dr
User avatar
Jackolantern
 
Posts: 10889
Joined: Wed Jul 01, 2009 6:00 pm
Location: Houston, TX
Has thanked: 22 times
Been thanked: 92 times
Blog: View Blog (1)

Re: Decided To Stay With PHP Battle, but?

Postby MikuzA » Tue Nov 18, 2014 11:15 am

Code: Select all
    var x = 0;
   
    setTimeout(function() {
        document.status.msgbx.value = battleResults[x++];
    }, 3000);


This might need a closer look.







psst.. missing JS loop.


PS.

Jacko, you are almost at 10000 posts!
Why so serious?

PHP, HTML, JavaScript
MySQL, DB2
Pentaho, DataStage
User avatar
MikuzA
 
Posts: 391
Joined: Thu Aug 08, 2013 3:57 am
Location: Helsinki, Finland
Has thanked: 0 time
Been thanked: 21 times

PreviousNext

Return to Advanced Help and Support

Who is online

Users browsing this forum: No registered users and 1 guest

cron

x