Because I'm rather new to PHP and Object Oriented Programming, I'm often looking for some practice. Thus I decided to focus on making a browser-based Mafia game that should work like a Dutch variant: http://www.bendes.nl/.
Before I go any further, I would like to put a disclaimer right here. I'm new to StackExchange and especially CodeReview. My project is not finished, but I still felt like my question suited to this platform. If it's not, please feel free to give me the right directions.
I'm not trying to improve in MySQL right now, just the fundamentals of PHP and OOP
The concept is simple: the player needs to commit crimes in order to make money. They can spend their money on food/meds to generate health, weapons to increase probability and so on.
Firstly, I created a class player
in player.php that has several methods that influence the player's stats. For example, raiseXp()
raises the experience points of the player, but calls the levelUp()
function when it reaches the maximum value of the current rank (exp
).
player.php
<?php
class Player {
var $name;
var $exp = 1;
var $xp = 0;
var $max_xp = 100;
var $wealth = 0;
var $health = 100;
function __construct($name) {
$this->name = $name;
}
function initXp() {
$factor = 1.11;
$this->max_xp *= $factor;
}
function raiseXp($amount) {
$this->xp += $amount;
if ($this->xp >= $this->max_xp) {
$this->levelUp($this->xp - $this->max_xp);
}
}
function levelUp($leftover) {
$reward = 5 * $this->rank;
$this->exp++;
$this->xp = $leftover;
$this->initXp();
}
function setHealth($value) {
if ($value > 0 && $value < 101) {
$this->health = $value;
}
}
function increaseHealth($amount) {
if (100 - $this->health >= $amount) {
$this->health += $amount;
} else {
$this->health = 100;
}
}
function decreaseHealth($amount) {
$this->health -= $amount;
if ($this->health <= 0) {
$this->goToHospital();
}
}
function goToHospital() {
$this->setHealth(70);
$this->forceDecreaseWealth(300);
}
function increaseWealth($amount) {
$this->wealth += $amount;
}
function decreaseWealth($amount) {
if ($this->wealth >= $amount) {
$this->wealth -= $amount;
} else {
// Error
}
}
function forceDecreaseWealth($amount) {
if ($this->wealth >= $amount) {
$this->wealth -= $amount;
} else {
$this->wealth -= $amount;
$debt = $this->wealth - $this->wealth * 2;
}
}
}
?>
At the top of my index file, I included the player
class, checked for an open session and built a new player object inside a session variable:
index.php
<?php
// Include controllers
include "controller/player.php";
// Start session handler
session_start();
// Check if there's a login session active
if (isset($_SESSION["player"])) {
$player = $_SESSION["player"];
} else {
$_SESSION["player"] = new Player('Testspeler');
$player = $_SESSION["player"];
}
?>
You might have noticed I included something above session_start()
. From what I know, you should always put session_start()
at the very top of you document. The thing is, $_SESSION["player"]
wouldn't remember if I put it at the top.
Next, I created a Crime class. This is the main blueprint of a crime with a method (commit()
).
<?php
class Crime {
var $reward;
var $xp_boost;
var $probability;
function __construct($reward, $xp_boost, $probability) {
$this->reward = $reward;
$this->xp_boost = $xp_boost;
$this->probability = $probability;
}
function commit() {
$result = mt_rand(0, 101);
if ($result <= $this->probability) {
return true;
} else {
return false;
}
}
}
$crimes = array(
"Winkelwagens stelen" => new Crime(1, $player->rank, 80)
);
?>
I put these in a table using a foreach loop. I can't seem to understand how a user would select a crime and "launch" the commit function.
<table>
<tr>
<th colspan="100%">Misdaden</th>
</tr>
<tr>
<th>Misdaad</th>
<th>XP</th>
<th>Geld</th>
<th>Waarschijnlijkheid</th>
<th></th>
</tr>
<?php foreach ($crimes as $x => $y) : ?>
<tr>
<td><?php echo $x; ?></td>
<td><?php echo $y->xp_boost; ?></td>
<td><?php echo $y->reward; ?></td>
<td><?php echo $y->probability; ?></td>
<td><!-- Somehow call $y->commit() when the user clicks? --></td>
</tr>
<?php endforeach; ?>
My Question(s)
- Writing style. What would you change about my writing style? Is it neat? Is it inconsistent? Vague?
- Choices and approaches. Do you see any choices I made that you wouldn't have dared to make at all?