예제 #1
0
 public static function getPlanets()
 {
     $Config = ConfigManager::declareConfigValue();
     //get overview page html
     $overview = GlobalFunc::httpGet($Config['OVERVIEW_URL'], $Config['COOKIE_FILE']);
     $ov_dom = GlobalFunc::loadHtml($overview);
     $planet_list_dom = $ov_dom->getElementById("planetList");
     $pList = $planet_list_dom;
     if (!is_object($pList)) {
         echo "Error occur, Planet List not found, return empty planet list.\n";
         return array();
     }
     //list 下 每個div都是一個星球
     //每個div下都有兩個span
     //<span class="planet-name  ">jones4</span>
     //<span class="planet-koords  ">[1:83:6]</span>
     $planets_dom = $pList->getElementsByTagName('div');
     //echo "There ". $planets_dom->length . " Planet(s).";
     //存放月亮的起始編號(最後一顆星球之後就是月亮)
     $moon_num = $planets_dom->length + 1;
     //create planets
     $planets = array();
     for ($i = 0; $i < $planets_dom->length; $i++) {
         //get planet link
         $a_list = $planets_dom->item($i)->getElementsByTagName('a');
         $a_dom = $a_list->item(0);
         //產生星球
         $planet = Planet::createPlanetObj($a_dom, $planets_dom, $i);
         //將星球obj 加入list
         $planets[$i] = $planet;
         //確認是否有月球
         $has_moon = false;
         $moon_href = "";
         $moon_coord = array();
         $a_count = $a_list->length;
         $moon_obj = array();
         Planet::updateMoonInfo($a_list, $a_count, $planet, $has_moon, $moon_href, $moon_coord);
         if ($has_moon == true) {
             //有月亮則加到清單最後面
             $moon_obj = new Planet("月亮", $moon_coord, null, $moon_href, $moon_num);
             //將月亮obj 加入list
             //idx為編號-1
             $planets[$moon_num - 1] = $moon_obj;
             $moon_num++;
             //月亮編號+1
         }
     }
     return $planets;
 }
예제 #2
0
 public static function getResources($any_page)
 {
     // feed html str not dom element
     $page = GlobalFunc::loadHtml($any_page);
     $resource;
     $d_dom = $page->getElementById("resources_deuterium");
     $c_dom = $page->getElementById("resources_crystal");
     $m_dom = $page->getElementById("resources_metal");
     if (!is_object($d_dom)) {
         echo "Error occur, getResources {resources_deuterium, resources_crystal, resources_metal} not found, return &.\n";
         return "&";
     }
     $deuterium = $d_dom->textContent;
     $crystal = $c_dom->textContent;
     $metal = $m_dom->textContent;
     $resource['deuterium'] = GlobalFunc::strToIntByTakeCommaOff(trim($deuterium));
     $resource['crystal'] = GlobalFunc::strToIntByTakeCommaOff(trim($crystal));
     $resource['metal'] = GlobalFunc::strToIntByTakeCommaOff(trim($metal));
     return $resource;
 }
예제 #3
0
 function extractSeesion($script)
 {
     $doc = GlobalFunc::loadHtml($script);
     //print_r($doc);
     $value = $doc->textContent;
     //print_r("Value: ".$value."\n\r");
     $s_key = "http://";
     $e_key = "'";
     $s_pos = strrpos($value, $s_key, 0);
     $e_pos = strrpos($value, $e_key, -1);
     $len = $e_pos - $s_pos;
     $location = substr($value, $s_pos, $len);
     //print_r("s_pos: ".$s_pos);
     //print_r("e_pos: ".$e_pos);
     //print_r("len: ".$len);
     //print_r("Location: ".$location);
     $session_s_key = "PHPSESSID=";
     $session_pos = strrpos($location, $session_s_key);
     $session = substr($location, $session_pos);
     //print_r("session: ".$session);
     return $session;
     //return $location;
 }
