View Single Post
  #6  
Old 11-17-2014, 10:24 PM
Kyttias's Avatar
Kyttias Kyttias is offline
Premium Member
 
Join Date: Jan 2014
Posts: 857
Gender: Unknown/Other
Credits: 86,964
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