Daily Reward Mod

Set up a simple daily-reward system where once a day users can collect a reward on a specific page.


Step One:

First we need to set up the database by adding a column. In adopts_users add a new column with this information:
Name: daily
Type: varchar
Length: 3
Default: as defined (no)
And save

Step Two:

Now we're going to set up a cron job file (read about them here). Set up your cron.php like so:

<?php


class CronController extends AppController{


public function __construct(){


}

public function index(){


}

public function DailyReward(){

$mysidia = Registry::get("mysidia");

$mysidia->db->update("users", array("daily" => 'no'));


}


}

?>

Save that. Now in your C-Panel go to Cron Jobs, we're going to add a new one using the function we just created. Under Common Settings select Once Per Day (0 0 * * *), and in the command line add this:
curl http://YOURSITE.com/cron/DailyReward

Make sure you replace YOURSITE.com with your site's URL. Save that and you're good to go on the cronjob front. Once a day at midnight the database will now be updated so all of the adopts_users daily section will be switched to no.

Step 3:

Now it's time for the Daily Rewards page. Create a new file and call it daily.php and add this:

<?php


class DailyController extends AppController{


public function __construct(){

parent::__construct("member");

}

public function index(){

$mysidia = Registry::get("mysidia");

if($mysidia->input->post("submit")){

$mysidia->db->update("users", array("daily" => 'yes'), "username = '{$mysidia->user->username}'");

}


if($mysidia->input->post("currency")){

$mysidia->db->update("users", array("daily" => 'yes'), "username = '{$mysidia->user->username}'");

}


}

}

?>

Save and close this as we're done here. Now go into your view folder and create a new file called dailyview.php, and add the following:

<?php

class DailyView extends View{

public function index(){

$mysidia = Registry::get("mysidia");

$document = $this->document;

$document->setTitle("Daily Reward");

//Upon collecting...

if($mysidia->input->post("submit")){

$document->setTitle("Daily Reward Claimed");

$item = $mysidia->input->post("itemname");

//Check to see if they already have the item

$owned_qty = $mysidia->db->select("inventory", array("quantity"), "itemname ='{$item}' and owner ='{$mysidia->user->username}'")->fetchColumn();

if($owned_qty > 0){ // the item already exists, update the row for user's inventory

$new_qty = $owned_qty + 1;

$mysidia->db->update("inventory", array("quantity" => $new_qty), "itemname ='{$item}' and owner='{$mysidia->user->username}'");

} else { // the item does not exist yet

# find what $item's item category is so we can add it to the user's inventory

$category = $mysidia->db->select("items", array("category"), "itemname ='{$item}'")->fetchColumn();

# insert a new row into user's inventory

$mysidia->db->insert("inventory", array("iid" => NULL, "category" => $category, "itemname" => $item, "owner" => $mysidia->user->username, "quantity" => 1, "status" => 'Available'));

}

header( "Location: /daily" );

return;

}

//If currency reward

if($mysidia->input->post("currency")){

$document->setTitle("Daily Reward Claimed");

$amount = $mysidia->input->post("amount");

$cash = $mysidia->db->select("users", array("money"), "username ='{$mysidia->user->username}'")->fetchColumn();

$new_cash = $cash + $amount;

$mysidia->db->update("users", array("money" => $new_cash), "username ='{$mysidia->user->username}'");

header( "Location: /daily" );

return;

}

$document->add(new Comment("By visiting this page once a day, you are able to collect an item."));

//Now we have to choose what to show based on if the user has already collected or not.

$hascollected = $mysidia->db->select("users", array("daily"), "username ='{$mysidia->user->username}'")->fetchColumn();

if($hascollected == 'yes'){

$document->setTitle("Daily Reward");

$document->add(new Comment("<div style='text-align:center;'><br><br>You have collected your daily reward. Come back tomorrow.</div>"));

return;

}

//Now we get the user's group...

$group = $mysidia->db->select("users", array("usergroup"), "username ='{$mysidia->user->username}'")->fetchColumn();

//Now let's get the daily reward based on the user group! These are just examples, customize it to fit your needs. ITEMNAME must be EXACT.

switch($group){

case 3; //regular member

$reward_type = "currency";

$amount = 100;

break;

default; //the default that shows if the user's group isn't defined above

//Example of randomly selecting items...

$rand=rand(1,4);

if($rand == 1){

$reward_type = "item";

$item = "Beef Steak";

}

elseif($rand == 2){

$reward_type = "currency";

$amount = 100;

}

elseif($rand == 3){

$reward_type = "item";

$item = "Beef Steak";

}

elseif($rand == 4){

$reward_type = "item";

$item = "Turkey Leg";

}

break;

}

//Now let's add the form for them to collect the item!

if($reward_type == "currency"){

$document->add(new Comment("<br><br><div style='text-align:center;'>CURRENCY IMAGE HERE?<br>Today you can collect {$amount} coins!

<form action='../../daily' method='post'>

<input type='hidden' name='amount' value='{$amount}'>

<input type='submit' class='w3-button w3-green' name='currency' value='Collect'>

</form></div>"));

}

elseif($reward_type == "item"){

$itemurl = $mysidia->db->select("items", array("imageurl"), "itemname = '{$item}'")->fetchColumn();

$document->add(new Comment("<br><br><div style='text-align:center;'><img src='{$itemurl}'><br>Today you can collect a <b>{$item}</b>!

<form action='../../daily' method='post'>

<input type='hidden' name='itemname' value='{$item}'>

<input type='submit' class='w3-button w3-green' name='submit' value='Collect'>

</form></div>"));

}

}


}

?>


Save, and that's it! Now you can visit the page at yoursite.com/daily

Just make sure to customize the item sections, currency, user group section, and, optionally, the flavor text. Feel free to reach out on the Mysidia Discord if you have any questions!