Mys 1.3.6 Alchemy Mod

Forum
Last Post
Threads / Messages

Hall of Famer

Administrator
Staff member
Administrator
Joined
Dec 15, 2008
Messages
4,564
Points
48
Location
United States
Mysidian Dollar
214,223
The next Mod that I choose to update for Mys v1.3.6 compatibility is the Alchemy Mod. The idea of Alchemy is to mix two old items(ingredients) to create a new item(product), which is normally rarer and more valuable(otherwise it does not justify the economics lol). This Mod allows you to achieve just that, you can create some items that are rare or even impossible to obtain without performing alchemy. This will make your site considerably more interesting to a good number of your users, it also offers incentives for the act of item collection.

This Mod can be downloaded from the link below:


Below is a screenshot of the basic alchemy page, which allows you to choose two items to produce a new one. Note at this time being only two items can be used to produce a new item, there is no way to mix more than two:

alchemy_practice.png



This mod also comes with ACP integration, in which the admins can create alchemy practices that allow users to create new items from the old ones. You may also decide the chance for the alchemy practice to succeed(failure will consume the two ingredient items without producing a new item, but the users will not lose any money if this happens). You can also specify a recipe required for this alchemy practice to work, users will need to obtain such a recipe item first in order to mix the two ingredients for making a new item.

alchemy_creation.png



Admins can also manipulate the settings for Alchemy System on their sites. The entire system can be turned on/off at will, while admins can specify whether alchemy success chance is 100% always or less. Other settings such as cost of action, recipe/license requirement for performing alchemy are also configurable.

alchemy_settings.png



To install the mod, simply run the installer script located at yoursite.com/install/alchemy.php. If your site is in a subdirectory, just change the example url accordingly. I assume you all know what I mean by this so I wont explain further.

If you have a brand new site or have never ever modified any part of the script, you may simply download the .zip file I've provided in the earlier post and uploading them to your server through ftp. If the only changes you made are the mods I've offered lately(gender ratio and item drop), you may give a try just uploading the files as well as good chance my mods are compatible with each other.

However, for advanced users who have heavily modified sites, you will need to make some changes manually. I will post a brief description on how to do that in the next post.

Hall of Famer
 
Last edited:
For advanced users who have heavily modified sites, you may follow the procedure below to install this mod on your site. Ignore this post if your site works fine with simply uploading/overwriting files.

First of all, upload and run the installer script at yoursite.com/install/alchemy.php. This step is identical for all users, unless you wish to create tables/insert rows into database manually, but will be a pain with this mod.

Second, upload the following new files to your server. Most of these files do not exist in the base script so you wont run into file overwriting problems. The new files are:
/controller/admincp/alchemycontroller.php
/view/admincp/alchemyview.php
/model/domainmodel/alchemy.php
/model/settings/alchemysetting.php
/service/validator/alchemyvalidator.php
/lang/admincp/lang_alchemy.php

You may do the same for the lang file: /lang/main/lang_inventory.php since I assume nobody has ever modified it. If the answer is opposite however, you may wish to search through the differences of the two lang files to see how to append new lang vars to your site. It shouldnt be difficult.


Alright then, in the next step you will need to modify some old script files. I hope you are ready. Now go to file /model/viewmodel/adminsidebar.php and find these line(these are related to the menu for inventory items):

PHP:
        $components->add(new Division(new Comment("Inventory", FALSE)));
        $inventory = new Division;
        $inventory->add(new Link("admincp/inventory/add", "Give Item to User"));
        $inventory->add(new Link("admincp/inventory/edit", "Edit User Inventory"));
        $inventory->add(new Link("admincp/inventory/delete", "Delete Users items"));
        $components->add($inventory);

Add below:

PHP:
        $components->add(new Division(new Comment("Alchemy", FALSE)));
        $alchemy = new Division;
        $alchemy->add(new Link("admincp/alchemy/add", "Create new Alchemy Practice"));
        $alchemy->add(new Link("admincp/alchemy/edit", "Edit Alchemy Practices"));
        $alchemy->add(new Link("admincp/alchemy/delete", "Remove Alchemy Practices"));
        $alchemy->add(new Link("admincp/alchemy/settings", "Change Alchemy Settings"));      
        $components->add($alchemy);

Next, go to file /services/helper/itemtablehelper.php and find this method:

