Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 09-17-2005, 05:27 PM Help with many things
Skilled Talker

Posts: 64
Trades: 0
I'm creating my own text-based MMORPG, using PHP and MySQL. I've got quite a bit done, but I'm having problems creating weapons.

I want to be abble to allow users to buy weapons, then have them either equiped or in their inventory.

This is the shop (well, the bit you need to see):

PHP Code:
<h2>Gareths Weapon Shop</h2><br /><br /><br />Welcome to my weapon shopOnly the finest weapons are stocked here!<br />
    
Here are my wares:<br /><br />
    <
table cellspacing="0" cellpadding="0" border="0" align="center" width="95%">
                        <
tr>
                            <
td width="31%" height="30" align="left" valign="top">
                                <
b>Name</b>

                            </
td>
                            
                            
                            <
td width="17%" align="left" valign="top"><b>Cost</b></td>
                            <
td width="38%" height="30" align="center" valign="top">

                                <
b>Strength needed</b>
                            </
td>
                            <
td width="14%" height="30" align="left" valign="top">&nbsp;
                                
                            </
td>
                        </
tr>
                        <
tr><td valign="top" align="left" height="20">Stick</td>
                          <
td valign="top" align="center">100</td>
                        <
td valign="top" align="center">5</td><td valign="top" align="left">[<a href="weapons_processor.php">Buy</a>]</td></tr><tr><td valign="top" align="left" height="20">Rusty Knife</td>
                            <
td valign="top" align="center">250</td>
                          <
td valign="top" align="center">10</td><td valign="top" align="left">[<a href="weapons_processor.php?aome=2">Buy</a>]</td></tr><tr>
                          <
td valign="top" align="left" height="20">Wooden Club</td>
                          <
td valign="top" align="center">500</td>
                          <
td valign="top" align="center">15</td><td valign="top" align="left">[<a href="weapons_processor?name=3">Buy</a>]</td></tr><tr>
                            <
td valign="top" align="left" height="20">Rusty Short Sword</td>
                            <
td valign="top" align="center">1000</td>
                            <
td valign="top" align="center">25</td>
                        <
td valign="top" align="left">[<a href="weapons_processor.php?name=4">Buy</a>]</td></tr>        <tr>

                            <
td valign="top" align="left" colspan="6">
                                <
br><br>
                                [<
a href="village.php">Back</a>]
                            </
td>
                        </
tr>
                        </
table
So, this takes them to the weapons_processor.php page when they click a link. Then, it simply puts the weapon into the database, under their name, in unused_weapon1

I have a script that lets them "equip" this item, so that they can use it as their weapon:


PHP Code:
<?php $weapon_query "UPDATE login SET weapon='$unused_weapon' WHERE email = '{$_SESSION['logname']}'" or die ("You suck");
          
mysql_query($weapon_query)or die ("Your weapon 2 not updated"); 
          
$weapon_query2 "UPDATE login SET unused_weapon='' WHERE email = '{$_SESSION['logname']}'" or die ("You suck");
          
mysql_query($weapon_query2)or die ("Your weapon 2 not updated"); 
          
mysql_close();

echo 
"You've equiped your $unused_weapon";?>
However, I also need a script that allows them to un-equip this weapon. I made this

PHP Code:
<?php $weapon_query "UPDATE login SET unused_weapon='$weapon' WHERE email = '{$_SESSION['logname']}'" or die ("You suck");
          
mysql_query($weapon_query)or die ("Your weapon 2 not updated"); 
          
$weapon_query2 "UPDATE login SET weapon='' WHERE email = '{$_SESSION['logname']}'" or die ("You suck");
          
mysql_query($weapon_query2)or die ("Your weapon 2 not updated"); 
          
mysql_close();

echo 
"You've unequiped your $weapon";?>
However, if they click the wrong link, say they try to unequip nothing, then they lose their unequiped weapon. Is there a way to prevent this.


I also have the problem of multiple weapons. I want people to be able to have several weapons, but I've found problems. If they buy a weapon from the shop, then it places it into the unused_weapons1 column, but what if they want to buy two weapons? How can I make it so that it stores it into a different place?


I know it's complex, but that's why I need help. I need someone to make some of the code to point me in the right direction, and explain what it all does.


I'll be very grateful to anyone who can help me!

Last edited by Tomos; 09-17-2005 at 05:40 PM..
Tomos is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-17-2005, 05:58 PM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
I'd look at changing your database design a little, to take advantage of the relational nature of mySQL - at the moment you are storing weapons in one column of the user table which restricts you to one unused weapon as you said. Instead I would suggest having an ID number column in the users table which uniquely identifies them. Then you can have a weapon_types table with an ID number for each, a name, strength required, base damage dealt, etc. Then you can have a table to connect the two, each row having a user_id, a weapon_id, and a status flag such as 0 for unequipped, 1 for equipped etc. You can have multiple entries for one user so the table might look like
Code:
user_id weapon_id status
  1           3            1
  2           2            0
  2           7            1
  3           3            1
  3           1            1
  1           2            0
  3           2            0
  4           12           1
Meaning user 1 has a club and a short sword, of which the sword is equipped, user 2 has a club and a spear, with the spear equipped, user 3 has a club, an axe, and a short sword with the club equipped, and user 4 has an ultra-magitechno-heat-seeking-nuclear-mushroom-launcher equipped and no other weapons.

