I have a game called Ninja Gold that I have already coded successfully. I am using PHP with CodeIgniter. How it works is when the index page loads the session variables (Gold and Activities) are set if there are not any. By clicking each location, gold within a certain amount range is added to the total amount. The exception is the casino location where money could possibly be lost as well. This is created using the an odds generator to determine if the amount from the casino is won or lost. Once the location is clicked and the gold is added or lost, an activity message is displayed to tell the user what happened. Here is the code in my Ninjas controller:
defined('BASEPATH') OR exit('No direct script access allowed');
date_default_timezone_set('America/Los_Angeles');
class Ninjas extends CI_Controller {
public function index()
{
if (!$this->session->userdata('gold') && !$this->session->userdata('activities'))
{
$this->session->set_userdata('gold', 0);
$this->session->set_userdata('activities', [" ", "win"]);
}
$this->load->view('index');
}
public function process_money()
{
$building = $this->input->post('building');
//for an alternative to if/else, going to use case/switch
switch ($building) {
case 'farm':
$gold = rand(10,20);
break;
case 'cave':
$gold = rand(5,10);
break;
case 'house':
$gold = rand(2,5);
break;
case 'casino':
$odds = rand(0,1);
$gold = rand(0,50);
if ($odds == 0)
{
$gold *= -1;
}
break;
default:
$gold = 0;
break;
}
if ($gold < 0)
{
$activity = ['You entered a casino and lost ' . $gold . ' gold(s). Ouch!', 'loss'];
}
else
{
$activity = ['You entered a ' . $building . ' and earned ' . $gold . ' gold(s)', 'win'];
}
// $activities = $this->session->userdata('activities');
// array_unshift($activities, $activity);
$this->session->set_userdata('activities', $activity);
$running_total = $this->session->userdata('gold');
$running_total += $gold;
$this->session->set_userdata('gold', $running_total);
redirect('http://localhost:8888/');
}
}
Here is the index page code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Ninja Gold</title>
<link rel="stylesheet" href="<?= base_url('assets/css/style.css'); ?>">
</head>
<body>
<h1>Your Gold: <?= $this->session->userdata('gold') ?></h1>
<form action="/process_money" method="post">
<h2>Farm</h2>
<p>Earns 10-20 golds</p>
<input type="hidden" name="building" value="farm">
<input type="submit" value="Find Gold!">
</form>
<form action="/process_money" method="post">
<h2>Cave</h2>
<p>Earns 5-10 golds</p>
<input type="hidden" name="building" value="cave">
<input type="submit" value="Find Gold!">
</form>
<form action="/process_money" method="post">
<h2>House</h2>
<p>Earns 2-5 golds</p>
<input type="hidden" name="building" value="house">
<input type="submit" value="Find Gold!">
</form>
<form action="/process_money" method="post">
<h2>Casino</h2>
<p>Earns/Loses 0-50 golds</p>
<input type="hidden" name="building" value="casino">
<input type="submit" value="Find Gold!">
</form>
<div class="activities">
<?php foreach ($this->session->userdata('activities') as $activity) { ?>
<p class="<?= $activity[1] ?>"><?= $activity[0] ?></p>
<?php } ?>
</div>
</body>
</html>
I am using forms to submit the data with POST methods. What I want to do is recode the game to only have the index function in the Ninjas controller and not submit to another URL (ninjas/process_money) when the locations forms (name = "building" in code) are submitted. That way I can have music play in the background and not be interrupted. What is the best way to do this? Questions:
Would it be best to use Ajax to submit the data via forms so the page doesn't have to be refreshed? I could easily recode the game using JavaScript but I want to do this using PHP session variables if possible to show my PHP skills in my portfolio.
Is it possible to nest functions inside the index function to make this work? I believe this isn't but want to make sure. Even if so I suspect this wouldn't be best practice correct?