Author Topic: Cards - Random Item  (Read 18164 times)

Offline inkstains

Cards - Random Item
« on: March 23, 2007, 12:33:20 am »
code updated 28-09-07

This is based on a idea by GhostWriter http://www.daniel15.com/forum/index.php/topic,703.0.html


I've tested as best i can but if you have any problems let me know also if any of the code can be done in a better way (especially the image stuff) please tell me as i'm still learning php also i think i would need some error catching on the remove credits code in case the user has no credits but i'm not sure how i would do that any recommendations would be appreciated.

save this file as Cards.php and upload to you shops item directory

Code: [Select]
<?php

if (!defined('SMF'))
die('Hacking attempt...');

class 
item_Cards extends itemTemplate
{
function getItemDetails()
{
$this->authorName 'Inkstains';
$this->authorWeb '';
$this->authorEmail 'inkstains@nurcharecords.com';

$this->name 'Cards';
$this->desc 'Try your luck and win. Karma, Credits and more up for grabs, but dont cry if you get burnt.';
$this->price 100;

$this->require_input false;
$this->can_use_item true;
$this->addInput_editable true;
}


function 
getAddInput()
{
global $item_info$db_prefix;
return '<br><b>You must assign a value to each card</b><br>
                        Ace/Joker Maximum amount to increase/decrease post count by:  <input type="text" name="info1" value="' 
$item_info[1] . '" /><br />
                        King/Joker Maximum amount to increase/decrease credits by:     <input type="text" name="info2" value="' 
$item_info[2] . '" /><br />
                        Queen/Joker Maximum amount to increase/decrease karma by:       <input type="text" name="info3" value="' 
$item_info[3] . '" /><br />
                        Jack/Joker Maximum amount to increase/decrease time online by: <input type="text" name="info4" value="' 
$item_info[4] . '" /><br />';
                }

function onUse()
{
global $db_prefix$ID_MEMBER$item_info$settings$context$boardurl$txt$scripturl;

                
$value mt_rand(18);

                if (
$value == 1) {

if ($item_info[1] ==  0)
                          {return 
"All cards must be assigned a value please edit item settings";
                                  die();}
                          else{          
$value1 mt_rand(0$item_info[1]);
                                       
$result db_query("UPDATE {$db_prefix}members SET posts = posts + {$value1} WHERE ID_MEMBER = {$ID_MEMBER}"__FILE____LINE__);
return "<img src='".$boardurl."/Sources/shop/item_images/Cards_ace_d.gif' align='texttop'/><br><br>You got an Ace and increased your post count by ". ($value1) ."";
}}
        elseif (
$value == 2) {
if ($item_info[2] ==  0)
                          {return 
"All cards must be assigned a value please edit item settings";
                                  die();}
                          else{          
$value1 mt_rand(0$item_info[2]);
                                       
$result db_query("UPDATE {$db_prefix}members SET money = money + {$value1} WHERE ID_MEMBER = {$ID_MEMBER}"__FILE____LINE__);
return "<img src='".$boardurl."/Sources/shop/item_images/Cards_king_d.gif' align='texttop'/><br><br>You got a King and gained "formatMoney($value1) ."";
}}
elseif ($value == 3) {
if ($item_info[3] ==  0)
                          {return 
"All cards must be assigned a value please edit item settings";
                                  die();}
                          else{          
$value1 mt_rand(0$item_info[3]);
                                       
$result db_query("UPDATE {$db_prefix}members SET karmaGood = karmaGood + {$value1} WHERE ID_MEMBER = {$ID_MEMBER}"__FILE____LINE__);
return "<img src='".$boardurl."/Sources/shop/item_images/Cards_queen_d.gif' align='texttop'/><br><br>You got a Queen and increased you karma by ". ($value1) ."";
}}
elseif ($value == 4) {
if ($item_info[4] ==  0)
                          {return 
"All cards must be assigned a value please edit item settings";
                                  die();}
                          else{          
$value1 mt_rand(0$item_info[4]);
                                       
$result db_query("UPDATE {$db_prefix}members SET totalTimeLoggedIn = totalTimeLoggedIn + {$value1} WHERE ID_MEMBER = {$ID_MEMBER}"__FILE____LINE__);
return "<img src='".$boardurl."/Sources/shop/item_images/Cards_jack_d.gif' align='texttop'/><br><br>You got a Jack and increased your time online by ". ($value1) ."";
}}
elseif ($value == 5) {
if ($item_info[1] ==  0)
                          {return 
"All cards must be assigned a value please edit item settings";
                                  die();}
                          else{   
$value1 mt_rand(0$item_info[1]);
                                       
$result db_query("UPDATE {$db_prefix}members SET posts = posts - {$value1} WHERE ID_MEMBER = {$ID_MEMBER}"__FILE____LINE__);
return "<img src='".$boardurl."/Sources/shop/item_images/Cards_joker_d.gif' align='texttop'/><br><br>You got a Joker!! and decreased your post count by ". ($value1) ."";
}}
        elseif (
$value == 6) {
        if (
$item_info[2] ==  0)
                          {return 
"All cards must be assigned a value please edit item settings";
                                  die();}
                          else{          
$value1 mt_rand(0$item_info[2]);
                                       
$result db_query("UPDATE {$db_prefix}members SET money = money - {$value1} WHERE ID_MEMBER = {$ID_MEMBER}"__FILE____LINE__);
return "<img src='".$boardurl."/Sources/shop/item_images/Cards_joker_d.gif' align='texttop'/><br><br>You got a Joker!! and lost "formatMoney($value1) ."";
}}
elseif ($value == 7) {
               if ($item_info[3] ==  0)
                          {return 
"All cards must be assigned a value please edit item settings";
                                  die();}
                          else{
         $value1 mt_rand(0$item_info[3]);
                                       
$result db_query("UPDATE {$db_prefix}members SET karmaBad = karmaBad + {$value1} WHERE ID_MEMBER = {$ID_MEMBER}"__FILE____LINE__);
return "<img src='".$boardurl."/Sources/shop/item_images/Cards_joker_d.gif' align='texttop'/><br><br>You got a Joker!! and decreased your karma by ". ($value1) ."";
}}
elseif ($value == 8) {
if ($item_info[4] ==  0)
                          {return 
"All cards must be assigned a value please edit item settings";
                                  die();}
                          else{          
$value1 mt_rand(0$item_info[4]);
                                       
$result db_query("UPDATE {$db_prefix}members SET totalTimeLoggedIn = totalTimeLoggedIn - {$value1} WHERE ID_MEMBER = {$ID_MEMBER}"__FILE____LINE__);
return "<img src='".$boardurl."/Sources/shop/item_images/Cards_joker_d.gif' align='texttop'/><br><br>You got a Joker!! and decreased your time online by ". ($value1) ."";
}}
        }
}
?>


