2/3/17 update: There is now a time-based options for riddles. Read the 3rd post for more info! (Attachments and tutorial updated)
Next to do... Add option for choice between items and pets.
Hey all!
This is something I've been developing for a while (don't laugh, I'm not very handy with PHP).
So, a great activity on adoptable sites is rewarding people with exclusive adoptables by having them solve a riddle! Unfortunately Mysidia's promocode system doesn't make this easy, as you don't want one person adopting hundreds of these exclusive pets by just re-entering the code.
That is where this modification comes in!
What it does: This mod allows admins to create riddles with a riddle name, clue, and reward (must be an adoptable at this time). Then the admin can set a chosen riddle to be the active riddle, meaning that only one will be shown and solvable at a time. This means you can create a bunch of riddles in one go, but only use whichever one you want at that time.
Users then visit the page and see the riddle. If they haven't yet solved it then they can try to. If it is incorrect they can try again. If it is correct then they are rewarded with the pet you set as a reward! Then when they go to visit the page again, if they've already answered the current active riddle then they will not be able to solve it again, even if they get rid of their reward pet. Then, when the admin sets a new active riddle, they are able to solve once again!
So, let's get started with the mod! First you'll need to run this SQL:
Code:
CREATE TABLE IF NOT EXISTS `adopts_active_riddle` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `adopts_riddle` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
`clue` varchar(500) DEFAULT NULL,
`solve` varchar(50) DEFAULT NULL,
`fromdate` varchar(20) DEFAULT NULL,
`reward` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `adopts_solved_riddles` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
`uid` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
That will create 3 new tables for you. If you ever forget what the clue, reward, name, etc is for a specific riddle just go to adopts_riddle in your database.
Now we're going to make the files, it is quite simple, no worries! ((Note that the two needed files are also attachments! Just make sure you put them in the correct directories!))
First up is riddle.php which goes in your home directory (the file the script is installed in):
PHP Code:
<?php
class RiddleController extends AppController{
public function __construct(){
parent::__construct("member");
}
public function index(){
$mysidia = Registry::get("mysidia");
}
}
?>
Save it (upload if you need to). Next up is the view file, which goes in the view folder; riddleview.php
PHP Code:
<?php
use Resource\Native\String;
use Resource\Collection\LinkedList;
class RiddleView extends View{
public function index(){
$mysidia = Registry::get("mysidia");
$document = $this->document;
$document->setTitle("Riddle");
$active = $mysidia->db->select("active_riddle", array("name"))->fetchColumn();
$ridname = $mysidia->db->select("riddle", array("name"), "name = '{$active}'")->fetchColumn();
$ridclue = $mysidia->db->select("riddle", array("clue"), "name = '{$active}'")->fetchColumn();
$ridsolve = $mysidia->db->select("riddle", array("solve"), "name = '{$active}'")->fetchColumn();
$reward = $mysidia->db->select("riddle", array("reward"), "name = '{$active}'")->fetchColumn();
date_default_timezone_set('EST');
$today = date('m/d/Y');
if($mysidia->input->post("answer")){
if($mysidia->input->post("solved") == $ridsolve){
$document->add(new Comment( "Congratulations, you have solved the riddle. You have recieved one {$reward} as a reward!"));
$mysidia->db->insert("solved_riddles", array("name" => $active, "uid" => $mysidia->user->uid));
$newadopt = new StockAdopt($reward);
$newadopt->append($mysidia->user->username);
$document->add(new Comment("<meta http-equiv='refresh' content='1;url=riddle' />"));
return TRUE;
}
else{
$document->add(new Comment( "Sorry, that is not the correct answer. Feel free to try again!"));
$document->add(new Comment("<meta http-equiv='refresh' content='1;url=riddle' />"));
return TRUE;
}
}
if($mysidia->input->post("add")){
$mysidia->db->insert("riddle", array("id" => NULL, "name" => $mysidia->input->post("name"), "clue" => $mysidia->input->post("clue"), "fromdate" => $mysidia->input->post("fromdate"), "solve" => $mysidia->input->post("solve"), "reward" => $mysidia->input->post("reward")));
$document->add(new Comment( "You have created a new riddle called {$mysidia->input->post("name")}."));
$document->add(new Comment("<meta http-equiv='refresh' content='1;url=riddle' />"));
return TRUE;
}
$checkdate = $mysidia->db->select("riddle", array("fromdate"), "fromdate ='{$today}'")->fetchColumn();
$name = $mysidia->db->select("riddle", array("name"), "fromdate ='{$today}'")->fetchColumn();
if($checkdate){
$mysidia->db->query("TRUNCATE TABLE adopts_active_riddle");
$mysidia->db->insert("active_riddle", array("id" => NULL, "name" => $name));
}
if($mysidia->input->post("activate")){
$mysidia->db->query("TRUNCATE TABLE adopts_active_riddle");
$mysidia->db->insert("active_riddle", array("id" => NULL, "name" => $mysidia->input->post("currname")));
$document->add(new Comment( "You have activated a new riddle."));
$document->add(new Comment("<meta http-equiv='refresh' content='1;url=riddle' />"));
return TRUE;
}
if($mysidia->user instanceof Admin){
$document->add(new Comment( "<h2>Manage Riddles</h2>"));
$document->add(new Comment( "You can add a new riddle below. Creating a new riddle will not make it the new shown riddle, you do that below.
Riddle answers and names must be the exact name of the pet reward, and they are case sensitive."));
$currForm = new Form("currform", "", "post");
$currForm->add(new Comment("<br><u>Set the Active Riddle:</u><br>", TRUE, "b"));
$currForm->add(new Comment("<br><b>Current Active Riddle:</b> {$ridname}<br>", TRUE, ""));
$currForm->add(new Comment("Name:"));
$currForm->add(new TextField("currname"));
$currForm->add(new Button("Activate", "activate", "submit"));
$document->add($currForm);
$ridForm = new Form("addform", "", "post");
$ridForm->add(new Comment("<br><u>Create A New Riddle:</u><br>", TRUE, "b"));
$ridForm->add(new Comment("Name:"));
$ridForm->add(new TextField("name"));
$ridForm->add(new Comment("Clue:"));
$ridForm->add(new TextArea("clue"));
$ridForm->add(new Comment("Solve: (answer to the riddle)"));
$ridForm->add(new TextField("solve"));
$ridForm->add(new Comment("Start Date:(MM/DD/YYYY) or leave blank"));
$ridForm->add(new TextField("fromdate"));
$ridForm->add(new Comment("Reward:(the adoptable your member can obtain by solving this riddle.)"));
$ridForm->add(new TextField("reward"));
$ridForm->add(new Button("Create Riddle", "add", "submit"));
$document->add($ridForm);
$document->add(new Comment( "<hr>"));
}
else{
$document->add(new Comment( ""));
}
$done = $mysidia->db->select("solved_riddles", array("name", "uid"), "name = '{$active}' and uid = '{$mysidia->user->uid}'")->fetchObject();
if($done){
$document->add(new Comment("<h2>Riddle Center</h2><br> {$today} Welcome to the Riddle Center! Here you can put your mind to the test and try to solve a riddle for the chance of a reward."));
$doneForm = new Form("doneform", "", "post");
$doneForm->add(new Comment("<br><h3><u>Active Riddle</u></h3>", TRUE, "b"));
$doneForm->add(new Comment("<b>{$ridname}</b>", TRUE, "b"));
$doneForm->add(new Comment("{$ridclue}"));
$doneForm->add(new Comment("<br><h3>You have completed this riddle, good job!</h3>", TRUE, "b"));
$document->add($doneForm);
}
else{
$document->add(new Comment("<h2>Riddle Center</h2><br> {$today} Welcome to the Riddle Center! Here you can put your mind to the test and try to solve a riddle for the chance of a reward."));
$solveForm = new Form("solveform", "", "post");
$solveForm->add(new Comment("<br><h3><u>Active Riddle</u></h3>", TRUE, "b"));
$solveForm->add(new Comment("<b>{$ridname}</b>", TRUE, "b"));
$solveForm->add(new Comment("{$ridclue}<br><br>"));
$solveForm->add(new Comment("Answer:", "", "b"));
$solveForm->add(new TextField("solved"));
$solveForm->add(new Button("Submit Answer", "answer", "submit"));
$document->add($solveForm);
}
}
}
?>
Save, and now go visit your new page at
http://yoursite.com/riddle
As long as you're logged in as an admin then you'll be able to create and manage riddles! Regular users cannot see this part, don't worry
Thank you
Thank you to DinoCanid, your raffle mod gave me the inspiration and some resources needed to complete this mod.
Thank you to IntoRain, your help with some of my faulty code made this possible.
Let me know if you encounter any issues or have questions :)