Mysidia Adoptables Support Forum

Mysidia Adoptables Support Forum (http://www.mysidiaadoptables.com/forum/index.php)
-   Tutorials and Tips (http://www.mysidiaadoptables.com/forum/forumdisplay.php?f=27)
-   -   Using cronjobs (http://www.mysidiaadoptables.com/forum/showthread.php?t=5480)

Dinocanid 06-30-2017 02:58 PM

Using cronjobs
 
I thought this might be helpful for people who want some automatic functions on their site. Special thanks to Kyttias and IntoRain!
Disclaimer: It is not recommended that you have several cronjobs running at once unless you have a really good server, because it can potentially slow things down; especially if the runtime is frequent (IE: running several times a day)

-Introduction-
Create a new file in public_html (or wherever you have mysidia installed) called cron.php. Inside, paste this:
PHP Code:

<?php

class CronController extends AppController{

    public function 
__construct(){

    }
    
    public function 
index(){

    }   

}
?>

-Adjusting the database-
More often then not, you're going to want to do some math. Whether it's giving pets realtime needs or some sort of daily rewards system, the database needs to know how to math. Go to class_database.php and add this: Pastebin Link

Here are the examples IntoRain gave on how these can now be used in scripts:
PHP Code:

//decrease mood of every owned adoptable by 2
$mysidia->db->update_decrease("owned_adoptables", array("mood"), 2);

//decrease mood and totalclicks by 1, when an adoptable has totalclicks = 1
$mysidia->db->update_decrease("owned_adoptables", array("mood""totalclicks"), 1"totalclicks = 1"); 

-Creating your first cronjob function-
From here, it's just like coding for any other part of the script. Go to cron.php and create a new function like this (do not add anything to the index function or construct function!):
PHP Code:

public function YourFunction(){
    
$mysidia Registry::get("mysidia");
    
//coding goes here

    


Here's an example of how I rotate seasons for my site (feel free to use if you'd like):
PHP Code:

public function changeSeason(){
    
//Allows shops, explore, and other site features to rotate automatically. Runs once a month.    
    
$mysidia Registry::get("mysidia");
    
    
$month date('F');
    
    if(
$month 'March' OR 'April' OR 'May'){
        
$mysidia->db->update("seasons", array("season" => 'Spring'));
    }

    if(
$month 'June' OR 'July' OR 'August'){
        
$mysidia->db->update("seasons", array("season" => 'Summer'));
    }

    if(
$month 'September' OR 'October' OR 'November'){
        
$mysidia->db->update("seasons", array("season" => 'Fall'));
    }
    
    if(
$month 'December' OR 'January' OR 'February'){
        
$mysidia->db->update("seasons", array("season" => 'Winter'));
    }
    
    } 

Now in my other scripts, I can easily add seasonal things in a simple switch statement without getting too lengthy.
Adding little notes right inside the functions is something I recommend so you can easily tell when it should run at a glance. They're also helpful if you have multiple coders, so notes are good!

-Making it go-
Now that you created your function, you need to run it. You might have noticed that cron.php has no coding on actually running the functions, and that's intentional! Many server hosts (including mysidia host) have CPanel available. Access it and navigate to cronjobs under the "advanced" section.
http://www.clipular.com/c/5756875527...0rQFo27WwB3CH0
(cpanel)
http://www.clipular.com/c/5747837439...rIaNHWyuAA3FNA
(retro cpanel)
http://www.clipular.com/c/5629838020...scZhEgwBzkYITA
(mysidiahost client area)

Luckily for us, the page is full of presets. Clicking on the dropdowns allows you to specify exactly when you want the cronjob to run (even down to an exact minute once a year!) Customize it however you want. Afterwards use this for the command section:
Code:

curl http://YOURSITE.com/cron/FUNCTION
Replace "function" with the name of the function you created in cron.php. This is the command for my seasons:
Code:

curl http://adopttest.mysidiahost.com/cron/changeSeason
For every new function you make, you have to create a new cronjob for it. (excluding index and _construct)

parayna 06-30-2017 03:13 PM

This is pretty handy, thanks :D

Abronsyth 06-30-2017 08:58 PM

This is fantastic, I was struggling earlier trying to figure this out! Thank you for sharing this :colonu:

Edit; I am super tired and therefore lazy...so I'll ask you about this!

So I am trying to run a cronjob that will allow me to lower all pets' Mood by 10 each day...if a pet's mood is already at 0, I do not want it to go below 0, so if it's already 0 then it will simply remain at 0.

Any idea how'd you go about doing that?

Dinocanid 06-30-2017 09:58 PM

Hmmm, I think that could work if you use both a cronjob and myadopts.php. With the cronjob you lower mood by 10 daily, then on myadoptview (or myadopts) you can check the amount whenever a user manages their pet. So if the pet's mood is below zero, it sets it to zero. Though I suppose that wouldn't work to well if the user decides to use an item on their pet without checking up on them.

Maybe you could use this somehow?
PHP Code:

$mysidia->db->update_decrease("owned_adoptables", array("mood"), 10"mood >= 10"); 

This would only remove 10 mood from a pet who has at least 10...but then 10 would become the minimum :el:
It's late for me too, so I'll have to look at this more when I'm not tired lol.

KatFennec 07-02-2017 02:40 PM

Quote:

Originally Posted by Dinocanid (Post 36586)
Hmmm, I think that could work if you use both a cronjob and myadopts.php. With the cronjob you lower mood by 10 daily, then on myadoptview (or myadopts) you can check the amount whenever a user manages their pet. So if the pet's mood is below zero, it sets it to zero. Though I suppose that wouldn't work to well if the user decides to use an item on their pet without checking up on them.

Maybe you could use this somehow?
PHP Code:

$mysidia->db->update_decrease("owned_adoptables", array("mood"), 10"mood >= 10"); 

This would only remove 10 mood from a pet who has at least 10...but then 10 would become the minimum :el:
It's late for me too, so I'll have to look at this more when I'm not tired lol.

Try this instead:
PHP Code:

 public function lowerMood() {
        
$mysidia Registry::get("mysidia");
        
$mysidia->db->update_decrease("owned_adoptables", array("mood"), 10);
        
$mysidia->db->update("owned_adoptables", array("mood" => 0), "mood<0");
    } 


Abronsyth 07-04-2017 09:55 AM

I'll try that out and let you know how it works!

Hall of Famer 07-05-2017 08:12 AM

Nice tutorial, it will help a lot of users. We plan to implement a pseudo-cronjob system for next major release, but of course real cronjob will always work better.

Abronsyth 07-06-2017 08:21 AM

Quote:

Originally Posted by KatFennec (Post 36596)
Try this instead:
PHP Code:

 public function lowerMood() {
        
$mysidia Registry::get("mysidia");
        
$mysidia->db->update_decrease("owned_adoptables", array("mood"), 10);
        
$mysidia->db->update("owned_adoptables", array("mood" => 0), "mood<0");
    } 


Just letting you know that this works perfectly, thank you!


All times are GMT -5. The time now is 01:28 AM.

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