I've included the images for this item as a zip just extract and upload them to your shops item_images folder all images have transparent backgrounds so they should be cool on any theme

http://www.nurcharecords.com/scrawl/Cards_images.zip


It would be cool to be able to do an interface for this where it shows the back of the card on getUseInput and when the user clicks on it it flips to reveal what they got. i don't know if that would be possible and is probably a little to advanced for me but does anyone know if it's possible?
« Last Edit: September 28, 2007, 12:26:19 pm by inkstains »

Offline inkstains

Re: Cards - Random Item
« Reply #1 on: March 23, 2007, 12:35:38 am »
also can anyone recommend some good php tutorial sites?

Offline Basil Beard

Re: Cards - Random Item
« Reply #2 on: March 23, 2007, 02:02:28 am »
Arrrrr!

Offline GhostWriter

Re: Cards - Random Item
« Reply #3 on: March 23, 2007, 02:49:20 am »
That works great...I installed it and everythingf is fine wit it. I do like the idea about the flipping of the card..

NOW, I need to really learn PHP...

Offline inkstains

Re: Cards - Random Item
« Reply #4 on: March 23, 2007, 07:09:56 am »
http://www.w3schools.com/php/default.asp

Thanks Basil Beard


That works great...I installed it and everythingf is fine wit it. I do like the idea about the flipping of the card..

NOW, I need to really learn PHP...

Yeh i would have liked to have done it that way as well but a bit beyond me for the moment, maybe someone else will offer it up.

Offline jdv

Re: Cards - Random Item
« Reply #5 on: March 23, 2007, 12:08:20 pm »
First off, i like how you incorporated how the admin can modify how much karma, credit and so on will be effected. As for me i hate it when it affects the karma, because thats not what karma is all about. So i like that feature. My only question is what in the world is the probability set at?

