Solved

PHP Development for Multiplayer Game

Posted on 2008-10-26
11
768 Views
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.
0
Comment
Question by:Edwat
  • 3
  • 2
  • 2
  • +3
11 Comments
 
LVL 10

Expert Comment

by:kyleb84
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.
0
 
LVL 6

Expert Comment

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

Author Comment

by:Edwat
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.
0
 
LVL 3

Expert Comment

by:DarkFish
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.
0
 
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.
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

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 11

Expert Comment

by:jgordos
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.

-john
0
 

Author Comment

by:Edwat
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.
0
 
LVL 39

Assisted Solution

by:Roger Baklund
Roger Baklund earned 200 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?
0
 
LVL 3

Accepted Solution

by:
DarkFish earned 300 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,
0
 
LVL 11

Expert Comment

by:jgordos
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.

-john
0
 

Author Comment

by:Edwat
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.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now