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 11-17-2014, 11:24 PM
Kyttias's Avatar
Kyttias Kyttias is offline
Premium Member
 
Join Date: Jan 2014
Posts: 857
Gender: Unknown/Other
Credits: 145,893
Kyttias is on a distinguished road
Default

Well I got some work done re-factoring the code. I've made the timestamps down to the hour:min:sec, created a table just for bonus data, and made it an independent function/method.

-

New instructions (so far) are as follows:

Open phpMyAdmin to access your MySQL database. Open up the database for Mysidia. On the left hand side there should be a long list of tables for Mysidia, with the prefix that you put in during your initial install. The default prefix is adopts_. If your prefix is different, you will want to change it in two places in the code below.

Now, look to right side, at the top. The second tab, between Structure and Search, should be SQL. Open this up, and paste in the following code to run, then press "Go".
Code:
CREATE TABLE IF NOT EXISTS `adopts_kyt_bonuses` (
  `id` int(10) unsigned NOT NULL,
  `bonus_name` varchar(20) NOT NULL,
  `bonus_type` varchar(20) NOT NULL,
  `status` varchar(5) NOT NULL,
  `amount` varchar(20) NOT NULL,
  `timestamp` varchar(20) NOT NULL,
  `expiration` varchar(20) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `adopts_kyt_bonuses` (`id`, `bonus_name`, `bonus_type`, `status`, `amount`, `timestamp`, `expiration`) VALUES (1, 'Bonus1', 'Type1', 'off', '2', '2014-11-17 16:33:07', '2014-11-18 17:06:41');

INSERT INTO `adopts_kyt_bonuses` (`id`, `bonus_name`, `bonus_type`, `status`, `amount`, `timestamp`, `expiration`) VALUES (2, 'Bonus2', 'Type2', 'off', '6', '2014-11-17 21:49:59', '2014-11-18 22:23:33')
This created two example bonuses, one for each type. Type1 is based on the total site interactions made today. For example, if there's been a total of at least 5 interactions, the bonus kicks in. Type2 is based on interactions made since the last timestamp was marked down. For example, when the community has made at least 100 interactions, regardless of how long it took them, a bonus kicks in. Bonuses last for 24 hours.

You will now be able to find the table we created, adopts_kyt_bonuses, in the list on the left. If you open it up, you should see a table that looks like this:



*Disclaimer: Sometimes I'll be calling things functions, other times I'll be referring to things as methods. However, methods are just functions found inside a class. *thumbs up*

Onward! Open up ~/classes/class_template.php. We're going to be creating a new method called checkCommunityBonuses(). To start, we'll add it to the method used to construct the template, that way it knows to run. If you haven't modified this file, in version 1.3.4, you'll find public function __construct(Path $path) at around line 62. Inside this method that constructs the template, after the list of other functions it knows it needs to run, add in:
PHP Code:
$this->checkCommunityBonuses(Bonus1);
$this->checkCommunityBonuses(Bonus2); 
Next, we'll be creating our method properly. After the closing bracket for the construction method, you can paste in the following code:
PHP Code:
    public function checkCommunityBonuses($bonus_name){
        
$mysidia Registry::get("mysidia");
        
$now = new DateTime(); 
        
$today $now->format('Y-m-d H:i:s');

        
# The name of the bonus.
        
$bonus_name $bonus_name;
        
# This is the amount of interactions needed for the effect to kick in.
        
$amount $mysidia->db->select("kyt_bonuses", array("amount"), "bonus_name = '$bonus_name'")->fetchColumn();
        
# This is the type of bonus that this is.
        
$bonus_type $mysidia->db->select("kyt_bonuses", array("bonus_type"), "bonus_name = '$bonus_name'")->fetchColumn();
        
# Moving on, let's get the status... is the effect on or off?
        
$effect_status $mysidia->db->select("kyt_bonuses", array("status"), "bonus_name = '$bonus_name'")->fetchColumn();

        
# Inside kyt_bonuses, fetch the value in the timestamp column where the bonus_name column holds of a value of '$bonus_name'.
        
$timestamp $mysidia->db->select("kyt_bonuses", array("timestamp"), "bonus_name = '$bonus_name'")->fetchColumn();
        
# Convert this to a timestamp PHP can play with.
        
$time date("Y-m-d H:i:s"strtotime($timestamp));
        
# This is so we can find out what day is tomorrow.
        
$oneday strtotime('+1 day');
        
# And now we math...
        
$new_expiration date("Y-m-d H:i:s"$time $oneday);
        
# So, has the timestamp expired? We better check.
        
$timestamp_expiration $mysidia->db->select("kyt_bonuses", array("expiration"), "bonus_name = '$bonus_name'")->fetchColumn();
        
# If the expiration date is greater than or equal to day, then yes, it's expired
        
if ($timestamp_expiration <= $today) { 
            
# Because the effect should expire now, we will turn it's value in the 'status' column to 'off'
            
$mysidia->db->update("kyt_bonuses", array("status" => 'off'), "bonus_name = '$bonus_name'");
            
#Let's reset the timestamp.
            
$mysidia->db->update("kyt_bonuses", array("timestamp" => $today), "bonus_name = '$bonus_name'");
        }

        
# Inside vote_voters, count how many rows have {$today} in their date column.
        
$interactions_today $mysidia->db->select("vote_voters", array(), "date = '{$now->format('Y-m-d')}'")->rowCount();
        
# Now count how many interactions have happened since the timestamp went into effect.
        
$ts date("Y-m-d"strtotime($timestamp)); // This is regathering the timestamp in a form the vote_voters can understand.
        
$interactions_since_timestamp $mysidia->db->select("vote_voters", array(), "date between '{$ts}' and '{$today}'")->rowCount();  

        
/*** BONUS TYPE 1 ***/
        
if ($bonus_type == "Type1"){ //Type1 is based on interactions made today.
           # So, if the effect is on, should it be?
            
if ($effect_status == 'on'){ 
                if (
$interactions_today $amount){ // No
                    # Why is this even on? turn it off!
                    
$mysidia->db->update("kyt_bonuses", array("status" => 'off'), "bonus_name = '$bonus_name'");     
                }
                if (
$interactions_today >= $amount){ // Yes
                    // Nothing yet, but we'll put in a function here - the true magic of the effect!
                
}
            }

            
# Well, if the effect is off, are there are enough interactions for the effect to be turned on?
            
if ($effect_status == 'off') {
                if (
$interactions_today $amount){ // No
                    # Great, stay off.
                    
$mysidia->db->update("kyt_bonuses", array("status" => 'off'), "bonus_name = '$bonus_name'"); 
                }
                if (
$interactions_today >= $amount) {  // Yes
                    # Great, turn it on.
                    
$mysidia->db->update("kyt_bonuses", array("status" => 'on'), "bonus_name = '$bonus_name'");
                    
#Let's reset the timestamp.
                    
$mysidia->db->update("kyt_bonuses", array("timestamp" => $today), "bonus_name = '$bonus_name'");
                    
# And make sure the timestamp knows when it should expire!
                    
$mysidia->db->update("kyt_bonuses", array("expiration" => $new_expiration), "bonus_name = '$bonus_name'");
                }
            }
          
        } 
// end type1

        /*** BONUS TYPE 2 ***/
        
if ($bonus_type == "Type2"){ //Type2 is based on interactions since the timestamp last rolled over.
            # So, if the effect is on, should it be?
            
if ($effect_status == 'on'){
                if (
$interactions_since_timestamp $amount){ // No
                    # Why is this even on? Turn it off!
                    
$mysidia->db->update("kyt_bonuses", array("status" => 'off'), "bonus_name = '$bonus_name'"); 
                }
                if (
$interactions_since_timestamp >= $amount){ // Yes
                    // Nothing yet, but we'll put in a function here - the true magic of the effect!
                
}
            }

            
# Well, if the effect is off, are there are enough interactions for the effect to be turned on?
            
if ($effect_status == 'off'){
                if (
$interactions_since_timestamp $amount){ // No
                    # Great, stay off.
                    
$mysidia->db->update("kyt_bonuses", array("status" => 'off'), "bonus_name = '$bonus_name'"); 
                }
                if (
$interactions_since_timestamp >= $amount){ // Yes
                    # Great, turn it on.
                    
$mysidia->db->update("kyt_bonuses", array("status" => 'on'), "bonus_name = '$bonus_name'");
                    
#Let's reset the timestamp.
                    
$mysidia->db->update("kyt_bonuses", array("timestamp" => $today), "bonus_name = '$bonus_name'");
                    
# And make sure the timestamp knows when it should expire!
                    
$mysidia->db->update("kyt_bonuses", array("expiration" => $new_expiration), "bonus_name = '$bonus_name'"); 
                }
            }

        } 
// end type2

        # And let's get an update on the effect status, time stamp, and expiration...
        
$effect_status $mysidia->db->select("kyt_bonuses", array("status"), "bonus_name = '$bonus_name'")->fetchColumn();
        
$timestamp $mysidia->db->select("kyt_bonuses", array("timestamp"), "bonus_name = '$bonus_name'")->fetchColumn();
        
$timestamp_expiration $mysidia->db->select("kyt_bonuses", array("expiration"), "bonus_name = '$bonus_name'")->fetchColumn(); 

        
# This is a smarty variable of {$timestamp_data}, for a visual read out of the data, and can be placed any where in the main theme template file.
        
$this->assign("timestamp_data","Bonus Effect: {$bonus_name}</br> Effect Status: {$effect_status} <br/> Current Timestamp: {$timestamp} <br/> Expiration On: {$timestamp_expiration} <br/> Interactions Today: {$interactions_today} <br/> Interactions Since Timestamp: {$interactions_since_timestamp}");
        
# This is a smarty variable of {$interactionstoday} that will just show how many interactions have happened today.
        
$this->assign("interactionstoday","Interactions Today: {$interactions_today}");

    } 
// end community bonus check 
Now is find a place in your theme's template.tpl to store {$timestamp_data}, just like you would the {$sidebar} or {$footer}, to see it working. To display just the total amount of interactions made today somewhere, you can use {$interactionstoday}.

Assuming IntoRain's modification to levelup.php mentioned in a post above works out just fine (replacing of course the line linking to the effect in the database), we'll be doing something similar to that. Exact instructions for this part aren't written up yet, aha.

-

Things I need help with now:

1 - You'll see that in the construct method for the template I'm calling the checkCommunityBonuses() function I made with a parameter holding the bonus name. Obviously not ideal at this point! I need to make another function that pulls from the database the number of bonuses that exist, loop through them, and then call checkCommunityBonuses() for each of them. ( And it's much too late tonight for my brain to want to do that.)

2 - H-how do I make Smarty variables with a variable in the name? *nervous laugh* I left the code in there from last time (give or take a few tweaks) but it'll only pull up data from the last bonus it came across, I'm sure. I want to make an admin panel here, so telling users they have to go in and make these variables by hand is... not ideal. Obviously, they'll want to be able to customize the contents of these -- rather than reading off hard data they could put in an image and a tooltip to represent the bonus being on or off.

3 - So. New settings to add the AdminCP... Man, would I appreciate some pointers here. I'll figure it out later given enough time, but I don't have any specific questions right now because I haven't even begun to peer down that rabbit hole. This is on my to-do list...

4 - The modifications to levelup.php - so, what if we've got multiple bonuses going on? And, what if I create room in the database for the bonus clicks and currency so users can do that in the adminCP? I don't know how many bonuses a site owner might create, but I would know their settings for bonus clicks and currency, then.

5 - Eh, should I just forgo the adminCP business, since there's going to be so much to do by hand, anyway?
__________________
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
  #2  
Old 11-19-2014, 02:46 PM
IntoRain's Avatar
IntoRain IntoRain is offline
Moderator
 
Join Date: Jul 2013
Location: Portugal
Posts: 461
Gender: Female
Credits: 23,564
IntoRain is on a distinguished road
Default

Sorry, I've been busy lately and couldn't reply earlier x.x

When creating a table, you can set your ID to be a primary key and auto_increment. That way you don't need to add an id manually when inserting a new row, it will increment the ID's for you

Quote:
Originally Posted by Kyttias View Post
Things I need help with now:

1 - You'll see that in the construct method for the template I'm calling the checkCommunityBonuses() function I made with a parameter holding the bonus name. Obviously not ideal at this point! I need to make another function that pulls from the database the number of bonuses that exist, loop through them, and then call checkCommunityBonuses() for each of them. ( And it's much too late tonight for my brain to want to do that.)
You might want to reduce the number of times you query your database. Since you need all columns anyway, instead of doing a select() for each columns, you can just get the full row instead with a single query.


So, basically you can call your function like this:

PHP Code:
        //obtain all rows
        
$all_bonuses $mysidia->db->select("kyt_bonuses", array(), "");

    
//for each row    
    
while($bonus $all_bonuses->fetchObject()){
                
//run this function
        
$this->checkCommunityBonuses($bonus);
    } 
Then inside the function you can do it like this:

PHP Code:
    private function checkCommunityBonuses($bonus){
            
//bonus_name, amount, etc... are what the columns are called in the database
        
$bonus_name $bonus->bonus_name;
        
$amount $bonus->amount;
        
$bonus_type $bonus->bonus_type;
        
$effect_status $bonus->status;
        
$timestamp $bonus->timestamp;
        
$timestamp_expiration $bonus->expiration;
(...)
    } 

Quote:
Originally Posted by Kyttias View Post
2 - H-how do I make Smarty variables with a variable in the name? *nervous laugh* I left the code in there from last time (give or take a few tweaks) but it'll only pull up data from the last bonus it came across, I'm sure. I want to make an admin panel here, so telling users they have to go in and make these variables by hand is... not ideal. Obviously, they'll want to be able to customize the contents of these -- rather than reading off hard data they could put in an image and a tooltip to represent the bonus being on or off.
You want to pass all bonuses into a Smarty variable? You can pass arrays too and go through each element (in this case, it would be all_bonuses instead of this->messages). Here's an example I did some time ago, to display the 3 latest unread messages:

assigning:

PHP Code:
$this->messages $mysidia->db->select("messages", array(), "touser='{$mysidia->user->username}' and status='unread' ORDER BY id DESC LIMIT 3")->fetchAll();

//I'm assigning an array with at most 3 messages (3 rows)
$mysidia->template->assign("messages",$this->messages); 
template.tpl:

PHP Code:
<!-- Iterate through the messages array -->
                {foreach 
from=$messages item=message}
                <
li class="message-preview">
                  <
a href="/messages/read/{$message.id}">
                    <
span class="name">{$message.fromuser}:</span>
                    <
span class="message">{$message.messagetitle}</span>
                    <
span class="time"><class="fa fa-clock-o"></i> {$message.datesent}</span>
                  </
a>
                </
li>
                <
li class="divider"></li>
                {/foreach} 
Quote:
Originally Posted by Kyttias View Post
3 - So. New settings to add the AdminCP... Man, would I appreciate some pointers here. I'll figure it out later given enough time, but I don't have any specific questions right now because I haven't even begun to peer down that rabbit hole. This is on my to-do list...
The AdminCP works like the normal pages: It has a controller and a view, which you need to create to make a new page. In the controller you do the checking and database access, in the view you do the display. Basically, you display a form to edit whatever you need from the database and a button to submit. In the controller, after submiting, you get the values filled by the user and do a database update.
I can be more specific when you go into making the admincp pages ^^

Quote:
Originally Posted by Kyttias View Post
4 - The modifications to levelup.php - so, what if we've got multiple bonuses going on? And, what if I create room in the database for the bonus clicks and currency so users can do that in the adminCP? I don't know how many bonuses a site owner might create, but I would know their settings for bonus clicks and currency, then.
It might be better to define specific types of bonuses, instead of letting the users create the bonuses themselves. Instead, you can let the user "activate" the bonuses they want to use and the amounts. Because there are bonuses like click and currency that go into levelup.php, and other bonuses that go in other pages, depending on what they change. This way, in each page that might suffer changes from bonuses, you just need to check if the site admin is using that bonus and if they are, do the normal checking.

Quote:
Originally Posted by Kyttias View Post
5 - Eh, should I just forgo the adminCP business, since there's going to be so much to do by hand, anyway?
I advise just finishing the feature itself and worrying about the admincp after you have everything finished. As long as the values are in the database, it should be simple to find a way to change them ^^
__________________


asp.net stole my soul.
Reply With Quote
Reply

Tags
bonus, clicks, community, interaction, mod


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

Similar Threads
Thread Thread Starter Forum Replies Last Post
The community here is brilliant! CallumCharlton Newcomer Center 3 03-11-2015 11:29 AM
Opinions on Progress RoconzaArt Art Gallery 9 01-20-2011 11:38 PM
Which is best open source community software? chriskrich878 Other Chat 3 11-23-2010 10:06 PM
Still a work in Progress MyBBSkinz Adoptables Sites Showcase 7 08-31-2008 09:13 AM
Urm Yeah mybb community forums Ajof Other Chat 5 06-29-2008 05:12 PM


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

Currently Active Users: 4136 (0 members and 4136 guests)
Threads: 4,082, Posts: 32,047, 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 - 2025, vBulletin Solutions Inc.
vBCommerce I v2.0.0 Gold ©2010, PixelFX Studios
vBCredits I v2.0.0 Gold ©2010, PixelFX Studios
Emoticons by darkmoon3636