I assume and figured out... getting the joker is bad! because out of 5 tries i got the joker 4 times and then i get the king and get a measly 10 credits? ha ha.

Offline inkstains

Re: Cards - Random Item
« Reply #6 on: March 23, 2007, 12:14:02 pm »
First off, i like how you incorporated how the admin can modify how much karma, credit and so on will be effected. As for me i hate it when it affects the karma, because thats not what karma is all about. So i like that feature. My only question is what in the world is the probability set at?

I assume and figured out... getting the joker is bad! because out of 5 tries i got the joker 4 times and then i get the king and get a measly 10 credits? ha ha.

you mean whats the probability of getting a king over a queen and a jack over a joker etc? it's based on a generated random number which is assigned to an outcome none of the numbers have a higher probability of being chosen than others it's just random. but that'd be good to add, though i'm not sure how it would be done but i'll look into it.


anyone wanna add a suggestion on how to do that?

Offline jdv

Re: Cards - Random Item
« Reply #7 on: March 23, 2007, 12:22:13 pm »
well, whats the point of the game? to get anything but the joker?

maybe im misunderstanding the intent of the game.

Offline inkstains

Re: Cards - Random Item
« Reply #8 on: March 23, 2007, 12:30:57 pm »
i guess it's basically a game of chance. the cards are basically irrelevant but were included in GhostWriters idea and so were included in the item (though you could use it without them or with something different) it'd be good to do an interactive animated version as i was saying earlier in this thread but is a bit beyond my abilities at the moment (but i've come up with some ideas on how to do it)  someone else may offer something like that up. I just sorta did this as practice i'm still learning php so seemed like something interesting to learn from coding.

Offline jdv

Re: Cards - Random Item
« Reply #9 on: March 23, 2007, 12:36:09 pm »
oh yeah. its pretty neat. dont get me wrong there. I think its cool. Im guessing though since i only allowed credits affected, that only the ace or the king will do anything, and the joker. the queen and jack wouldnt do anything as i didnt put anything in for karma or post count.

keep it up. good job.

Offline inkstains

Re: Cards - Random Item
« Reply #10 on: March 23, 2007, 01:01:34 pm »
yeh i never really thought of that i just tested that and i get an error if anything but a card that corresponds to credits comes up. i guess the easiest thing to do is state all sections must be assigned a value or else remove what you don't want to use from the code. i will try and add some error handling for this soon though.

oh yeah. its pretty neat. dont get me wrong there. I think its cool. Im guessing though since i only allowed credits affected, that only the ace or the king will do anything, and the joker. the queen and jack wouldnt do anything as i didnt put anything in for karma or post count.

keep it up. good job.

and yeh whatever cards don't correspond to the settings you've made they will not do anything

but by only using the credits you are pretty much better off just using the random credits item as that does exactly what you want.
« Last Edit: March 24, 2007, 06:27:28 pm by inkstains »

Offline inkstains

Re: Cards - Random Item
« Reply #11 on: March 23, 2007, 01:50:59 pm »
code removed check first post
« Last Edit: September 28, 2007, 12:26:53 pm by inkstains »

Offline Lady Ambrosia

Re: Cards - Random Item
« Reply #12 on: March 24, 2007, 04:28:39 pm »
I tested this on my forum, and so far everything seems to be working perfectly!!  Awesome little item, I'm sure my members will love it!!
Ambrosia's Castle - My Personal Site
The Dark Castle - My Forum
Avatar by:  Me

Offline Max

Re: Cards - Random Item
« Reply #13 on: March 26, 2007, 03:48:27 am »

Ive tried this and shows an error every time the item is used?!

Code: [Select]
Undefined variable: cardIs
File: \Sources\shop\items\Cards.php
Line: 102

Offline inkstains

Re: Cards - Random Item
« Reply #14 on: March 26, 2007, 01:33:35 pm »

Ive tried this and shows an error every time the item is used?!

Code: [Select]
Undefined variable: cardIs
File: \Sources\shop\items\Cards.php
Line: 102

I'll have a look at that when i get home Max couldn't say why it's doing that at the moment

what version of shop and smf are you running? just in case

though i think i might have just missed an declaration though i've had no problems and nobody else has as far as i know
« Last Edit: March 26, 2007, 04:50:07 pm by inkstains »