function run() { //ini_set('date.timezone','UTC'); date_default_timezone_set('Asia/Taipei'); $config = ConfigManager::declareConfigValue(); $before_wakeup = 0; while (1 == 1) { $count = 0; $period = rand(3000, 9000); $one_minute = 600; $before_wakeup = $period; while ($count < $period) { $count += $one_minute; echo date('l jS \\of F Y h:i:s A') . ": Log Manager Alive. before " . $before_wakeup . " secs to ask session again.\n"; // bat alive reply $before_wakeup = $period - $count; sleep($one_minute); } //更新login session syn start $overview_string = GlobalFunc::synExecute("Login Manager Get Session", function () { return $this->newSessionGetOverview(); }); //syn end $this->checkServerTimeDiff($overview_string); } }
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 function sendAllFleetsToShield() { $MOVEMENT = $this->Config['MOVEMENT']; //init $this->fleetInfo["id"] = -1; //應該要做synchronization //syn start //lock file echo iconv("UTF-8", "big5", "一個派船出航\n") . "\n"; $movement_page = GlobalFunc::synExecute("Sent fleet to shield", function () { echo "FleetCommander: This Planet[" . $this->coord[0] . ":" . $this->coord[1] . ":" . $this->coord[2] . "] "; //echo "-> Shield Planet[". $this->target_coord[0]. ":".$this->target_coord[1]. ":". $this->target_coord[2]."]\n"; //切換星球 call global function $page = GlobalFunc::changeFocusPlanet($this->href); $this->rUri = ""; //取得Fleet1頁面 ///game/index.php?page=fleet1&galaxy=1&system=104&position=13&type=1&mission=1 $f1_page = $this->getFleet1Page(); //取得所有的戰船數量 $fleets_uri = $this->getFleetsAmount($f1_page); $this->rUri .= "&speed=1" . $fleets_uri; //取得Fleet2頁面 //galaxy=1&system=104&position=13&type=1&mission=1&speed=10&am204=&am205=&am206=&am202=&am203=1&am209=&am210= $f2_page = $this->getFleet2Page(); //$f2 = GlobalFunc::loadHTML($f2_page); //取得Escape List echo iconv("UTF-8", "big5", "開始找尋適合的shield.\n"); $my_coord_text = GlobalFunc::getCoordTextWithType($this->coord); $ESList = EscapeFilter::getEscapeList(); if (isset($ESList[$my_coord_text])) { $shields = $ESList[$my_coord_text]["shields"]; $sh_count = sizeof($shields); $fcheck_page = 1; //儲存最終傳回的結果 來確定有沒有一個可行的座標 for ($sh_i = 0; $sh_i < $sh_count; $sh_i++) { $sh_coord = $shields[$sh_i]; $sh_text = GlobalFunc::getCoordTextWithType($sh_coord); //嘗試取得fleetcheck頁面 $fcheck_page = $this->getFleetCheck($sh_coord); echo "fcheck_page:" . $fcheck_page; if ($fcheck_page == "0") { echo iconv("UTF-8", "big5", "Check通過 決定Shield座標({$sh_text}).\n"); $this->target_coord = $sh_coord; break; } else { if ($fcheck_page == "1") { echo iconv("UTF-8", "big5", "此target座標({$sh_text})不存在.\n"); } else { if ($fcheck_page == "2") { echo iconv("UTF-8", "big5", "此target處於假期模式({$sh_text}) 無法派遣.\n"); } else { echo iconv("UTF-8", "big5", "fcheck_page:{$fcheck_page} 不等於任何已知結果\n"); } } } } } else { echo iconv("UTF-8", "big5", "沒有Escape List.\n"); $fcheck_page = $this->getFleetCheck($this->target_coord); } //加入目的地uri $type = 1; //出發星球的type if (isset($this->target_coord[3]) && $this->target_coord[3] != NULL) { $type = $this->target_coord[3]; } $posi_uri = "galaxy=" . $this->target_coord[0] . "&system=" . $this->target_coord[1] . "&position=" . $this->target_coord[2] . "&type=" . $type . "&mission=3"; $this->rUri = $posi_uri . $this->rUri; echo "this->uri: " . $this->rUri . "\n"; //file_put_contents($this->Config['FailInfo'], $f2_page); $f3_page = $this->getFleet3Page(); //派遣船隊 $movement_page = $this->sendFleetOut($f3_page); //echo "movement_page result strlen: ". strlen($movement_page) . "\n"; if (strlen($movement_page) < $this->Config['AskPageLeastStrLen']) { echo "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"; echo "Sent fleet failed, result strlen: " . strlen($movement_page); echo "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"; } return $movement_page; }); //syn end echo "FleetCommander: movement_page result strlen: " . strlen($movement_page) . "\n"; if (strlen($movement_page) == 0) { //echo "movement_page strlen: 0!\n"; echo "Error occur! No fleet info to return! return 0 instead.\n"; return 0; } //取得的duration是錯誤的 因為他的計算都在javascript 因此無法取得 date_default_timezone_set('Asia/Taipei'); $n_time = time(); $time_diff = $n_time - $this->time_before_sent; $predicted_start_time = $this->time_before_sent + $time_diff / 2; $mp = GlobalFunc::loadHTML($movement_page); //找尋船隊的detail資訊 $inhalt = $mp->getElementById("inhalt"); if (!is_object($inhalt)) { echo "Error occur, inhalt is not an object, no way to find fleet detail\n"; return 0; } $this->fleetInfo = $this->getfleetDetails($inhalt, $this->coord, $this->target_coord); //取得船隊的id //$this->fleetInfo['id']; //取得船隊召回的get url //get ?page=movement&return=id $this->fleetInfo["withdraw_link"] = $MOVEMENT . "&return=" . $this->fleetInfo["id"]; if (!isset($this->fleetInfo["id"])) { $this->fleetInfo["id"] = -1; } if ($this->fleetInfo["id"] == -1 || strlen(trim($this->fleetInfo["id"])) == 0) { echo "Can't get fleet id, return 0.\n"; echo iconv("UTF-8", "big5", '船隊撤退失敗, 請檢查 重氫是否不足\\n'); return 0; } //取得船隊的抵達時間 //$this->fleetInfo["arrivetime"]; //echo "My fleet info in the end of send: duration = ".$this->duration; //取得船隊的飛行時間 //$this->fleetInfo["flighttime"] = $this->duration; //取得船隊的出發時間 //$this->fleetInfo["starttime"] = $this->fleetInfo["arrivetime"] - $this->duration; //echo "arrivetime = ".$this->fleetInfo["arrivetime"]." duration = $this->duration | starttime = ".$this->fleetInfo["starttime"]."\n"; $this->fleetInfo["starttime"] = $predicted_start_time; //echo "My fleet info in the end of send: "; //var_dump($this->fleetInfo); return $this->fleetInfo; }
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"; }