class GameController < ApplicationController def finish_activity @results = [] return unless current_char.activity_time_remaining <= 0 current_char.update(activity_started_at: Time.now) current_char.activity.whatnot["results"].each do |result| type = result["type"] case type when "item" next if rand > result["chance"] table_roll = rand result["table"].sort_by { |_, v| -v["score"] }.each do |item_gid, item_data| quantity = item_data["quantity"] || 1 if table_roll >= item_data["score"] current_char.shift_item(item_gid, quantity) @results.push({ type: type, item: Item.find_by_gid(item_gid), quantity: quantity }) break end end else raise "Invalid result type (#{type})" # TODO: Improve this. end end end end