From c2c6a800f29c61b5e7696eaa58d49fc1def284cb Mon Sep 17 00:00:00 2001 From: Matchu Date: Wed, 23 May 2012 20:00:38 -0400 Subject: [PATCH] track pet state gender/mood --- Gemfile.lock | 2 +- app/models/pet.rb | 1 + app/models/pet_state.rb | 21 +++++++++++++++++++ ...d_unconverted_and_labeled_to_pet_states.rb | 15 +++++++++++++ db/schema.rb | 10 ++++++--- 5 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20120521164652_add_female_and_mood_and_unconverted_and_labeled_to_pet_states.rb diff --git a/Gemfile.lock b/Gemfile.lock index e06db3db..899be4bb 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -31,7 +31,7 @@ GIT GIT remote: git://github.com/matchu/neopets.git - revision: c00b44ab75064815c5d433f0053bbf612c8465fd + revision: d0127de12eae650fd85c56b45bc0b52e64e34ffe specs: neopets (0.0.1) nokogiri (~> 1.5.2) diff --git a/app/models/pet.rb b/app/models/pet.rb index 799479bf..142f52c6 100644 --- a/app/models/pet.rb +++ b/app/models/pet.rb @@ -39,6 +39,7 @@ class Pet < ActiveRecord::Base biology = pet_data.biology_by_zone biology[0] = nil # remove effects if present @pet_state = self.pet_type.add_pet_state_from_biology! biology + @pet_state.label_by_pet(self, pet_data.owner) @items = Item.collection_from_pet_type_and_registries(self.pet_type, contents.object_info_registry, contents.object_asset_registry) true diff --git a/app/models/pet_state.rb b/app/models/pet_state.rb index 39e549ab..93eea67e 100644 --- a/app/models/pet_state.rb +++ b/app/models/pet_state.rb @@ -61,6 +61,26 @@ class PetState < ActiveRecord::Base rel.save! end end + + def label_by_pet(pet, username) + # If this pet is already labeled with a gender/mood, or it's unconverted + # and therefore has none, skip. + return false if self.labeled? || self.unconverted? + + # Find this pet on the owner's userlookup, where we can get both its gender + # and its mood. + user_pet = Neopets::User.new(username).pets. + find { |user_pet| user_pet.name.downcase == pet.name.downcase } + self.female = user_pet.female? + self.mood_id = user_pet.mood.id + self.labeled = true + + true + end + + def mood + Neopets::Pet::Mood.find(self.mood_id) + end def self.from_pet_type_and_biology_info(pet_type, info) swf_asset_ids = [] @@ -112,6 +132,7 @@ class PetState < ActiveRecord::Base end end pet_state.parent_swf_asset_relationships_to_update = relationships + pet_state.unconverted = (relationships.size == 1) pet_state end diff --git a/db/migrate/20120521164652_add_female_and_mood_and_unconverted_and_labeled_to_pet_states.rb b/db/migrate/20120521164652_add_female_and_mood_and_unconverted_and_labeled_to_pet_states.rb new file mode 100644 index 00000000..3dcabac1 --- /dev/null +++ b/db/migrate/20120521164652_add_female_and_mood_and_unconverted_and_labeled_to_pet_states.rb @@ -0,0 +1,15 @@ +class AddFemaleAndMoodAndUnconvertedAndLabeledToPetStates < ActiveRecord::Migration + def self.up + add_column :pet_states, :female, :boolean + add_column :pet_states, :mood_id, :integer + add_column :pet_states, :unconverted, :boolean + add_column :pet_states, :labeled, :boolean, :null => false, :default => false + end + + def self.down + remove_column :pet_states, :labeled + remove_column :pet_states, :unconverted + remove_column :pet_states, :mood_id + remove_column :pet_states, :female + end +end diff --git a/db/schema.rb b/db/schema.rb index 0500a942..5d866571 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20120321231507) do +ActiveRecord::Schema.define(:version => 20120521164652) do create_table "auth_servers", :force => true do |t| t.string "short_name", :limit => 10, :null => false @@ -140,8 +140,12 @@ ActiveRecord::Schema.define(:version => 20120321231507) do end create_table "pet_states", :force => true do |t| - t.integer "pet_type_id", :limit => 3, :null => false - t.text "swf_asset_ids", :null => false + t.integer "pet_type_id", :limit => 3, :null => false + t.text "swf_asset_ids", :null => false + t.boolean "female" + t.integer "mood_id" + t.boolean "unconverted" + t.boolean "labeled", :default => false, :null => false end add_index "pet_states", ["pet_type_id"], :name => "pet_states_pet_type_id"