HOME       >>       Programming

8ennett's Text-based Mmorpg: Part 5


8ennett

Before starting on this tutorial be sure to run through the first 4.

 

Part 1: Advanced User Account System

Part 2: Welcome Home

Part 3: Administrative

Part 4: Profiles

 

########################

Part 5: Messaging & Inventory

########################

 

Download Example5.rar

 

Now this tutorial is going to cover quite a lot. I thought I'd best pick up the pace so you can get learning in larger chunks now you are starting to understand the code. In this tutorial we are going to introduce a few new things, such as cron jobs and javascript. Also you will notice I have changed the design of the example site. Here is a list of all the features added in this next tutorial.

 

Messaging System

 

Send, receive and reply to messages

Turn inbox on/off

Accept messages from friends only or all players

Mark read messages as unread

Delete messages

Inventory System

 

Player inventory manager, including equipping, selling and sending items

Admin page for creating new/editing exisiting items and their attributes and uploading images

Admin panel for creating new items in a players inventory

Admin alerts and auto ban for same ip and password transfers

Livesearch for players to send items to

So first of all we are going to setup our cron jobs. A cron job is an action which runs every predefined amount of time. In this case we will be executing two php scripts, one every five minutes and the other once a day. Our five minute cron job will increase players health by 10% and energy by 5% if either of those is below the maximum. Our daily cron is our server maintenance.

 

First of all it turns off the server, logging everyone out so it can run through quickly without causing drag on the server and offers an equal chance to all players for our dailies (which we haven't got any of yet). Next it decides randomly how much strength and defense to take off each player. As with real life, players will need to maintain their training (in the gym we haven't built yet) otherwise their stats will go down. This is another form of evening the playing field. It would be unfair if someone who was new to playing going against a player who hasn't been on in a long time but has started playing again.

 

After this we delete all the item prices for yesterday and change the current days prices to 2 which means yesterday. Then we can loop through and create the new prices for the new day for each item. It creates the new price by creating a random figure between 90% and 110% of the items average price. So if you create an item with an average price of $100, then the new days price would be between $90 and $110. This helps to prevent the market from getting stagnant and encourages players to buy large quantities of goods and hold them until they can sell them again for a profit.

 

Next up, the cron job we switch the server back on so people can log back in again and start playing the new day.

 

We will need to put our crons in a place where they can't be accessed by anyone who visits your site, so putting them in your root directory in a file named crons so you can simply type in http://forums.xisto.com/no_longer_exists/ to refill your health wouldn't be advisable. Instead put them outside your public_html directory (also htdocs, www depending on your server). So create a new folder outside your public_html folder named crons and place the day.php and five.php documents in the archive in to this directory. Next edit line 3 in five.php and lines 3 and 43 in day.php to point towards your config file. So on an Xisto server you would change this to '../public_html/lib/config.php'.

 

I'll show you how to get the cron running on cPanel X on Xisto. First of all you need to open the cron jobs section and create two new crons. Here is how each should be configured:

 

Daily Cron

Minute = 0

Hour = 0

Day = *

Month = *

Weekday = *

Command: php /home/mysite/Crons/day.php

Five Minute Cron

Minute = */5

Hour = *

Day = *

Month = *

Weekday = *

Command: php /home/mysite/Crons/five.php

Each of these crons should now run both daily and every five minutes. You can try them out by editing the database to reduce your health and energy and watch them increase every 5th minute.

 

Next up we have our messaging system. For this we created a new folder in the modules directory called inbox and also inserted a new menu item for the inbox as well. We have also created a new feature for a menu item where if the user has unread email in his/her inbox then it will be displayed next to the link as shown below.

 

This image shows the inbox displaying messages in ascending order from the date they were received, however I have changed this to descending so the newest is on the top of the list. So we have our list of messages in our inbox. We are able to open these messages by clicking the subject or delete these messages by clicking on the X next to them. We can also create a new message, there is another button for turning off/on our inbox and choose who we accept messages from. Click the "Accept Messages From Anyone" button will change your inbox settings and the button will change to "Accept Messages From Friends Only".

Below is what is displayed if we click on a messages subject.

