lib/bnet/diablo3/hero.rb in bnet-0.0.2 vs lib/bnet/diablo3/hero.rb in bnet-0.0.3

- old
+ new

@@ -1,22 +1,32 @@ class Bnet::Diablo3::Hero < Bnet::BnetResource attr_accessor :paragon_level, :seasonal, :name, :hero_id, :level, :hardcore, :gender, :dead, :hero_class, :last_update, - :active_skills, :passive_skills, :region, :battle_tag, :career + :active_skills, :passive_skills, :region, :battle_tag, :career, + :items, + # stats + :life, :damage, :attack_speed, :armor, :strength, :dexterity, :vitality, + :intelligence, :physical_resist, :fire_resist, :cold_resist, + :lightning_resist, :poison_resist, :arcane_resist, :crit_damage, + :block_chance, :block_amount_min, :block_amount_max, :damage_increase, + :crit_chance, :damage_reduction, :thorns, :life_steal, :life_per_kill, + :gold_find, :magic_find, :life_on_Hit, :primary_resource, + :secondary_resource + PARAMS_MAPPING = { "paragonLevel" => :paragon_level, "seasonal" => :seasonal, "name" => :name, "id" => :hero_id, "level" => :level, "hardcore" => :hardcore, "gender" => :gender, "dead" => :dead, "class" => :hero_class, - "last-updated" => :last_updated + "last-updated" => :last_updated, } # TODO:Implement # def career @@ -97,35 +107,89 @@ return bnet_object end # Create an instance by passing in the args from the response def self.from_api(response) - bnet_resource = super(response) + hero = super(response) - if bnet_resource && response["skills"] - bnet_resource.active_skills = response["skills"]["active"].collect do |active| + if hero + assign_skills_from_raw_skills(hero, response["skills"]) if response["skills"] + assign_stats_from_raw_stats(hero, response["stats"]) if response["stats"] + assign_items_from_raw_items(hero, response["items"]) if response["items"] + end - skill = Bnet::Diablo3::Skill.new - if active["skill"] - skill.name = active["skill"]["name"] - end - if active["rune"] - skill.rune = active["rune"]["name"] - end + return hero + end - skill + private + def self.assign_items_from_raw_items(hero, raw_items) + hero.items = raw_items.collect do |location, item_props| + item = Bnet::Diablo3::Item.new + item.location = location + item.item_id = item_props["id"] + item.name = item_props["name"] + item + end + + return hero + end + + def self.assign_skills_from_raw_skills(hero, raw_skills) + hero.active_skills = raw_skills["active"].collect do |active| + skill = Bnet::Diablo3::Skill.new + if active["skill"] + skill.name = active["skill"]["name"] end + if active["rune"] + skill.rune = active["rune"]["name"] + end - bnet_resource.passive_skills = response["skills"]["passive"].collect do |passive| - skill = Bnet::Diablo3::Skill.new - if passive["skill"] - skill.name = passive["skill"]["name"] - end - skill + skill + end + + hero.passive_skills = raw_skills["passive"].collect do |passive| + skill = Bnet::Diablo3::Skill.new + if passive["skill"] + skill.name = passive["skill"]["name"] end + skill end - bnet_resource + return hero + end + + def self.assign_stats_from_raw_stats(hero, raw_stats) + hero.life = raw_stats["life"] + hero.damage = raw_stats["damage"] + hero.attack_speed = raw_stats["attackSpeed"] + hero.armor = raw_stats["armor"] + hero.strength = raw_stats["strength"] + hero.dexterity = raw_stats["dexterity"] + hero.vitality = raw_stats["vitality"] + hero.intelligence = raw_stats["intelligence"] + hero.physical_resist = raw_stats["physicalResist"] + hero.fire_resist = raw_stats["fireResist"] + hero.cold_resist = raw_stats["coldResist"] + hero.lightning_resist = raw_stats["lightningResist"] + hero.poison_resist = raw_stats["poisonResist"] + hero.arcane_resist = raw_stats["arcaneResist"] + hero.crit_damage = raw_stats["critDamage"] + hero.block_chance = raw_stats["blockChance"] + hero.block_amount_min = raw_stats["blockAmountMin"] + hero.block_amount_max = raw_stats["blockAmountMax"] + hero.damage_increase = raw_stats["damageIncrease"] + hero.crit_chance = raw_stats["critChance"] + hero.damage_reduction = raw_stats["damageReduction"] + hero.thorns = raw_stats["thorns"] + hero.life_steal = raw_stats["lifeSteal"] + hero.life_per_kill = raw_stats["lifePerKill"] + hero.gold_find = raw_stats["goldFind"] + hero.magic_find = raw_stats["magicFind"] + hero.life_on_Hit = raw_stats["lifeOnHit"] + hero.primary_resource = raw_stats["primaryResource"] + hero.secondary_resource = raw_stats["secondaryResource"] + + return hero end end