Home Backend Development PHP Tutorial Xnova (ogame) source code interpretation for PHP web game learning (15)_PHP tutorial

Xnova (ogame) source code interpretation for PHP web game learning (15)_PHP tutorial

Jul 13, 2016 am 10:26 AM
php Source code interpretation Web games

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);
}
Copy after login

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", "");
Copy after login

The above function structure is clear, the code is clear, and the comments are also very clear.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/824740.htmlTechArticle18. Fleet Activities (FlyingFleetHandler.php) In the previous article, we have preliminary studied the source code of fleet activities and provided Have been triggered by fleet activities. Now I further analyze the fleet in Xnova...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

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

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

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

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

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

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

See all articles