Page 1 of 1

Decided To Stay With PHP Battle, but?

Posted: Wed Nov 12, 2014 8:01 am
by Epiales
Okay, I have decided to just stay with php battle, as JS is just getting too much for me. My next goal is looping the battle until it's done... here is the code:

Code: Select all

<?php 
$outputList = '';
$outputList1 = '';
$outputList2 = '';
$outputList3 = '';
$outputList4 = '';
$outputList5 = '';

$sql = "SELECT * FROM npc_characters WHERE userid = '$_SESSION[userid]' AND defeated = '0'";
$user_query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($user_query);
$row = mysqli_fetch_array($user_query, MYSQLI_ASSOC);

$arena_id = $row['userid'];
$arena_char_name = $row['charname'];
$character = $row['charid'];
$arena_char_hp = $row['hpoints'];
$arena_char_maxhp = $row['maxhpoints'];

$sql = "SELECT * FROM characters where id ='$character'"; 
$user_query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($user_query);
while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {

$playerHp = $user_health;
$enemyHp = $arena_char_hp;
$playermaxhp = $user_maxhp;
$enemymaxhp = $arena_char_maxhp;
$playername = $user_username;
$enemyname = $arena_char_name;

//Player's Attack Turn
?>
<?php 
    $player_accuracy = mt_rand($user_accuracy, 100);
    $player_atkdam = mt_rand(0,$user_strength);
    $player_critical = mt_rand(0,$user_strength)*2;

if($player_accuracy > 80 && $player_critical>=$user_strength){

    $outputList .= 'Your critical attack hits and ' . $arena_char_name . ' suffers ' . $player_critical . ' health <br />';

}elseif($player_accuracy < 80 || $player_accuracy > 80) {

    $outputList .= 'Your attack hits and ' . $arena_char_name . ' suffers ' . $player_atkdam . ' health <br />';

}else{

    $outputList .= 'Your attacked missed!  You really suck ! <br />';

    }
}
?>

<?php
///////////////NPC Attack Turn//////////////////////

$sql = "SELECT * FROM characters where id ='$character' LIMIT $character"; 
$user_query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($user_query);
$row = mysqli_fetch_array($user_query, MYSQLI_ASSOC);

    $enemy_accuracy = mt_rand($characters_accuracy, 100);
    $enemy_atkdam = mt_rand(0,$row['attack']);
    $enemy_critical = mt_rand(0,$row['attack'])*2;

if($enemy_accuracy > 80 && $enemy_critical>=$row['attack']){

    $outputList .= 'The enemies critical attack hit you.  You suffer ' . $enemy_critical . ' health <br />';

}elseif($enemy_accuracy < 80 || $enemy_accuracy > 80) {

    $outputList .= 'The enemy hits and you suffer ' . $enemy_atkdam . ' health <br />';

}else{

    $outputList .= 'Your enemy missed.  They really suck ! <br />';

}
?>

Code: Select all

<div style="visibility: hidden; height: 2em; background-color:#A78D84; font-size: 20px; color: black; vertical-align:middle" align="center" id="lose_bullets">
<?php echo $outputList; ?>
</div>

Now I know I need to add a variable like $battlestart = 0; and then do a maxloop, but not sure where to place this. I've tried for awhile now, doing many trials, but getting nowhere, so I came to you good folks :) Any idears? I think one issue is that when you click the attack button? The attack button is in a JS popup, so I think the server is trying to run the code before I even am able to click attack.

Re: Decided To Stay With PHP Battle, but?

Posted: Wed Nov 12, 2014 12:11 pm
by KyleMassacre
What you can do is after you have declared your local variables you can start a for loop and declare the max turns in the round like

Code: Select all

