- #1
ngkamsengpeter
- 195
- 0
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
Please help me !Thanks
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-Job- said: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.
how ? please provide me the source code or the tutorial link so that I can learn .neurocomp2003 said:store the event of the move int a message queue.
Table 1: UserInformation
ID | UserName | FirstName | LastName | ... etc
-------------------------------------------
1 | Job | Bobby | ... etc
2 | ngkamsengpeter | ... | ... etc
Table 2: Messages
ID | FromUser | ToUser | Message | HasBeenRead
------------------------------------------------
1 | 1 | 2 | Hey there | False
2 | 2 | 1 | Hello | True
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 a lot more efficient to use some other format, such as <fromuser>~!~<message>***<fromuser>~!~<message> ... etc)SELECT t2.UserName, t1.Message FROM Messages AS t1
JOIN UserInformation AS t2
ON (t2.FromUser = t1.ID)
WHERE t2.ToUser = <Logged in user's id here> AND t1.HasBeenRead = False
-Job- said: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: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: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 a lot 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.
Then , how to code the game server and game client ?-Job- said: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 a lot of fun nonetheless).
In order to set up a MySQL database for your online multiplayer game, you will first need to have access to a MySQL server. You can either set up your own server or use a hosting service. Once you have access to a server, you can use a tool such as phpMyAdmin to create a new database and set up your tables and fields.
PHP is a server-side scripting language that is commonly used in web development. In the context of creating an online multiplayer game with MySQL support, PHP is used to handle the server-side logic and communicate with the MySQL database. This includes tasks such as user authentication, storing and retrieving game data, and handling multiplayer interactions.
To ensure the security of your MySQL database for your online multiplayer game, there are a few steps you can take. First, make sure to use secure passwords for your database and regularly update them. Additionally, you can use encryption methods to protect sensitive data such as user passwords. It is also important to regularly back up your database to prevent data loss.
When designing a database for an online multiplayer game, there are a few key considerations to keep in mind. First, determine the types of data that will need to be stored, such as user profiles, game data, and chat logs. Then, plan out the structure of your tables and fields to efficiently store and retrieve this data. It is also important to consider scalability and potential future updates to your game.
Yes, you can use other databases instead of MySQL for your online multiplayer game with PHP. Some other popular options include PostgreSQL, MongoDB, and SQLite. However, keep in mind that the specific functions and syntax may vary depending on the database you choose, so you may need to make adjustments to your code accordingly.