예제 #4
0
 function buildResourceItem($blocks, $tCoor)
 {
     $PLANETS = $this->PLANETS;
     if (!isset($PLANETS)) {
         echo "No Planet list! return false.\n";
         return false;
     }
     $page = $blocks[0];
     for ($i = 0; $i < sizeof($PLANETS); $i++) {
         if (GlobalFunc::isCoordinateMatch($PLANETS[$i]->coord, $tCoor)) {
             echo "ResourceManager: Planet[" . $tCoor[0] . ":" . $tCoor[1] . ":" . $tCoor[2] . "](" . $tCoor[3] . ") isFound.\n";
             //取得切換到該星球的 url
             $pos = strpos($PLANETS[$i]->href, "cp=");
             $cpText = substr($PLANETS[$i]->href, $pos);
             if ($page == "R") {
                 //資源
                 $resourceUrl = $this->Config['RESOURCE_URL'] . "&" . $cpText;
             } else {
                 if ($page == "S") {
                     //設施
                     $resourceUrl = $this->Config['STATION_URL'] . "&" . $cpText;
                 } else {
                     if ($page == "T") {
                         //科技
                         $resourceUrl = $this->Config['TECH_URL'] . "&" . $cpText;
                     } else {
                         echo "ResourceManager.buildResourceItem(): error, invalid page value: (" . $page . ") found. return 1 to skip.\n";
                         return 1;
                     }
                 }
             }
             //切換星球 順便取得resources page
             $this->resourceUrl = $resourceUrl;
             $resources_page = GlobalFunc::synExecute("Change focused planet and get resources page", function () {
                 echo "resourceUrl: " . $this->resourceUrl;
                 return GlobalFunc::changeFocusPlanet($this->resourceUrl);
             });
             if (strlen($resources_page) < $this->Config['AskPageLeastStrLen']) {
                 echo "ResourceManager: resources_page len " . strlen($resources_page) . " too few, return 0.\n";
                 return 0;
                 //發送的請求可能失敗了
             }
             $resources_dom = GlobalFunc::loadHtml($resources_page);
             //找金屬礦
             //$resource_item_name = "button1";
             //$but1 = $resources_dom->getElementById( $resource_item_name );
             if ($page == "R" || $page == "S") {
                 $item_id = $blocks[1];
                 $but1 = $this->getResourceStationBtn($resources_dom, $item_id);
             } else {
                 if ($page == "T") {
                     $item_id = $blocks[1];
                     $item_num = $blocks[2] - 1;
                     $but1 = $this->getTechnologyBtn($resources_dom, $item_id, $item_num);
                 } else {
                     echo "Page is not R, S or T, invalid. return 1 to skip this request.";
                     return 1;
                 }
             }
             //發送 蓋資源 的請求
             return $this->sendBuildRequest($but1, $item_id);
         }
     }
     //找不到該星球
     return -1;
 }
예제 #5
0
 public static function filterAttackByEventFleetStr($event_fleets_str, $th_list, &$isThreaten)
 {
     //傳入Event Fleet String
     if (!isset($event_fleets_str) || $event_fleets_str == "") {
         echo "Error, filterAttackByEventFleetStr(): event_fleets_str empty, return 0\n";
         return 0;
     }
     $fleet_dom = GlobalFunc::loadHtml('<?xml encoding="UTF-8">' . $event_fleets_str);
     $fleet_tds = $fleet_dom->getElementsByTagName('td');
     $fleet_td_count = $fleet_tds->length;
     echo "event_fleets_str len: " . strlen($event_fleets_str) . "\n";
     echo "filterAttackByEventFleetStr(): fleet_td_count = {$fleet_td_count}\n";
     for ($f_idx = 0; $f_idx < $fleet_td_count; $f_idx++) {
         //取得UTF-8編碼下的characters
         $text_got = $fleet_tds->item($f_idx)->textContent;
         $text_got = trim($text_got, ":");
         //echo "[$f_idx]: " . $text_got . "\n";
         if (isset($th_list[$text_got])) {
             //若是船艦 idx+1 即為船艦數量
             $ship_count = $fleet_tds->item($f_idx + 1)->textContent;
             $ship_count = GlobalFunc::strToIntByTakeCommaOff($ship_count);
             //跳躍讀取船艦數量 idx要自動加1
             $f_idx++;
         } else {
             //不在過濾的船艦清單中
             echo iconv("UTF-8", "big5//IGNORE", "[{$f_idx}]{$text_got} 此項目不在清單中\n");
             continue;
         }
         //echo iconv("UTF-8", "big5//IGNORE", "$text_got 數量:$ship_count\n");
         //比對清單 看是否有威脅性
         echo iconv("UTF-8", "big5//IGNORE", "[{$f_idx}]text_got 門檻:" . $th_list[$text_got] . " 判斷運算子(<=) 攻擊數量:{$ship_count}\n");
         if ($th_list[$text_got] <= $ship_count) {
             //有威脅性
             echo iconv("UTF-8", "big5//IGNORE", "此項目已超過門檻 具有威脅性.\n");
             $isThreaten = true;
             //不用再偵測了 已經確定此工及具有威脅性
             break;
         }
     }
     return 1;
 }
