/**
  * Returns array of objects where each object is a type of ship. See ships class
  * for details. In addition, the objects have the field:
  * 
  * - value fraction (total ship cost / total cost for all ships)
  * 
  * If $truncate == true, then ships objects of amount = 0 aren't returned.
  * 
  * @return unknown_type
  */
 public function get_all_ships($truncate)
 {
     // instantiate fleet objects
     $f0 = new member_fleet(0, $this->user_id);
     $f1 = new member_fleet(1, $this->user_id);
     $f2 = new member_fleet(2, $this->user_id);
     $f3 = new member_fleet(3, $this->user_id);
     // get ships from each fleet
     $s0 = $f0->get_ships_in_fleet(false, false);
     $s1 = $f1->get_ships_in_fleet(false, false);
     $s2 = $f2->get_ships_in_fleet(false, false);
     $s3 = $f3->get_ships_in_fleet(false, false);
     // cycle through the entire array and add up
     $ships_new = array();
     foreach ($s0 as $i => $ship) {
         // 1 - copy ship
         $ship_new = $ship;
         // 2 - add total amount from all fleets
         $ship->set_amount($s0[$i]->get_amount() + $s1[$i]->get_amount() + $s2[$i]->get_amount() + $s3[$i]->get_amount());
         // 3 - add to array
         $ships_new[] = $ship;
     }
     // compute total value
     $total_cost = 0;
     foreach ($ships_new as $ship) {
         $total_cost += $ship->get_amount() * $ship->get_cost();
     }
     // cycle through ships array and set fractional value
     foreach ($ships_new as $ship) {
         if ($total_cost == 0) {
             $ship->set_cost_fraction(0);
         } else {
             $ship->set_cost_fraction(round($ship->get_amount() * $ship->get_cost() / $total_cost, 2));
         }
     }
     // if $truncate = true; drop all ships with $amount = 0 from array
     if ($truncate) {
         foreach ($ships_new as $i => $ship) {
             if ($ship->get_amount() == 0) {
                 unset($ships_new[$i]);
             }
         }
         $ships_new = array_values($ships_new);
     }
     // return
     return $ships_new;
 }