So here we have a display of our message. The date and time, a clickable link with the senders name, the subject, the messages content (with full bbcode processing) and our message controls. Clicking the "Mark as Unread" button will take us back to the inbox and obviously mark our message as unread. "Delete" deletes it and etc. Now if we click on the reply button this will take us to the same page as the "Compose New" button in the inbox however the users id field has already been filled in.

 

Also, there is a new button on the user profile page called "Send Message" which also fills in the user id field. So here we have our message creation system. We can write our bbcode as well as our message in to the message field. Now our subject field is fairly interesting, if we are replying to a message it will add "Re:" however if the message subject already contains "Re:" or any variation it will remove these first then add its own "Re:" to prevent message subjects from becoming something like "Re: Re: Re: Re: Just another example".

 

Here we have our events list found on the menu. This is subject to the same tests as the inbox link where a box is displayed with the amount of new events eg. [5 new]. So when ever a user has had some interaction with your account we can send the user an event alert to let them know what it is. Now as you can see above we also have a clear events button. This will delete any and all events the user has had. Like the emails however, this will not actually delete them but instead set the status to deleted so admins are able to view everything before it is deleted from the server. We haven't written in to the cron to delete messages or events over a a certain amount of time, I thought I needed to break this process up a bit as this is quite an extensive part of the tutorial already. Another feature of the events list is listing our events in pages. If there are more than 10 events then a button will be displayed saying next page, this takes us back to this page but with the $_GET['page'] variable set. If this variable is more than 1 then a previous page button is displayed.

 

Next we can move on to the inventory system.

 

So here we have five different types of items. Weapon, Armour, Consumable, Drug and Other. Each item has its own options (although drugs and other are the same for now) which for now are hard coded in to the inventory.php file in the modules directory. Each item has several values in the database numbered 1-5. These will have different effects depending on the item type but this will be explained in the admin item editor later. So you can see we can equip our weapons and armour which will place them in the equipped armour and weapon slots at the top. We can also unequip them. Another thing we have to remember is if we have an item equipped but then we sell it, we will then have to unequip that item as well. Clicking each items image will open a pop-up window containing all the information about the item except current and previous prices.

 

So the values listed in the database each have their own property for a consumable item as show above. It also displays the average price of the item, although the current price could be anywhere from 90% to 110% of this items average price. We may later want to create items that get us out of hospital faster, prison or some other thing but this will be covered in another tutorial.

 

So if we choose to sell an item we will see this.

 

So like we covered earlier, you can see todays price and yesterdays price with arrows letting us know the difference more easily. Ok moving on to sending an item to a person.

 

So here we have our live search example. By entering a full or partial users name in the input field a list of results (up to 10 max) will be displayed underneath. Clicking one of these names will complete the input field allowing you to send the item.

 

This will also to check to make sure the users inventory is not full. Another check that is performed during the transfer is wether or not the user sending the item has the same last ip as the user receiving the item. If so we then check to see if both user accounts use the same password. If not then it is not likely the user accounts are owned by the same user and so an alert is put in to the admin inbox (both users might just be on the same network) and then the admin can decide if they want to ban the accounts or not. If the passwords do match then the accounts are both automatically disabled for 6 months and the user will be logged out. An alert will also be sent to the admin inbox (which we are yet to build) if this happens.

 

So that is the basics of our inventory system. Next we need a way to create and edit items.

 

Now we have our new game editors button on the admin panel and two new editors. We can either edit and create new items or send created items to different users.

 

So as stated earlier, when we are creating a new item and change the item type, it also changes the values 1-5 input fields using javascript.

 

Also the new item will not be created unless a valid image has been selected to upload as well. Now I have included five items to get you started, also I have given you a template item image which is just the green background. You can find it in images/inventory/template.png. Now when we select an item to edit we see the below screen.

 

http://forums.xisto.com/no_longer_exists/404.png

Changing the items type will change the fields the same as creating a new item. You can also choose to delete the item or upload a new image. If you make changes to the item but do not select a new image then the old one will stay the same. Next we have the send item option. This merely sends whatever item you choose to a user and inputs an alert in to that users events.

 

http://forums.xisto.com/no_longer_exists/404.png

There is also another link on a users profile for admins sending items to users and automatically fills in the user id field using the $_GET variable. The admin control panel will appear on a users profile if you are an admin or a super admin (obviously).

 

