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; }
function newSession() { $config = ConfigManager::declareConfigValue(); $COOKIE_FILE = $config['COOKIE_FILE']; $USER_UNIVERSE = $config['USER_UNIVERSE']; $USER_ACCOUNT = $config['USER_ACCOUNT']; $USER_PASS = $config['USER_PASS']; //First to ask for data $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://tw.ogame.gameforge.com/main/login"); curl_setopt($ch, CURLOPT_POST, true); // 啟用POST curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //not output result curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); //Ignore redirection curl_setopt($ch, CURLOPT_COOKIESESSION, 1); curl_setopt($ch, CURLOPT_COOKIEFILE, $COOKIE_FILE); curl_setopt($ch, CURLOPT_COOKIEJAR, $COOKIE_FILE); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array("kid" => "", "uni" => $USER_UNIVERSE, "login" => $USER_ACCOUNT, "pass" => $USER_PASS))); $login_result = curl_exec($ch); $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $header = substr($login_result, 0, $header_size); $body = substr($login_result, $header_size); curl_close($ch); //extract query url and data info $location = $this->extractHeader($header); //print_r("Out Location = ". $location . PHP_EOL); //Ask for new session id $login_return = GlobalFunc::httpGet($location, $COOKIE_FILE); // modify cookie file for correcting cookie edit mistake (session live time is set to 0) $this->editCookieFile($COOKIE_FILE); // extract correct session id $SESSION = $this->extractSeesion($login_return); //updateConfigInfoByNewSession( $SESSION ); $over_page = GlobalFunc::httpGet($config['OVERVIEW_URL'] . "&" . $SESSION, $config['COOKIE_FILE']); if ($SESSION) { echo "Login Manager got a new session.\n"; } else { echo "Get Session Fail.\n"; return 0; } return $SESSION; }
private function getFleet1Page() { $COOKIE_FILE = $this->Config['COOKIE_FILE']; $FLEET1 = $this->Config['FLEET1']; $url = $FLEET1 . "&" . $this->rUri; // . "&". $SESSION; //echo "get fleet1 = ". $url; return GlobalFunc::httpGet($url, $COOKIE_FILE); }
function sendBuildRequest($but_dom, $resource_item_name) { if (isset($but_dom)) { echo "Resource Item: " . $resource_item_name . " is found!\n"; $but_class = $but_dom->getAttribute('class'); echo "but_class: " . $but_class . "\n"; if (strstr($but_class, "disabled")) { //不能蓋 (可能是資源不足 或 其他正在建) echo iconv("UTF-8", "big5", "不能蓋") . "\n"; return -1; } else { if (strstr($but_class, "off")) { echo iconv("UTF-8", "big5", "but_class = off, not able to build return 1 to skip.\n") . "\n"; return 1; } } //可以蓋 //若正在該的就是此項目 則class為on 仍不為disable //要確定是否真的可以蓋 找過所有div 確定沒有class名為 construction 的子div $isThisBuilding = false; $divs = $but_dom->getElementsByTagName('div'); for ($dIdx = 0; $dIdx < $divs->length; $dIdx++) { $div = $divs->item($dIdx); $div_class = $div->getAttribute('class'); if ($div_class == "construction") { $isThisBuilding = true; break; } } if ($isThisBuilding == true) { echo iconv("UTF-8", "big5", "此項目正在建造中") . "\n"; return -1; } echo iconv("UTF-8", "big5", "可以蓋") . "\n"; //找正確的tag_a $tag_a = $but_dom->getElementsByTagName('a')->item(0); $onclick_str = $tag_a->getAttribute('onclick'); if (strlen($onclick_str) < 10) { $tag_a = $but_dom->getElementsByTagName('a')->item(1); $onclick_str = $tag_a->getAttribute('onclick'); } //從onclick string中找到網址 //網址應為 'http://....' $onclick_str = substr($onclick_str, strpos($onclick_str, "http://")); $onclick_str = substr($onclick_str, 0, strpos($onclick_str, "'")); echo "onclick_str: " . $onclick_str . "\n"; //送出http request $this->onclickStr = $onclick_str; $result_page = GlobalFunc::synExecute("Sent build resource request", function () { echo "onclick_str: " . $this->onclickStr . "\n"; $result_page = GlobalFunc::httpGet($this->onclickStr, $this->Config['COOKIE_FILE']); return $result_page; }); $resultLen = strlen($result_page); if ($resultLen > $this->Config['AskPageLeastStrLen']) { return 1; } else { file_put_contents($this->Config['FailInfo'], $result_page); echo "ResourceManager: Sent build resource request result len " . $resultLen . " too few, content has written into FailInfo file, return 0.\n"; return 0; //發送的請求可能失敗了 } } else { echo "Resource Item: " . $resource_item_name . " is not found!\n"; return 0; //發送的請求可能失敗了 所以找不到該item } }
public static function changeFocusPlanet($href) { $Config = ConfigManager::declareConfigValue(); $COOKIE_FILE = $Config['COOKIE_FILE']; return GlobalFunc::httpGet($href, $COOKIE_FILE); }
function run() { /* 船隊ID:1026133 抵達時間 - 飛行長度 = 出發時間 fleetInfo["starttime"] 現在時間 - 出發時間 = 回來所需時間 #攻擊到達時間 = 所有針對該星球的攻擊 最近的那個 但如果攻擊之間只相距不到1分鐘 則視為同一個攻擊事件 #不用定期抓event list 因為defender反應的時候攻擊就已經快要到達了 現在時間 + 回來所需時間 - 攻擊到達時間 > 10 秒 召回 */ /* 以下三個重要的動作要做失敗判斷 1.$AttackDetector->detectAttack(); 2.$this->defendAttack() 3.Withdraw fleets */ while (1 == 1) { //<These code should be sychronized> //echo "Defender[".$this->p_coord[0].":".$this->p_coord[1].":".$this->p_coord[2]."]: ask for key.\n"; $resultlen = GlobalFunc::synExecute("Defender[" . $this->p_coord[0] . ":" . $this->p_coord[1] . ":" . $this->p_coord[2] . "](" . $this->p_coord[3] . "):", function () { date_default_timezone_set('Asia/Taipei'); $last_update_time = file_get_contents($this->Config['DLastUpt'], true); echo " last update time: " . date('H:i:s', $last_update_time) . "\n"; $nowTime = time(); if ($nowTime - $last_update_time <= 5) { //5秒內更新過的 可以直接使用 echo "Last update time: " . date('H:i:s', $last_update_time) . "; File is updated within 5 second.\n"; //確保通知外面 已經拿到想要的東西了 return $this->Config['AskPageLeastStrLen'] + 1; } else { echo "Last update time: " . date('H:i:s', $last_update_time) . "; File is not updated ask for overview page\n"; //更新overview string的內容 $overview_string = ""; $overview_string = GlobalFunc::getOverviewPage(); //echo "\noverview result strlen: " .strlen($overview_string) . "\n"; if (strlen($overview_string) < $this->Config['AskPageLeastStrLen']) { //access failed; echo "Defender ask for overview page failed, return 0\n"; return 0; } $event_list = GlobalFunc::getEventList(); //update overview page and event list info $put1 = file_put_contents($this->Config['ADOverview'], $overview_string); $put2 = file_put_contents($this->Config['ADEventList'], $event_list); //update last update time that other threads can check $put3 = file_put_contents($this->Config['DLastUpt'], time()); echo " Put1={$put1}, Put2={$put2}, Put3={$put2}\n"; } //成功取得頁面 return strlen($overview_string); }); echo "Defend read overview page result length: {$resultlen}\n"; if ($resultlen < $this->Config['AskPageLeastStrLen']) { echo "Defender: Error occur, failed to update overview page. login in again, resltlen: " . $resultlen . "\n"; $LoginManager = new LoginManager(); continue; //登入後 重跑回圈 } //</These code should be sychronized> //偵測攻擊 $AttackDetector = new AttackDetector($this->p_coord); $arrivetime = $AttackDetector->detectAttack(); //$arrivetime == 0表示不需要做任何事 //$arrivetime == "xx:xx:xx" 表示在這個時間攻擊會到達 $fleet_already_withdraw = false; $withdraw_arr_time = 0; //偵測到攻擊則開始抵禦 //不是數字 就表示有攻擊 echo "Defender: get returned arrivetime: "; var_dump($arrivetime); if (strcmp($arrivetime, "0") != 0) { echo "[" . $this->p_coord[0] . ":" . $this->p_coord[1] . ":" . $this->p_coord[2] . "](" . $this->p_coord[3] . ") start defese.\n"; $this->fleetInfo = $this->defendAttack(); //會更新fleet info if ($this->fleetInfo == 0) { $halt_period = $this->Config["sendFleetFailHaltPeriod"]; echo "Defender: fleetInfo return 0, error occur.\n"; echo "Defender: No withdraw link to withdraw fleets.\n"; echo "Ignore withdraw command. Sleep " . $halt_period . " second.\n"; if (strlen($halt_period) == 0) { $halt_period = 10; } sleep($halt_period); continue; } echo "Fleet Start Time: " . date('H:i:s', $this->fleetInfo["starttime"]) . "\n"; echo "Fleet Withdraw link: " . $this->fleetInfo["withdraw_link"] . "\n"; while (1 == 1) { //算出回來所需時間 每個loop更新一次 $withdraw_arr_left_sec = time() - $this->fleetInfo["starttime"]; $attack_arr_left_sec = GlobalFunc::timeFromNowInSecond($arrivetime, false); if ($withdraw_arr_left_sec - $attack_arr_left_sec >= 60 && $fleet_already_withdraw == false) { //撤退回到星球所需時間 > 攻擊到達剩餘時間 //可以撤退船隊了 //要發送撤回船隊的指令 //Syn start $result = GlobalFunc::synExecute("Fleet Withdraw", function () { // start time為預估時間 取不到實際值 因此很可能會有誤差 $result = GlobalFunc::httpGet($this->fleetInfo["withdraw_link"], $this->Config['COOKIE_FILE']); echo "Withdraw Link: " . $this->fleetInfo["withdraw_link"] . "\n"; return $result; }); if (strlen($result) < $this->Config['AskPageLeastStrLen']) { echo "Error occur, Fleet Withdraw result page len < AskPageLeastStrLen, may login in again.\n"; $LoginManager = new LoginManager(); continue; } //Syn end //標記船隊已經在回程途中 //取得艦隊會到家的時間 $fleet_already_withdraw = true; $withdraw_arr_time = $withdraw_arr_left_sec + time(); echo "Defender: [" . $this->p_coord[0] . ":" . $this->p_coord[1] . ":" . $this->p_coord[2] . "](" . $this->p_coord[3] . ") "; echo "withdraw fleets. result len: " . strlen($result) . ".\n"; } else { if ($fleet_already_withdraw == false) { echo "Defender: [" . $this->p_coord[0] . ":" . $this->p_coord[1] . ":" . $this->p_coord[2] . "](" . $this->p_coord[3] . ") "; echo "if " . ($withdraw_arr_left_sec - $attack_arr_left_sec) . " >= 60, withdraw fleets.\n"; } else { if ($withdraw_arr_time - time() < 0) { //船隊順利回到家 $idle_sec = $this->Config['idlePeriod']; echo "Defender: [" . $this->p_coord[0] . ":" . $this->p_coord[1] . ":" . $this->p_coord[2] . "](" . $this->p_coord[3] . ") "; echo "Fleets got home, lose defending ability for next 5 seconds.\n"; sleep($idle_sec); // prevent wrong prediction of the time that fleets arrive break; //break以後馬上會重跑回圈 若無sleep很可能因為誤差 船還沒到家 //若有下一輪攻擊 很可能造成程式失效 //因此當抵達的 $idle_sec 秒將沒有抵禦能力 //(船通常會晚個10~30秒到家,依據目前設定是10秒) } else { echo "Defender: [" . $this->p_coord[0] . ":" . $this->p_coord[1] . ":" . $this->p_coord[2] . "](" . $this->p_coord[3] . ") "; echo "Fleets will get home after " . ($withdraw_arr_time - time()) . " second.\n"; } } } sleep(2); } } else { echo "Defender: [" . $this->p_coord[0] . ":" . $this->p_coord[1] . ":" . $this->p_coord[2] . "](" . $this->p_coord[3] . ") "; //$detect_period = $this->Config['DetectPeriod']; $detect_period = rand($this->Config['DetectPeriod'] / 2, $this->Config['DetectPeriod']); echo "sleep " . $detect_period . " second.\n"; sleep($detect_period); //sleep(60); } } echo "Defend loop out\n"; }