diff options
author | David Gay <david@davidgay.org> | 2021-05-23 17:06:34 -0400 |
---|---|---|
committer | David Gay <david@davidgay.org> | 2021-05-23 17:06:34 -0400 |
commit | aba212e13062c43a1192ef885b05145ac1cc9fe7 (patch) | |
tree | 61e3850ec0c86913a7e2d42076bcd73aa59f480a /app/controllers | |
parent | 44facc2e567eb3c045ce082428f42276e45b0202 (diff) |
Bazaar with buy orders, sell orders, and order cancellation
Diffstat (limited to 'app/controllers')
-rw-r--r-- | app/controllers/bazaar_controller.rb | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/app/controllers/bazaar_controller.rb b/app/controllers/bazaar_controller.rb new file mode 100644 index 0000000..b31e8fb --- /dev/null +++ b/app/controllers/bazaar_controller.rb @@ -0,0 +1,91 @@ +class BazaarController < ApplicationController + def index + @bazaar_orders = BazaarOrder.all + @items = Item.all + 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 |