class BazaarController < ApplicationController def index @bazaar_orders = BazaarOrder.joins(:item).order("items.name ASC, price_each ASC, created_at ASC") @items = Item.all.order(:name) end def create_order item = Item.find(params[:item_id]) quantity = params[:quantity].to_i price_each = params[:price_each].to_i if params[:buy_order] == "1" BazaarOrder.transaction do current_char.shift_item("vestige", (-price_each * quantity)) current_char.bazaar_orders.create!(item: item, quantity: quantity, price_each: price_each, buy_order: true) flash[:notice] = "Posted buy order for #{item.name} at #{price_each} vg each." end else BazaarOrder.transaction do current_char.shift_item(item, -quantity) current_char.bazaar_orders.create!(item: item, quantity: quantity, price_each: price_each, buy_order: false) flash[:notice] = "Posted sell order for #{item.name} at #{price_each} vg each." end end rescue ItemQuantityError flash[:alert] = "Failed to post order. Make sure you have vestige or items." ensure redirect_to bazaar_path end def accept_offer @bazaar_order = BazaarOrder.find(params[:id]) if @bazaar_order.buy_order? BazaarOrder.transaction do quantity = [params[:quantity].to_i, @bazaar_order.quantity].min profit = quantity * @bazaar_order.price_each current_char.shift_item(@bazaar_order.item, -quantity) current_char.shift_item("vestige", profit) @bazaar_order.character.shift_item(@bazaar_order.item, quantity) @bazaar_order.decrement(:quantity, quantity) @bazaar_order.save! flash[:notice] = "You sold #{quantity} #{@bazaar_order.item.name} and got #{profit} vg." end else BazaarOrder.transaction do quantity = [params[:quantity].to_i, @bazaar_order.quantity].min cost = quantity * @bazaar_order.price_each current_char.shift_item("vestige", -cost) current_char.shift_item(@bazaar_order.item, quantity) @bazaar_order.character.shift_item("vestige", cost) @bazaar_order.decrement(:quantity, quantity) @bazaar_order.save! flash[:notice] = "You bought #{quantity} #{@bazaar_order.item.name} for #{cost} vg." end end rescue ItemQuantityError flash[:alert] = "Failed to accept order. Make sure you have vestige or items." ensure redirect_to bazaar_path end def cancel_offer @bazaar_order = BazaarOrder.find(params[:id]) unless @bazaar_order.character == current_char flash[:alert] = "You can't cancel someone else's order." redirect_to bazaar_path and return end if @bazaar_order.buy_order? BazaarOrder.transaction do vestige_to_return = @bazaar_order.quantity * @bazaar_order.price_each @bazaar_order.character.shift_item("vestige", vestige_to_return) @bazaar_order.destroy! flash[:notice] = "You canceled your buy order for #{@bazaar_order.item.name} and got" \ " #{vestige_to_return} vg back." end else BazaarOrder.transaction do items_to_return = @bazaar_order.quantity @bazaar_order.character.shift_item(@bazaar_order.item, items_to_return) @bazaar_order.destroy! flash[:notice] = "You canceled your sell order for #{@bazaar_order.item.name} and got" \ " #{items_to_return} of them back." end end rescue ItemQuantityError flash[:alert] = "Failed to cancel order." ensure redirect_to bazaar_path end end