MasterACE14 Posted October 14, 2007 Share Posted October 14, 2007 Morning Everyone, I have a script that gives a player "money"(in a text game) if they have a successful attack. But occasionally, the way the math works out will give the player their money, but the person they attacked will get a negative amount of money, due to the fact that the amount of money the math works out for the attacking player to receive is greater then the amount of money the his/her opponent has. So basically what I want to add to the following script is, if the attacking player is going to take more money then what his/her opponent has, the attacking player will only receive the remainder of their opponents money, and their opponents money will go as low as zero. instead of going down to a negative number. here is the script(relevent parts only, I can show more if needed): <?php // workout how much $ you get $taken_money = (($player_strikeaction * $used_ammo) / 50); // run the MySQL queries to update both users accounts $rss = mysql_connect("localhost", "ace_ACE", "*****"); $rss = mysql_select_db("ace_cf", $rss); // run the queries for your account first - grab your current money - ATTACKING PLAYER $sql = "SELECT `money` FROM `cf_users` WHERE `id`='" . $player_accountid . "' LIMIT 1"; $current_money = mysql_query($sql) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error()); // run the queries for your enemy - grab the current money - DEFENDING PLAYER $sqlll = "SELECT `money` FROM `cf_users` WHERE `id`='" . $other_player_accountid . "' LIMIT 1"; $other_player_current_money = mysql_query($sqlll) or die('Query:<br />' . $sqlll . '<br /><br />Error:<br />' . mysql_error()); /* I'm guessing that I need to add something here to stop the opponent from getting a negative amount of money, and the attacking player not getting more then the defending player has */ // current money + taken money = your new money $new_money = $current_money + $taken_money; I'm sure this is really simple, I'm just really bad at maths lol :-X Regards ACE Quote Link to comment https://forums.phpfreaks.com/topic/73148-solved-give-the-remainder-no-negatives/ Share on other sites More sharing options...
marcus Posted October 14, 2007 Share Posted October 14, 2007 Just do some if statements. if($money < 0){ //error it's negative } if(abs($attack_give) > abs($defend_take)){ //attack will get too much from the other guy } Quote Link to comment https://forums.phpfreaks.com/topic/73148-solved-give-the-remainder-no-negatives/#findComment-368897 Share on other sites More sharing options...
corbin Posted October 14, 2007 Share Posted October 14, 2007 just do something like: if($taken_money > $other_player_current_money) { $taken_money = $other_player_current_money; } Quote Link to comment https://forums.phpfreaks.com/topic/73148-solved-give-the-remainder-no-negatives/#findComment-368898 Share on other sites More sharing options...
kratsg Posted October 14, 2007 Share Posted October 14, 2007 Ok, you can make it so it always takes a percentage of the player's money, but never all of it. Or you can make it to set the money = 0 if it's less than 0. Which one do you want to do? Quote Link to comment https://forums.phpfreaks.com/topic/73148-solved-give-the-remainder-no-negatives/#findComment-368899 Share on other sites More sharing options...
AndyB Posted October 14, 2007 Share Posted October 14, 2007 * I'm guessing that I need to add something here to stop the opponent from getting a negative amount of money, and the attacking player not getting more then the defending player has */ // current money + taken money = your new money $new_money = $current_money + $taken_money; There's no guess about it. You do. It's not bad at math that's stopping you, it's not applying some very simple logic ... if taken_money<what they have, etc. Try it yourself. It's trivial. Quote Link to comment https://forums.phpfreaks.com/topic/73148-solved-give-the-remainder-no-negatives/#findComment-368900 Share on other sites More sharing options...
MasterACE14 Posted October 14, 2007 Author Share Posted October 14, 2007 I've made the neccesary changes, but I am getting some strange results. I'll post the full code now, and show one of my attacks. code: <?php // players stats page when you click their link in attack page // error_reporting(E_ALL); /* includes */ include_once '/home/ace/public_html/conflictingforces/functions.php'; include_once '/home/ace/public_html/conflictingforces/weapons.php'; include_once '/home/ace/public_html/conflictingforces/armors.php'; include_once '/home/ace/public_html/conflictingforces/vehicles.php'; player_session(); /**********************/ // Player Select Variables // /**********************/ // User Information - your information $player_accountid = player_table("id"); $player_username = player_table("username"); $player_email = player_table("email"); $player_race = player_race(); // Military Effectiveness - yours $player_strikeaction = player_table("strikeaction"); $player_defenceaction = player_table("defenceaction"); $player_covertaction = player_table("covertaction"); // General Statistics - yours $player_level = player_table("level"); $player_currentexp = player_table("currentexp"); $player_neededexp = player_table("neededexp"); $player_skilllevel = player_table("skilllevel"); $player_skillpoints = player_table("skillpoints"); $player_strength = player_table("strength"); $player_agility = player_table("agility"); $player_intelligence = player_table("intelligence"); // Talent Statistics - yours $player_talentid = player_table("talentid"); $player_talentname = player_table("talentname"); $player_talentlevel = player_table("talentlevel"); $player_talentpoints = player_table("talentpoints"); // Alliance Information - yours $player_allianceid = player_table("allianceid"); $player_alliancename = player_table("alliancename"); $player_alliancepostion = player_table("allianceposition"); // Inventory - yours $player_weaponid = player_table("weaponid"); $player_armorid = player_table("armorid"); $player_vehicleid = player_table("vehicleid"); // Equipped Items // Names - yours $equippedweaponname = select_array($weapons,$player_weaponid,"weapon"); $equippedarmorname = select_array($armors,$player_armorid,"armor"); $equippedvehiclename = select_array($vehicles,$player_vehicleid,"vehicle"); // ID - yours $equippedweaponid = select_array($weapons,$player_weaponid,"id"); $equippedarmorid = select_array($armors,$player_armorid,"id"); $equippedvehicleid = select_array($vehicles,$player_vehicleid,"id"); /*****************************/ // Other Player Select Variables // /*****************************/ $other_players_id = $_POST["otherplayersid"]; // User Information - their information $other_player_accountid = player_table_other("id",$other_players_id); $other_player_username = player_table_other("username",$other_players_id); $other_player_race = player_race_other($other_players_id); // Military Effectiveness - theirs $other_player_strikeaction = player_table_other("strikeaction",$other_players_id); $other_player_defenceaction = player_table_other("defenceaction",$other_players_id); $other_player_covertaction = player_table_other("covertaction",$other_players_id); $other_player_totalaction = (($other_player_strikeaction + $other_player_defenceaction) + $other_player_covertaction); // General Statistics - theirs $other_player_money = player_table_other("money",$other_players_id); $other_player_level = player_table_other("level",$other_players_id); $other_player_currentexp = player_table_other("currentexp",$other_players_id); $other_player_neededexp = player_table_other("neededexp",$other_players_id); $other_player_skilllevel = player_table_other("skilllevel",$other_players_id); $other_player_skillpoints = player_table_other("skillpoints",$other_players_id); $other_player_strength = player_table_other("strength",$other_players_id); $other_player_agility = player_table_other("agility",$other_players_id); $other_player_intelligence = player_table_other("intelligence",$other_players_id); // Talent Statistics - theirs $other_player_talentid = player_table_other("talentid",$other_players_id); $other_player_talentname = player_table_other("talentname",$other_players_id); $other_player_talentlevel = player_table_other("talentlevel",$other_players_id); $other_player_talentpoints = player_table_other("talentpoints",$other_players_id); // Alliance Information - theirs $other_player_allianceid = player_table_other("allianceid",$other_players_id); $other_player_alliancename = player_table_other("alliancename",$other_players_id); $other_player_allianceposition = player_table_other("allianceposition",$other_players_id); // Inventory - theirs $other_player_weaponid = player_table_other("weaponid",$other_players_id); $other_player_armorid = player_table_other("armorid",$other_players_id); $other_player_vehicleid = player_table_other("vehicleid",$other_players_id); // Equipped Items // Names - theirs $other_equippedweaponname = select_array($weapons,$other_player_weaponid,"weapon"); $other_equippedarmorname = select_array($armors,$other_player_armorid,"armor"); $other_equippedvehiclename = select_array($vehicles,$other_player_vehicleid,"vehicle"); // ID - theirs $other_equippedweaponid = select_array($weapons,$other_player_weaponid,"id"); $other_equippedarmorid = select_array($armors,$other_player_armorid,"id"); $other_equippedvehicleid = select_array($vehicles,$other_player_vehicleid,"id"); // --- $_POST's --- // $used_ammo = $_POST["ammo"]; /*****************************Do the Attack**********************************/ if( is_numeric( $used_ammo ) ) { // run the MySQL querie to see if you have enough Ammo and take the Ammo away $rs = mysql_connect("localhost", "ace_ACE", "*****"); $rs = mysql_select_db("ace_cf", $rs); // run the queries for your account first - grab your current money $sqlq = "SELECT `ammo` FROM `cf_users` WHERE `id`='" . $player_accountid . "' LIMIT 1"; $ammo_check = mysql_query($sqlq) or die('Query:<br />' . $sqlq . '<br /><br />Error:<br />' . mysql_error()); if( $used_ammo >= $ammo_check ) { die("You do not have enough Ammo!"); } if( $used_ammo != null ) { // take your strike away from their defence $attack_result = $player_strikeaction - $other_player_defenceaction; // workout how much $ you get $taken_money = (($player_strikeaction * $used_ammo) / 50); // workout how much damage you dealt $damage_you_did = ($player_strikeaction * $used_ammo); // workout how much damage your opponent dealt $damage_opponent_did = $other_player_defenceaction; // workout what the result was if($damage_you_did > $damage_opponent_did) { $result = "Success"; } elseif($damage_you_did == $damage_opponent_did) { $result = "Stalemate"; } elseif($damage_you_did < $damage_opponent_did) { $result = "Failure"; } // run the MySQL queries to update both users accounts $rss = mysql_connect("localhost", "ace_ACE", "*****"); $rss = mysql_select_db("ace_cf", $rss); // ------------------------------- Run Your Queries -----------------------------------/ // run the queries for your account first - grab your current money $sql = "SELECT `money` FROM `cf_users` WHERE `id`='" . $player_accountid . "' LIMIT 1"; $current_money = mysql_query($sql) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error()); // run the queries for your account first - grab your current ammo $sql_ammo = "SELECT `ammo` FROM `cf_users` WHERE `id`='" . $player_accountid . "' LIMIT 1"; $current_ammo = mysql_query($sql_ammo) or die('Query:<br />' . $sql_ammo . '<br /><br />Error:<br />' . mysql_error()); // current ammo - ammo used = your new amount of ammo $new_amount_ammo = $current_ammo - $used_ammo; // run the queries for your enemy - grab the current money $sqlll = "SELECT `money` FROM `cf_users` WHERE `id`='" . $other_player_accountid . "' LIMIT 1"; $other_player_current_money = mysql_query($sqlll) or die('Query:<br />' . $sqlll . '<br /><br />Error:<br />' . mysql_error()); if($taken_money > $other_player_current_money) { $taken_money = $other_player_current_money; } // current money + taken money = your new money $new_money = $current_money + $taken_money; // run the queries for your account first - insert your new amount of money $sqll = "UPDATE `cf_users` SET `money` = '$new_money' WHERE `id`='" . $player_accountid . "' LIMIT 1"; $rsss = mysql_query($sqll) or die('Query:<br />' . $sqll . '<br /><br />Error:<br />' . mysql_error()); // run the queries for your account first - insert your new amount of money $sql_ammoq = "UPDATE `cf_users` SET `ammo` = ammo-$used_ammo WHERE `id`='" . $player_accountid . "' LIMIT 1"; $new_ammo = mysql_query($sql_ammoq) or die('Query:<br />' . $sql_ammoq . '<br /><br />Error:<br />' . mysql_error()); //############# Take away your health and/or power ########// //############### Give you your earned experience #######// // --------------------------------- Run Enemy Queries ---------------------------------/ if($other_player_current_money < $taken_money) { $other_player_new_money = 0; } else { // current money - taken money = your new money $other_player_new_money = $other_player_current_money - $taken_money; } // run the queries for your account first - insert your new amount of money $sqllll = "UPDATE `cf_users` SET `money` = '$other_player_new_money' WHERE `id`='" . $other_player_accountid . "' LIMIT 1"; $rssss = mysql_query($sqllll) or die('Query:<br />' . $sqllll . '<br /><br />Error:<br />' . mysql_error()); //######### Take away your enemies health and/or power ########// //########### Give your enemy their earned experience #######// /***************************************************************************/ } } else { die("You do not have that much Ammo!"); } ?> <div id = "attack"> <table class = "fix"> <tr> <td> <center><b><?php echo $result; ?></b></center> </td> </tr> <tr> <td><p> You fire your weapon at <b><?php echo $other_player_username; ?></b> dealing <b><?php echo number_format($damage_you_did); ?></b> damage. <b><?php echo $other_player_username; ?></b> starts returning fire and deals <b><?php echo number_format($damage_opponent_did); ?></b> damage to you! <br><br> <?php if( $result == 'Success' ) { ?> You took <b>$<?php echo number_format($taken_money); ?></b> from <b><?php echo $other_player_username; ?></b> and headed back to base. <?php } elseif( $result == 'Failure' ) { ?> You suffered injuries from <b><?php echo $other_player_username; ?>'s</b> defence, and headed back to base. <?php } elseif( $result == 'Stalemate' ) { ?> You and <b><?php echo $other_player_username; ?></b> both suffered injuries and retreated. <?php } ?> </p></td> </tr> </table> </div> attack: You fire your weapon at jazzydog14 dealing 26,000 damage. jazzydog14 starts returning fire and deals 0 damage to you! You took $79 from jazzydog14 and headed back to base. and the result was "Success" ________________________________________________________________________________________________ I have done another attack, this time recording the amount of Ammo and Money I have before and after attack, and how much Ammo I used in the attack. And also how much money my opponent had before and after the attack: Before - I had: Ammo: 62 Money: $156 I used 5 ammo After - I had: Ammo: 57 Money: $154 Before my Opponent had: Money: $2321 After my Opponent had: Money: $0 the result of the attack: You fire your weapon at Rillepite dealing 32,500 damage. Rillepite starts returning fire and deals 0 damage to you! You took $78 from Rillepite and headed back to base. Result was "Success" Quote Link to comment https://forums.phpfreaks.com/topic/73148-solved-give-the-remainder-no-negatives/#findComment-368919 Share on other sites More sharing options...
MasterACE14 Posted October 14, 2007 Author Share Posted October 14, 2007 bump Quote Link to comment https://forums.phpfreaks.com/topic/73148-solved-give-the-remainder-no-negatives/#findComment-368990 Share on other sites More sharing options...
darkfreaks Posted October 14, 2007 Share Posted October 14, 2007 whats the problem Ace ??? <?php if (abs($taken_money) > abs($other_player_current_money)){ $taken_money = $other_player_current_money; ?> Quote Link to comment https://forums.phpfreaks.com/topic/73148-solved-give-the-remainder-no-negatives/#findComment-369021 Share on other sites More sharing options...
darkfreaks Posted October 14, 2007 Share Posted October 14, 2007 also you need something like <?php if ($opponents_attack < 0||$opponents_attack=="0"){ echo " You have dealt no damage to your opponent";} elseif ($opponents_attack > $defense_attack){ $openents_health= $current_health-50;} else { $your_health= $health-50; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/73148-solved-give-the-remainder-no-negatives/#findComment-369040 Share on other sites More sharing options...
darkfreaks Posted October 14, 2007 Share Posted October 14, 2007 and you wonder why it says "Success" even when you get negative numbers or a zero damage. if you had a statement that said to ignore it and echo whatever it would not do anything. unless you got creative like i did with the health and upon attack failure or defense failure had it subtract damage. Quote Link to comment https://forums.phpfreaks.com/topic/73148-solved-give-the-remainder-no-negatives/#findComment-369047 Share on other sites More sharing options...
MasterACE14 Posted October 14, 2007 Author Share Posted October 14, 2007 ok, I'm trying what you first posted, and as for the health, I still have to implement that EDIT: this is strange, I attack someone, its a successful attack, it says I got $80 from them, and it updates my account in the database but my money went from $160 down to $158. I don't get why? Quote Link to comment https://forums.phpfreaks.com/topic/73148-solved-give-the-remainder-no-negatives/#findComment-369061 Share on other sites More sharing options...
darkfreaks Posted October 14, 2007 Share Posted October 14, 2007 well the if statement i had on my second post should make it so if damage equals negative and zero it wont do anything but echo " you did no damage" and then i did all the health stuff Quote Link to comment https://forums.phpfreaks.com/topic/73148-solved-give-the-remainder-no-negatives/#findComment-369062 Share on other sites More sharing options...
MasterACE14 Posted October 14, 2007 Author Share Posted October 14, 2007 Ok, I'll integrate that now and see what happens. Quote Link to comment https://forums.phpfreaks.com/topic/73148-solved-give-the-remainder-no-negatives/#findComment-369065 Share on other sites More sharing options...
darkfreaks Posted October 14, 2007 Share Posted October 14, 2007 and as for the abs you dont need the absolute value Quote Link to comment https://forums.phpfreaks.com/topic/73148-solved-give-the-remainder-no-negatives/#findComment-369072 Share on other sites More sharing options...
MasterACE14 Posted October 14, 2007 Author Share Posted October 14, 2007 ok, I added this part: <?php if ($opponents_attack < 0||$opponents_attack=="0"){ echo " You have dealt no damage to your opponent";} and I also added what you said in your first post. I have put the health code in, but have commented it out for now, as I will have to do a couple more queries for that. But I just want to get this part working first. Now, I just did another attack. The results are still strange. But it's reversed itself now. And I also made a new variable. I had this variable at the top of the page: <?php // workout how much $ you get $taken_money = (($player_strikeaction * $used_ammo) / 50); and I added a replicate of it straight after: <?php $taken_money_from_opponent = (($player_strikeaction * $used_ammo) / 50); And I have used the second one for parts later on in the script, as the value of the first one, I have found out, is altered depending on a few factors. So I thought that may of been the problem all a long. But it doesn't seem to be the problem. I recorded some information from the database on some paper, to compare after my next attack. here's what I got: Me - before Money - $158 sissorhands - before Money - $2321 Me - after Money - $160 (notice this time my money has gone up 2 instead of down 2) sissorhands - after Money - $0 (still zero) and here is the result of the attack directly from my site(Note: my strike action is 6500 and I used 5 ammo, so 5 times 6500 is the amount of damage I dealt): You fire your weapon at sissorhands dealing 32,500 damage. sissorhands starts returning fire and deals 6,500 damage to you! You took $81 from sissorhands and headed back to base. Quote Link to comment https://forums.phpfreaks.com/topic/73148-solved-give-the-remainder-no-negatives/#findComment-369088 Share on other sites More sharing options...
darkfreaks Posted October 14, 2007 Share Posted October 14, 2007 everything seems to be working normally except the zero part thats because if yo utake there money your telling it if i rob you of $78 and you have $5000 to echo $0 which is wrong. <?php if ($taken_money < $current_money){ echo $current_money;} //// echos how much you have left ?> Quote Link to comment https://forums.phpfreaks.com/topic/73148-solved-give-the-remainder-no-negatives/#findComment-369092 Share on other sites More sharing options...
MasterACE14 Posted October 14, 2007 Author Share Posted October 14, 2007 thats bizarre, I added this: <?php echo "$taken_money = " . $taken_money . "\n"; echo "$other_player_current_money = " . $other_player_current_money . "\n"; ?> and it said this: Resource id #78 = Resource id #78 Resource id #78 = Resource id #78 EDIT: I changed the above removing the $ sign from the start of the echo, and it says this: taken_money = Resource id #81 other_player_current_money = Resource id #81 Quote Link to comment https://forums.phpfreaks.com/topic/73148-solved-give-the-remainder-no-negatives/#findComment-369116 Share on other sites More sharing options...
MasterACE14 Posted October 14, 2007 Author Share Posted October 14, 2007 bump Quote Link to comment https://forums.phpfreaks.com/topic/73148-solved-give-the-remainder-no-negatives/#findComment-369159 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.