Home  >  Article  >  Backend Development  >  Xnova (ogame) source code interpretation for PHP web game learning (6)_PHP tutorial

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

WBOY
WBOYOriginal
2016-07-13 10:24:19727browse

9. Public code (common.php)

Common.php is the public code of Xnova and will be executed almost every time it is clicked. Therefore, if it is optimized here, the efficiency of the entire program will be improved; of course, other codes called by this file must also be optimized. .

$game_config  = array();
$user     = array();
$lang     = array();
$link     = "";
$IsUserChecked = false; 

The variable $game_config stores the parameter data of the entire game. The database must be read every time and can be optimized. The optimization method is such as saving it in a file; the variable $user stores the logged-in user data; $lang stores the local language data; $ IsUserChecked records whether the user has been checked. Then set some system constants, which will be used in future codes; then include some function files, including the small functions we introduced before, etc.


Next, we need to use a constant INSTALL, which is defined during installation; except during installation, this judgment will be entered. After entering, some files are included. The includes/vars.php file is very important. I will explain it specifically and continue now. Here we also need to obtain data from the database and fill in the variable $game_config.

if ($InLogin != true) {
 $Result    = CheckTheUser ( $IsUserChecked );
 $IsUserChecked = $Result['state'];
 $user     = $Result['record'];
} elseif ($InLogin == false) {
 if( $game_config['game_disable']) {
 if ($user['authlevel'] < 1) {
  message ( stripslashes ( $game_config['close_reason'] ), $game_config['game_name'] );
 }
 }
}

The above is to check the user login data stored in the cookie. When $InLogin = true, it will not be executed; now we are talking about the daily operation of the user, so the value of $InLogin here is false. First, the function CheckTheUser() is called, and the value of the parameter $IsUserChecked is false; CheckTheUser() is declared in the includes/functions/CheckUser.php file, and it calls the CheckCookies() function declared in includes/functions/CheckCookies.php. The function of the function CheckCookies() is to use the information in Cookies to check the user. I will not list the code.

If CheckTheUser() succeeds, the variable $user will be filled with the user's data, and then the following branch will be executed. This branch checks the server parameter settings. If the server is set to be shut down by the administrator, the user's permissions are checked; if the permissions are insufficient, an error message is displayed.


Then we have to deal with the fleet in user activities, which is divided into two sections of logic:

1. The current time is greater than the time for the fleet to arrive at its destination, and the logic of combat, transportation, etc. needs to be processed

2. The current time is greater than the time for the fleet to return to the starting point. The logic of fleet return after combat and transportation must also be processed

Each activity calls the function FlyingFleetHandler() for processing. We will analyze this function later. Now we only need to know the function. If users click frequently, this logic will be very resource-consuming, so it can also be optimized here, such as caching or not executing it every time, etc.

The fleet activities have been dealt with, but what about the missile activities? The next rak.php file is used to handle interstellar missiles. File functions include missile interception, the number of defenses destroyed by missiles, sending messages to the attacked party, etc. We will also skip the specific logic and analyze it later.

Then call the function SetSelectedPlanet() to obtain the planet the player was on last time. Note that the parameters of this function are passed by address. The function is declared in includes/functions/SetSellectPlanet.php. Next, two variable values ​​need to be obtained. $planetrow stores the data of the user's current planet; $galaxyrow stores the galaxy coordinate data of the current planet.

Finally, call the function CheckPlanetUsedFields() to update the usage space of the current planet. The same parameters are passed by address and are declared in includes/functions/CheckPlanetUsedFields.php.

The Common.php file has been analyzed. If you understand it, you should know that efficiency here is very important. I hope everyone can optimize it together, and the same goes for other parts.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/825511.htmlTechArticle9. Public code (common.php) Common.php is the public code of Xnova. Almost every click will be Execution, so if the optimization here is good, the efficiency of the entire program will be improved...
Statement:
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