(1.3.6) Users can pound their favpet & DB doesn't update

Forum
Last Post
Threads / Messages

KittHaven

Member
Member
Joined
May 21, 2013
Messages
379
Points
18
Age
23
Location
Devon, UK
Mysidian Dollar
7,231
Hello! I just realised that users are able to pound their favourite pet and it doesn't update the database to remove the favpet if they go through with it. I have the favpet displaying in the sidebar and when a user pounds them it doesn't change. So I looked in the database and the favpet in users_profile stays the same. This means users can still see a link to manage their pounded adopt but when they click it they obviously get an error as they no longer own the pet. This isn't too bad on their profile as the adopt spotlight links to click the pet, but my sidebar links to manage the pet.

How would I go about preventing this? Ideally I would like to make it so that users cannot pound their favourite pet, but I would be okay with allowing them so long as the database resets to 0 for favpet ID once they do pound them. Though it needs to only reset if the pounded adopt is indeed the favpet as well, so users don't find their favpet resetting every time they pound something haha.

Any ideas? I have been looking around but I am still a bit confused by the new file structure and where everything is :ROFLMAO: Pretty sure this is in the base script too as I haven't touched anything to do with the pound.

Users favpet ID is 84:

2022-01-06 04_17_11-localhost _ MySQL _ beanpets _ adopts_users_profile _ phpMyAdmin 5.1.1 and...png

Same ID is in the pound:

2022-01-06 04_17_02-localhost _ MySQL _ beanpets _ adopts_pounds _ phpMyAdmin 5.1.1 and 13 mor...png

Favpet on sidebar:

2022-01-06 04_18_35-Bean Pets and 13 more pages - Personal - Microsoft Edge.png

Error:

2022-01-06 04_18_46-Bean Pets and 13 more pages - Personal - Microsoft Edge.png

Thank you!!
 

Attachments

  • 2022-01-06 04_15_45-localhost _ MySQL _ beanpets _ adopts_users_profile _ phpMyAdmin 5.1.1 and...png
    2022-01-06 04_15_45-localhost _ MySQL _ beanpets _ adopts_users_profile _ phpMyAdmin 5.1.1 and...png
    6.3 KB · Views: 1
  • 2022-01-06 04_16_06-localhost _ MySQL _ beanpets _ adopts_pounds _ phpMyAdmin 5.1.1 and 13 mor...png
    2022-01-06 04_16_06-localhost _ MySQL _ beanpets _ adopts_pounds _ phpMyAdmin 5.1.1 and 13 mor...png
    5.6 KB · Views: 1

Hall of Famer

Administrator
Staff member
Administrator
Joined
Dec 15, 2008
Messages
4,533
Points
48
Location
United States
Mysidian Dollar
213,979
The best solution I can think of, is to add a check for favpet in the PoundService method pound($aid). If the adoptable happens to be your favpet, it should display an error message suggesting that the adoptable cannot be pounded if it is your favpet.
 

KittHaven

Member
Member
Joined
May 21, 2013
Messages
379
Points
18
Age
23
Location
Devon, UK
Mysidian Dollar
7,231
I think I've managed to do it by using poundservice and poundvalidator, thanks for pointing me in the right direction! It seems to work, but I am only one person and still not great at this so if anyone sees this and wants to test it out for bugs feel free :ROFLMAO:

In service/applicationservice/poundservice.php scroll to the bottom and where it says private function validatePound(), replace it with this:

PHP:
private function validatePound(OwnedAdoptable $adopt){
        $validations = new ArrayObject(["adopt", "owner", "number", "recurrence", "money", "favpet"]);
        $validator = new PoundValidator($adopt, $this->getCost($adopt, "pound"), $this->settings, $validations);
        $validator->validate();
    }

That adds a check for favpet, which we need to set up.

