How do browser strategy games work in PHP?

I started learning programming like a month ago. I already knew html and css, I thought I should learn PHP. I learned a lot from textbooks and books, now I make mysql-based sites for practice.

I always played browser-based games like travian when I was a kid. I was thinking about how these sites work. I had no problems until I realized that the game really worked after you closed the browser. For instance; You log in to your account and begin building and logging out. But even after you close the browser, the game knows that in “x” time it is required to update the data of this particular building.

can someone tell me how it works? is it something with php or mysql or another programming language? even if you can tell me what to search on the Internet, that will be enough.

+4
source share
3 answers

PHP is a server language. Each time someone accesses a PHP program on the server, it starts, regardless of who the client is.

So, imagine a program containing a counter. It stores this in a database. Each time updatecounter.php is called, the counter is updated one.

You are viewing updatecounter.php and it tells you that the counter is now 34.

The next time you go to updatecounter.php, it will tell you that the counter is 53.

He grew 18 more than expected.

This is because updatecounter.php is launched without your intervention. It was ruled by other people.

Now, if you looked at updatecounter.php, you can see the following code:

require_once("my_code.php); $counterValue = increment_counter_value(); echo "New Counter Value = ".$counterValue; 

Please note that the main core of the program is stored in a separate program than the program you are calling.

Also, note that instead of calling increment_counter_value, you can call anything. Therefore, every time someone looked at updatecounter.php or whatever game you called, you could launch an internal game. For example, you can have an hourly charter management procedure that will check every time it is called, if it has been running in the last hour, and if it has not performed all the statistics.

Now, what if no one is playing your game? If this happens, then the hourly status management will not be called, and your game world will die. So, what you need to do is create another program whose only function is to run your statistics. Then you plan to run this program on the server at hourly intervals. You do this using something called CRON . You will probably find that your host is already built into this object if you are in Apache. I will no longer go into details about scheduling tasks, not knowing that your environment cannot give the correct answer. But basically, you need to plan to run the PHP program on the server to do hourly maintenance.

Here is a CRON assignment tutorial:

http://net.tutsplus.com/tutorials/other/scheduling-tasks-with-cron-jobs/

I did not use it myself, but I had no problems with other things on tutsplus, so you should be fine.

+3
source

Despite the fact that someone likes to practice steep learning curves, I would advise you not to try to jump into something that requires background processes until you have a little more programming experience.

But anyway, here is what you need to know:

Normal PHP process

PHP usually works:

  • The user enters the URL into the browser and gets into it (or simply clicks on the link)
  • The request is sent to a bunch of servers and magically finds its way to the correct web server (outside the scope of this answer).
  • A server program such as Apache or IIS listening on port 80 captures the request
  • Apache sees that the requested page has a .php extension
  • Apache searches if any processors have been assigned to .php and finds php.exe
  • The requested page is loaded into php.exe
  • php.exe starts a new process for a specific user, runs everything on a script, returns its result
  • Then the result is returned to the user.
  • When the user closes the browser and ends the "session", the process starts php output

So the problem that you encounter when you want to work in the background is that PHP is usually accessible through a web server in most cases, and usually requires a browser (and the user makes requests through the browser). And after closing the browser, the process ends, so you need a way to run php scripts without a browser.

Fortunately, PHP can be accessed outside the web server as a normal process on the server. But then the problem is that you need to access the server. You probably don't want your users to upload ssh to your server to run scripts manually (and I assume that you don't want to do this manually on behalf of your users every time). Therefore, you have options for creating cronjobs that will automatically execute a command at a specific frequency, as if you typed it yourself on the command line of your server. Another option is to manually run the script once, which will not be completed if your server does not shut down.

Running a script based on time:

Cron , which is the task scheduler on * nix systems and the Windows Task Scheduler on Windows. What you can do is configure cronjob to run a specific php file at a certain frequency and complete all the "background" tasks that you need to complete from the inside.

One way to do this would be to have a mysql table containing things that need to be done along with them when they need to be done. The script then queries the table based on time to find out what tasks need to be completed, execute them, and then mark them as completed (or just deleting them) in the mysql table.

This is the basic form of a process queue.

Build a Queue Server

This is much more advanced, but here is a tutorial on creating a script that will queue in the background without the need for any external databases: Creating a queue server in PHP .

Let me know if that makes sense or you have any questions :)

+3
source

This is not just php. The browser game is a combination of php / mysql / javascript / html. A lot of technologies are used for this kind of work. When you do something in the browser, say, adding a building, an ajax request is sent to the server so that the server updates the database (I can not wait for logout, because then other users will not know your status for playback (in case of multiparty) .

+1
source

Source: https://habr.com/ru/post/1494905/


All Articles