roulette system :)

This commit is contained in:
Emi Matchu 2011-01-27 16:35:46 -05:00
parent 1e8ec13bfe
commit 4b66a560e7
7 changed files with 109 additions and 25 deletions

View file

@ -10,9 +10,9 @@ class PetsController < ApplicationController
}
def load
# TODO: include point value with JSON once contributions implemented
if params[:name] == '!'
redirect_to roulette_path
else
raise Pet::PetNotFound unless params[:name]
@pet = Pet.load(params[:name])
if user_signed_in?
@ -35,6 +35,7 @@ class PetsController < ApplicationController
end
end
end
end
protected

View file

@ -0,0 +1,6 @@
class RoulettesController < ApplicationController
def new
@roulette = Roulette.new
redirect_to wardrobe_path(:anchor => @roulette.to_query)
end
end

View file

@ -71,6 +71,10 @@ class Item < ActiveRecord::Base
zones
end
def affected_zones
restricted_zones + occupied_zones
end
def species_support_ids
@species_support_ids_array ||= read_attribute('species_support_ids').split(',').map(&:to_i) rescue nil
end

View file

@ -1,6 +1,8 @@
class ParentSwfAssetRelationship < ActiveRecord::Base
set_table_name 'parents_swf_assets'
belongs_to :item, :foreign_key => 'parent_id'
belongs_to :biology_asset, :class_name => 'SwfAsset', :foreign_key => 'swf_asset_id', :conditions => {:type => 'biology'}
belongs_to :object_asset, :class_name => 'SwfAsset', :foreign_key => 'swf_asset_id', :conditions => {:type => 'object'}
@ -8,16 +10,12 @@ class ParentSwfAssetRelationship < ActiveRecord::Base
self.swf_asset_type == 'biology' ? self.biology_asset : self.object_asset
end
def item
parent
end
def item=(replacement)
self.parent_id = replacement.id
end
def pet_state
parent
PetState.find(parent_id)
end
def pet_state=(replacement)

69
app/models/roulette.rb Normal file
View file

@ -0,0 +1,69 @@
class Roulette
attr_accessor :pet_type, :pet_state, :item_ids
def initialize
# Choose a random pet type, and get a random state
@pet_type = PetType.offset(rand(PetType.count)).first # random pet type
states = @pet_type.pet_states
@pet_state = states.offset(rand(states.count)).first
fitting_swf_assets = SwfAsset.object_assets.fitting_body_id(@pet_type.body_id)
# Keep going until we have all zones occupied. Find a random SWF asset for
# a given unoccupied zone, then look up its item to see if it affects or
# restricts zones we've already used. If so, try again. If not, add it to
# the list.
unoccupied_zone_ids = Zone.all.map(&:id)
swf_asset_count_by_zone_id = fitting_swf_assets.count(:group => :zone_id)
@item_ids = []
while !unoccupied_zone_ids.empty?
zone_id = unoccupied_zone_ids.slice!(rand(unoccupied_zone_ids.count))
if swf_asset_count = swf_asset_count_by_zone_id[zone_id]
used_swf_asset_ids = []
first_asset = true
found_item = false
while !found_item && used_swf_asset_ids.size < swf_asset_count
base = fitting_swf_assets
unless first_asset
first_asset = false
base = base.where(fitting_swf_assets.arel_table[:id].not_in(used_swf_asset_ids))
end
swf_asset = base.
where(:zone_id => zone_id).
offset(rand(swf_asset_count)).
includes(:object_asset_relationships => :item).
first
used_swf_asset_ids.push(swf_asset.id)
swf_asset.object_asset_relationships.each do |rel|
item = rel.item
pass = true
item.affected_zones.each do |zone|
checked_zone_id = zone.id
next if checked_zone_id == zone_id
if i = unoccupied_zone_ids.find_index(zone_id)
unoccupied_zone_ids.slice! zone_id
else
# This zone is already occupied, so this item is no good.
pass = false
end
end
if pass
found_item = true
@item_ids << item.id
break
end
end
break if found_item
end
end
end
end
def to_query
{
:color => @pet_type.color_id,
:species => @pet_type.species_id,
:state => @pet_state.id,
:objects => @item_ids
}.to_query
end
end

View file

@ -8,4 +8,8 @@ class StaticResource
def self.find(id)
@objects[id-1]
end
def self.count
@objects.size
end
end

View file

@ -10,6 +10,8 @@ OpenneoImpressItems::Application.routes.draw do |map|
match '/pet_states/:pet_state_id/swf_assets.json' => 'swf_assets#index', :as => :pet_state_swf_assets
match '/species/:species_id/color/:color_id/pet_type.json' => 'pet_types#show'
match '/roulette' => 'roulettes#new', :as => :roulette
resources :contributions, :only => [:index]
resources :items, :only => [:index, :show] do
collection do