public function test_get_total_weight() { $x = new CharacterSheet($this->dbObj, __METHOD__, 1); $this->assertTrue(is_numeric($x->characterId), ToolBox::debug_print($x)); $this->assertEquals(0, $x->get_total_weight(false)); $this->assertEquals($x->get_total_weight(true), $x->get_total_weight(false)); //now create some gear. $manualWeight = 0; $itemList = array(); $createThis = array(array('torches', 1, 10), array('lead nuggets', 10, 10), array('misc', 4, 200)); $g = new Gear(); $g->characterId = $x->characterId; foreach ($createThis as $data) { $manualWeight += round($data[1] * $data[2], 1); $xData = array('character_id' => $x->characterId, 'gear_name' => $data[0], 'weight' => $data[1], 'quantity' => $data[2]); $id = $g->create($this->dbObj, $xData); $itemList[$id] = $xData; } $this->assertEquals($manualWeight, Gear::calculate_list_weight($itemList)); //now, at first, this should be 0 because we haven't re-loaded the sheet. $this->assertEquals(0, $x->get_total_weight(false)); $this->assertEquals(0, $x->get_total_weight(true)); $x->load(); $this->assertEquals($manualWeight, $x->get_total_weight(false)); $this->assertEquals($manualWeight, $x->get_total_weight(true)); $withWeapons = $manualWeight; $w = new Weapon(); $w->characterId = $x->characterId; $wpns = array('great_sword' => 5, 'long_sword' => 2, 'short_sword' => 1); foreach ($wpns as $name => $wgt) { $xData = array('character_id' => $x->characterId, 'weapon_name' => $name, 'weight' => $wgt); $id = $w->create($this->dbObj, $xData); $itemList[$id] = $xData; $withWeapons += $wgt; } $x->load(); $this->assertNotEquals($manualWeight, $withWeapons); $this->assertTrue($withWeapons > $manualWeight); $this->assertEquals($manualWeight, $x->get_total_weight(false)); $this->assertEquals($x->get_total_weight(), $x->get_total_weight(false)); $this->assertEquals($withWeapons, $x->get_total_weight(true)); $withArmor = $withWeapons; $a = new Armor(); $rmr = array('big' => 10, 'small' => 1); foreach ($rmr as $name => $wgt) { $xData = array('character_id' => $x->characterId, 'armor_name' => $name, 'weight' => $wgt); $id = $a->create($this->dbObj, $xData); $itemList[$id] = $xData; $withArmor += $wgt; } $x->load(); $this->assertNotEquals($manualWeight, $withArmor); $this->assertTrue($withArmor > $withWeapons); $this->assertEquals($manualWeight, $x->get_total_weight(false)); $this->assertEquals($x->get_total_weight(), $x->get_total_weight(false)); $this->assertEquals($withArmor, $x->get_total_weight(true)); }
public function get_total_weight(Database $dbObj) { $weight = 0; $allGear = Gear::get_all($dbObj, $this->characterId); if (is_array($allGear) && count($allGear) > 0) { foreach ($allGear as $k => $data) { $itemWeight = 0; if (isset($data['weight']) && is_numeric($data['weight']) && $data['weight'] > 0) { if (isset($data['quantity']) && is_numeric($data['quantity']) && $data['quantity'] > 0) { $itemWeight = $itemWeight * $data['quantity']; } else { $itemWeight = $data['weight']; } } $weight += $itemWeight; } } return $weight; }
public function test_gear_total_weight() { $char = new Character(__METHOD__, 1, $this->dbObj); $gear = new Gear(); $gear->characterId = $char->characterId; $createThis = array('torches', 'silk rope', 'bullseye lantern'); $manualWeight = 0; $testData = array(); foreach ($createThis as $name) { $_createData = array('character_id' => $char->characterId, 'gear_name' => $name, 'weight' => rand(1, 10), 'quantity' => rand(1, 10)); $manualWeight += $_createData['weight'] * $_createData['quantity']; $id = $gear->create($this->dbObj, $_createData); $this->assertTrue(is_numeric($id)); $this->assertTrue($id > 0); $this->assertFalse(isset($testData[$id])); $testData[$id] = $gear->load($this->dbObj); $this->assertTrue(is_array($testData[$id])); $this->assertTrue(count($testData[$id]) > 0); } $this->assertEquals(count($testData), count($createThis)); $allGear = Gear::get_all($this->dbObj, $char->id); $this->assertEquals(count($allGear), count($testData)); $manualWeight = 0; foreach ($testData as $k => $v) { $this->assertTrue(isset($allGear[$id])); $this->assertTrue(is_array($allGear[$id])); $this->assertEquals($v, $allGear[$k]); } $this->assertEquals($manualWeight, $char->get_total_weight($this->dbObj)); }
public function handle_delete($type, $recordId) { $retval = "Invalid section... type=(" . $type . "), recordId=(" . $recordId . ")"; // $log = new cs_webdblogger($this->dbObj, "Character"); switch ($type) { case 'weapon': case Weapon::sheetIdPrefix: $x = new Weapon(); $x->load($this->dbObj, $recordId); $retval = $x->delete($this->dbObj); break; case 'armor': case Armor::sheetIdPrefix: $x = new Armor(); $x->load($this->dbObj, $recordId); $retval = $x->delete($this->dbObj); break; case 'skill': case Skill::sheetIdPrefix: $x = new Skill(); $x->load($this->dbObj, $recordId); $retval = $x->delete($this->dbObj); break; case 'feat': case 'specialAbility': case SpecialAbility::sheetIdPrefix: $x = new SpecialAbility(); $x->load($this->dbObj, $recordId); $retval = $x->delete($this->dbObj); break; case 'gear': case Gear::sheetIdPrefix: $x = new Gear(); $x->load($this->dbObj, $recordId); $retval = $x->delete($this->dbObj); break; } // $log->log_by_class("type=(". $type ."), recordId=(". $recordId ."), result::: ". $retval, "delete"); return $retval; }
public function test_get_all() { $x = new Gear(); $x->characterId = $this->id; $createThis = array('torches', 'silk rope', 'bullseye lantern'); $list = array(); foreach ($createThis as $name) { $_createData = array('character_id' => $x->characterId, 'gear_name' => $name); $id = $x->create($this->dbObj, $_createData); $this->assertTrue(is_numeric($id)); $this->assertTrue($id > 0); $list[$id] = $x->load($this->dbObj); } $this->assertEquals(count($createThis), count($list)); $allGear = $x->get_all($this->dbObj, $x->characterId); $this->assertTrue(is_array($allGear)); $this->assertTrue(count($allGear) > 0); foreach ($allGear as $k => $v) { $this->assertTrue(isset($list[$k])); $this->assertEquals($v, $list[$k]); $loadedThis = $x->load($this->dbObj, $k); $this->assertEquals($v, $loadedThis); } }