PHP:
    /**
     * The buildItemNameList method, builds a dropdown list for available item names.
     * @param String  $name
     * @access public
     * @return DropdownList
     */      
    public function buildItemNameList($name){
        $mysidia = Registry::get("mysidia");
        $itemList = new DropdownList($name);
        $stmt = $mysidia->db->select("items", ["itemname", "id"], "1 ORDER BY id");
        $items = $mysidia->db->fetchMap($stmt);
        $itemList->add(new Option("Select an Item", "none"));
        $itemList->fill($items);
        return $itemList;
    }

Add another 2 methods below the ending }:

PHP:
    /**
     * The buildRecipeNameList method, builds a dropdown list for available recipe item names.
     * @param String  $name
     * @access public
     * @return DropdownList
     */      
    public function buildRecipeNameList($name){
        $mysidia = Registry::get("mysidia");
        $itemList = new DropdownList($name);
        $stmt = $mysidia->db->select("items", ["itemname", "id"], "`function` = 'Recipe' ORDER BY id");
        $items = $mysidia->db->fetchMap($stmt);
        $itemList->add(new Option("Select a Recipe", "none"));
        $itemList->fill($items);
        return $itemList;
    }
   
    /**
     * The buildInventoryItemList method, builds a dropdown list for available items in the user's inventory.
     * @param String  $name
     * @access public
     * @return DropdownList
     */      
    public function buildInventoryItemList($name){
        $mysidia = Registry::get("mysidia");
        $itemList = new DropdownList($name);
        $stmt = $mysidia->db->join("items", "items.id = inventory.item")
                            ->select("inventory", ["itemname", "id"], "owner = '{$mysidia->user->getID()}'");
        $items = $mysidia->db->fetchMap($stmt);
        $itemList->add(new Option("Select an Item", "none"));
        $itemList->fill($items);
        return $itemList;
    }

Now we need to go to the file /controller/main/inventorycontroller.php. First locate these lines at the top of the script:
PHP:
namespace Controller\Main;
use Model\DomainModel\ItemException;
use Model\DomainModel\OwnedItem;
use Resource\Collection\ArrayList;
use Resource\Core\AppController;
use Resource\Core\Registry;
use Resource\Exception\InvalidIDException;
use Resource\Native\MysString;


Replace by:

PHP:
namespace Controller\Main;
use ArrayObject, Exception;
use Model\DomainModel\Alchemy;
use Model\DomainModel\ItemException;
use Model\DomainModel\OwnedItem;
use Model\Settings\AlchemySettings;
use Resource\Collection\ArrayList;
use Resource\Core\AppController;
use Resource\Core\Registry;
use Resource\Exception\InvalidActionException;
use Resource\Exception\InvalidIDException;
use Resource\Exception\NoPermissionException;
use Resource\Native\MysString;
use Service\Validator\AlchemyValidator;

This allows us to import the new classes we've added for the Alchemy Mod, as well as some other built-in classes needed for the alchemy action. Next, we need to add the alchemy action. Find the method toss($confirm) which should look like this:

PHP:
    public function toss($confirm = NULL){
        $mysidia = Registry::get("mysidia");
        $item = new OwnedItem($mysidia->input->post("item"), $mysidia->user->getID());
        if(!$item->inInventory()) throw new ItemException("toss_none");
       
        if($confirm){
            $item->toss();
        }
        $this->setField("item", $item);
        $this->setField("confirm", $confirm ? new MysString("confirm") : NULL);
    }

Add the new method alchemy after it:

PHP:
    public function alchemy(){
        $mysidia = Registry::get("mysidia");
        $alchemySettings = new AlchemySettings($mysidia->db);
        if($alchemySettings->system == "disabled") throw new ItemException("alchemy_disabled");
       
        if($mysidia->input->post("iid") && $mysidia->input->post("iid2")){
            try{
                $alchemy = new Alchemy($mysidia->input->post("iid"), $mysidia->input->post("iid2"));
                $alchemyValidator = new AlchemyValidator($alchemy, $alchemySettings, new ArrayObject(["license", "items", "recipe", "usergroup", "cost", "chance"]));
                $alchemyValidator->validate();
                $newItem = $alchemy->mix($alchemySettings->cost);
                $this->setField("newItem", $newItem);
            }
            catch(Exception $e){
                throw new InvalidActionException($e->getMessage());
            }
            return;
        }
        $this->setField("settings", $alchemySettings);
    }

