search
HomeBackend DevelopmentPHP TutorialXnova (ogame) source code interpretation for PHP web game learning (6)_PHP tutorial

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
Optimize PHP Code: Reducing Memory Usage & Execution TimeOptimize PHP Code: Reducing Memory Usage & Execution TimeMay 10, 2025 am 12:04 AM

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft