I made a fight with the bot:

1) We beat:

<?php session_start(); $host = ''; $username = ''; $password = ''; $db_name = ''; $tbl_name = ''; mysql_connect($host, $username, $password) or die("can't connect"); mysql_select_db($db_name) or die(mysql_error()); mysql_query("SELECT * FROM $tbl_name"); mysql_query("UPDATE `enemy` SET `username`='".$_SESSION['username']."' ")or die(mysql_error()) ; $sql = mysql_query("INSERT INTO `battle` (img,name,level,life,atk,def,type,username) SELECT img,name,level,life,atk,def,type,username FROM `enemy` ORDER BY RAND() LIMIT 1")or die(mysql_error()); ?> <table> <tr> <TD><?php $result = mysql_query("SELECT * FROM startpoke WHERE id_poke = '".$_SESSION['id']."'")or die("can't connect") ; while( $row = mysql_fetch_array( $result ) ){ echo "<br>{$row['name_poke']}" ; echo " - {$row['level']} lvl"; echo "<br>{$row['img']}"; ?> <div style='width:250px;border:0px solid black;margin-center: 0%;'> <div align="left" style='center:0px;width:100%;background-color:green;'><?php echo "{$row['all_life']}"; echo "/{$row['life']}"; ?></div> <div style='width:250px;border:0px solid black;margin-center: 0%;'> <div align="left" style='center:0px;width:100%;background-color:blue;'><?php echo "{$row['exp']}"; ?></div> </TD> <TD><a href="javascript: onClick=win1=window.open('def.php','width=650,height=400,scrollbars=yes');return true;"><?php echo "{$row['atk']}"; } ?></a></TD> <TD><?php $res = mysql_query("SELECT * FROM battle WHERE username = '".$_SESSION['username']."' LIMIT 1")or die(mysql_error()); while( $row = mysql_fetch_array( $res ) ){ echo "<br>{$row['name']}" ; echo " - {$row['level']} lvl"; echo "<br> {$row['img']}";?> <div style='width:250px;border:0px solid black'> <div align="left" style='center:0px;width:100%;background-color:green;'><?php echo "{$row['life']}"; } ?></div></TD> </tr> </table> </center> </body> </html> 

2) They beat us:

 <?php session_start(); $host = $username = $password = $db_name = mysql_connect($host, $username, $password) or die(mysql_error()) ; mysql_select_db($db_name) or die(mysql_error()); $res = mysql_query("SELECT * FROM battle WHERE username='".$_SESSION['username']."'"); $pok = mysql_fetch_array($res); $sql = mysql_query("SELECT * FROM startpoke WHERE id_poke='".$_SESSION['id']."'"); $row = mysql_fetch_array($sql); if($pok['life'] < 0 ){ mysql_query("DELETE FROM `pokelight620`.`battle` WHERE `battle`.`username` = '".$_SESSION['username']."'")or die(mysql_error()) ; echo "Победа! <a href='loc=1.php'>Уйти</a>"; } else{ if($row['life'] > 0 ){ mysql_query("UPDATE `startpoke` SET `life`=`life`-3 WHERE id_poke='".$_SESSION['id']."'")or die(mysql_error()) ; mysql_query("UPDATE `battle` SET `life`=`life`-3 WHERE username='".$_SESSION['username']."'")or die(mysql_error()) ; echo "<script>location.href='fight.php';</script>"; } else{ mysql_query("UPDATE `startpoke` SET `life`=0 WHERE id_poke='".$_SESSION['id']."'")or die(mysql_error()) ; mysql_query("DELETE FROM `pokelight620`.`battle` WHERE `battle`.`username` = '".$_SESSION['username']."'")or die(mysql_error()) ; echo "Вы проиграли! <a href='loc=1.php'>Уйти</a>"; } } ?> 

