PDA

View Full Version : Mys 1.3.4 Achievments system


Dinocanid
01-16-2017, 02:00 PM
-What This Does-
Users are able to unlock achievements by completing certain tasks, owning certain adoptables, or owning certain items. This is the same system I use on my site.

-How This Works-
There is no adminCP functionality, so new achievements have to be coded in manually; especially if you want the conditions to be complex. The example achievement goes by "fullpantry", but you can change the name to whatever since I was just too lazy to do it myself for the sake of this tutorial.

-Step 1-
To get started, we need to create two php files. In your public_html folder, create a new file called achievements.php. We're going to make a simple achievement that unlocks when a user owns a certain amount of adoptables.
Inside achievements.php, paste this:
<?php
class AchievementsController extends AppController{

public function __construct(){
parent::__construct("member");
}

public function index(){
$mysidia = Registry::get("mysidia");
}

}
?>

-Step 2-
You will most likely never have to touch the above code again. Now go to your view folder and create a new file called achievementsview.php. Inside, paste this:
<style>
.s_top {
overflow:hidden;
display: block;
}
.sc_npc_text{
width: 300px;
float: left;
position: relative;
left: 38%;
height: 70px;
padding: 15px;
margin: 10px;
margin-top: 30px;
font-family: 'Trebuchet MS', Helvetica, sans-serif;
overflow: auto;
}
.sc_npc_img{
position: relative;
}

