


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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c
