//Assuming you already have a mysql connection //Assuming you already have your sessions set with one for username and password
if(!$_POST['attack']){ die("Du bliver nødt til at sende angreb afsted før denne side virker"); //Stops the script if the user has not submitted the form on start_attack.php }
//Query user's row $query = mysql_query("SELECT * FROM brugere WHERE username='$username' AND password='$password'"); if(mysql_num_rows($query) != 1){ die("We did not find your account. Please relogin"); //If row is not found, kill script } while($r = mysql_fetch_assoc($query)){ //Define variables for user's unit1 and unit2 for $trop1 = $r['trop1']; $trop2 = $r['trop2']; $trop1b= $r['trop1b']; $trop2b= $r['trop2b']; $sten = $r['sten']; $trae = $r['trae']; $metal = $r['metal']; $korn = $r['korn']; }
mysql_connect("localhost", "traviannews_dk", "***") or die("There was an error connecting to the mysql server."); mysql_select_db("traviannews_dk");
$query = mysql_query("SELECT * FROM brugere WHERE username='$defender'"); if(mysql_num_rows($query) != 0){ die("You have entered an invalid country name, please go back and try again."); //If row is not found, kill script and tell them to retry } while($r = mysql_fetch_assoc($query)){ //Define variables for defender's unit1 and unit2 for $def_trop1 = $r['trop1']; $def_trop2 = $r['trop2']; $def_sten = $r['sten']; $def_trae = $r['trae']; $def_metal = $r['metal']; $def_korn = $r['korn']; }
//Units' powers $trop1_pwr = 20; //Set that to whatever you want, a higher number makes that unit stronger $trop2_pwr = 30; //Same deal $trop1_def_pwr = 40; $trop2_def_pwr = 30;
//As a percent, 0.10 will give 10% of the defenders money to the attacker with a successful attack
//Check if units sent are not too many if($trop1_sent > $trop1){ die("Du har ikke så mange tropper!."); //kill script if too many units were sent }
//Set powers //Now that we have unit power values, we can determine who has the highest overall power $power = $trop1_sent * $trop1_pwr; $def_power = $def_trop1 * $trop1_def_pwr;
//Check who won if($power > $def_power){ $new_trop1 = $trop1*.98; //Kill 2% of attacking unit1 $new_trop2 = $trop2*.98; //Kill 2% of attacking unit2 $new_def_unit1 = $def_unit1*.9; //Kill 10% of defending unit1 $new_def_unit2 = $def_unit2*.9; //Kill 10% of defending unit2 $new_money = $money + $def_money*$money_cqr; //add the conquer percent of money $new_def_money = $def_money - $def_money*$money_cqr; //subtract the conquer percent of money
//Set a message to be outputted $message = "Attack successful! You lost ". $unit1 - $new_unit1. " unit1, ". $unit2 - $new_unit2. " unit2. You killed ". $def_unit1 - $new_def_unit1. " unit1, ". $def_unit2 - $new_def_unit2. " unit2. You conquered ". $new_money - $money. "!"; }
else{ $new_unit1 = $unit1*.9; //Kill 10% of attacking unit1 $new_unit2 = $unit2*.9; //Kill 10% of attacking unit2 $new_def_unit1 = $def_unit1*.98; //Kill 2% of defending unit1 $new_def_unit2 = $def_unit2*.98; //Kill 2% of defending unit2 $new_money = $money; $new_def_money = $def_money;
//Set a message to be outputted $message = "Attack unsuccessful. You lost ". $unit1 - $new_unit1. " unit1, ". $unit2 - $new_unit2. " unit2. You killed ". $def_unit1 - $new_def_unit1. " unit1, ". $def_unit2 - $new_def_unit2. " unit2.";
}
echo "$message"; //Output the message you've got
mysql_connect("localhost", "traviannews_dk", "***") or die("There was an error connecting to the mysql server."); mysql_select_db("traviannews_dk"); //Now let's update the database mysql_query("UPDATE accounts SET unit1 = $new_unit1, unit2 = $new_unit2, money = $new_money WHERE username='$username' AND password='$password'"); mysql_query("UPDATE accounts SET unit1 = $new_def_unit1, unit2 = $new_def_unit2, money = $new_def_money WHERE username = '$defender'");
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.