.sc_item {
display: inline-table;
padding: 5px;
text-align: center;
font-family: 'Trebuchet MS', Helvetica, sans-serif;
font-size: 14px;
margin-bottom: 3px;
}
.s_panel {
border-radius: 2px;
border: 1px solid #CCC;
background-color: #FBFDF2;
}
.s_lockedpanel {
border-radius: 2px;
border: 1px solid #000000;
background-color: #898989;
}
</style>
<?php
class AchievementsView extends View{

public function index(){
$mysidia = Registry::get("mysidia");
$document = $this->document;
$status = $mysidia->user->getstatus();

$document->setAlign(new Align("center", "middle"));
$document->setTitle("Achievements");

$document->add(new Paragraph());
$document->add(new Comment("<h2> Unlocked </h2>"));
if($status->fullpantry == "unlocked"){
$document->add(new Comment("
<div class=\"s_panel sc_item\">
<img rel='tooltip' title='<em>Achievement Description<em>' src='InsertImageHere'/}>
<br/>
<b>Achievement Name</b>
<br/>

</div>", FALSE));
}

$document->add(new Paragraph());
$document->add(new Comment("<h2> Locked </h2>"));


if($status->fullpantry == "locked"){
$document->add(new Comment("
<div class=\"s_lockedpanel sc_item\">
<img rel='tooltip' title='<em>Own 10 or more adoptables to unlock<em>' src='InsertImageHere'/}>
<br/>
<b>???</b>
<br/>
</div>", FALSE));
}
}

}
(I know the style tags aren't exactly optimal, though it doesn't throw any errors, so feel free to add them to your css instead if you wish. It's the same code from the item shop mod.)
Looking at it now, this code is supposed to unlock when a user owns 10 or more adoptables. The images are used for the achievement images. I recommend using a "mystery image"/silhouette for the locked items and a normal image for the unlocked one.

-Step 3-
Now we go to myadoptsview.php and add this in public_function index:
$adoptAmount = $mysidia->db->select("owned_adoptables", array("aid"), "owner = '{$mysidia->user->username}'")->rowCount();
if($adoptAmount >= 10){
$mysidia->db->update("users_status", array("fullpantry" => 'unlocked'), "username='{$mysidia->user->username}'");
}

-Step 4-
Now we go to phpMyAdmin. Go to adopts_users_status and create a new column with this information:

Name: fullpantry
Type: VARCHAR
Length/Values: 8
Default: (as defined) locked
Collation: latin1_swedish_ci
Check the null box
Comment: Milestone Start

-End-
Simple, enough? Now we have an achievement that unlocks when the user owns 10 or more pets. It uses tooltips for the descriptions, so go here (http://www.mysidiaadoptables.com/forum/showthread.php?t=4741) if you want to know how those work.

-Pet based achievements-
Now what if you want to reward the user for owning a certain pet? In your achievmentsview.php, you would add this line under $status,
$Adoptcheck = $mysidia->db->select("owned_adoptables", array("type"), "type ='Species Name' and owner ='{$mysidia->user->username}'")->rowCount();
if($Adoptcheck >= 1){$mysidia->db->update("users_status", array("OwnCertainAdopt" => 'unlocked'), "username='{$mysidia->user->username}'");}

Then you put this in the "locked" section of your script:
if($status->OwnCertainAdopt == "locked"){
$document->add(new Comment("
<div class=\"s_lockedpanel sc_item\">
<img rel='tooltip' title='<em>Own InsertSpeciesHere to unlock<em>' src='IMAGE'/}>
<br/>
<b>???</b>
<br/>

</div>", FALSE));
}

And this in the "unlocked" section:
if($status->OwnCertainAdopt == "unlocked"){
$document->add(new Comment("
<div class=\"s_panel sc_item\">
<img rel='tooltip' title='<em>Description<em>' src='IMAGE'/}>
<br/>
<b>Achievment Name</b>
<br/>

</div>", FALSE));
}

Now you go to phpMyAdmin and add a new column with this information under adopts_users_status,

Name: OwnCertainAdopt
Type: VARCHAR
Length/Values: 8
Default: (as defined) locked
Collation: latin1_swedish_ci
Check the null box


-Item based achievements-
To reward a user for owning a certain item, follow the same formula by adding this with all the other "check" code:
$Itemcheck = $mysidia->db->select("inventory", array("itemname"), "itemname ='ItemNameHere' and owner ='{$mysidia->user->username}'")->rowCount();
if($Itemcheck >= 1){$mysidia->db->update("users_status", array("OwnCertainItem" => 'unlocked'), "username='{$mysidia->user->username}'");}

Now add this in the locked section:
if($status->OwnCertainItem == "locked"){
$document->add(new Comment("
<div class=\"s_lockedpanel sc_item\">
<img rel='tooltip' title='<em>Own InsertItemHere to unlock<em>' src='IMAGE'/}>
<br/>
<b>???</b>
<br/>

</div>", FALSE));
}

And this in the unlocked section:
if($status->OwnCertainItem == "unlocked"){
$document->add(new Comment("
<div class=\"s_panel sc_item\">
<img rel='tooltip' title='<em>Description<em>' src='IMAGE'/}>
<br/>
<b>Achievment Name</b>
<br/>

</div>", FALSE));
}

Finally, add the achievement to phpMyAdmin like we did in the past steps with this information:
Name: OwnCertainItem
Type: VARCHAR
Length/Values: 8
Default: (as defined) locked
Collation: latin1_swedish_ci
Check the null box

-Extending this system-
Having trouble making your own conditions? Just reply to let me know and I'll try to help you out!

SilverDragonTears
01-16-2017, 02:20 PM
Nice!!! Thank you for sharing!

SilverDragonTears
01-18-2017, 09:08 PM
Can you help me? Where exactly does this go?

if($status->OwnCertainAdopt == "locked"){
$document->add(new Comment("
<div class=\"s_lockedpanel sc_item\">
<img rel='tooltip' title='<em>Own InsertSpeciesHere to unlock<em>' src='IMAGE'/}>
<br/>
<b>???</b>
<br/>

Dinocanid
01-18-2017, 09:57 PM
It would go in like this (following the example in step 2):
public function index(){
$mysidia = Registry::get("mysidia");
$document = $this->document;
$status = $mysidia->user->getstatus();

$document->setAlign(new Align("center", "middle"));
$document->setTitle("Achievements");

$document->add(new Paragraph());
$document->add(new Comment("<h2> Unlocked </h2>"));
if($status->fullpantry == "unlocked"){
$document->add(new Comment("
<div class=\"s_panel sc_item\">
<img rel='tooltip' title='<em>Achievement Description<em>' src='InsertImageHere'/}>
<br/>
<b>Achievement Name</b>
<br/>

</div>", FALSE));
}

$document->add(new Paragraph());
$document->add(new Comment("<h2> Locked </h2>"));


if($status->fullpantry == "locked"){
$document->add(new Comment("
<div class=\"s_lockedpanel sc_item\">
<img rel='tooltip' title='<em>Own 10 or more adoptables to unlock<em>' src='InsertImageHere'/}>
<br/>
<b>???</b>
<br/>
</div>", FALSE));
}

if($status->OwnCertainAdopt == "locked"){
$document->add(new Comment("
<div class=\"s_lockedpanel sc_item\">
<img rel='tooltip' title='<em>Own InsertSpeciesHere to unlock<em>' src='IMAGE'/}>
<br/>
<b>???</b>
<br/>
</div>", FALSE));
}

SilverDragonTears
01-18-2017, 10:20 PM
Thank you :)

So once you unlock it the locked images go away?

Dinocanid
01-18-2017, 11:12 PM
Yep. The code is set up so the locked image only appears when it was "locked" in phpMyAdmin. Same goes for the unlocked image, which only appears when said achievement says "unlocked" in phpMyAdmin; so they can't appear at the same time.

SilverDragonTears
01-20-2017, 04:07 PM
What would be some other achievement ideas to add to this?

IntoRain
01-20-2017, 04:24 PM
Hmm maybe for amount of money reached? Or number of clicks per day. That's all I can remember besides owning certain creatures or items.

Dinocanid
01-20-2017, 06:11 PM
I added a part for item-based achievements. I didn't test it myself though, so let me know if it causes any errors or doesn't work.

Amount of money reached sounds simple enough, but I'm not sure where the amount of clicks from a user is stored to be able to call it.

IntoRain
01-21-2017, 10:50 AM
It's not stored I think, you'd have to count it with rowCount() from the table vote_voters

Abronsyth
01-28-2017, 09:02 AM
Awesome mod, though I haven't had the chance to play with it yet...I was looking to add something like this :D

parayna
06-18-2017, 11:51 AM
I was just adding notes to the checklist I have for what I need to do on my site and got to the one about installing this mod when I had a slight idea regarding the achievements... eventually your user status might get very long if you have loads of achievements, so couldn't you create a new database thing called users_achievements that is generated when a new user signs up (the same way all users database rows are) and the default is set as locked. I actually don't think this would be that difficult so as soon as the film I'm watching is over I'll perhaps go upstairs and try and do it :smile: I'm mostly just talking "out loud" here but this would probably be just a way of keeping achievements separated for those like me with organisation issues lol