PHP Development for Multiplayer Game

Posted on 2008-10-26
Medium Priority
Last Modified: 2013-12-26
I'm planning to develop a multiplayer game that running on Linux server using PHP & MYSQL & Flash. Flash & PHP will be used for the front end, and I am thinking to use PHP as the back end or the game engine. Do you think I can use PHP as the backend / engine? It is a multiplayer game so the engine has to update and check players respond at least for every 5 secs. I am thinking to run a PHP script that doing loop every 5 secs as the engine.

Do you guys think I can rely on PHP to do the engine or I just use other method for the engine such as Java or shell scripting? I'm only familiar with PHP that's why I'm trying to use PHP for the engine. Please advise me about this matter, and if you guys have any solutions for this game engine let me know. I will appreciate it. Thanks.
Question by:Edwat
  • 3
  • 2
  • 2
  • +3
LVL 10

Expert Comment

ID: 22809892
Hmm I think Java would be more suitable for that task.

PHP isn't supposed to be the guts of a "game" engine, you might find it'll be quite a bit harder doing it that way.

Expert Comment

ID: 22810671
If it is a browser game, PHP would probably do the job.

Author Comment

ID: 22810880
the game will be accessed by web browser. that's why i'm thinking to do it in PHP. what do you think will be the good solution to run the PHP engine / backend and stable. the engine will keep running by doing loop to update the game status and get player respond.
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.


Expert Comment

ID: 22811173
It would honestly depend on exactly how your game operates. If it is a web browser text based game with "ticks" (e.g. intervals where a player gets a response after a certain amount of time) PHP would be a fine solution.

If the game is more immediate but still browser text based, then PHP should also be fine.

However, if your game is in real time and is using a Flash based (or similar) front end (for instance a live 3d or 2d game with other players) then PHP would probably not be suitable and your best bet would be another language such as Java.

Essentially what I'm trying to say is that PHP would probably be suitable for a multiplayer game if an "instant" server response was not required (for example in Flash, calculating a players exact position and streaming that back to other players.) PHP probably could do it, it just might be better done in another language.

Additionally, PHP probably would be suitable for something like a browser based text game where instant responses are required but you do not interact directly with other players, only objects or items that other players own; if you see what I mean.

PHP can be run as a socket server if you are looking down that route, however you would probably be better off - if you are looking for a client-server socket connection - using Java or another similar language.
LVL 39

Expert Comment

by:Roger Baklund
ID: 22811402
A typical game engine will run in a loop, maintain state and keep connections to all players. You can not easily do this with PHP and a web based game. PHP scripts are "short-lived", they start at the user request, and ends when the page is sent to the user. You can not run the PHP script in loop and have multiple users "talking" to the same script (unless the script is a socket server).

I think you need to store the state of the game in the database, and PHP code that will load state from the database and send to each user on request. Depending on the complexity of the game, you could send the complete state to the client, or you could send a part of it, as requested by the client. For example, if the game consists of multiple rooms, you could send the state of the room the current player is in, along with some global state variables (admin messages, player-player communication, "game over", count of current players and so on).

I don't know what kind of game you are planning, below are some ideas for a datamodel for a "typical" dungeon type of game, it's just a draft, not complete and not tested, just to give you some ideas.
1,'joe',1,0   # in room 1, has 0 points
2,'eve',2,10  # in room 2, has 10 points
1,'By the big cave','You are at the entrance of a big cave, bla-bla'
2,'Big cave','You are within a big cave, bla-bla'
1,1,'Enter cave'
2,1,'Leave cave'
1,1,'go:2'            # enter cave
1,2,'get:1,score:+10' # get the torch. 10 points
2,1,'go:1'            # leave cave
2,2,'public_msg:2:"<playername> is shouting",public_msg:1:"Someone is shouting inside the cave"'
3,'small sword',NULL
4,'leather shield',NULL
5,'wooden shield',NULL
1,1,10,<timestamp>,2    # player 1 torch is getting weak
1,3,100,<timestamp>,95  # player 1 small sword is at 95%
1,4,100,<timestamp>,90  # player 1 leather shield is at 90%
1,1,No,NULL,NULL       # a torch available for all players
2,2,Yes,1,<timestamp>  # player 1 has dropped his knife in the cave

Open in new window

LVL 11

Expert Comment

ID: 22813044
I am coming a bit late to the party, but I think it will probably help you to understand what it is you're really trying to do, in terms you're more familiar with.

So, think about how you intend to communicate with the engine...

The game is running in the browser...

It uses an HTTP/post or HTTP/get to send a message from the client to the server....

The server examines the message... and generates some sort of "response..."

The game in the browser reacts to the "response" and supplies the necessary feeback to the player...

Repeat this step, every time the user makes a move...

So, think of each game ACTION (the client talking to the server who responds) as a PAGE VIEW...

Your question, really, is HOW MANY PAGE VIEWS will my server be able to handle, before it falls on it's face?

That's an easier question for many beginners who are familliar with web design to get their head around.

So, if you intend to "poll" your game simulation, every five seconds let's say...  you now have a better feel for what your system load will be.


Author Comment

ID: 22818880
thanks guys for you comments. you guys are great for sharing this with me.

the game that i'm trying to develop is more like a multiplayer card game. and the reason i'm still thinking to use PHP as the engine because it doesnt need exactly to get instant response from the server.and i'm more familiar with PHP than Java so I would prefer to do the PHP if it's still acceptable. the engine will just need to update or broadcast the message every 5 to 10 secs interval database.

I know that I couldn't depend on client request to run the PHP script engine. what I did before is place a cron job to run the PHP script every minute and sleep for 30 secs to get the engine broadcast every 30 secs interval. but this time I need to get shorter interval. Do you guys any idea to have the server run the script stable. Someone advise me to do the shell script that run PHP script. Is this a good idea?

Thanks again.
LVL 39

Assisted Solution

by:Roger Baklund
Roger Baklund earned 600 total points
ID: 22819941
Will all the players be within a LAN? You can't broadcast over the internet. You would have to connect OUT from your system and to each player, this would require for them to open a port in their firewall, and the client software would have to act as a server (i.e. listen on  port). You say you can't depend on request from client to run the PHP script, why is that?

Accepted Solution

DarkFish earned 900 total points
ID: 22820489
A PHP script can be run without any human interaction using an SSH command such as "nohup"; you would have to account for this in your script by having a main application loop and using that loop to conduct your game logic.

It is possible for PHP to listen on a port and act like a socket server, though this might not be the best approach depending on how your front end is setup.

Because it is a card game it revolves around one player making one action per time; if I had to do this using PHP and Flash, I would have Flash send a request to a PHP script using a request object, that PHP script could either work out the logic and determine if the request was legal (or it could act as a proxy and forward the request onto a socket server and let the PHP socket server do the logic) and then return a positive or negative result to the client; though all the other clients would have to be polling the server every second or so for an update; or they would all have to setup a socket connection to the server and work that way; I'm not entirely sure if Flash can do socket connections without adjusting your firewall, but if it can, the socket server is probably the way to go.

Hope that helps,
LVL 11

Expert Comment

ID: 22832979
Actually, I'd do it as a "poll" from the client to the server....

Something like a GET request to "<servername>/giveMeTheLatest" and then check it to see if it's changed since the last time you saw it.

You can poll this easily with javascript... look at an AJAX request.


Author Comment

ID: 22840100
I think I would running the PHP engine within shell script or SSH. And the script will do looping and update the main database, all player will get any messages stored in database. thanks guys for your help. I appreciate it.

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question