Mysidia Adoptables Support Forum  

Home Community Mys-Script Creative Off-Topic
Go Back   Mysidia Adoptables Support Forum > Mysidia Adoptables > Tutorials and Tips

Notices

Reply
 
Thread Tools Display Modes
  #1  
Old 06-30-2017, 02:58 PM
Dinocanid's Avatar
Dinocanid Dinocanid is offline
Member
 
Join Date: Aug 2016
Location: Maryland, USA
Posts: 516
Gender: Unknown/Other
Credits: 78,645
Dinocanid is on a distinguished road
Default 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.

(cpanel)

(retro cpanel)

(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)
__________________
Reply With Quote
  #2  
Old 06-30-2017, 03:13 PM
parayna's Avatar
parayna parayna is offline
Member
 
Join Date: May 2013
Location: Devon, UK
Posts: 342
Gender: Female
Credits: 22,428
parayna is on a distinguished road
Default

This is pretty handy, thanks :D
__________________
It's been a long time. I had so much fun making a site back in 2016 that recently, when I started thinking about it again, I decided to come back and work on something small. It'll probably just be a personal project but who knows? We'll see, anyway.

Reply With Quote
  #3  
Old 06-30-2017, 08:58 PM
Abronsyth's Avatar
Abronsyth Abronsyth is offline
A Headache Embodied
 
Join Date: Aug 2011
Location: NY
Posts: 1,011
Gender: Male
Credits: 132,720
Abronsyth is on a distinguished road
Default

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

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?
__________________
My Mods Site (1.3.4, 2020 Mods)

Last edited by Abronsyth; 06-30-2017 at 09:39 PM.
Reply With Quote
  #4  
Old 06-30-2017, 09:58 PM
Dinocanid's Avatar
Dinocanid Dinocanid is offline
Member
 
Join Date: Aug 2016
Location: Maryland, USA
Posts: 516
Gender: Unknown/Other
Credits: 78,645
Dinocanid is on a distinguished road
Default

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
It's late for me too, so I'll have to look at this more when I'm not tired lol.
__________________
Reply With Quote
  #5  
Old 07-02-2017, 02:40 PM
KatFennec's Avatar
KatFennec KatFennec is offline
Member
 
Join Date: Apr 2017
Posts: 57
Gender: Female
Credits: 9,369
KatFennec is on a distinguished road
Default

Quote:
Originally Posted by Dinocanid View Post
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
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");
    } 
__________________

Last edited by KatFennec; 07-02-2017 at 02:45 PM.
Reply With Quote
  #6  
Old 07-04-2017, 09:55 AM
Abronsyth's Avatar
Abronsyth Abronsyth is offline
A Headache Embodied
 
Join Date: Aug 2011
Location: NY
Posts: 1,011
Gender: Male
Credits: 132,720
Abronsyth is on a distinguished road
Default

I'll try that out and let you know how it works!
__________________
My Mods Site (1.3.4, 2020 Mods)
Reply With Quote
  #7  
Old 07-05-2017, 08:12 AM
Hall of Famer's Avatar
Hall of Famer Hall of Famer is offline
Administrator, Lead Coder
 
Join Date: Dec 2008
Location: South Brunswick
Posts: 4,448
Gender: Male
Credits: 484,329
Hall of Famer is on a distinguished road
Default

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.
__________________


Mysidia Adoptables, a free and ever-improving script for aspiring adoptables/pets site.
Reply With Quote
  #8  
Old 07-06-2017, 08:21 AM
Abronsyth's Avatar
Abronsyth Abronsyth is offline
A Headache Embodied
 
Join Date: Aug 2011
Location: NY
Posts: 1,011
Gender: Male
Credits: 132,720
Abronsyth is on a distinguished road
Default

Quote:
Originally Posted by KatFennec View Post
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!
__________________
My Mods Site (1.3.4, 2020 Mods)
Reply With Quote
Reply


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


All times are GMT -5. The time now is 06:13 PM.

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