With this, we've added the action for users to perform alchemy. But we are not done yet, we need to add a view to this action. Go to file /view/main/inventoryview.php file and find these lines at the top:

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\Option;
use Resource\GUI\Component\PasswordField;
use Resource\GUI\Container\DropdownList;
use Resource\GUI\Container\Form;
use Resource\GUI\Container\TCell;
use Resource\GUI\Element\Align;
use Service\Builder\FormBuilder;
use Service\Builder\TableBuilder;
use Service\Helper\ItemTableHelper;

Replace by:

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\Link;
use Resource\GUI\Component\Option;
use Resource\GUI\Component\PasswordField;
use Resource\GUI\Container\DropdownList;
use Resource\GUI\Container\Form;
use Resource\GUI\Container\TCell;
use Resource\GUI\Document\Comment;
use Resource\GUI\Element\Align;
use Service\Builder\FormBuilder;
use Service\Builder\TableBuilder;
use Service\Helper\ItemTableHelper;

Now we need to add the view for alchemy action. Again, find the method for toss at here:

PHP:
    public function toss(){
        $mysidia = Registry::get("mysidia");
        $document = $this->document;
        $item = $this->getField("item");
        $confirm = $this->getField("confirm");
        if($confirm){
            $document->setTitle($this->lang->global_action_complete);
            $document->addLangvar("{$this->lang->toss}{$item->getItemname()}{$this->lang->toss2}");
            return;
        }
   
        $document->setTitle($this->lang->toss_confirm);
        $document->addLangvar("{$this->lang->toss_warning}{$item->getItemname()}?<br>{$this->lang->toss_warning2}");  
        $confirmForm = new FormBuilder("confirmform", "toss/confirm", "post");
        $confirmForm->buildPasswordField("hidden", "action", "toss")
                    ->buildPasswordField("hidden", "item", $mysidia->input->post("item"))
                    ->buildButton("Please Toss", "confirm", "confirm");
        $document->add($confirmForm);          
    }

And add the action for alchemy after toss method:

PHP:
    public function alchemy(){
        $mysidia = Registry::get("mysidia");
        $document = $this->document;      
        if($mysidia->input->post("iid") && $mysidia->input->post("iid2")){
            $newItem = $this->getField("newItem");
            $document->setTitle($this->lang->alchemy_success);
            $document->addLangvar($this->lang->alchemy_newitem . $newItem->getItemname() . $this->lang->alchemy_newitem2);  
            return;      
        }

        $document->setTitle($this->lang->alchemy_title);
        $document->addLangvar($this->lang->alchemy);
        $settings = $this->getField("settings");
        $alchemyForm = new FormBuilder("alchemyform", "alchemy", "post");
        $alchemyForm->add(new Comment("<b>Cost of performing Alchemy: {$settings->cost} {$mysidia->settings->cost}</b><br>"));
       
        $alchemyForm->add(new Comment($mysidia->lang->alchemy_choose));
        $alchemyForm->buildDropdownList("iid", "InventoryItemList");
        $alchemyForm->add(new Comment($mysidia->lang->alchemy_choose2));
        $alchemyForm->buildDropdownList("iid2", "InventoryItemList");            
        $alchemyForm->add(new Button("Let's mix the items now!", "submit", "submit"));
        $document->add($alchemyForm);      
    }

We are almost done. The last step is optional, but may be helpful for the user experience of your site. Simply find the index view of inventory:

PHP:
        $document = $this->document;
        $document->setTitle($this->lang->inventory);

And add three more lines below these to provide a link of alchemy page to users visiting their inventory:

PHP:
        $document->addLangvar("You may manage these items as you like, or go to the alchemy page to make your own items: <br>");  
        $document->add(new Link("inventory/alchemy", "Use the Alchemy Service Now!"));  
        $document->add(new Comment("<br><br>"));

You are done with the modifications now, it was easier than you thought right? Anyway you may run into some problems, and in this case please post in this thread to let me know. I will find out where the errors are and fix them for you as soon as I get a chance.
 
Last edited:

Similar threads

Users who are viewing this thread

  • Forum Contains New Posts
  • Forum Contains No New Posts

Forum statistics

Threads
4,277
Messages
33,118
Members
1,602
Latest member
BerrieMilk
BETA

Latest Threads

Latest Posts

Top