summaryrefslogtreecommitdiff
path: root/app/controllers/bazaar_controller.rb
blob: d51a4196767d5ba0989469386abd5b4faabe6dc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
class BazaarController < ApplicationController
  def index
    @bazaar_orders = BazaarOrder.joins(:item)
    @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!
        Message.create(recipient: @bazaar_order.character, subject: "Bought #{quantity} #{@bazaar_order.item.name}",
                       body: "#{current_char.name} sold #{quantity} #{@bazaar_order.item.name} to you" \
                             " for #{profit} vg.")
        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!
        Message.create(recipient: @bazaar_order.character, subject: "Sold #{quantity} #{@bazaar_order.item.name}",
                       body: "#{current_char.name} bought #{quantity} #{@bazaar_order.item.name} from you" \
                             " for #{cost} vg.")
        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