aboutsummaryrefslogtreecommitdiffstats
path: root/inc/Inventory.inc
diff options
context:
space:
mode:
Diffstat (limited to 'inc/Inventory.inc')
-rw-r--r--inc/Inventory.inc70
1 files changed, 69 insertions, 1 deletions
diff --git a/inc/Inventory.inc b/inc/Inventory.inc
index 3e0137d..4a1add8 100644
--- a/inc/Inventory.inc
+++ b/inc/Inventory.inc
@@ -2,18 +2,44 @@
require_once("perso.inc");
+/**
+ * Represent the player's Inventory.
+ * Implemented as a singleton in the session.
+ *
+ * @package inc\Inventory.inc
+ * @author Alexandre Renoux
+ * @author Pierre-Emmanuel Novac
+ */
class Inventory {
-
+ /**
+ * @var array Arrays of array with Item object and Item's count as int
+ */
public $items = array();
+ /**
+ * Checks if the Inventory was already created in the session
+ *
+ * @return boolean true if Inventory was already created, false otherwise
+ */
public static function created() {
return !empty($_SESSION["inventory"]);
}
+ /**
+ * Returns the Inventory's content from the session.
+ *
+ * @return array array of Items and number
+ */
public static function sendContent() {
return self::get()->items;
}
+ /**
+ * Returns the Inventory from the session.
+ * Implements the singleton pattern.
+ *
+ * @return Inventory the Inventory object
+ */
public static function get() {
if(!self::created()) {
$_SESSION["inventory"] = new Inventory();
@@ -21,6 +47,12 @@ class Inventory {
return $_SESSION["inventory"];
}
+ /**
+ * Adds an Item object to the Inventory, incrementing the Item's count if it was already present.
+ *
+ * @param Item $item Item to add to the Inventory
+ * @return array Item and Item's count
+ */
private function _addItem($item) {
foreach($this->items as $k => $object){
if($object[0] == $item){
@@ -33,12 +65,24 @@ class Inventory {
return $tab;
}
+ /**
+ * Adds an Item object to the singleton's Inventory from session.
+ *
+ * @param Item $item Item to add to the Inventory
+ * @return array Item and Item's count
+ */
public static function addItem($item) {
$inv = self::get();
$tab = $inv->_addItem($item);
return $tab;
}
+ /**
+ * Removes an Item object from the Inventory, not taking into account the Item's count.
+ *
+ * @param Item $item Item to remove from the Inventory
+ * @return void
+ */
private function _removeItem($item) {
foreach($this->items as $k => $object) {
if($object[0] == $item) {
@@ -48,11 +92,23 @@ class Inventory {
}
}
+ /**
+ * Removes an Item object to the singleton's Inventory from session, not taking into account the Item's count.
+ *
+ * @param Item $item Item to remove from the Inventory
+ * @return void
+ */
public static function removeItem($item) {
$inv = self::get();
$inv->_removeItem($item);
}
+ /**
+ * Consumes an Item object from the Inventory, decrementing the Item's count and removing it if count reaches 0.
+ *
+ * @param Item $item Item to consume from the Inventory
+ * @return array|false Item and updated Item's count, false if it was not found
+ */
private function _useItem($item) {
foreach($this->items as $k => $object){
if($object[0] == $item) {
@@ -70,12 +126,24 @@ class Inventory {
return false;
}
+ /**
+ * Consumes an Item object to the singleton's Inventory from session, decrementing the Item's count and removing it if count reaches 0.
+ *
+ * @param Item $item Item to consume from the Inventory
+ * @return array|false Item and updated Item's count, false if it was not found
+ */
public static function useItem($item) {
$inv = self::get();
$it = $inv->_useItem($item);
return $it;
}
+ /**
+ * Generates an XML tree describing the Inventory
+ *
+ * @param SimpleXMLElement $root root XML element to add the Inventory's Items to
+ * @return void
+ */
public function addToXML($root) {
foreach($this->items as $item)
$item[0]->addToXML($root, $item[1]);