Auction views stuff

master
E. Almqvist 3 years ago
parent c9140e640a
commit 05939e82a8
  1. 8
      src/config.rb
  2. 8
      src/const.rb
  3. 59
      src/lib/db_models.rb
  4. 20
      src/routes/auction.rb
  5. 32
      src/views/auction/view.slim
  6. 13
      src/views/stylesheets/style.sass

@ -32,11 +32,13 @@ LOGIN_ERRORS = {
fail: "Wrong password and/or email" fail: "Wrong password and/or email"
} }
# Auction stuff
AH_BUYOUT_FACTOR = 1.8
AH_BIDS_FACTOR = 1.01 # min 1%
AUCTION_ERRORS = { AUCTION_ERRORS = {
titlelen: "Title length must be between #{MIN_TITLE_LEN} and #{MAX_TITLE_LEN} characters!", titlelen: "Title length must be between #{MIN_TITLE_LEN} and #{MAX_TITLE_LEN} characters!",
initprice: "The initial price must be at least #{MIN_INIT_PRICE}!", initprice: "The initial price must be at least #{MIN_INIT_PRICE}!",
deltatime: "Time span is too short! Must be at least one day!" deltatime: "Time span is too short! Must be at least one day!",
bidamount: "Bid amount must be at least #{((AH_BIDS_FACTOR-1)*100).round(2)}% greater than the highest bid!"
} }
# Auction stuff
AH_BUYOUT_FACTOR = 1.8

@ -65,6 +65,14 @@ BIO_REGEX_STR = "{#{MIN_BIO_LEN},#{MAX_BIO_LEN}}"
DESC_REGEX_STR = "{#{MIN_DESC_LEN},#{MAX_DESC_LEN}}" DESC_REGEX_STR = "{#{MIN_DESC_LEN},#{MAX_DESC_LEN}}"
TITLE_REGEX_STR = "{#{MIN_TITLE_LEN},#{MAX_TITLE_LEN}}" TITLE_REGEX_STR = "{#{MIN_TITLE_LEN},#{MAX_TITLE_LEN}}"
TIME_FORMATS = {
w: 604800,
d: 86400,
h: 3600,
m: 60,
s: 1
}
# Routes that needs auth # Routes that needs auth
AUTH_ROUTES = %w[/settings /auction /user /admin] AUTH_ROUTES = %w[/settings /auction /user /admin]

@ -283,12 +283,12 @@ class Auction < EntityModel
attr_reader :user_id, :title, :description, :init_price, :start_time, :end_time attr_reader :user_id, :title, :description, :init_price, :start_time, :end_time
def initialize(data) def initialize(data)
super data super data
@user_id = data["user_id"] @user_id = data["user_id"].to_i
@title = data["title"] @title = data["title"]
@description = data["description"] @description = data["description"]
@init_price = data["init_price"] @init_price = data["init_price"].to_i
@start_time = data["start_time"] @start_time = data["start_time"].to_i
@end_time = data["end_time"] @end_time = data["end_time"].to_i
end end
def self.validate_ah(title, description, init_price, delta_time) def self.validate_ah(title, description, init_price, delta_time)
@ -356,8 +356,43 @@ class Auction < EntityModel
Time.now.to_i > @end_time Time.now.to_i > @end_time
end end
def time_left
@end_time - Time.now.to_i
end
def time_left_s
left = self.time_left
result = []
TIME_FORMATS.each do |sym, count|
amount = left.to_i / count
if amount > 0 then
result << "#{amount}#{sym.to_s}"
puts "#{sym} #{count}, #{left} : #{amount} : #{result}"
left -= count*amount
end
end
return result.join ", "
end
def bids def bids
return [] Bid.get_bids(@id)
end
def place_bid(uid, amount, message)
Bid.place(@id, uid, amount, message)
end
def max_bid
max_bid = self.bids.max_by {|bid| bid.amount}
end
def current_bid
mbid = self.max_bid
if mbid != nil then
return mbid.amount
else
return @init_price
end
end end
end end
@ -372,8 +407,17 @@ class Bid < EntityModel
@message = data["message"] @message = data["message"]
end end
def self.get_bids(ahid)
data = self.get "*", "auction_id = ?", ahid
data && data.map! {|dat| self.new(dat)}
end
def self.place(ahid, uid, amount, message) def self.place(ahid, uid, amount, message)
# TODO: check if bid is greater than prev ah = Auction.find_by_id
if not ah then return false, "Invalid auction" end
max_bid = ah.max_bid
if amount >= max_bid.amount * AH_BIDS_FACTOR then
payload = { payload = {
auction_id: ahid, auction_id: ahid,
user_id: uid, user_id: uid,
@ -381,6 +425,9 @@ class Bid < EntityModel
message: message message: message
} }
self.insert(payload) self.insert(payload)
else
return false, AUCTION_ERRORS[:bidamount]
end
end end
end end

