1
0
Fork 0
forked from OpenNeo/impress

Merge branch 'gender_mood'

This commit is contained in:
Emi Matchu 2012-06-05 12:52:27 -04:00
commit a436362f26
9 changed files with 66 additions and 24 deletions

View file

@ -33,12 +33,14 @@ gem 'right_aws', '~> 2.1.0'
gem "character-encodings", "~> 0.4.1", :platforms => :ruby_18 gem "character-encodings", "~> 0.4.1", :platforms => :ruby_18
gem "nokogiri", "~> 1.5.0" gem "nokogiri", "~> 1.5.2"
gem 'sanitize', '~> 2.0.3' gem 'sanitize', '~> 2.0.3'
gem 'newrelic_rpm' gem 'newrelic_rpm'
gem 'neopets', :git => 'git://github.com/matchu/neopets.git'
group :development_async do group :development_async do
# async wrappers # async wrappers
gem 'eventmachine', :git => 'git://github.com/eventmachine/eventmachine.git' gem 'eventmachine', :git => 'git://github.com/eventmachine/eventmachine.git'

View file

@ -29,6 +29,13 @@ GIT
em-synchrony (1.0.0) em-synchrony (1.0.0)
eventmachine (>= 1.0.0.beta.1) eventmachine (>= 1.0.0.beta.1)
GIT
remote: git://github.com/matchu/neopets.git
revision: d0127de12eae650fd85c56b45bc0b52e64e34ffe
specs:
neopets (0.0.1)
nokogiri (~> 1.5.2)
GIT GIT
remote: git://github.com/oldmoe/mysqlplus.git remote: git://github.com/oldmoe/mysqlplus.git
revision: 3dbaa7c00ff0bb75ad9538cdef176c72de35d231 revision: 3dbaa7c00ff0bb75ad9538cdef176c72de35d231
@ -118,7 +125,7 @@ GEM
msgpack (0.4.6) msgpack (0.4.6)
mysql2 (0.2.6) mysql2 (0.2.6)
newrelic_rpm (3.3.3) newrelic_rpm (3.3.3)
nokogiri (1.5.0) nokogiri (1.5.2)
open4 (1.3.0) open4 (1.3.0)
openneo-auth-signatory (0.1.0) openneo-auth-signatory (0.1.0)
ruby-hmac ruby-hmac
@ -221,8 +228,9 @@ DEPENDENCIES
msgpack (~> 0.4.3) msgpack (~> 0.4.3)
mysql2 (< 0.3) mysql2 (< 0.3)
mysqlplus! mysqlplus!
neopets!
newrelic_rpm newrelic_rpm
nokogiri (~> 1.5.0) nokogiri (~> 1.5.2)
openneo-auth-signatory (~> 0.1.0) openneo-auth-signatory (~> 0.1.0)
rack-fiber_pool rack-fiber_pool
rails (= 3.0.5) rails (= 3.0.5)

View file

@ -12,17 +12,15 @@ class NeopetsUser
end end
def load! def load!
doc = Nokogiri::HTML(open(url)) user = Neopets::User.new(@username)
unless pets_wrapper = doc.at('#userneopets') begin
raise NotFound, "Could not find user #{username}" pets = user.pets
end rescue Neopets::User::Error => e
raise NotFound, e.message
pets = pets_wrapper.css('a[href^="/petlookup.phtml"]').map do |link|
name = link['href'].split('=').last
Pet.find_or_initialize_by_name(name)
end end
pets = pets.map { |pet| Pet.find_or_initialize_by_name(pet.name) }
items = pets.each(&:load!).map(&:items).flatten items = pets.each(&:load!).map(&:items).flatten
item_ids = items.map(&:id) item_ids = items.map(&:id)
@ -46,13 +44,6 @@ class NeopetsUser
false false
end end
protected class NotFound < RuntimeError; end
URL_PREFIX = 'http://www.neopets.com/userlookup.phtml?user='
def url
URL_PREFIX + @username
end
class NotFound < RuntimeError;end
end end

View file

@ -39,6 +39,7 @@ class Pet < ActiveRecord::Base
biology = pet_data.biology_by_zone biology = pet_data.biology_by_zone
biology[0] = nil # remove effects if present biology[0] = nil # remove effects if present
@pet_state = self.pet_type.add_pet_state_from_biology! biology @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, @items = Item.collection_from_pet_type_and_registries(self.pet_type,
contents.object_info_registry, contents.object_asset_registry) contents.object_info_registry, contents.object_asset_registry)
true true

View file

@ -62,6 +62,26 @@ class PetState < ActiveRecord::Base
end end
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) def self.from_pet_type_and_biology_info(pet_type, info)
swf_asset_ids = [] swf_asset_ids = []
info.each do |zone_id, asset_info| info.each do |zone_id, asset_info|
@ -112,6 +132,7 @@ class PetState < ActiveRecord::Base
end end
end end
pet_state.parent_swf_asset_relationships_to_update = relationships pet_state.parent_swf_asset_relationships_to_update = relationships
pet_state.unconverted = (relationships.size == 1)
pet_state pet_state
end end

View file

@ -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

View file

@ -10,7 +10,7 @@
# #
# It's strongly recommended to check this file into your version control system. # 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| create_table "auth_servers", :force => true do |t|
t.string "short_name", :limit => 10, :null => false t.string "short_name", :limit => 10, :null => false
@ -140,8 +140,12 @@ ActiveRecord::Schema.define(:version => 20120321231507) do
end end
create_table "pet_states", :force => true do |t| create_table "pet_states", :force => true do |t|
t.integer "pet_type_id", :limit => 3, :null => false t.integer "pet_type_id", :limit => 3, :null => false
t.text "swf_asset_ids", :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 end
add_index "pet_states", ["pet_type_id"], :name => "pet_states_pet_type_id" add_index "pet_states", ["pet_type_id"], :name => "pet_states_pet_type_id"

Binary file not shown.

BIN
vendor/cache/nokogiri-1.5.2.gem vendored Normal file

Binary file not shown.