So I think that's about all covered in this next part. As usual leave any comments or questions. Also sorry about having to post some of the images links instead of images but the forum won't allow too many images.


8ennett

I've updated the archive to include an installer for the website. the index.php page will first test to see if install.php exists, if it does then it will redirect the page to there instead of the login page or what ever. Here is how the first part of the installer will probably look for you.

 

Now as you can see it has tested all the relevant directories and files to make sure they have the proper permissions set. If any of the directories or files do not have the proper permissions then parts of the site may not work properly (including the installer) so be sure to chmod everything it tells you to. Now I know a lot of people out there are really shirty when it comes to setting directories to 777 and so on, but I'm confident that on the basic users side of this game all security holes have been sealed and manipulation of the open directories is impossible. Once you have gone through and changed all the permissions you will then see the information input page like below.

 

So here you will need to enter all your database information. If you are unsure about which port your database server is on then best just leave this as the default 3306, this is usually the right one anyway. Now you have entered all your database information we will need to enter a few details about the Super admin account which our installer also creates for us. Now this is just a quick version of the installer and will be more advanced and do more checks in the next tutorial, for now be careful what details you enter here. Basically try and use only letters and numbers for your name and email (except the @ and .com of course) and make sure your name is less then 15 characters in length. If you don't enter a matching password or no password entirely for the Super admin account, it will create the account anyway using the password 'password'.

 

So if you enter all the details correctly (not including admin password) and everything runs smoothly, you should hopefully see the page below.

 

Now the MySQL Connection refers to if it can make a succesful connection to the sql server using the information you have supplied. If so then success, if not then failed. Database connection is the same thing except for the database (obviously). Next we attempt to create all the tables we need for the game in the database. This query string has already been hard coded in to the install.php file so table.sql is no longer going to be supplied in the archive. If there is a problem with creating any of the tables then it would say failed and a white box will appear underneath the failed line with information about the error. Next it checks the length of the admin passwords entered are greater than 0 and they both match, if not then like I said before it will create the account with the password set as 'password' (without the speech marks of course). Then it moves on to create the admin account, open the lib/config.php file for editing, then write the new database values we entered in to the config.php file. Like it says above as well, you will need to delete the install.php file before you can start using the site as well, and also to prevent other people from using it as it is only meant as a temporary file.

 

Now assuming something goes wrong with the installation, you will see a page similar to this.

 

So it gives you a list of where the errors have occured and tells you to either re-run the setup or fix the problems manually. It also displays the information form with most of the fields already filled out with the $_POST data from your last attempt. This is just in case you mistyped something and can quickly change it. You can run the install as many times as you like, the only error it may throw back after running it the first time is the create tables line as it also tries to insert the new inventory items I created for you, and with duplicate id's that part will fail but it won't affect the rest of the code.

 

So there we have it, we also have a natty little installer for our new site which helps us to setup our example without tearing our hair out trying to figure what we did wrong.


spawN-eD

good job dude


8ennett

Thanks Wait until you see the forums i'm writing for it at the minute. Once the forums are finished though I might start work on building the site for the open-source project. Try and get a few more developers involved once it is at a easily modifiable stage. That way eventually this could become an entirely GUI based system which is highly customisable (basically able to edit everything including layout and appearance) and user-friendly. Maybe get a module database on the go with a seperate installer. Anyway, that's the hope lol


spawN-eD

heh nice dude keep working


8ennett

Finished part 6 yesterday but it's still waiting approval


spawN-eD

i have question for u how many parts will be and what will be in each part ?


8ennett

I'm considering making part 6 the last one. I've started building the site to make this tutorial in to an open-source project at Project Praetorian. Is it just the end result you are looking for or are you genuinely interested in learning more about programming your own game?


Frazzman

Looks amazing i really want this such a pitty the site is down i would die for this, looks awesome, would be very helpful and get me closer to the dream of making a game similar to movoda.net good luck with whatever your making and i hope you fufil my request lol



REGISTER GET FREE HOSTING

Xisto.com offers Free Web Hosting to its Members for their participation in this Community. We moderate all content posted here but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 2001-2019 by Xisto Corporation. All Rights Reserved.