Mysidia Adoptables Support Forum

Mysidia Adoptables Support Forum (http://www.mysidiaadoptables.com/forum/index.php)
-   Mys v1.3.x Mods (http://www.mysidiaadoptables.com/forum/forumdisplay.php?f=42)
-   -   Mys v1.3.x Giftbox Mod (Randomly appearing, currency giving~) (http://www.mysidiaadoptables.com/forum/showthread.php?t=4600)

Kyttias 07-15-2014 06:11 PM

Giftbox Mod (Randomly appearing, currency giving~)
 
I know this code to work in Mysidia 1.3.4, because in classes/class_member.php, a certain public function changecash($amount) exists and also because in the Admin CP, Widgets and Modules can be made. Please look for these in older versions of Mysidia before beginning. ^^ As always, make backups of your files.

First of all, you will need jQuery included in your template files. If it is not, or you're not sure, please open up your template.tpl and observe the space before the end of the </body>. If you need a link to jQuery, it is hosted via CDN, so all you need to do is add this line:
Code:

<script src="//cdn.jsdelivr.net/jquery/1.10.0/jquery.min.js"></script>
This next step I'm going to have you do is important. It creates an id wrapper around a user's current cash amount, so that later my script will create a visual showing that the money has increased. Without this first step, a user would not see their new total until they've reloaded or moved on to a new page. Boo! So... in classes/class_sidebar.php, inside function setMoneyBar(), change line

PHP Code:

$this->moneyBar->add(new Comment("You have {$mysidia->user->money} {$mysidia->settings->cost}.")); 

to

PHP Code:

$this->moneyBar->add(new Comment("You have <span id='cashonhand'>{$mysidia->user->money}</span> {$mysidia->settings->cost}.")); 

Note that if you have any other location you are displaying a user's money that you would like to be updated, such as if you made your own display bar, you should be wrapping that variable, or the placeholder for that variable that renders the cash as a number, in a span with the same id as above!

Now, log into your Admin CP and create a new Widget named 'giftbox'. If you name it anything else, you'll be calling it with that name. (Later we'll be calling this widget with {$giftbox}.) Set the Controller Level to 'all', the Widget Order to '50', and Widget Status to 'enabled'.

Next you'll be making a Module with a Parent Widget of 'giftbox', which you just made. You should be able to name the module whatever you like. For amusement's sake, I named mine 'WrappingPaper'. I'm pretty sure the name here will not matter. The Required Userlevel should be 'member'.

Module HTML Code:
Code:

<style>.giftbox {text-align: center;}
.blankbox {background: url(http://fc05.deviantart.net/fs70/f/2014/099/4/f/small_giftbox_by_kyttias-d7dqg9o.png); border: 0px; background-size: 100% 100%; width: 60px; height: 50px;}
.blankbox:hover {background: url(http://fc05.deviantart.net/fs70/f/2014/099/4/f/small_giftbox_by_kyttias-d7dqg9o.png); border: 0px; background-size: 100% 100%; width: 60px; height: 50px;}
</style>

<form id="giftbox" action="giftbox" name="giftbox" method="post" role="form">
<input type="hidden" value="500" name="amount" />
<button class="blankbox" id="submit" value="submit" name="submit" type="submit">*</button>
</form>

<div id="result"></div>

<script>
$(function () {
        $('.giftbox').animate({'height':'55px'});
            var frm = $('#giftbox');
        frm.submit(function (ev) {
                        $.ajax({
                                    type: frm.attr('method'),
                                    url: frm.attr('action'),
                                  data: frm.serialize(),
                                    success: function (data) {
                                $( "#giftbox" ).css("display","none");
                                $amount = $('#giftbox').find( 'input[name=\"amount\"]' ).val();
                                $( "#result" ).html( "Gained "+$amount+" CURRENCY!" );
                                $cashonhand = $('.cashonhand:first').text();
                                $newcash = Number($cashonhand)+Number($amount);
                                $('.cashonhand').text($newcash);
                                $('.giftbox').animate({ height: 0, opacity: 0 },  { duration: 2000  });
                                    }
                });
        ev.preventDefault();
        });
});
</script>

Examine the code above. Find where I wrote CURRENCY and replace it with the name of your own. Because this is Javascript/jQuery and not PHP, I can't have it magically know your currency name, unfortunately. (If I try to have it print a variable, it will print the variable instead of it's value. It has to do with how webpages render.)

Technically, from the code above, the style css you can just add to your stylesheet. You'll recognize that this is where you should change the image of the box.

Lastly, look at the form itself, see this?
Code:

<input type="hidden" value="500" name="amount" />
You can change value to anything, BUT below, you'll see the PHP code is checking that the value is, in fact, 500. Be sure to change both so they match if you want to give users less or more. This is to prevent hackers from inserting their own values.

Module PHP Code:
PHP Code:

if((isset($_POST['amount']) && !empty($_POST['amount'])) && $_POST['amount'] == 500) {
    
$amount $_POST['amount'];
    
$mysidia->user->changecash($amount);


Module Order should be 0, Module Status to be 'enabled'.

All done!

Open up your template and it's template.tpl file and place {$giftbox} to test it out. As you'll see, anywhere {$giftbox} exists, a box will show that you can click.

Now that you have this power in your hands, I know you'll only want your giftbox to show on special circumstances. (I assume) you can use it on your own custom pages, and you can also write scripts. If you'd like that giftbox to show up 5% of the time, this can go in your template (or on a custom page):
Code:

{if rand(0,100) <= 5} {$giftbox} {/if}
You are free to make your own giftbox image or continue using the giftbox image I provided. This example giftbox is, however, transparent. It is also dynamically resized with css, so here it is full size - feel free to color it, then replace the links to it (cool hint: make a second version to fill the reference to it on the 'hover' css - maybe make it wiggle?):
http://fc05.deviantart.net/fs70/f/20...as-d7dqg9o.png

Question time!

- Savvy viewers, how could I go about modifying the php function to deliver an item to the player's inventory? What about an egg directly into their owned adoptables table?

- Are there issues with it appearing on custom pages? I haven't made any/haven't tried to use Smarty variables in them.

(( If you think this was awesome, you can always donate to my PayPal e-mail address, which is my username here @gmail.com. ^^ It'll go towards getting my cat a cool tower to play on. ))

squiggler 07-16-2014 01:53 PM

This is amazing! I will probably add this! Is there any way to either a) make the currency obtained fluctuate (rand function?), or b) put items in there? Thank you!

Kyttias 07-16-2014 02:57 PM

^ I'll work on making one that fluctuates. Like... you'll get a random amount between 500 and 2000?

Items I'm not sure on yet. I've got some time in the next few weeks, so I'm hoping to develop something like that.

squiggler 07-16-2014 03:29 PM

Yeah, that's what I mean. Can we just use the rand function?

Thank you! That will be fun.

Kyttias 07-16-2014 03:49 PM

This is untested, but, to change the value from '500' to 'random' on the form:
Code:

<input type="hidden" value="random" name="amount" />
Module PHP Code will now check that the value is 'random':
PHP Code:

if((isset($_POST['amount']) && !empty($_POST['amount'])) && $_POST['amount'] == "random") {
    
$amount rand(500,2000);
    
$mysidia->user->changecash($amount);


Should work and be plenty secure. ^^

squiggler 07-18-2014 01:04 AM

Okay, just tested it out with the randomizer and I got about 1000 each time. By the way, I've found out that it only gives you the money if you click on it.

But anyway, it works perfectly! Thank you!

Infernette 07-18-2014 02:59 AM

1.3.3 does not have a working widget system so it can only work with 1.3.4 btt (at least, a non-basic editable widget system, to my knowledge) Also is there any way that you can supply this in 'non-module' format for those of us with the slightly defunct version? :)

Kyttias 07-18-2014 06:39 PM

Well, I downloaded 1.3.3 and tried things for several hours and couldn't replicate what I'd done. I can randomly echo out giftbox or not on every page the sidebar exists (all of them), but not place it anywhere conveniently (it sits at the top of the page before the header where it echos out, not even in the sidebar itself, and it refuses to append anywhere else, despite my attempts), or on any particular page. Someone else will have to make an attempt.

Woodchipz 08-20-2015 02:42 AM

Hey this is an awesome addition! Thanks for this!

Just a little help getting it fully implemented would be awesome! You are quite skilled at what you do here.


I followed all the directions and upon trying to create the module for the widget I am met with the following error:



Warning: require(../inc/config.php): failed to open stream: No such file or directory in /home/paradi17/public_html/classes/class_initializer.php on line 97

Warning: require(../inc/config.php): failed to open stream: No such file or directory in /home/paradi17/public_html/classes/class_initializer.php on line 97

Fatal error: require(): Failed opening required '../inc/config.php' (include_path='.:/opt/alt/php55/usr/share/pear:/opt/alt/php55/usr/share/php') in /home/paradi17/public_html/classes/class_initializer.php on line 97






Any help deciphering this would be great :) Thanks again!

-Chipz

Kyttias 08-20-2015 07:12 AM

Confirm that the error does not exist when the module is not being called. (ie, Remove {$giftbox} from your template. Is the error gone?) If the error isn't gone, it's nothing to do with this. If it's gone, then make sure the name is correct. (Again, as stated in the tutorial, if you named it anything else, you'll be calling it with that name.)

Otherwise... eh, this was one of my first mods. :catfish:


All times are GMT -5. The time now is 04:11 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.