예제 #6
0
 function detectAttack()
 {
     /******************************************/
     // 基本上 此物件裡面使用到的資訊
     // 都會從檔案中讀取 並不會產生http request
     // 不需要做synchronization
     /******************************************/
     $PLANET = $this->PLANET;
     //echo "\n";
     echo "AttackDetector: [" . $PLANET[0] . ":" . $PLANET[1] . ":" . $PLANET[2] . "](" . $PLANET[3] . ") ";
     echo "Start detecting attacks.\n";
     $final_arrive_time = "0";
     if ($this->hasAttack()) {
         //Attacked; get event list
         $eList = file_get_contents($this->Config['ADEventList'], true);
         //echo "event list result strlen: ". strlen($eList)."\n";
         if (strlen($eList) < $this->Config['AskPageLeastStrLen']) {
             $LoginManager = new LoginManager();
             $eList = GlobalFunc::getEventList();
         }
         $event_list_dom = GlobalFunc::loadHtml("<?xml encoding='UTF-8'>" . $eList);
         //每個tr都是一筆傳對往返的event
         $items = $event_list_dom->getElementsByTagName('tr');
         if (!is_object($items)) {
             echo "Error occur, <tr> tags are not found in event list, return 0.\n";
             return 0;
         }
         $items_num = $items->length;
         //items of fleets action report are sorted by arriving time from closest to farest
         //逐項檢查event list(items)裡面的每筆event(item)
         for ($i = 0; $i < $items_num; $i++) {
             //船隊的抵達時間
             $arrive_time = 0;
             //取得event(item)下的所有td
             $item = $items->item($i);
             $tds = $item->getElementsByTagName('td');
             $tds_num = $tds->length;
             //取得船隊的到達時間
             echo "Prepare to check arrival time.\n";
             for ($tdIdx = 0; $tdIdx < $tds_num; $tdIdx++) {
                 $tmp_td_item = $tds->item($tdIdx);
                 $tmp_class_value = $tmp_td_item->attributes->getNamedItem('class')->nodeValue;
                 if ($tmp_class_value == "arrivalTime") {
                     $arrive_time_str = $tmp_td_item->textContent;
                     //trim chinese code after the time string
                     $arrive_time = GlobalFunc::trimArrivalTime($arrive_time_str);
                     break;
                 }
             }
             if ($arrive_time != "") {
                 //data-arrival-time不為空值
                 //剩下多少時間會到達
                 $left_arrive_time = GlobalFunc::timeFromNowInSecond($arrive_time, true);
                 echo "AttackDetector: Arrive_time " . $arrive_time . ".\n";
                 echo "AttackDetector: Reaction time " . $this->ReactionTime . " seconds.\n";
                 echo "AttackDetector: An Attack arrive after " . $left_arrive_time . " seconds.\n";
                 if ($left_arrive_time > $this->ReactionTime) {
                     echo "AttackDetector: Attack over reaction time, do nothing. \n";
                     //(因為第一個偵測到的攻擊一定是最早到的攻擊)
                     //所以第一個都還沒到就可以直接結束偵測了
                     //到達時間超過反應時間 不需理會
                     break;
                 }
             } else {
                 echo "AttackDetector: No arrive time , it's not an attack.\n";
                 //沒有抵達時間 該event應該不是攻擊
                 break;
             }
             //紀錄event狀態的flag
             $isTDAttackEvent = false;
             $isGroupAttackEvent = false;
             //是聯合攻擊
             $isTDEventOver = true;
             $dest_coord = array(0, 0, 0);
             $dest_type = 0;
             $total_fleets_count = 10000;
             $eventFleetStr = "";
             //查看該event的所有<td>並取得重要內容
             for ($j = 0; $j < $tds_num; $j++) {
                 $td_dom = $tds->item($j);
                 $td_class_value = $td_dom->attributes->getNamedItem('class')->nodeValue;
                 if ($td_class_value == "missionFleet") {
                     //確認該事件是否為攻擊事件
                     //被攻擊的會有這個圖示 以圖片網址來判定
                     //<td class="missionFleet">
                     //<img src="http://gf1.geo.gfsrv.net/cdn9a/cd360bccfc35b10966323c56ca8aac.gif" class="tooltipHTML" title="">
                     //</td>
                     $img = $td_dom->getElementsByTagName('img')->item(0);
                     $icon_source = $img->attributes->getNamedItem('src')->nodeValue;
                     if ($icon_source == "http://gf1.geo.gfsrv.net/cdn9a/cd360bccfc35b10966323c56ca8aac.gif") {
                         $isTDAttackEvent = true;
                     } else {
                         if ($icon_source == "http://gf2.geo.gfsrv.net/cdnad/87d615c4fb395b75ec902b66b7757e.gif") {
                             $isTDAttackEvent = true;
                             $isGroupAttackEvent = true;
                             echo "AttackDetector: This is a group attack event. Withdraw fleets whatever.\n";
                         } else {
                             echo "AttackDetector: This is not an known attack event. ";
                             echo "Check if is attack event. Icon Source = " . $icon_source . "\n";
                         }
                     }
                 } else {
                     if ($td_class_value == "detailsFleet") {
                         $span_dom = $td_dom->getElementsByTagName('span')->item(0);
                         $total_fleets_count = $span_dom->textContent;
                         //一定要拿掉三位撇 否則會錯
                         //"6,643" <= 20 = true (錯誤結果)
                         //"6643"  <= 20 = false
                         echo "total_fleets_count before trim: " . $total_fleets_count . "\n";
                         $total_fleets_count = GlobalFunc::strToIntByTakeCommaOff($total_fleets_count);
                         echo "total_fleets_count after trim: " . $total_fleets_count . "\n";
                         /*if($total_fleets_count <= $this->Config['ThreatenCount']){
                         			echo "\n================= Ignore Attack ==================\n";
                         			echo "\nFleets count Total(".$total_fleets_count.") <= Threshold(".$this->Config['ThreatenCount']."), no big deal. Ignore the attack.\n";
                         			echo "AttackDetector: Attack arrive after ". $left_arrive_time . " seconds.\n";
                         			echo "\n==================================================\n";
                         			break;
                         		}else{
                         			echo "AttackDetector: This attack is dangerous, hide fleets.\n";
                         		}*/
                     } else {
                         if ($td_class_value == "icon_movement") {
                             //確認該船隊是否為前往狀態
                             //icon_movement存在表示艦隊正在前往
                             $isTDEventOver = false;
                             //取得eventFleetStr
                             $eventFleetStr = EscapeFilter::getEventFleetStr($td_dom);
                         } else {
                             if ($td_class_value == "destFleet") {
                                 //找出被攻擊的星球type
                                 $dest_type = GlobalFunc::getTargetTypeByTdDom($td_dom);
                                 if ($dest_type != $PLANET[3]) {
                                     //打的不是本Planet(type不一樣)
                                     $isTDAttackEvent = false;
                                 }
                             } else {
                                 if ($td_class_value == "destCoords") {
                                     //取得目的地星球座標
                                     //<td class="destCoords">
                                     //<a href="http://s113-tw.ogame.gameforge.com/game/index.php?page=galaxy&amp;galaxy=1&amp;system=79" target="_top">
                                     //[1:79:10]
                                     //</a>
                                     //</td>
                                     $a_dom = $td_dom->getElementsByTagName('a')->item(0);
                                     $dest_coord_string = trim($a_dom->textContent);
                                     $dest_coord_string = substr($dest_coord_string, 1, strlen($dest_coord_string) - 2);
                                     // trim notation '[]'
                                     $dest_coord = explode(":", $dest_coord_string);
                                     //echo "dest_coord_string = ".$dest_coord_string;
                                     //var_dump($dest_coord);
                                     //echo PHP_EOL;
                                 }
                             }
                         }
                     }
                 }
             }
             //取得目標的type
             $dest_coord[3] = $dest_type;
             if ($isTDAttackEvent && !$isTDEventOver) {
                 //確定該星球正在被攻擊 且 攻擊船隊是前往的狀態
                 if (GlobalFunc::isCoordinateMatch($dest_coord, $PLANET)) {
                     //判斷威脅性 若此event沒有威脅性 則coninue
                     $p_coord_text = GlobalFunc::getCoordTextWithType($PLANET);
                     //是聯合攻擊的話 不管怎樣直接逃
                     if ($isGroupAttackEvent == false) {
                         $isThreaten = EscapeFilter::escapeIfThreatenFilter($total_fleets_count, $eventFleetStr, EscapeFilter::getEscapeList(), $p_coord_text);
                         if ($isThreaten != true) {
                             //不具威脅性
                             echo "================================= Ignore Attack =================================\n";
                             echo iconv("UTF-8", "big5//IGNORE", "\n此攻擊(arrive_time:{$arrive_time})(目標:{$p_coord_text}) 不具威脅性 忽略.\n");
                             echo "\n================================= Ignore Attack =================================\n";
                             continue;
                         }
                     }
                     //宣告該星球是被攻擊的星球
                     echo "AttackDetector: [{$dest_coord['0']}:{$dest_coord['1']}:{$dest_coord['2']}]({$dest_coord['3']}):\n";
                     echo "final_arrive_time: ";
                     var_dump($final_arrive_time);
                     if (strcmp($final_arrive_time, "0") == 0) {
                         $final_arrive_time = $arrive_time;
                         echo "Attack arrive time is updated, after," . $left_arrive_time . " s.\n";
                     } else {
                         //之前已經有一個攻擊被偵測到了了
                         //之前被偵測到的攻擊 還剩幾秒鐘會到?
                         $latest_attack_left_arr_time = GlobalFunc::timeFromNowInSecond($final_arrive_time, true);
                         //這次攻擊抵達的時間 一定要 比之前檢查到的晚到(因為對方伺服器給的資料有照時間順序排序)
                         //所以如果 attack_interval < 0 表示有error
                         $attack_interval = $left_arrive_time - $latest_attack_left_arr_time;
                         if ($attack_interval < 0) {
                             echo "\n!!!!!!!!!!!!! error !!!!!!!!!!!!!!\n";
                             echo "attack_interval = " . $attack_interval . "\n";
                             echo "There must be some error happened.\n";
                             echo "\n!!!!!!!!!!!!! error !!!!!!!!!!!!!!\n";
                         }
                         //確認attack interval是否太近 會造成危險
                         //若太接近則視為同一起攻擊事件,
                         //		取最晚到的攻擊做為final_arrive_time
                         //但	取最早到的攻擊做為判斷船隻是否需要撤離的條件
                         //(只要 $final_arrive_time 不為0, 船隻就會被送走)
                         echo "{$attack_interval}: " . $attack_interval . " CAttackPvtInterval: " . $this->Config['CAttackPvtInterval'] . "\n";
                         if ($attack_interval < $this->Config['CAttackPvtInterval']) {
                             $final_arrive_time = $arrive_time;
                             echo "Second Attack is detected, arrive time is updated, after " . $left_arrive_time . " sec.\n";
                         } else {
                             echo "Second Attack is detected, it's too far from the preceding ones.\n";
                         }
                     }
                 }
             }
         }
     } else {
         echo "AttackDetector: [" . $PLANET[0] . ":" . $PLANET[1] . ":" . $PLANET[2] . "](" . $PLANET[3] . ") ";
         echo " No Attack Detected.\n";
     }
     echo "AttackDetector: [" . $PLANET[0] . ":" . $PLANET[1] . ":" . $PLANET[2] . "](" . $PLANET[3] . ") ";
     echo "Detecting finished.\n";
     return $final_arrive_time;
 }