Link to home
Start Free TrialLog in
Avatar of Edwat
Edwat

asked on

PHP Development for Multiplayer Game

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.
Avatar of kyleb84
kyleb84
Flag of Australia image

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.
If it is a browser game, PHP would probably do the job.
Avatar of Edwat
Edwat

ASKER

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.
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.
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.
player:
player_id,player_name,current_room,current_score
1,'joe',1,0   # in room 1, has 0 points
2,'eve',2,10  # in room 2, has 10 points
 
room:
room_id,room_name,room_description
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'
 
action:
room_id,action_no,action_name
1,1,'Enter cave'
2,1,'Leave cave'
2,2,'Shout'
 
action_effect:
room_id,action_no,effect
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"'
 
object:
object_id,object_description,lifetime_seconds
1,'torch',120
2,'knife',NULL
3,'small sword',NULL
4,'leather shield',NULL
5,'wooden shield',NULL
 
weapon:
object_id,durability,attack_strength,defence_strength
2,10,10,8
3,100,100,80
4,100,0,100
5,200,0,200
 
owned_object:
player_id,object_id,pickup_time,state
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%
 
room_objects:
room_id,object_id,singleton,dropped_by_player,dropped_time
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

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.

-john
Avatar of Edwat

ASKER

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.
SOLUTION
Avatar of Roger Baklund
Roger Baklund
Flag of Norway image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.

-john
Avatar of Edwat

ASKER

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.