Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do browser based strategy games made in PHP work?

Tags:

php

mysql

i started to learn programming like a month ago. I already knew html and css, i thought i should learn PHP. I learned alot of it from from tutorials and books, now I am making mysql based websites for practice.

I always used to play browser based strategy games like travian when i was a kid. I was thinking about how those sites worked. I didnt have any problem till i realized that the game actually worked after you closed the browser. For example; you log in to your account and start a construction and log off. But even after you close the browser, game knows that in "x" amount of time it needs to update your data of that specific building.

can someone tell me how that works? is it something with php or MySQL or some other programming language? even if you can tell me what to search online, it would be enough.

like image 330
ayihan Avatar asked Aug 02 '13 07:08

ayihan


People also ask

Can you make games with PHP?

As you have seen that PHP interacts smoothly with the console, it is particularly easy to make text-based games with PHP. However, thanks to the work of Brian Wang and Markus Fischer, you can even create graphical games with PHP. Naturally it is much easier to create text-based games, so that is where we will start.

What is a browser based strategy game?

Browser based strategy games are easy to play from any computer with an Internet connection. You simply open the browser, navigate to the game, and log in to start. Gamers who prefer strategy games like it when their decisions mean something. From your loadout to your moves in the game, every choice you make contributes to the outcome.

What is the hardest part about creating multiplayer browser games?

The hardest part about creating multiplayer browser games is the networking. If you create a multiplayer game, you can’t have thousands of players all in one room, or even one server. I mean you could, but it’d likely be a horrible experience for everyone. You have to take into account network code, data transfer…in fact, let’s break it down.

What is the best framework for making HTML5 games?

There are a lot of frameworks out there for getting started with creating HTML5/Javascript games, but two of the most popular: Phaser.io: Possibly the most popular framework for beginners, Phaser.io allows you to develop games that target browser and mobile with Canvas and WebGL technology.

What is the best way to make a simple browser game?

Most simple browser games are created using HTML5/Javascript, so an engine framework is a great place to start. There are a lot of frameworks out there for getting started with creating HTML5/Javascript games, but two of the most popular:


3 Answers

Despite being someone who loves tackling steep learning curves, I would advise against trying jump into something that requires background processes until you have a bit more programming experience.

But either way, here's what you need to know:

Normal PHP Process

The way that PHP normally works is the following:

  1. User types a url into the browser and hits enter (or just clicks on a link)
  2. Request is sent to a bunch of servers and magically finds its way to the right web server (beyond scope of this answer)
  3. Server program like Apache or IIS listening on port 80 grabs the request
  4. Apache sees that there's a .php extension on the requested page
  5. Apache looks up if any processors have been assigned to .php and finds php.exe
  6. The requested page is fed into php.exe
  7. php.exe starts up a new process for the specific user, runs everything on the script, returns its result
  8. The result is then sent back to the user
  9. When the user closes the browser and ends the "session", the process started by php exits

So the problem you encounter when you want something running in the background is that PHP in most cases is generally accessed through the web server, and hence usually requires a browser (and user making requests through the browser). And since closing the browser ends the process, so you need a way to run php scripts without a browser.

Luckily PHP can be accessed outside of just the webserver as a normal process on the server. But then the problem is that you have to access the server. You probably don't want your users to ssh into your server in order to manually run scripts (and I'm assuming you don't want to do it manually on behalf of your users every single time either). Hence you have the options either creating cronjobs that will automatically execute a command at a specific frequency as if you had typed it in yourself on your server's commandline. Another option is to manually start a script once that doesn't shutdown unless your server shuts down.

Triggering a Script based on Time:

Cron that is a task scheduler on *nix systems and Windows Task Scheduler on Windows. What you can do is set up a cronjob to run a specific php file at a specific frequency, and execute all the "background" tasks you need to run from within there.

One way of doing this would be to have a mysql table containing things that need to be executed along with when they need to be executed. The script then queries the table based on time to retrieve which tasks need to be executed, executes them, and then marks them executed (or just deletes them) in the mysql table.

This is a basic form of process queuing.

Building a Queue Server

This is a lot more advanced, but here's a tutorial for creating a script that will queue processes in the background without the need for any external databases: Building a Queue Server in PHP .

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

like image 131
asifrc Avatar answered Oct 17 '22 08:10

asifrc


PHP is a server side language. Any time anybody accesses a PHP program on the server, it runs, irrespective of who is a client.

So, imagine a program that holds a counter. It stores this in a database. Every time updatecounter.php is called, the counter gets updated by one.

You browse to updatecounter.php, and it tells you that the counter is now at 34.

Next time you browse to updatecounter.php it tells you that the counter is at 53.

Its gone up by 18 more counts than you were expecting.

This is because updatecounter.php was being run without your intervention. It was being run by other people.

Now, if you looked at updatecounter.php, you might see code like this:

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

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

Also, notice that instead of calling increment_counter_value, you could call anything. So every time somebody browsed to updatecounter.php, or whatever your game would be called, the internal game mechanics could be run. You could for instance, have an hourly stat management routine which would check each time it was called if it had been run in the last hour, and if it hadn't it would perform all the stats.

Now, what if nobody else is playing your game? If that happens, then the hourly stat management wouldn't get called, and your game world would die. So what you would need to do is create another program who's sole function is to run your stats. You would then schedule that program on the server to run at an hourly interval. You do this using something called a CRON job. You will probably find that your host already has this facility built in, if you are on Apache. I won't go into any more detail about task scheduling as without knowing your environment its impossible to give the correct answer. But basically, you would need to schedule a PHP program to run on the server to perform the hourly maintenance.

Here's a tutorial on CRON jobs:

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

I haven't used it myself but I've had no problems with other stuff on tutsplus so you should be ok.

like image 33
Relaxing In Cyprus Avatar answered Oct 17 '22 07:10

Relaxing In Cyprus


This is not only php . Browser based game are combination of php/mysql/javascript/html . There are lot of technologies being used for this kind of work. When you are doing something on the browser, lets say adding a building ,an ajax request is being sent to the server so the server updates the database (can't wait until logout because then other users won't know your status to play (in case of multiparty) .

like image 2
poojitha Avatar answered Oct 17 '22 08:10

poojitha