Not sure which bits you need but here is the whole thing. The conf file has all the database bits:
Code: Select all
<?php
include("conf.php");
$random_chance = '25';
// seed with microseconds to make the random function work
function make_seed() {
list($usec, $sec) = explode(' ', microtime());
return (float) $sec + ((float) $usec * 100000);
}
srand(make_seed());
//get u-boat details
$query = "SELECT unumber, crew_skill, fatigue, accuracy, mechanical, damage, experience, torpedos, gridletter, gridnumber, attack_scorecard, defense_scorecard FROM myboat WHERE id = '1' ";
$boatresults = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
// if a result is returned
if (mysql_num_rows($boatresults) > 0)
{
// turn it into an object
$boat = mysql_fetch_object($boatresults);
}
$convoy_array = explode('|', "$boat->attack_scorecard");
$escort_array = explode('|', "$boat->defense_scorecard");
if ($attack)
{
$num_torpedos_fired = ($target1 + $target2 + $target3 + $target4 + $target5 + $target6 + $target7 + $target8 + $target9 + $target10 + $target11 + $target12 + $target13 + $target14 + $target15);
if ($num_torpedos_fired > $boat->torpedos)
{
echo "Kapitän! We do not have enough torpedos left to make that many attacks! Click back and try again!";
}
else
{
$target_num_array = array(1 => $target1, $target2, $target3, $target4, $target5, $target6, $target7, $target8, $target9, $target10, $target11, $target12, $target13, $target14, $target15);
$target_name_array = array(1 => $targetname1, $targetname2, $targetname3, $targetname4, $targetname5, $targetname6, $targetname7, $targetname8, $targetname9, $targetname10, $targetname11, $targetname12, $targetname13, $targetname14, $targetname15);
$target_tonnage_array = array(1 => $tonnage1, $tonnage2, $tonnage3, $tonnage4, $tonnage5, $tonnage6, $tonnage7, $tonnage8, $tonnage9, $tonnage10, $tonnage11, $tonnage12, $tonnage13, $tonnage14, $tonnage15);
$target_type_array = array(1 => $type1, $type2, $type3, $type4, $type5, $type6, $type7, $type8, $type9, $type10, $type11, $type12, $type13, $type14, $type15);
$target_nation_array = array(1 => $nation1, $nation2, $nation3, $nation4, $nation5, $nation6, $nation7, $nation8, $nation9, $nation10, $nation11, $nation12, $nation13, $nation14, $nation15);
$target_num = '1';
print_r($target_tonnage_array[$target_num]);
echo "<p><p>";
foreach ($target_name_array As $target)// for each target or ship
{
$num_of_hits = 0; //resets the hit count for the next ship
//for each torpedo fired at that ship
$torpedos_fired = $target_num_array[$target_num];
while ($torpedos_fired > '0')
{
$chance_percentage = ($boat->experience + $boat->accuracy + $boat->crew_skill + $boat->mechanical + $boat->fatigue + $boat->damage + $random_chance)/10;
$random_hit_factor = rand(0, 100);
if ($random_hit_factor <= $chance_percentage)
{
$num_of_hits = $num_of_hits + 1; //add one to the number of hits if successful
}
$torpedos_fired = $torpedos_fired - '1';
}
//now work out the amount of damage done to the ship//
if ($num_of_hits > '0')
{
$tonnage = $target_tonnage_array[$target_num];
//print_r($tonnage);
switch ($target_tonnage_array[$target_num]) {
case $target_tonnage_array[$target_num]>10000:
if ($random_hit_factor > 70) //30% chance of sinking the ship
{
$status = 'sunk heavy';
}
break;
case $target_tonnage_array[$target_num]<10000:
if ($random_hit_factor > 5) //50% chance of sinking the ship
{
$status = 'sunk light';
}
break;
default:
$status = 'damaged';
}
}
else
{
$status = 'undamaged';
}
$misses = $target_num_array[$target_num] - $num_of_hits;
echo "$target_num You fired <b>$target_num_array[$target_num]</b> torpedos at <i>$target</i> $tonnage. $misses torpedos missed. You got $num_of_hits hit(s). Ship is $status !<br>";
// $new_ship_data = array($target_name_array[$target_num], $target_nation_array[$target_num], $target_tonnage_array[$target_num], $target_type_array[$target_num], $status);
// $new_convoy_data[] = implode(',', $new_ship_data);
$target_num++;
}
$updated_score = implode('|', $new_convoy_data);
$query = "UPDATE myboat SET fatigue = '$fatigue', vunerability = '$vunerability', accuracy = '$accuracy', mechanical = '$mechanical', damage = '$damage', speed = '$speed', experience = '$experience', morale = '$morale', hours = '$hours_left' WHERE id = '1'";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
}
}
else
{
echo "<form method="post" action="$PHP_SELF">";
echo "<table width=500><tr><td colspan=6>Targets</td><tr>"
."<tr><td>Ship</td><td>Nation</td><td>Tonnage</td><td>Type</td><td>Condition</td></tr>";
foreach ($convoy_array As $ship)
{
echo "<tr>";
$ship_details = explode(',', $ship);
$target_id++;
$target_name = $ship_details[0];
$target_nation = $ship_details[1];
$target_tonnage = $ship_details[2];
$target_type = $ship_details[3];
$target_condition = $ship_details[4];
foreach ($ship_details As $detail)
{
echo "<td>$detail</td>";
}
if ($target_condition != "sunk")
{
echo "<td><input name="target$target_id" type="text" value="0" size="2" maxlength="2"></td></tr>";
}
else
{
echo "<td></td></tr>";
}
echo "<input type="hidden" name="targetname$target_id" value="$target_name">";
echo "<input type="hidden" name="nation$target_id" value="$target_nation">";
echo "<input type="hidden" name="tonnage$target_id" value="$target_tonnage">";
echo "<input type="hidden" name="type$target_id" value="$target_type">";
}
echo "<tr><td colspan=6><center><input type="submit" name="attack" value="Fire Torpedos!"></center></td><tr></table></form>";
}
?>