9. Public code ()
It is Xnova's public code, which will be executed almost every click, so if the optimization is good, the efficiency of the entire program will be improved; of course, other codes called in this file must also be optimized together.
$game_config = array(); $user = array(); $lang = array(); $link = ""; $IsUserChecked = false;
The variable $game_config stores the parameter data of the entire game. You have to read the database every time. You can optimize it. The optimization method is for example saved in a file; the variable $user stores logged in user data; $lang stores local language data; $IsUserChecked records whether checked has been made by the user. Then set some system constants, which will be used in future code; then include some function files, including small functions we have introduced before, etc.
Next, you need to use a constant INSTALL, which is defined during installation; except for installation, this judgment will be entered. After entering, include some files, including/files are very important, I will explain them specifically, and continue now. Here we also need to obtain data from the database and populate 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. It will not be executed when $InLogin = true; now we are talking about the user's daily operations, so the value of $InLogin is false here. First, the function CheckTheUser() is called, and the value of the parameter $IsUserChecked is false; CheckTheUser() is declared in include/functions/ file, and it calls the CheckCookies() function declared in include/functions/. The function CheckCookies() function is to use the information in the cookie to check the user, and I will not list the code.
If CheckTheUser() succeeds, then the variable $user will be filled with the user's data and then the following branch is executed. This branch checks the server parameter settings. If the server is set to off by the administrator, checks the user's permissions; if the permissions are not enough, an error message is displayed.
Then we have to deal with the fleet in user activities, which is divided into two logics:
1. The current time is greater than the time when the fleet arrives at its destination, and it is necessary to deal with logic such as combat and transportation.
2. The current time is greater than the time when the fleet returns to the starting point. The fleet return logic after the battle and after transportation must also be handled.
Each activity calls the function FlyingFleetHandler() to handle it. We will analyze this function later. Now we only need to know the function. If the user clicks frequently, this logic will be very resource-consuming, so it can also be optimized here, such as cache or not executed every time, etc.
The fleet activities have been processed, so what should we do with the missile activities? The next file is used to deal with interstellar missiles. The file functions include interception of missiles, the number of defenses destroyed by missiles, sending messages to the attacked parties, etc. We will skip the specific logic and analyze it later.
Then call the function SetSelectedPlanet() to get the planet where the player was last located. Note that the parameters of this function are addressed and the function declaration is in include/functions/. The next thing to get two variable values is, $planetrow stores the data of the user's current planet; $galaxyrow stores the galaxy coordinate data of the current planet.
Finally, the function CheckPlanetUsedFields() is called to update the usage space of the current planet. The same parameter is passed and declared in include/functions/.
The file analysis is completed. If you understand it, you should know that the efficiency here is very important; I hope everyone will optimize it together, and the other parts are the same.