예제 #1
0
 private function getfleetDetails($inhalt_dom, $oring_pos, $target_pos)
 {
     $divs = $inhalt_dom->getElementsByTagName("div");
     $len = $divs->length;
     //取得所有的fleet div(每一筆飛行事件)
     $fleet_divs = array();
     $f_idx = 0;
     for ($i = 0; $i < $len; $i++) {
         $attrs = $divs->item($i)->attributes;
         if ($attrs != null) {
             $id = $attrs->getNamedItem('id');
         }
         if ($id != null) {
             $id = $id->nodeValue;
             if (substr($id, 0, 5) == "fleet") {
                 //echo "---------------------------Div found---------------------------";
                 $fleet_divs[$f_idx] = $divs->item($i);
                 $f_idx++;
             }
         }
         //var_dump($attrs);
     }
     $fleet_div_length = $f_idx;
     //從飛行事件比對 取得相同origin星球且相同target星球的fleet div
     for ($fd_i = $fleet_div_length - 1; $fd_i >= 0; $fd_i--) {
         //從後面開始找, 逃出的船都設10%速度 正常來說都在後排
         $f_div = $fleet_divs[$fd_i];
         //比對每筆飛行事件
         $spans = $f_div->getElementsByTagName("span");
         $span_num = $spans->length;
         //找出每筆fleet div下的span 取得需要的資訊
         $this_origin = null;
         //class="originData"
         $this_target = null;
         //class="destinationData"
         $this_origin_str;
         $this_target_str;
         //紀錄星球的種類
         $origin_type = 0;
         $dest_type = 0;
         $this_id = null;
         //ref="1026133" 取ref != null
         $this_arrive_time = $f_div->attributes->getNamedItem("data-arrival-time")->nodeValue;
         //div 的 data-arrival-time="1407182351"
         for ($i = 0; $i < $span_num; $i++) {
             $span = $spans->item($i);
             //prevent null class
             if ($span->attributes->getNamedItem("class")) {
                 $s_class = $span->attributes->getNamedItem("class")->nodeValue;
             }
             if ($s_class == "originData") {
                 //起始座標的parent span
                 $child_spans = $span->getElementsByTagName("span");
                 $child_num = $child_spans->length;
                 for ($j = 0; $j < $child_num; $j++) {
                     $child_span = $child_spans->item($j);
                     //prevent null class
                     if ($child_span->attributes->getNamedItem("class")) {
                         $s_class = $child_span->attributes->getNamedItem("class")->nodeValue;
                     }
                     if ($s_class == "originCoords tooltip") {
                         $this_origin_str = $child_span->getElementsByTagName("a")->item(0)->textContent;
                         $this_origin = GlobalFunc::getCoordArrayByStr($this_origin_str);
                     }
                     if ($s_class == "originPlanet") {
                         $origin_type = GlobalFunc::getTargetTypeByTdDom($child_span);
                     }
                 }
             } else {
                 if ($s_class == "destinationData") {
                     //目標座標的parent span
                     $child_spans = $span->getElementsByTagName("span");
                     $child_num = $child_spans->length;
                     for ($j = 0; $j < $child_num; $j++) {
                         $child_span = $child_spans->item($j);
                         //prevent null class
                         if ($child_span->attributes->getNamedItem("class")) {
                             $s_class = $child_span->attributes->getNamedItem("class")->nodeValue;
                         }
                         if ($s_class == "destinationCoords tooltip") {
                             $this_target_str = $child_span->getElementsByTagName("a")->item(0)->textContent;
                             $this_target = GlobalFunc::getCoordArrayByStr($this_target_str);
                         }
                         $pos = strpos($s_class, "destinationPlanet");
                         //注意 必須用 === 或 !== 運算元
                         //因為false = 0(boolean)
                         //第一個字吻合 = 0(int) 位置為0
                         //要用全等號===來判斷是否連型態都吻合
                         if ($pos !== false) {
                             //有找到
                             //找到後下面可能有很多span, 其中沒有class value的才是正確的
                             $in_sp_array = $child_span->getElementsByTagName("span");
                             $in_sp_len = $in_sp_array->length;
                             for ($inner_idx = 0; $inner_idx < $in_sp_len; $inner_idx++) {
                                 $in_sp = $in_sp_array->item($inner_idx);
                                 $in_class = $in_sp->attributes->getNamedItem("class");
                                 if (!is_object($in_class)) {
                                     $dest_type = GlobalFunc::getTargetTypeByTdDom($in_sp);
                                 }
                             }
                         }
                     }
                 } else {
                     if ($span->attributes->getNamedItem("ref") != null) {
                         $this_id = $span->attributes->getNamedItem("ref")->nodeValue;
                     }
                 }
             }
         }
         //更新星球的type
         $this_origin[3] = $origin_type;
         $this_target[3] = $dest_type;
         //var_dump($this_target);
         /*echo "This: ";
           var_dump($this_target);
           var_dump($this_origin);
           echo "Compare with: ";
           var_dump($oring_pos);
           var_dump($target_pos);*/
         //目前只比對起始及目的座標 ; 若怕有重複 可考慮比較其他資訊
         if (GlobalFunc::isCoordinateMatch($this_origin, $oring_pos) && GlobalFunc::isCoordinateMatch($this_target, $target_pos)) {
             echo "\n====================== Get Fleet Info After Send ========================\n";
             echo "Found ID: " . $this_id . "\n";
             echo "Found similar fleet info Origin:" . $this_origin_str . " | Target: " . $this_target_str . "\n";
             echo "Right info: Origin[" . $oring_pos[0] . ":" . $oring_pos[1] . ":" . $oring_pos[2] . "]({$oring_pos['3']}) | ";
             echo "Target: [" . $target_pos[0] . ":" . $target_pos[1] . ":" . $target_pos[2] . "]({$target_pos['3']})\n";
             echo "\n===============================================================+=========\n";
             //$f_info["arrivetime"] = $this_arrive_time - $this->Config['timeDiff'] ;
             $f_info["id"] = $this_id;
             //echo "My fleet info in get detail: ";
             //var_dump($f_info);
             return $f_info;
             break;
         }
         //echo "============". $this_origin ."============";
         //echo "============". $this_target ."============";
         //echo "============". $this_id ."============";
     }
 }