Sorry for the big code, just for something else! So, when the fight.php page (this is when we are beaten) is updated, another bot is entered and constantly has to beat different bots, but only one is needed, and so I decided to check it all with the user's login, for example: the user started the battle and attacked the bot, in the table where the bot itself weighs there is a username column where the user's login is entered, which means that you need to pull out the bot with the user's login, but since the fight.php page is constantly updated, bots with the user's login are constantly added, and you need to pull only one I decided to do If the record with the user's login name already means not enters a new robot, the question of how to do it? if possible with the code, thanks in advance!

  • one
    I put a tooth, no one will answer. - Ozim
  • one
    knes, thank you very much =) Ozim, someone will go to the dentist =) - k0mar
  • one
    @knes, but if it's not a secret, why? So that the idiotic questions, the answers to which are in any book in volume with the training manual, multiply further?) - Sh4dow
  • 2
    Not a secret. 9x% of all questions here are in one or another training manual, chewed and disassembled to trifles. The remaining 10-x percent is divided into 9y% of the forums that were parsed, such as the stack-flow and 10-y%, which really need attention. But the task of this community is to collect all the answers in one pile. That it was necessary to view ONE source and get an answer. And regardless of the level of training and the ability to speak with Google in the same language. It remains a bit: wait until when a normal search will work here, and not the similarity that does not work here. %) - knes
  • 2
    @knes it will pass, the next time he dumps all his (your) ton of code, which you will need to double-check, and then start writing another game, and will know that you can not even try to start learning elementary things, I can say with confidence - you do not help him, you ruin him as a programmer. - Ozim

1 answer 1

For example, there is an option like this:

 <?php session_start(); mysql_connect($host, $username, $password) or die(mysql_error()) ; mysql_select_db($db_name) or die(mysql_error()); $res = mysql_query("SELECT * FROM battle WHERE username='".$_SESSION['username']."'"); $pok = mysql_fetch_array($res); $sql = mysql_query("SELECT * FROM startpoke WHERE id_poke='".$_SESSION['id']."'"); $row = mysql_fetch_array($sql); while($pok['life'] > 0){ if($row['life'] > 0 ){ mysql_query("UPDATE `startpoke` SET `life`=`life`-3 WHERE id_poke='".$_SESSION['id']."'")or die(mysql_error()) ; mysql_query("UPDATE `battle` SET `life`=`life`-3 WHERE username='".$_SESSION['username']."'")or die(mysql_error()) ; echo "<script>location.href='fight.php';</script>"; }else{ mysql_query("UPDATE `startpoke` SET `life`=0 WHERE id_poke='".$_SESSION['id']."'")or die(mysql_error()) ; mysql_query("DELETE FROM `pokelight620`.`battle` WHERE `battle`.`username` = '".$_SESSION['username']."'")or die(mysql_error()) ; echo "Вы проиграли! <a href='loc=1.php'>Уйти</a>"; exit(); } flush();//Это чтобы показывался каждый ход, даже если их будет очень много. } mysql_query("DELETE FROM `pokelight620`.`battle` WHERE `battle`.`username` = '".$_SESSION['username']."'")or die(mysql_error()) ; echo "Победа! <a href='loc=1.php'>Уйти</a>"; ?> 
  1. We do not need to overload the page every time. (Remember the fight that I once wrote to you). This will reduce the load on the server at times. The remaining errors did not rule: I am too lazy to create a database and check. But the line of activity is. This is how you can do it.

  2. If you do with a reboot, you can get two session variables $ _SESSION ['pok'] and $ _SESSION ['row'], in which you enter the values ​​pulled out from the base for the ID of the fighting pokemon. Then at each boot we will pull out the same monsters from the database. This is NOT recommended.

  3. What is recommended? We pull out of the database ONE time of the pokemon and the enemy. Cycle through the battle to the death of one of them (no need to update the database for each stroke!). Spit the result of the battle. We bring in a DB of change.

  4. How to make the battle was long? (Like, delay between hits) We do as in paragraph 3. But with the help of javascript we do not show everything at once, but in parts, using the set_interval or set_timeout function.

PS people, in general, an interesting question, in vain you are so. Another thing is, Joke again so carried away by the development, that he insanely wanted minuses. As a result, the question asked is crooked. ^^

PS @Ozim :
|OO| @| .. |@ |UUUUUUUU| |ПППП ППП|

  • thanks a lot, you are the coolest as always =) - k0mar
  • heh =) I have a fight going on for 5 minutes and knocking somewhere 5k xn = ( - k0mar