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 handle_update($name, $value)
 {
     $changesByKey = array();
     $result = "not set";
     $bits = preg_split('/__/', $name);
     $prefix = $bits[0];
     $realName = $bits[1];
     $recordId = null;
     if (isset($bits[2])) {
         $recordId = $bits[2];
     }
     $fieldsToUpdate = array($realName => $value);
     $debug = "realName=(" . $realName . "), id=(" . $recordId . ")";
     //		$log = new cs_webdblogger($this->dbObj, "Character");
     switch ($prefix) {
         case Ability::sheetIdPrefix:
             if (is_numeric($recordId)) {
                 $allAbilities = Ability::get_all_abilities($this->dbObj, true);
                 $obj = new Ability();
                 if (!is_null($value) && strlen($value) == 0) {
                     $fieldsToUpdate[$realName] = null;
                 }
                 $changesByKey = $obj->update_and_get_changes($this->dbObj, $fieldsToUpdate, $recordId);
                 $this->load();
                 $abilityName = $allAbilities[$obj->ability_id];
                 $changesByKey[$name] = $value;
                 if (preg_match('/temp/', $realName)) {
                     //update temporary modifier
                     $changesByKey[$this->create_sheet_id($prefix, $abilityName . '_temporary_modifier')] = $obj->get_temp_modifier();
                 } elseif (preg_match('/^ability/', $realName) || preg_match('/^score$/', $realName)) {
                     //update (standard) modifier
                     $changesByKey[$this->create_sheet_id($prefix, $abilityName . '_modifier')] = $obj->get_modifier();
                     //Updating skill info is NEW.
                     $depSkills = Skill::get_all($this->dbObj, $this->characterId, $obj->ability_id);
                     foreach ($depSkills as $k => $v) {
                         $changesByKey[$this->create_sheet_id(Skill::sheetIdPrefix, 'ability_mod', $k)] = $obj->get_modifier();
                         $changesByKey[$this->create_sheet_id(Skill::sheetIdPrefix, 'skill_mod', $k)] = Skill::calculate_skill_modifier($v);
                     }
                     $saveList = Save::get_all($this->dbObj, $this->characterId, $obj->ability_id);
                     foreach ($saveList as $k => $v) {
                         $changesByKey[$this->create_sheet_id(Save::sheetIdPrefix, 'ability_mod', $k)] = $v['ability_mod'];
                         $changesByKey[$this->create_sheet_id(Save::sheetIdPrefix, 'total', $k)] = $v['total_mod'];
                     }
                     //TODO: update misc fields...
                     switch ($abilityName) {
                         case 'str':
                             $miscUpdates = $this->get_strength_stats();
                             foreach ($miscUpdates as $k => $v) {
                                 $changesByKey[$this->create_sheet_id('generated', $k)] = $v;
                             }
                             break;
                     }
                 } else {
                     throw new InvalidArgumentException(__METHOD__ . ": invalid field (" . $realName . ")");
                 }
             }
             $mData = $this->get_misc_data();
             foreach ($mData as $k => $v) {
                 $changesByKey[$k] = $v;
             }
             break;
         case Character::sheetIdPrefix:
             $char = new Character($this->characterId, $this->ownerUid, $this->dbObj);
             $char->load($this->dbObj);
             if ($realName == 'xp_change') {
                 $xpCurrent = $char->xp_current;
                 $fieldsToUpdate = array('xp_current', $xpCurrent + $value);
             }
             $changesByKey = $char->update_and_get_changes($this->dbObj, $fieldsToUpdate, $recordId);
             $this->load();
             if (preg_match('/^melee/', $realName)) {
                 $changesByKey[$this->create_sheet_id('main', 'melee_total')] = $this->get_attack_bonus('melee');
             } elseif (preg_match('/^ranged/', $realName)) {
                 $changesByKey[$this->create_sheet_id('main', 'ranged_total')] = $this->get_attack_bonus('ranged');
             }
             break;
         case Save::sheetIdPrefix:
             $x = new Save();
             $changesByKey = $x->update_and_get_changes($this->dbObj, $fieldsToUpdate, $recordId);
             break;
         case Skill::sheetIdPrefix:
             $x = new Skill();
             $changesByKey = $x->update_and_get_changes($this->dbObj, $fieldsToUpdate, $recordId);
             break;
         case Weapon::sheetIdPrefix:
             $x = new Weapon();
             $changesByKey = $x->update_and_get_changes($this->dbObj, $fieldsToUpdate, $recordId);
             break;
         case Armor::sheetIdPrefix:
             $x = new Armor();
             $changesByKey = $x->update_and_get_changes($this->dbObj, $fieldsToUpdate, $recordId);
             break;
         case SpecialAbility::sheetIdPrefix:
             $x = new SpecialAbility();
             $changesByKey = $x->update_and_get_changes($this->dbObj, $fieldsToUpdate, $recordId);
             break;
         case Gear::sheetIdPrefix:
             $x = new Gear();
             $changesByKey = $x->update_and_get_changes($this->dbObj, $fieldsToUpdate, $recordId);
             $changesByKey[$x::sheetIdPrefix . '__total_weight__generated'] = csbt_gear::calculate_list_weight($x->get_all($this->dbObj, $this->characterId));
             break;
         default:
             $details = __METHOD__ . ": invalid prefix (" . $prefix . ") or unable to update field (" . $realName . ")";
             //				$log->log_by_class($details, "exception in code");
             throw new InvalidArgumentException($details);
     }
     //		$log->log_by_class("Updating characterId=". $this->characterId .", ". $name ."=(". $value .")", "update");
     $retval = array('debug' => $debug, 'result' => $result, 'changesbykey' => $changesByKey);
     return $retval;
 }
 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));
 }
Esempio n. 4
0
 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);
     }
 }