public static function CreateOrUpdate($data) { /* Check if characters stats already exist */ try { $key = 'character_id'; $stats = CharacterStats::where($key, $data[$key])->firstOrFail(); /* Character Exists - Update */ foreach ($data as $key => $val) { $stats->{$key} = $val; } $stats->save(); } catch (ModelNotFoundException $e) { /* Does not exist - Add */ CharacterStats::create($data); } catch (Exception $e) { \Log::error($e->getMessage()); } }
/** * Execute the command. * * @return void */ public function handle() { $api = new Api(); $member = $this->member; $data = $api->getProfileInfo($member->battletag); /* Update member paragon levels */ $member->paragon = $data['paragonLevel']; $member->paragon_hc = $data['paragonLevelHardcore']; $member->paragon_curr_season = $data['paragonLevelSeason']; $member->paragon_curr_season_hc = $data['paragonLevelSeasonHardcore']; $member->save(); /* Update members characters */ $characters = $data['heroes']; $charClass = new CharacterClass(); foreach ($characters as $char) { $char['class'] = $charClass->getClassId($char['class']); $char['owner_id'] = $member->id; $char_id = Character::CreateOrUpdate($char); /* Also create a stats entry so that we can use eager loading (join) - without losing characters */ $data = ['character_id' => $char_id]; CharacterStats::CreateOrUpdate($data); } }
/** * Update Stats. * * @param array $data Array returned by diablo api. */ public function updateStats($data) { if (!array_key_exists('kills', $data)) { echo 'Unable to update character ' . $this->character->name; return false; } $stats = []; $stats['character_id'] = $this->character->id; $stats['elite_kills'] = $data['kills']['elites']; $stats['life'] = $data['stats']['life']; $stats['damage'] = $data['stats']['damage']; $stats['toughness'] = $data['stats']['toughness']; $stats['healing'] = $data['stats']['healing']; $stats['attack_speed'] = $data['stats']['attackSpeed']; $stats['armor'] = $data['stats']['armor']; $stats['strength'] = $data['stats']['strength']; $stats['dexterity'] = $data['stats']['dexterity']; $stats['vitality'] = $data['stats']['vitality']; $stats['intelligence'] = $data['stats']['intelligence']; $stats['resist_physical'] = $data['stats']['physicalResist']; $stats['resist_fire'] = $data['stats']['fireResist']; $stats['resist_cold'] = $data['stats']['coldResist']; $stats['resist_lightning'] = $data['stats']['lightningResist']; $stats['resist_poison'] = $data['stats']['poisonResist']; $stats['resist_arcane'] = $data['stats']['arcaneResist']; $stats['crit_damage'] = $data['stats']['critDamage']; $stats['block_chance'] = $data['stats']['blockChance']; $stats['block_min'] = $data['stats']['blockAmountMin']; $stats['block_max'] = $data['stats']['blockAmountMax']; $stats['damage_increase'] = $data['stats']['damageIncrease']; $stats['crit_chance'] = $data['stats']['critChance']; $stats['damage_reduction'] = $data['stats']['damageReduction']; $stats['thorns'] = $data['stats']['thorns']; $stats['life_steal'] = $data['stats']['lifeSteal']; $stats['life_per_kill'] = $data['stats']['lifePerKill']; $stats['gold_find'] = $data['stats']['goldFind']; $stats['magic_find'] = $data['stats']['magicFind']; $stats['life_on_hit'] = $data['stats']['lifeOnHit']; $stats['primary_resource'] = $data['stats']['primaryResource']; $stats['secondary_resource'] = $data['stats']['secondaryResource']; CharacterStats::CreateOrUpdate($stats); }