//here is your local vars
$maxRounds = 100;
for($i = 1; $i <= $maxRounds; $i++) {
    if($i %2 == 0) { //their attack
        //insert their attack stuff here
    else { //your attack
        //enter your attack stuff here
    }
}

Re: Decided To Stay With PHP Battle, but?

Posted: Wed Nov 12, 2014 2:58 pm
by Epiales
KyleMassacre wrote:What you can do is after you have declared your local variables you can start a for loop and declare the max turns in the round like

Code: Select all

//here is your local vars
$maxRounds = 100;
for($i = 1; $i <= $maxRounds; $i++) {
    if($i %2 == 0) { //their attack
        //insert their attack stuff here
    else { //your attack
        //enter your attack stuff here
    }
}
I'll have to tinker with it. I don't know much about looping, but I really don't get the %2 thingy?

So if I"m understanding..... I will declare the maxrounds and then declare the loop... then the IF statement will be all the attack information and updated database health and such, and then in the else statement, I put the players attack info and all their updated health info?

Re: Decided To Stay With PHP Battle, but?

Posted: Wed Nov 12, 2014 11:35 pm
by KyleMassacre
The %2 basically means that if $i is a multiple of 2. Inside the if statement you would put the opponents info in their like damage done etc. In the else statement you would do the same but that would be the dude that is actually clicking the attack button. I do want you to note that this is a one-click attack system and I'm not sure if that is what you wanted.

Re: Decided To Stay With PHP Battle, but?

Posted: Thu Nov 13, 2014 8:59 am
by MikuzA
1 % 2 is modulo, so you get the remainder of the division as a result.
So basicly $i % 2 == 0 will just check if the $i is dividable by 2. (result will be zero if it is, and for example 49 % 2 = 1, due 48 is fine but there is the remainder of 1 ( I bet you know math but just wanted to make sure you know that this is just modulo :)).

**

What you need figure out is how you want it,
Looping it inside the PHP, the battle outcome will be calculated during page refresh. Then you can decide how you want to show the outcome with HTML/JS.

Add some check that if your or enemyhp goes to zero or below, then break the loop. (break;)

Re: Decided To Stay With PHP Battle, but?

Posted: Thu Nov 13, 2014 10:05 am
by Epiales
Okay, since the javascript is checking inside a variable if the player or NPC has zero or below health using php variables, I had to figure a way to insert a php break, so I wrote another function, but now the fight won't run at all:

Code: Select all

function break(){

var break = "<?php break; ?>";

}


function npc_check()
{

var enemyHp = "<?php echo $enemyHp; ?>";

if(enemyHp <= 0){
        document.status2.msgbx2.value = "Congratulations, you win!";
        break();

}else{

    }
}


function player_check()
{

var playerHp = "<?php echo $playerHp; ?>";

if(playerHp <= 0){
        document.status2.msgbx2.value = "You've lost the battle!";
        break();
}else{

    }
}


</script>
As you see it runs though the playercheck and npccheck to see if they win or not, and this works when you just page refresh it. But it won't stop if you keep going, so I needed to break it somewhere.... I guess I didn't do it right lmao I also tried a different name for the function, as break is common in scripting, so it might have been confused, so I tried function breakgame() but won't work either.

Re: Decided To Stay With PHP Battle, but?

Posted: Thu Nov 13, 2014 10:58 am
by Epiales
KyleMassacre wrote:The %2 basically means that if $i is a multiple of 2. Inside the if statement you would put the opponents info in their like damage done etc. In the else statement you would do the same but that would be the dude that is actually clicking the attack button. I do want you to note that this is a one-click attack system and I'm not sure if that is what you wanted.
Yes, it's a one click attack... i've been trying to get it to display each round on different lines but not working lmao... I'm confused about your else statement though...

do I put the below right after declaring the variables... and then you say put attack in one area and then npc attack in the else...it's all one battle, so I'm not sure where the else is supposed to go. I tried putting the below after the variables and then just closing it with a } at the very END of the scenario, but that doesn't work either. It does kinda, but won't stop at all. I've been working on trying to do breaking it in js, as the playerhp and npc hp is written in js.

Code: Select all

$maxRounds = 100;
for($i = 1; $i <= $maxRounds; $i++) {
    if($i %2 == 0) {

Re: Decided To Stay With PHP Battle, but?

Posted: Thu Nov 13, 2014 11:00 am
by MikuzA
There is something fishy here now!

PHP is server-side language, is fully executed from top to bottom during page-load.

JavaScript is client-side, and can do stuff after page has loaded.

If you do this

Code: Select all

function break(){

var break = "<?php break; ?>";

}
PHP code will break when creating this row, due break will be executed during page load. Not when JS Break is called.

Re: Decided To Stay With PHP Battle, but?

Posted: Thu Nov 13, 2014 11:18 am
by MikuzA
Epiales wrote: Yes, it's a one click attack... i've been trying to get it to display each round on different lines but not working lmao... I'm confused about your else statement though...

do I put the below right after declaring the variables... and then you say put attack in one area and then npc attack in the else...it's all one battle, so I'm not sure where the else is supposed to go. I tried putting the below after the variables and then just closing it with a } at the very END of the scenario, but that doesn't work either. It does kinda, but won't stop at all. I've been working on trying to do breaking it in js, as the playerhp and npc hp is written in js.

Code: Select all

$maxRounds = 100;
for($i = 1; $i <= $maxRounds; $i++) {
    if($i %2 == 0) {
Ok,

So you are trying to do the following,
You have an option to attack an npc/player. Once attacked, the battle is initiated and it will not 'stop' until either of them are dead or maxrounds has reached.

Right?

If so,

Do it like this,

Code: Select all

<?PHP
<<Attack code starts>>
$break = 0;
$maxRounds = 100;
for($i = 1; $i <= $maxRounds; $i++) {
  IF(player == DEAD) { $dosomething = ""; $break = 1; }
  ELSEIF($enemy == DEAD ] { $dosomething = ""; $break = 1; }
  ELSE{$dosomething = "";}
  $contentwriter .= "<div><p><anything you need>".$dosomething."</div>";
  if($break == 1) { break; }
}
<<Attack code ends>>
<<Displaying attack log starts>>
echo $contentwriter;
<<end
?>
Does this make sense?
If I would be you, do this whole thing without using PHP at all.. Just write an example of 10 rounds of fighting, check what you can repeat, generate those rows inside the loop. and output it as it is.

Re: Decided To Stay With PHP Battle, but?

Posted: Thu Nov 13, 2014 12:48 pm
by Epiales
MikuzA wrote:

Do it like this,

Code: Select all

<?PHP
<<Attack code starts>>
$break = 0;
$maxRounds = 100;
for($i = 1; $i <= $maxRounds; $i++) {
  IF(player == DEAD) { $dosomething = ""; $break = 1; }
  ELSEIF($enemy == DEAD ] { $dosomething = ""; $break = 1; }
  ELSE{$dosomething = "";}
  $contentwriter .= "<div><p><anything you need>".$dosomething."</div>";
  if($break == 1) { break; }
}
<<Attack code ends>>
<<Displaying attack log starts>>
echo $contentwriter;
<<end
?>
Does this make sense?
If I would be you, do this whole thing without using PHP at all.. Just write an example of 10 rounds of fighting, check what you can repeat, generate those rows inside the loop. and output it as it is.
Don't I still have to use php to call the mysql database? I'll try and rewrite the entire thing using javascript then... We'll see how it goes lol. I will post any updates here.