View Single Post
  #1  
Old 07-15-2014, 06:11 PM
Kyttias's Avatar
Kyttias Kyttias is offline
Premium Member
 
Join Date: Jan 2014
Posts: 857
Gender: Unknown/Other
Credits: 86,989
Kyttias is on a distinguished road
Arrow 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?):


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. ))
__________________
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; 07-16-2014 at 03:05 PM.
Reply With Quote