Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How to create a online multiplayer games with mysql support using php ?

  1. Sep 27, 2006 #1
    I want to create a online multiplayer games with php but I don't know how to let the members know their opponents' move realtime ?
    Please help me !Thanks
     
  2. jcsd
  3. Sep 27, 2006 #2
    events or messages?
     
  4. Sep 27, 2006 #3

    -Job-

    User Avatar
    Science Advisor

    It won't be very efficient using PHP alone. You should have something on the client side like Javascript, Java, or, ideally, Flash. Flash and Java have socket support which is what you want, to avoid refreshing the page or querying the server every so often.
    Javascript doesn't support sockets, but with AJAX you can contact the server without refreshing the page, even if you'll probably find yourself querying the server on some time interval, such as every 4 seconds.
     
  5. Sep 28, 2006 #4
    I will use javascript and ajax also but I don't know hot to show the opponents' move to the user . For example , I create a multiplayer online chess, and how can I show the move of the opponent to the player ? Please provide me some source code .Thanks
     
  6. Sep 28, 2006 #5
    store the event of the move int a message queue.
     
  7. Sep 29, 2006 #6
    how ? please provide me the source code or the tutorial link so that I can learn .
     
  8. Sep 29, 2006 #7

    -Job-

    User Avatar
    Science Advisor

    There's three phases to what you want to accomplish:
    1. Set up the schema in MySQL:
    This means creating whatever tables you'll need to store your information. You should have a table for your users, an example is:
    Code (Text):

    Table 1: UserInformation
    ID | UserName        | FirstName | LastName | ... etc
    -------------------------------------------
    1  | Job             | Bobby     |  ....       etc
    2  | ngkamsengpeter  | ...       |  ....       etc
     
    Then you should create a table to store your user events. Here, for example, we'll store user messages:
    Code (Text):
    Table 2: Messages
    ID | FromUser | ToUser | Message   | HasBeenRead
    ------------------------------------------------
    1  | 1        | 2      | Hey there | False
    2  | 2        | 1      | Hello     | True
    The Column "hasbeenread" is used to identify which messages users have received. In the above table i received your "Hello" and sent you my "Hey There" which you are yet to receive.

    2. Set up the server side scripts (in your case PHP)
    login.php - You should have a login script which authenticates a user and sets a session variable around with his/her username.
    getmessages.php - This script retrieves any unread messages for the logged in user. It does this by using the following SQL statement:
    Finally getmessages.php prints the messages on the page with a format that enables javascript to easily parse (you could use XML, but for simple information exchange such as what we're dealing with it's alot more efficient to use some other format, such as <fromuser>~!~<message>***<fromuser>~!~<message> ... etc)

    3. Set up the page that will display the new messages as they come.
    This page will make use of Javascript and AJAX to load the getmessages.php script. Basically you need to setup XMLHTTP (the heart of AJAX) then create a function "GetMessages()" that uses your XMLHTTP object to load the getmessages.php script, parse its contents and present the messages to the user in a neat way.
    Now you can call your GetMessages() function whenever you want to get the latest messages. You can for example use javascript's SetInterval to check for new messages every X miliseconds.
     
    Last edited: Sep 29, 2006
  9. Sep 29, 2006 #8
    That means that I want to store every player moves in the sql database and retrieve it .If my games is very long , that will use a lot of sql database space . Is it any other way of doing this ?Or can you suggest other programming language which is more suitable ?
    Besides , if I want to create a 3D online games , what programming language should I use . Can I use Maya ?
     
  10. Sep 30, 2006 #9

    -Job-

    User Avatar
    Science Advisor

    Naturally you can delete the read messages as they are read. The biggest problem is not the database size but the traffic the game will generate through the AJAX calls. For making web based games you should use Java, Flash or Director. Director ihas a browser plugin from Macromedia (now Adobe) just like Flash but much more powerful (it supports 3D graphics).
    But in my opinion, if you want a 3D online multiplayer game, then you should drop the browser and just code it from the ground up. This way you can use much more powerful programs like 3DSMAX. The disadvantage is that you'll need to code both a game server and a game client (which is alot of fun nonetheless).
     
  11. Oct 2, 2006 #10
    Then , how to code the game server and game client ?
     
  12. Oct 3, 2006 #11

    -Job-

    User Avatar
    Science Advisor

    You would code your game server & client the same way you would any other application. You pick a port number for your server/client communication. For the server you build an app that listens on a port for connections. For the client you build an app that connects to your server.
    The server has to keep track of connections and route/broadcast messages to users. The key step is to have all this network communication in place with a nice interface so that your game isn't concerned with the underlying communication details. In Java, or any other Object Oriented language, i would build a class "CommManager" that exposes an interface with methods such as "Connect", "Send Message" etc. Finally you would build the game on top of this framework.
    I'm not really the best person to tell you about 3D graphics, i've used 3DSMax in the past only superficially.
     
    Last edited: Oct 3, 2006
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: How to create a online multiplayer games with mysql support using php ?
  1. Php & mysql search (Replies: 6)

  2. PHP Web-based game (Replies: 3)

Loading...