Mysidia Adoptables Support Forum  

Home Community Mys-Script Creative Off-Topic
Go Back   Mysidia Adoptables Support Forum > Mysidia Adoptables > Questions and Supports

Notices

Reply
 
Thread Tools Display Modes
  #1  
Old 02-27-2016, 10:28 AM
Abronsyth's Avatar
Abronsyth Abronsyth is offline
A Headache Embodied
 
Join Date: Aug 2011
Location: NY
Posts: 1,011
Gender: Male
Credits: 117,850
Abronsyth is on a distinguished road
Default Check quantity of an item

I'm working on building very simple quests and I'm wondering if anyone knows how to ask the database to check if the quantity of a specific item a user has is, say 10 or more?

Edit: And then also remove the specified quantity of that item from the users inventory?

So say it's a quest and they need to turn in 10 rocks, first it verifies that they have 10 or more rocks, and then it removes 10 rocks from their inventory if they indeed have 10 or more.

Thank you,
-Abron
__________________
My Mods Site (1.3.4, 2020 Mods)

Last edited by Abronsyth; 02-27-2016 at 10:31 AM.
Reply With Quote
  #2  
Old 02-27-2016, 11:17 AM
Kyttias's Avatar
Kyttias Kyttias is offline
Premium Member
 
Join Date: Jan 2014
Posts: 857
Gender: Unknown/Other
Credits: 91,409
Kyttias is on a distinguished road
Default

How about a function like this?