@ -57,3 +57,23 @@ get "/auctions/:id" do
raise Sinatra::NotFound raise Sinatra::NotFound
end end
end end
post "/auctions/:id/bids" do
id = params[:id].to_i
auction_obj = Auction.find_by_id id
amount = params[:amount].to_f
message = params[:message]
if !auction_obj.nil? then
success, resp = auction_obj.place_bid( session[:userid], amount, message)
if success then
flash[:success] = "Placed bid."
else
flash[:error] = resp
end
redirect "/auctions/#{id}"
else
raise Sinatra::NotFound
end
end

@ -18,19 +18,29 @@
#auctioninfo.card #auctioninfo.card
h2 = auction.title h2 = auction.title
- if auction.categories.length > 0
ul.list-container#category-list ul.list-container#category-list
ul
| Categories:
- auction.categories.each do |category| - auction.categories.each do |category|
ul = category.name ul style="color: #{category.color}" = category.name
- if auction.bids.length >= 1 - if not auction.expired?
.bid-container .card.border
- auctions.bids.each do |bid| h3
.bid | Current bid:
| BID span.green = "#{COINS_PREFIX}#{auction.current_bid}#{COINS_SUFFIX}"
h3
| Minimum bid factor:
span.gray = "#{((AH_BIDS_FACTOR-1)*100).round(2)}%"
h3
| Buyout factor:
span.gray = "#{((AH_BUYOUT_FACTOR-1)*100).round(2)}%"
h3
| Expires in:
span.red = "#{auction.time_left_s}"
- else - else
p = "No bids! Be the first to bid on this item!" h3.red.tcenter
| Auction Expired
script src="/js/slides.js" type="text/javascript" script src="/js/slides.js" type="text/javascript"

@ -15,6 +15,7 @@ $shadow_clr: $bg_dark_clr
$border_clr: $bg_alt_clr $border_clr: $bg_alt_clr
$green_clr: #5cb85c $green_clr: #5cb85c
$red_clr: #de2a1d $red_clr: #de2a1d
$yellow_clr: #FFBF00
$gray_clr: #888 $gray_clr: #888
$special_clr: #4776C1 $special_clr: #4776C1
@ -309,6 +310,8 @@ ul.list-container
color: $green_clr color: $green_clr
.red .red
color: $red_clr color: $red_clr
.yellow
color: $yellow_clr
.gray .gray
color: $gray_clr color: $gray_clr
@ -458,12 +461,22 @@ ul.list-container
#auctioninfo #auctioninfo
grid-area: in grid-area: in
.card
margin-top: 2rem
h2 h2
padding: 0
font-size: 1.8rem font-size: 1.8rem
h3
font-size: 1.5rem
#category-list #category-list
flex-direction: row flex-direction: row
height: unset height: unset
ul
padding: 0
margin-right: 1rem
.slideshow-container .slideshow-container
grid-area: im grid-area: im

Loading…
Cancel
Save