Buying from the shop involves adding new entries to the table, possibly checking the users strength is sufficient and that they are not carrying too much already, and equipping/unequipping involves changing the flags in the status column - this means the weapon won't become lost, the player just has nothing equipped. If a weapon is sold, lost or broken then you can delete it from the table.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)

Last edited by 0beron; 09-17-2005 at 06:05 PM..
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 09-17-2005, 06:13 PM
Skilled Talker

Posts: 64
Trades: 0
I'll try and decipher that tomorrow, when it's not past 11 pm. Thanks for the help! I hope it works...

So, how would I update it when a user buys something from the shop? What code do I need now?

I'll add my attempt at it, but I don't think I'll get it right....

Last edited by Tomos; 09-18-2005 at 06:28 AM.. Reason: Added question
Tomos is offline
Reply With Quote
View Public Profile
 
Old 09-18-2005, 07:13 AM
Skilled Talker

Posts: 62
Trades: 0
As 0beron mentioned, you'd be looking at a table which joins weapons to users - here's a basic layout of relevant tables and columns:

Users Table
user_id

Weapons Table
weapon_id
weapon_name
weapon_strength

Weapons_to_Users Table
link_id (INT, primary key, auto_increment - included for stability and table info queries)
user_id (INT - corresponds to Users Table)
weapon_id (INT - corresponds to Weapons Table)
weapon_enabled (TINYINT - As 0beron suggested, denotes whether weapon is active in binary)

Sample Code to Add a Weapon to user's inventory:
Code:
INSERT INTO weapons_to_users SET user_id='$user_id', weapon_id='$purchased_weapon_id'
Disable old weapon and enable new weapon in inventory - a three-step process:
Code:
UPDATE weapons_to_users SET weapon_enabled='0' WHERE user_id='$user_id'
Code:
SELECT link_id AS weapon_link_id FROM weapons_to_users WHERE user_id='$user_id' AND weapon_id='$enabled_weapon_id'
Code:
UPDATE weapons_to_users SET weapon_enabled='1' WHERE link_id='$last_query['weapon_link_id']'
Remove a weapon from a user's inventory (by quantity) - a two-step process:
Code:
SELECT link_id AS weapon_link_id FROM weapons_to_users WHERE user_id='$user_id' AND weapon_id='$dropped_weapon_id' LIMIT $quantity_to_drop
Code:
DELETE FROM weapons_to_users WHERE link_id='$last_query['link_id']
(looped until done)

Benefits:

The weapons_to_users link table would allow your users to carry several of the same weapon - additionally, you could add things like a weapon_quality or weapon_modifiers column to the weapons_to_users table, effectively allowing you to create damaged or modified items for your users to carry.
__________________

Please login or register to view this content. Registration is FREE
danlefree is offline
Reply With Quote
View Public Profile Visit danlefree's homepage!
 
Old 09-18-2005, 07:18 AM
Skilled Talker

Posts: 64
Trades: 0
How do I get the weapons id to be $enabled_weapon_id. You've really confused me here.

Last edited by Tomos; 09-18-2005 at 07:32 AM..
Tomos is offline
Reply With Quote
View Public Profile
 
Old 09-18-2005, 10:50 AM
Skilled Talker

Posts: 64
Trades: 0
Ok, ignore last post. My problem is displaying a user his or her weapons. I assume I'll need to use a loop, so that it can display all of their weapons, and wether they're unequiped. Any ideas how to do this?
Tomos is offline
Reply With Quote
View Public Profile
 
Old 10-30-2005, 02:40 AM
Skilled Talker

Posts: 62
Trades: 0
Sorry about the delay in my response - life gets pretty crazy sometimes...

PHP Code:
<ul>
<?php
$user_weapons_query 
mysql_query("SELECT w2u.link_id, wpn.weapon_id, wpn.weapon_name  FROM weapons wpn, weapons_to_users w2u WHERE user_id='$user_id' ORDER BY w2u.weapon_enabled DESC");

while(
$user_weapons mysql_fetch_array($user_weapons_query)) {
?>
 <li><?php echo $user_weapons['weapon_name']; ?> - <?php if($user_weapons['weapon_enabled']=='1') { echo '<a href="' $_SERVER['PHP_SELF'] . '?link_id=' $user_weapons['link_id'] . '&enabled=0">Enabled</a>'; }else{ echo '<a href="' $_SERVER['PHP_SELF'] . '?link_id=' $user_weapons['link_id'] . '&enabled=1">Not Enabled</a>'; }; ?></li>
<?php
}
?>
</ul>
Grabbing the link_id so, if a change is requested, it can be implemented on the weapon - just add the block below if the user_id is set within a session variable. (This script chunk will reset the "enabled" status of all weapons belonging to the owner - assuming only one weapon can be enabled at a time - and then set the enabled / disabled status for the particular weapon selected)

PHP Code:
<?php
if(isset($_GET['enabled'])) {
mysql_query("UPDATE users_to_weapons SET weapon_enabled='0' WHERE user_id='" $_SESSION['user_id'] . "'");
mysql_query("UPDATE users_to_weapons SET weapon_enabled='" $_GET['enabled'] . "' WHERE link_id='" $_GET['link_id'] . "'");
}
?>
Note: On my fifth or sixth drink for the evening, so there will likely be bugs.
__________________

Please login or register to view this content. Registration is FREE
danlefree is offline
Reply With Quote
View Public Profile Visit danlefree's homepage!
 
Reply     « Reply to Problems
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.26588 seconds with 12 queries