In service/validator/poundvalidator.php, scroll to the bottom and put this (but make sure it's before the last curly bracket!):

PHP:
protected function checkFavpet(){
        $mysidia = Registry::get("mysidia");
        $favpetID = $mysidia->db->select("users_profile", array("uid", "favpet"), "uid = '{$mysidia->user->getID()}'")->fetchObject();
        if($favpetID->favpet == $this->adopt->getAdoptID()){
            throw new PoundException("favpet");
        }

Then open lang/main/lang_pound.php and put this somewhere:

PHP:
$lang['favpet'] = "This adopt is your favpet! You cannot pound them!";

And that seems to work. When you go to pound an adopt it will throw the above error and if you go to manage the favpet you still have them.

Now, this happens if you go to the pound page and click the confirm button. But what if you want the page to look different and stop you seeing the option to pound them in the first place?

Open view/main/poundview.php, go to public function pound(), and add this line in:

PHP:
$favpetID = $mysidia->db->select("users_profile", array("uid", "favpet"), "uid = '{$mysidia->user->getID()}'")->fetchObject();

I put it underneath:

PHP:
$adopt = $this->getField("adopt");
$confirm = $this->getField("confirm");

Just so it stays at the top lol. Then find:

PHP:
if($confirm){
            $cost = $this->getField("cost");
            $document->setTitle($this->lang->pound_complete);
            $document->addLangvar($this->lang->pound_success);
            $document->addLangvar(" at a cost of {$cost->getValue()} {$mysidia->settings->cost}");
            $document->addLangvar($this->lang->afterwards);
            return;
        }

And underneath that put this:

PHP:
if($favpetID->favpet == $adopt->getAdoptID()){
            $document->setTitle("Oops!");
            $document->add(new Comment("You cannot pound your favpet!"));   
            return;
        }

My full poundview.php in case you want to just copy it (this would only work if you haven't edited it yourself!):

  Spoiler: Full poundview.php 

PHP:
<?php

namespace View\Main;
use Resource\Collection\LinkedList;
use Resource\Core\Model;
use Resource\Core\Registry;
use Resource\Core\View;
use Resource\GUI\Component\Button;
use Resource\GUI\Component\Image;
use Resource\GUI\Component\Link;
use Resource\GUI\Container\Form;
use Resource\GUI\Container\TCell;
use Resource\GUI\Document\Comment;
use Resource\GUI\Document\Division;
use Resource\GUI\Element\Align;
use Service\Builder\TableBuilder;
use Service\Helper\AdoptTableHelper;

class PoundView extends View{
    
    public function index(){
        $document = $this->document;   
        $document->setTitle($this->lang->title);       
        $document->addLangvar($this->lang->default);
        
        $readoptForm = new Form("readoptform", "pound/adopt", "post");
        $readoptHeader = new Comment("Pounded Adoptables for adoption");
        $readoptHeader->setHeading(3);
        $readoptForm->add($readoptHeader);
        
        $helper = new AdoptTableHelper;
        $readoptTable = new TableBuilder("readopttable");
        $readoptTable->setAlign(new Align("center", "middle"));
        $readoptTable->buildHeaders("Select", "Image", "Basic Info", "Additional Info");
        $readoptTable->setHelper($helper);
        
        $poundMap = $this->getField("poundMap");       
        $iterator = $poundMap->iterator();       
        while($iterator->hasNext()){
            $entry = $iterator->next();
            $adopt = $entry->getKey();
            $cost = $entry->getValue();

            $cells = new LinkedList;           
            $cells->add(new TCell($helper->getPoundButton($adopt->getAdoptID())));
            $cells->add(new TCell($adopt->getImage(Model::GUI)));
            $cells->add(new TCell($helper->getBasicInfo($adopt->getName(), $cost->getValue())));
            $cells->add(new TCell($helper->getAdditionalInfo($adopt)));
            $readoptTable->buildRow($cells);
        }

        $notice = new Comment("Select an adoptable from above to become its new owner.");
        $notice->setHeading(3);       
        $readoptForm->add($readoptTable);
        $readoptForm->add($notice);
        $readoptForm->add(new Button("Adopt Me", "submit", "submit"));
        $document->add($readoptForm);
    }
    
    public function pound(){
        $mysidia = Registry::get("mysidia");
        $document = $this->document;
        $adopt = $this->getField("adopt");
        $confirm = $this->getField("confirm");
        $favpetID = $mysidia->db->select("users_profile", array("uid", "favpet"), "uid = '{$mysidia->user->getID()}'")->fetchObject();
        if($confirm){
            $cost = $this->getField("cost");
            $document->setTitle($this->lang->pound_complete);
            $document->addLangvar($this->lang->pound_success);
            $document->addLangvar(" at a cost of {$cost->getValue()} {$mysidia->settings->cost}");
            $document->addLangvar($this->lang->afterwards);
            return;
        }
        
        if($favpetID->favpet == $adopt->getAdoptID()){
            $document->setTitle("Oops!");
            $document->add(new Comment("You cannot pound your favpet!"));   
            return;
        }
        
        else{
            $document->setTitle($this->lang->pound_title);
            $document->add($adopt->getImage(Model::GUI));
            $document->add(new Comment("<br>{$this->lang->pound}<br><br>{$this->lang->pound_warning}<br>"));       
            $options = new Division("pound");
            $options->setAlign(new Align("center"));
            
            $options->add(new Image("templates/icons/delete.gif", "Pound"));
            $options->add(new Link("pound/pound/{$adopt->getAdoptID()}/confirm", "Pound {$adopt->getName()} - I don't want it anymore!", TRUE));
            $options->add(new Image("templates/icons/yes.gif", "Do not pound"));
            $options->add(new Link("myadopts/manage/{$adopt->getAdoptID()}", "DO NOT Pound {$adopt->getName()}"));
            $document->add($options);
        }
    
    }
    
    public function adopt(){
        $mysidia = Registry::get("mysidia");
        $document = $this->document;           
        if($mysidia->input->post("submit")){
            $document->setTitle($this->lang->global_action_complete);
            $document->addLangvar($this->lang->readopt_success);           
            $cost = $this->getField("cost");
            if($cost) $document->addLangvar(" at a cost of {$cost->getValue()} {$mysidia->settings->cost}");
        }
    }
}

That makes it so users see an error when attempting to pound their favourite pet. And if users have figured it out and sneakily go to add /confirm to the URL to pound their pet, they see the other error that we set up before! So they cannot pound their favpet and they also see a separate page attempting to pound them.

If anyone sees this and there's a different way of doing it let me know! I'm still learning and tinkering with the new structure so I'm up for changing things and seeing how to better do what I need to :ROFLMAO:
 

Similar threads

Users who are viewing this thread

  • Forum Contains New Posts
  • Forum Contains No New Posts

Forum statistics

Threads
4,184
Messages
32,466
Members
1,546
Latest member
Sly-Asteri
BETA

Latest Threads

Latest Posts

Top