Ejemplo n.º 1
0
 public function _clean_data_array()
 {
     return parent::_clean_data_array();
 }
 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;
 }