Mys 1.3.6 Higher/Lower Game with Prize: Playable Once a Day

Forum
Last Post
Threads / Messages

dlithehil

Member
Member
Joined
Jun 10, 2022
Messages
62
Points
8
Mysidian Dollar
499
First, we need to add the following columns to the [prefix]_users table:
1699664561577.png
  • hiloday and hilomonth will be a two digit representation of the day and month a player last played the game. (I included both so if they go exactly a month without playing, it won't mess up on them. You can also add a hiloyear but I thought that to be overkill.....)
  • hiloguess should be a single digit integer. It's default value should be one less than you intend to give guesses. I give 3 guesses, so my default value is 2.
  • randomnum is going to be changing every time a player plays. My number guessing game goes from 1-10, so I only need a 2 digit integer. I set it at 5 for the default, but like I said, it's going to be changing
Next, go to controller/main and create a file called hilocontroller.php and paste the following code:
PHP:
<?php

namespace Controller\Main;
use Resource\Core\AppController;
use Resource\Core\Registry;

class HiloController extends AppController {
    public function __construct() {
        parent::__construct("member");
    }
    public function index() {
        $mysidia = Registry::get("mysidia");
    }
}
?>

Finally, go to view/main and create a file called hiloview.php. Paste the following code:
PHP:
<?php

namespace View\Main;

use Controller\Main;
use Model\DomainModel\Member;
use Model\DomainModel\OwnedItem;
use Model\DomainModel\Item;
use Resource\Core\Registry;
use Resource\Core\View;
use Resource\GUI\Document\Comment;
use Resource\GUI\Component\Link;

class HiloView extends View
{
    public function index()
    {
        $mysidia = Registry::get("mysidia");
        $document = $this->document;
        $thisMonth = date("m");
        $today = date("d");
        $number = $_POST['number_entered'];
        $submitbutton = $_POST['submit'];
        $randomnumber = rand(1,10);
        $guessesLeft = $mysidia->user->hiloguess;

        $document->setTitle("Higher or Lower??");
        
        //this only lets players play once per day. at the end of the code, it sets hiloday and hilomonth to today's date.
        if (($mysidia->user->hiloday) != $today || ($mysidia->user->hilomonth) != $thisMonth) {
            $document->add(new Comment("
                <form action='' method='POST'>
                    Guess a number between 1 and 10!
                    <input type='text' name='number_entered' value=''/><br><br>
                    Result:
            "));

            if (!empty($submitbutton) && !empty($number)) {
                if (($number > 0) && ($number < 11)) {
                    if (($number != ($mysidia->user->randomnum)) && ($guessesLeft == 0)) {
                        $document->add(new Comment('Incorrect guess. The correct number was ', FALSE));
                        $document->add(new Comment(($mysidia->user->randomnum), FALSE));
                        $document->add(new Comment('. <br>Don\'t worry, you still get some a gold medal...'));
                        $mysidia->db->update(
                            "users",
                            [
                                "hiloday" => $today,
                                "hilomonth" => $thisMonth,
                            ],
                            "username='{$mysidia->user->getUsername()}'"
                        );
                        $mysidia->db->update(
                            "users",
                            ["hiloguess"=>2,
                            "randomnum"=>$randomnumber,],
                            "username='{$mysidia->user->getUsername()}'"
                        );
                        //change the item name to an item in your database to award it as a prize.
                        $ownedItem = new OwnedItem("Medal", $mysidia->user->getID());
                        $trueItem = new OwnedItem($ownedItem->getItemID(), $mysidia->user->getID());
                        $trueItem->add(1, $mysidia->user->getID());
                        
                        //if they're not out of guesses, it will tell them if they need to guess higher or lower.
                    } elseif (($number != ($mysidia->user->randomnum)) && ($guessesLeft != 0)) {
                        if ($number > ($mysidia->user->randomnum)) {
                            $document->add(new Comment('Incorrect. My number is lower. Try again.'));
                        } else {
                            $document->add(new Comment("Incorrect. My number is higher. Try again."));
                        }

                        $mysidia->db->update(
                            "users",
                            ["hiloguess" => ($guessesLeft -= 1)],
                            "username='{$mysidia->user->getUsername()}'"
                        );

                        $document->add(new Comment("You have ", FALSE));
                        $document->add(new Comment($guessesLeft, FALSE));
                        $document->add(new Comment(" guess(es) left."));
                    } else {
                        $document->add(new Comment(($mysidia->user->randomnum), FALSE));
                        $document->add(new Comment(' is correct! You got it right. You get the grand prize! Check your inventory!'));

                        $mysidia->db->update(
                            "users",
                            [
                                "hiloday" => $today,
                                "hilomonth" => $thisMonth,
                            ],
                            "username='{$mysidia->user->getUsername()}'"
                        );
                        $mysidia->db->update(
                             "users",
                            ["hiloguess"=>2,
                            "randomnum"=>$randomnumber,],
                            "username='{$mysidia->user->getUsername()}'"
                        );
                        //again, update the item name to one in your site.
                        $ownedItem = new OwnedItem("Gem", $mysidia->user->getID());
                        $trueItem = new OwnedItem($ownedItem->getItemID(), $mysidia->user->getID());
                        $trueItem->add(1, $mysidia->user->getID());
                    }
                }
            }

            $document->add(new Comment('
                <br><br>
                <input type="submit" name="submit" value="Guess"/><br><br>
                </form>
            '));
        } else {
            //this resets their random number again, but still won't let them play until the next day
            $document->add(new Comment("You've already played today. Come back tomorrow!"));
            $mysidia->db->update(
                "users",
                ["hiloguess"=>2,
                "randomnum"=>$randomnumber],
                "username='{$mysidia->user->getUsername()}'"
            );
        }
    }
}

This is my first time making a mod/addon so if you have any trouble with it or can think of a way to do it better, I'm here!!~
 
Is there a way to change this so it gives currency instead?

Change every instance of:

PHP:
$ownedItem = new OwnedItem("Medal", $mysidia->user->getID());
                        $trueItem = new OwnedItem($ownedItem->getItemID(), $mysidia->user->getID());
                        $trueItem->add(1, $mysidia->user->getID());

to:

PHP:
$mysidia->user->changeMoney($reward);

Change $reward to the amount you want to give. If you want the number to be random, you can use:

Code:
$reward = mt_rand(100,500);
            $mysidia->user->changeMoney($reward);

The 2 numbers are min and max so feel free to change to whatever you like.
 
Change every instance of:

PHP:
$ownedItem = new OwnedItem("Medal", $mysidia->user->getID());
                        $trueItem = new OwnedItem($ownedItem->getItemID(), $mysidia->user->getID());
                        $trueItem->add(1, $mysidia->user->getID());

to:

PHP:
$mysidia->user->changeMoney($reward);

Change $reward to the amount you want to give. If you want the number to be random, you can use:

Code:
$reward = mt_rand(100,500);
            $mysidia->user->changeMoney($reward);

The 2 numbers are min and max so feel free to change to whatever you like.
Ok so I think I edited it right... but how do you get to the game? is it [YOURSITE]/hiloview.php? Because if so it isn't working for me.

I think it's a syntax error that's throwing it off.
 
Last edited:
No, it would just be yoursite.com/hilo. You don't need the .php or the view. Essentially the view and controller files join together to create 1 page. And the functions are essentially sub-pages, excluding index which is what you see when you just visit /hilo. So if you had a public function view in hiloview and hilocontroller you could then go to yoursite.com/hilo/view
 
No, it would just be yoursite.com/hilo. You don't need the .php or the view. Essentially the view and controller files join together to create 1 page. And the functions are essentially sub-pages, excluding index which is what you see when you just visit /hilo. So if you had a public function view in hiloview and hilocontroller you could then go to yoursite.com/hilo/view
I can't get the view file to work with the edits.
 
Try these. I had to rework it a little but this should work.

hiloview:

PHP:
<?php

namespace View\Main;

use Controller\Main;
use Model\DomainModel\Member;
use Model\DomainModel\OwnedItem;
use Model\DomainModel\Item;
use Resource\Core\Registry;
use Resource\Core\View;
use Resource\GUI\Document\Comment;
use Resource\GUI\Component\TextField;
use Resource\GUI\Container\Form;
use Resource\GUI\Component\Button;

class HiloView extends View {

    public function index() {
        $mysidia = Registry::get("mysidia");
        $document = $this->document;
        $thisMonth = date("m");
        $today = date("d");
        $randomnumber = rand(1,10);
        $compReward = mt_rand(10,100);
        $grandReward = mt_rand(101,500);

        $document->setTitle("Higher or Lower??");
        
        //this only lets players play once per day. at the end of the code, it sets hiloday and hilomonth to today's date.
        if (($mysidia->user->hiloday) != $today || ($mysidia->user->hilomonth) != $thisMonth) {
            $document->add(new Comment("Guess a number between 1 and 10!"));
            $hiloForm = new Form("hiloform", "", "post");
            $hiloForm->add(new TextField("number_entered"));
            $hiloForm->add(new Comment("Result: ", FALSE));
            $hiloForm->add(new Button("Guess!", "submit", "submit"));

            $document->add($hiloForm);

            if ($mysidia->input->post("submit")) {
                if (($mysidia->input->post("number_entered") > 0) && ($mysidia->input->post("number_entered") < 11)) {
                    if (($mysidia->input->post("number_entered") != ($mysidia->user->randomnum)) && ($mysidia->user->hiloguess == 0)) {
                        $document->add(new Comment('Incorrect guess. The correct number was ', FALSE));
                        $document->add(new Comment(($mysidia->user->randomnum), FALSE));
                        $document->add(new Comment('. <br>Don\'t worry, here\s a compensation prize!'));
                        $mysidia->db->update("users",["hiloday" => $today,"hilomonth" => $thisMonth], "uid='{$mysidia->user->getID()}'");
                        $mysidia->db->update("users",["hiloguess" => 2,"randomnum" => $randomnumber], "uid='{$mysidia->user->getID()}'");
                        $mysidia->user->changeMoney($compReward);
                    }
                    elseif (($mysidia->input->post("number_entered") != ($mysidia->user->randomnum)) && ($mysidia->user->hiloguess != 0)) {
                        if ($mysidia->input->post("number_entered") > ($mysidia->user->randomnum)) {
                            $document->add(new Comment('Incorrect. My number is lower. Try again.'));
                        }
                        elseif ($mysidia->input->post("number_entered") < ($mysidia->user->randomnum)) {
                            $document->add(new Comment("Incorrect. My number is higher. Try again."));
                        }
                        $mysidia->db->update("users",["hiloguess" => ($mysidia->user->hiloguess -= 1)], "uid='{$mysidia->user->getID()}'"
                        );
                        $document->add(new Comment("You have ", FALSE));
                        $document->add(new Comment($mysidia->user->hiloguess, FALSE));
                        $document->add(new Comment(" guess(es) left."));
                    }
                    elseif ($mysidia->input->post("number_entered") == ($mysidia->user->randomnum)) {
                        $document->add(new Comment(($mysidia->user->randomnum), FALSE));
                        $document->add(new Comment(' is correct! You got it right. You get the grand prize! Check your money!'));
                        $mysidia->db->update("users",["hiloday" => $today, "hilomonth" => $thisMonth], "uid='{$mysidia->user->getID()}'");
                        $mysidia->db->update("users",["hiloguess" => 2, "randomnum" => $randomnumber], "uid='{$mysidia->user->getID()}'");
                        $mysidia->user->changeMoney($grandReward);
                    }
                }
            }

        }
        else{
            $document->add(new Comment("You've already played today! Come back tomorrow!"));
        }
    }
}
?>

hilocontroller:

PHP:
<?php

namespace Controller\Main;
use Resource\Core\AppController;
use Resource\Core\Registry;

class HiloController extends AppController {
    public function __construct() {
        parent::__construct("member");
    }
    public function index() {
        $mysidia = Registry::get("mysidia");
    }
}
?>
 
Try these. I had to rework it a little but this should work.

hiloview:

PHP:
<?php

namespace View\Main;

use Controller\Main;
use Model\DomainModel\Member;
use Model\DomainModel\OwnedItem;
use Model\DomainModel\Item;
use Resource\Core\Registry;
use Resource\Core\View;
use Resource\GUI\Document\Comment;
use Resource\GUI\Component\TextField;
use Resource\GUI\Container\Form;
use Resource\GUI\Component\Button;

class HiloView extends View {

    public function index() {
        $mysidia = Registry::get("mysidia");
        $document = $this->document;
        $thisMonth = date("m");
        $today = date("d");
        $randomnumber = rand(1,10);
        $compReward = mt_rand(10,100);
        $grandReward = mt_rand(101,500);

        $document->setTitle("Higher or Lower??");
       
        //this only lets players play once per day. at the end of the code, it sets hiloday and hilomonth to today's date.
        if (($mysidia->user->hiloday) != $today || ($mysidia->user->hilomonth) != $thisMonth) {
            $document->add(new Comment("Guess a number between 1 and 10!"));
            $hiloForm = new Form("hiloform", "", "post");
            $hiloForm->add(new TextField("number_entered"));
            $hiloForm->add(new Comment("Result: ", FALSE));
            $hiloForm->add(new Button("Guess!", "submit", "submit"));

            $document->add($hiloForm);

            if ($mysidia->input->post("submit")) {
                if (($mysidia->input->post("number_entered") > 0) && ($mysidia->input->post("number_entered") < 11)) {
                    if (($mysidia->input->post("number_entered") != ($mysidia->user->randomnum)) && ($mysidia->user->hiloguess == 0)) {
                        $document->add(new Comment('Incorrect guess. The correct number was ', FALSE));
                        $document->add(new Comment(($mysidia->user->randomnum), FALSE));
                        $document->add(new Comment('. <br>Don\'t worry, here\s a compensation prize!'));
                        $mysidia->db->update("users",["hiloday" => $today,"hilomonth" => $thisMonth], "uid='{$mysidia->user->getID()}'");
                        $mysidia->db->update("users",["hiloguess" => 2,"randomnum" => $randomnumber], "uid='{$mysidia->user->getID()}'");
                        $mysidia->user->changeMoney($compReward);
                    }
                    elseif (($mysidia->input->post("number_entered") != ($mysidia->user->randomnum)) && ($mysidia->user->hiloguess != 0)) {
                        if ($mysidia->input->post("number_entered") > ($mysidia->user->randomnum)) {
                            $document->add(new Comment('Incorrect. My number is lower. Try again.'));
                        }
                        elseif ($mysidia->input->post("number_entered") < ($mysidia->user->randomnum)) {
                            $document->add(new Comment("Incorrect. My number is higher. Try again."));
                        }
                        $mysidia->db->update("users",["hiloguess" => ($mysidia->user->hiloguess -= 1)], "uid='{$mysidia->user->getID()}'"
                        );
                        $document->add(new Comment("You have ", FALSE));
                        $document->add(new Comment($mysidia->user->hiloguess, FALSE));
                        $document->add(new Comment(" guess(es) left."));
                    }
                    elseif ($mysidia->input->post("number_entered") == ($mysidia->user->randomnum)) {
                        $document->add(new Comment(($mysidia->user->randomnum), FALSE));
                        $document->add(new Comment(' is correct! You got it right. You get the grand prize! Check your money!'));
                        $mysidia->db->update("users",["hiloday" => $today, "hilomonth" => $thisMonth], "uid='{$mysidia->user->getID()}'");
                        $mysidia->db->update("users",["hiloguess" => 2, "randomnum" => $randomnumber], "uid='{$mysidia->user->getID()}'");
                        $mysidia->user->changeMoney($grandReward);
                    }
                }
            }

        }
        else{
            $document->add(new Comment("You've already played today! Come back tomorrow!"));
        }
    }
}
?>

hilocontroller:

PHP:
<?php

namespace Controller\Main;
use Resource\Core\AppController;
use Resource\Core\Registry;

class HiloController extends AppController {
    public function __construct() {
        parent::__construct("member");
    }
    public function index() {
        $mysidia = Registry::get("mysidia");
    }
}
?>
That worked, thank you!!
 

Similar threads

Users who are viewing this thread

  • Forum Contains New Posts
  • Forum Contains No New Posts

Forum statistics

Threads
4,277
Messages
33,120
Members
1,602
Latest member
BerrieMilk
BETA

Latest Threads

Top