十八、艦隊活動(FlyingFleetHandler.php)
在前文中我們曾經初步研究過艦隊活動的源碼,提到過艦隊活動的觸發。現在我進一步分析Xnova中艦隊活動,這些在本質上是由玩家觸發的,並不是由定時器在進行定時計算。艦隊活動的觸發是在common.php中進行的,所以入口就在這文件中,代碼如下:
//檢查是否有到達目的地的艦隊 $_fleets = doquery("SELECT * FROM {{table}} WHERE 'fleet_start_time' <= '".time()."';", 'fleets'); // OR fleet_end_time <= ".time() //循環處理每個艦隊 while ($row = mysql_fetch_array($_fleets)) { $array = array(); $array['galaxy'] = $row['fleet_start_galaxy']; $array['system'] = $row['fleet_start_system']; $array['planet'] = $row['fleet_start_planet']; $array['planet_type'] = $row['fleet_start_type']; //艦隊處理函數 $temp = FlyingFleetHandler ($array); } //檢查是否有返回出發地的艦隊 $_fleets = doquery("SELECT * FROM {{table}} WHERE 'fleet_end_time' <= '".time()."';", 'fleets'); // OR fleet_end_time <= ".time() //循環處理每個艦隊 while ($row = mysql_fetch_array($_fleets)) { $array = array(); $array['galaxy'] = $row['fleet_end_galaxy']; $array['system'] = $row['fleet_end_system']; $array['planet'] = $row['fleet_end_planet']; $array['planet_type'] = $row['fleet_end_type']; //艦隊處理函數 $temp = FlyingFleetHandler ($array); }
大家看到了,前面這段代碼其實寫的不好,因為這裡每次都會被調用,效率太低了。因此我們在這裡可以進行優化,使每次循環只需要一個艦隊的ID傳遞給函數即可;相應的函數也要做修改,有興趣的自己修改下。
接下來看看函數FlyingFleetHandler(),這是集中調用艦隊活動的函數。
//鎖表,防止出現數據不同步等問題 doquery("LOCK TABLE {{table}}lunas WRITE, {{table}}rw WRITE, {{table}}errors WRITE, {{table}}messages WRITE, {{table}}fleets WRITE, {{table}}planets WRITE, {{table}}galaxy WRITE ,{{table}}users WRITE", ""); //這裡一大段就是取得艦隊數組,參數完全可以使用艦隊ID,優化之 $QryFleet = "SELECT * FROM {{table}} "; $QryFleet .= "WHERE ("; $QryFleet .= "( "; $QryFleet .= "`fleet_start_galaxy` = ". $planet['galaxy'] ." AND "; $QryFleet .= "`fleet_start_system` = ". $planet['system'] ." AND "; $QryFleet .= "`fleet_start_planet` = ". $planet['planet'] ." AND "; $QryFleet .= "`fleet_start_type` = ". $planet['planet_type'] ." "; $QryFleet .= ") OR ( "; $QryFleet .= "`fleet_end_galaxy` = ". $planet['galaxy'] ." AND "; $QryFleet .= "`fleet_end_system` = ". $planet['system'] ." AND "; $QryFleet .= "`fleet_end_planet` = ". $planet['planet'] ." ) AND "; $QryFleet .= "`fleet_end_type`= ". $planet['planet_type'] ." ) AND "; $QryFleet .= "( `fleet_start_time` < '". time() ."' OR `fleet_end_time` < '". time() ."' );"; $fleetquery = doquery( $QryFleet, 'fleets' ); //根據艦隊活動的目標不同,分別進入不同的函數處理 while ($CurrentFleet = mysql_fetch_array($fleetquery)) { switch ($CurrentFleet["fleet_mission"]) { case 1: // 普通攻擊 MissionCaseAttack ( $CurrentFleet ); break; case 2: // 這裡應該是ACS攻擊或者是其他攻擊,但是現在沒有用 doquery ("DELETE FROM {{table}} WHERE `fleet_id` = '". $CurrentFleet['fleet_id'] ."';", 'fleets'); break; case 3: // 運輸 MissionCaseTransport ( $CurrentFleet ); break; case 4: // 派遣 MissionCaseStay ( $CurrentFleet ); break; case 5: // 聯合派遣,即ACS防御 MissionCaseStayAlly ( $CurrentFleet ); break; case 6: // 偵查 MissionCaseSpy ( $CurrentFleet ); break; case 7: // 殖民 MissionCaseColonisation ( $CurrentFleet ); break; case 8: // 回收 MissionCaseRecycling ( $CurrentFleet ); break; case 9: // 毀月,厲害了 MissionCaseDestruction ( $CurrentFleet ); break; case 10: // 保留 !! break; case 15: // 遠征、探險 MissionCaseExpedition ( $CurrentFleet ); break; //其他情況刪除艦隊,這個是好習慣 default: { doquery("DELETE FROM {{table}} WHERE `fleet_id` = '". $CurrentFleet['fleet_id'] ."';", 'fleets'); } } } //解鎖表 doquery("UNLOCK TABLES", "");
上面的函數結構清晰,代碼明了,注釋也講的很清楚了。