PHP Code:
public function takeItem($item$qty){
    
$mysidia Registry::get("mysidia");
    
$owned $mysidia->db->select("inventory", array("quantity"), "itemname ='{$item}' and owner ='{$mysidia->user->username}'")->fetchColumn();
    if (
$owned >= $qty){ 
        
// If the user owns $qty amount or more...
        
if ($owned == $qty){ 
            
// If the user has exactly $qty left, delete the whole row.
             
$mysidia->db->delete("inventory""itemname='{$item}' and owner='{$mysidia->user->username}'");
         } else {
            
// Subtract $qty from user's inventory.
            
$owned_left $owned $qty;
            
$mysidia->db->update("inventory", array("quantity" => $owned_left), "itemname ='{$item}' and owner='{$mysidia->user->username}'");  
        }
    } else {
        throw new 
Exception("Sorry, you only have {$owned} {$item}, you need {$qty}!"); 
    }

__________________
Please do not contact me directly outside of Mysidia.
I also cannot troubleshoot code more than two years old - I legit don't remember it.
Reply With Quote
  #3  
Old 02-27-2016, 12:01 PM
Abronsyth's Avatar
Abronsyth Abronsyth is offline
A Headache Embodied
 
Join Date: Aug 2011
Location: NY
Posts: 1,011
Gender: Male
Credits: 117,850
Abronsyth is on a distinguished road
Default

OK I know I did something terribly unintelligent to get this error;
Fatal error: Uncaught exception 'Exception' with message 'Sorry, you only have Rock Cone, you need 10!' in /home/arieng/catisserie.net/view/completeq1view.php:48 Stack trace: #0 /home/arieng/catisserie.net/view/completeq1view.php(13): Completeq1View->takeItem('Rock Cone', 10) #1 /home/arieng/catisserie.net/classes/class_frontcontroller.php(100): Completeq1View->index() #2 /home/arieng/catisserie.net/index.php(74): FrontController->render() #3 /home/arieng/catisserie.net/index.php(78): IndexController::main() #4 {main} thrown in /home/arieng/catisserie.net/view/completeq1view.php on line 48

I'm sure this will make you cringe to look at but this is the file thus far;
PHP Code:
<?php 

class Completeq1View extends View
     
    public function 
index(){ 
        
$mysidia Registry::get("mysidia"); 
        
$document $this->document
        
$document->setTitle("Turn in 10 Rock Cones");

            
// Allow user to complete quest if they have not yet.  
            
if ($mysidia->user->quest1 "no") {                  
                        
$document->add(new Comment("<center>Text Text Text<center>"FALSE));
                    
$this->takeItem("Rock Cone",10);
                    }        
            else{
                
$document->add(new Comment("The man furrows his brows at you, <b>sorry, no reward if you don't have any rock cones.</b>"FALSE));
                }
    }

public function 
takeItem($item$qty){
    
$mysidia Registry::get("mysidia");
    
$owned $mysidia->db->select("inventory", array("quantity"), "itemname ='{$item}' and owner ='{$mysidia->user->username}'")->fetchColumn();
    if (
$owned >= $qty){ 
        
// If the user owns $qty amount or more...
        
if ($owned == $qty){ 
            
$amount 100;
            
$mysidia->user->changecash($amount);                    
                
$document->add(new Comment("<center>You return to the scientit's camp and present the head scientist with 10 rock cones.<br>
                <b>Wow, thank you! Here are some kibs!</b><br>
                You've obtained 
{$amount} kibs!</center>"FALSE));
                
// Update that they have done the quest 
                
$mysidia->db->update("users", array("quest1" => "yes"), "username = '{$mysidia->user->username}'");
            
// If the user has exactly $qty left, delete the whole row.
             
$mysidia->db->delete("inventory""itemname='{$item}' and owner='{$mysidia->user->username}'");
            
$amount 100;
            
$mysidia->user->changecash($amount);                    
                
$document->add(new Comment("<center>You return to the scientit's camp and present the head scientist with 10 rock cones.<br>
                <b>Wow, thank you! Here are some kibs!</b><br>
                You've obtained 
{$amount} kibs!</center>"FALSE));
                
// Update that they have done the quest 
                
$mysidia->db->update("users", array("quest1" => "yes"), "username = '{$mysidia->user->username}'");
         } else {
            
// Subtract $qty from user's inventory.
            
$owned_left $owned $qty;
            
$mysidia->db->update("inventory", array("quantity" => $owned_left), "itemname ='{$item}' and owner='{$mysidia->user->username}'");  
        }
    } else {
        throw new 
Exception("Sorry, you only have {$owned} {$item}, you need {$qty}!"); 
    }
}    

    
}
?>
Edit;
OK, may be because I didn't actually define $qty at any point...not really sure how to, though @_@ I know that you can see if a user owns a certain item, but not sure how to define $qty to check how many of that item they own.
__________________
My Mods Site (1.3.4, 2020 Mods)

Last edited by Abronsyth; 02-27-2016 at 12:06 PM.
Reply With Quote
  #4  
Old 02-27-2016, 12:37 PM
Kyttias's Avatar
Kyttias Kyttias is offline
Premium Member
 
Join Date: Jan 2014
Posts: 857
Gender: Unknown/Other
Credits: 91,409
Kyttias is on a distinguished road
Default

No, you did it right, with takeItem("Rock Cone",10); that 10 is defining $qty in the function. I wasn't entirely sure on how to construct the end of the function... rather than

PHP Code:
throw new Exception("Sorry, you only have {$owned} {$item}, you need {$qty}!"); 
Try... either:
PHP Code:
echo "Sorry, you only have {$owned} {$item}, you need {$qty}!"
or maybe just
PHP Code:
return false
I'm not totally sure what to do on failure, so I just had it throw an Exception.
__________________
Please do not contact me directly outside of Mysidia.
I also cannot troubleshoot code more than two years old - I legit don't remember it.
Reply With Quote
  #5  
Old 02-27-2016, 02:46 PM
Abronsyth's Avatar
Abronsyth Abronsyth is offline
A Headache Embodied
 
Join Date: Aug 2011
Location: NY
Posts: 1,011
Gender: Male
Credits: 117,850
Abronsyth is on a distinguished road
Default

OK, thank you! So no longer getting the error, but the text doesn't want to actually appear on the page XD

Rather it's appearing right above all of the page, oops!
__________________
My Mods Site (1.3.4, 2020 Mods)
Reply With Quote
  #6  
Old 02-27-2016, 05:52 PM
Kyttias's Avatar
Kyttias Kyttias is offline
Premium Member
 
Join Date: Jan 2014
Posts: 857
Gender: Unknown/Other
Credits: 91,409
Kyttias is on a distinguished road
Default

Yeah, I figured, that's what an echo does. You'll have to decide what you want to actually be doing as a result. It all depends on what you actually wanted to be doing with the function...?

I would suggest this:

PHP Code:
$takeItem $this->takeItem("Rock Cone"10);

if (
$takeItem){
    
$document->add(new Comment("Thank you for bringing me those Rock Cones!"FALSE));
} else {
    
$document->add(new Comment("Hey, this isn't enough Rock Cones!"FALSE));

but you'll need to have the function return a true or false value (I've added one or the other here to all possible outcomes):
PHP Code:
public function takeItem($item$qty){
    
$mysidia Registry::get("mysidia");
    
$owned $mysidia->db->select("inventory", array("quantity"), "itemname ='{$item}' and owner ='{$mysidia->user->username}'")->fetchColumn();
    if (
$owned >= $qty){ 
        
// If the user owns $qty amount or more...
        
if ($owned == $qty){ 
            
// If the user has exactly $qty left, delete the whole row.
             
$mysidia->db->delete("inventory""itemname='{$item}' and owner='{$mysidia->user->username}'");
             return 
true;
         } else {
            
// Subtract $qty from user's inventory.
            
$owned_left $owned $qty;
            
$mysidia->db->update("inventory", array("quantity" => $owned_left), "itemname ='{$item}' and owner='{$mysidia->user->username}'"); 
            return 
true;
        }
    } else {
        return 
false;
    }

I would NOT modify the function above as you did - if you want to reward the user with something, you should do it in the first half of this post after the success/fail message. That way the code is reusable regardless of the reward.
__________________
Please do not contact me directly outside of Mysidia.
I also cannot troubleshoot code more than two years old - I legit don't remember it.

Last edited by Kyttias; 02-27-2016 at 05:54 PM.
Reply With Quote
Reply


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

Forum Jump


All times are GMT -5. The time now is 09:32 AM.

Currently Active Users: 977 (0 members and 977 guests)
Threads: 4,080, Posts: 32,024, Members: 2,016
Welcome to our newest members, jolob.
BETA





What's New?

What's Hot?

What's Popular?


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
vBCommerce I v2.0.0 Gold ©2010, PixelFX Studios
vBCredits I v2.0.0 Gold ©2010, PixelFX Studios
Emoticons by darkmoon3636