|
十八、艦隊活動(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", "");
上面的函數結構清晰,代碼明了,注釋也講的很清楚了。
php技術:PHP網頁游戲學習之Xnova(ogame)源碼解讀(十五),轉載需保留來源!
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。