示例#2
0
     }
 }
 echo "</table>";
 echo "</div>";
 for ($j = 0; $j < 4; $j++) {
     if ($j == 0) {
         echo "<div class=\"content_subheader\">BASE FLEET <strong>&laquo;</strong></div>";
     } elseif ($j == 1) {
         echo "<div class=\"content_subheader\">FLEET 1 <strong>&laquo;</strong></div>";
     } elseif ($j == 2) {
         echo "<div class=\"content_subheader\">FLEET 2 <strong>&laquo;</strong></div>";
     } elseif ($j == 3) {
         echo "<div class=\"content_subheader\">FLEET 3 <strong>&laquo;</strong></div>";
     }
     // mf = member_fleet
     $mf = new member_fleet($j, $_GET['user_id']);
     $sf = $mf->get_ships_in_fleet(true, true);
     $return_tick = $mf->get_return_tick();
     echo "Return Tick: " . $return_tick . "<br />";
     echo "<div class=\"content_item\">";
     echo "<table>";
     echo "<tr class=\"title\">";
     echo "<td>Ship</td>";
     echo "<td>Class</td>";
     echo "<td>T1</td>";
     echo "<td>T2</td>";
     echo "<td>T3</td>";
     echo "<td>Type</td>";
     echo "<td>Amount</td>";
     echo "<td>Value</td>";
     echo "<td>Race</td>";
示例#3
0
 /**
  * Process a missions page parse.
  * 
  * @return (nothing)
  */
 private function process_missions()
 {
     // 0 - regular expressions
     // a - ships
     $re_ships = "/(.*)";
     $re_ships = $re_ships . "\\s+(Fighter|Corvette|Frigate|Destroyer|Cruiser|Battleship)";
     // class
     $re_ships = $re_ships . "\\s+(Fighter|Corvette|Frigate|Destroyer|Cruiser|Battleship|Asteroids|Structures|-)";
     // t1
     $re_ships = $re_ships . "\\s+(Fighter|Corvette|Frigate|Destroyer|Cruiser|Battleship|-)";
     // t2
     $re_ships = $re_ships . "\\s+(Fighter|Corvette|Frigate|Destroyer|Cruiser|Battleship|-)";
     // t3
     $re_ships = $re_ships . "\\s+(Normal|Emp|Cloak|Steal|Pod|Structure Killer)";
     // type
     $re_ships = $re_ships . "\\s+(\\d+)/";
     // b - various tt regex's that can be applied
     $re_1 = "/Galaxy\\s+ETA:\\s+(\\d+)\\s+ticks,\\s+Cluster\\s+Defence\\s+ETA:\\s+(\\d+)\\s+ticks,\\s+Universe\\s+ETA:\\s+(\\d+)\\s+ticks,\\s+Alliance\\s+ETA:\\s+(\\d+)\\s+ticks/";
     $re_2 = "/Launching\\s+in\\s+tick\\s+(\\d+),\\s+arrival\\s+in\\s+tick\\s+(\\d+)/";
     $re_3 = "/Return\\s+ETA:\\s+(\\d+)/";
     $re_4 = "/ETA:\\s+(\\d+),\\s+Return\\s+ETA:\\s+(\\d+)/";
     // 1 - explode string into an array
     $lines = explode("\r\n", $this->text);
     // 2 - check each array element for "Fleet	Location	Target (eta)	Mission"
     // 		if this matches, remember array element where the match occured (throw into $blocks_begin array)
     $limits = array();
     foreach ($lines as $i => $line) {
         $a = preg_match('/Fleet\\s+Location\\s+Target\\s+\\(eta\\)\\s+Mission/', $line);
         if ($a) {
             $limits[] = $i;
         }
     }
     // do all the following for every fleet block we find
     for ($j = 0; $j < 4; $j++) {
         // 3 - cut array apart
         if ($j == 3) {
             $fleet_array = array_slice($lines, $limits[$j]);
         } else {
             $fleet_array = array_slice($lines, $limits[$j], $limits[$j + 1] - $limits[$j]);
         }
         // 4 - implode the blocks
         $fleet_string = implode("\r\n", $fleet_array);
         // 5 - apply regular expressions for ships
         preg_match_all($re_ships, $fleet_string, $matches);
         // 6 - process ships
         $ships_name = $matches[1];
         $ships_amount = $matches[7];
         // build an array of ( ['shipname'] => $number_of_ships )
         $ships = array();
         foreach ($ships_name as $k => $ship_name) {
             $ship = new ship(trim($ship_name));
             // new ship
             $ship->set_amount($ships_amount[$k]);
             // set amount of ships
             $ships[] = $ship;
             // append to array
         }
         $mf = new member_fleet($j, $this->user_id);
         $mf->set_ships_in_fleet($ships);
         // 7 - apply regular expressions to determine return tick;
         // 		default value is the current_tick
         $return_tick = $this->tick;
         if (preg_match($re_4, $fleet_string, $match)) {
             $eta = $match[1];
             $return_eta = $match[2];
             $return_tick = $eta + $return_eta + $eta + $this->tick;
             $eta_bonus = $this->compute_eta_bonus($ships, $eta + $return_eta);
         } elseif (preg_match($re_3, $fleet_string, $match)) {
             $return_eta = $match[1];
             $return_tick = $return_eta + $this->tick;
         } elseif (preg_match($re_2, $fleet_string, $match)) {
             $launch = $match[1];
             $arrival = $match[2];
             $return_tick = $arrival + ($arrival - $launch);
             $eta_bonus = $this->compute_eta_bonus($ships, $arrival - $launch);
         } elseif (preg_match($re_1, $fleet_string, $match)) {
             $return_tick = $this->tick;
             $eta_bonus = $this->compute_eta_bonus($ships, $match[3]);
         }
         // 8 - save return tick and fleets
         $mf->set_return_tick($return_tick);
         $mf->save_fleet();
         // 9 - save eta bonus
         if (isset($eta_bonus)) {
             $member = new member($this->user_id);
             $member->set_eta_bonus($eta_bonus);
         }
         unset($ships);
         unset($mf);
     }
     if (isset($eta_bonus)) {
         $this->feedback = "Parsed Missions for " . $this->coords . " at Tick " . $this->tick . ".<br /><br />";
         $this->feedback = $this->feedback . "Detected ETA Bonus: " . $eta_bonus . ".";
     } else {
         $this->feedback = "Parsed Missions for " . $this->coords . " at Tick " . $this->tick . ".";
     }
 }