Home > Article > Backend Development > Xnova (ogame) source code interpretation for PHP web game learning (15)_PHP tutorial
18. Fleet activities (FlyingFleetHandler.php)
In the previous article, we have preliminary studied the source code of fleet activities and mentioned the triggering of fleet activities. Now I will further analyze the fleet activities in Xnova. These are essentially triggered by players and are not calculated by timers. Fleet activities are triggered in common.php, so the entry is in this file, and the code is as follows:
//检查是否有到达目的地的舰队 $_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); }
As you can see, the previous code is actually not well written, because it will be called every time, and the efficiency is too low. Therefore, we can optimize here so that each loop only needs to pass the ID of a fleet to the function; the corresponding function also needs to be modified. If you are interested, modify it yourself.
Next look at the function FlyingFleetHandler(), which is a function that centrally calls fleet activities.
//锁表,防止出现数据不同步等问题 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", "");
The above function structure is clear, the code is clear, and the comments are also very clear.