Skip to content

Commit

Permalink
#67 safe points
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Feb 5, 2020
1 parent 29cddae commit db39b49
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 9 deletions.
10 changes: 8 additions & 2 deletions objects/karma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,15 @@ def legend
]
end

def points
def points(safe: false)
earned = legend.map do |score, q, _text, _summary|
@pgsql.exec("SELECT COUNT(*) FROM (#{q}) AS q", [@author.id])[0]['count'].to_i * score
@pgsql.exec(
[
"SELECT COUNT(*) FROM (#{q}) AS q",
safe ? 'WHERE q.created < NOW() - INTERVAL \'2 DAY\'' : ''
].join(' '),
[@author.id]
)[0]['count'].to_i * score
end.inject(&:+)
paid = @pgsql.exec('SELECT SUM(points) FROM withdrawal WHERE author=$1', [@id])[0]['sum'].to_i
earned += 1000 if @author.vip?
Expand Down
2 changes: 1 addition & 1 deletion objects/projects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def submit(platform, coordinates)
end

def quota
10 - @pgsql.exec(
5 - @pgsql.exec(
'SELECT COUNT(*) FROM project WHERE created > NOW() - INTERVAL \'1 DAY\' AND author=$1',
[@author.id]
)[0]['count'].to_i
Expand Down
2 changes: 1 addition & 1 deletion objects/reviews.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def post(text)
end

def quota
10 - @pgsql.exec(
5 - @pgsql.exec(
'SELECT COUNT(*) FROM review WHERE created > NOW() - INTERVAL \'1 DAY\' AND author=$1',
[@project.author.id]
)[0]['count'].to_i
Expand Down
4 changes: 2 additions & 2 deletions objects/withdrawals.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ def initialize(pgsql, author, log: Loog::NULL)
# +points+ is the amount of Karma points to pay. Each Karma point will
# be converted to 1 USD.
def pay(wallet, points, wts, keygap)
raise Xia::Urror, 'Not enough karma to pay that much' if @author.karma.points < points
raise Xia::Urror, 'Not enough karma to pay that much' if @author.karma.points(safe: true) < points
rate = wts.usd_rate
zld = (points / rate).round(4)
wts.wait(wts.pull)
job = wts.pay(keygap, wallet, zld, "#{points} codexia karma points")
wts.wait(job)
@pgsql.exec(
'INSERT INTO withdrawal (author, wallet, points, zents) VALUES ($1, $2, $3, $4) RETURNING id',
[@author.id, wallet, points, zld.to_i]
[@author.id, wallet, points, Zold::Amount.new(zld: zld).to_i]
)[0]['id'].to_i
end

Expand Down
10 changes: 7 additions & 3 deletions views/karma.haml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@
- withdrawals.each do |w|
%p
%span.item
= w[:id]
%span.item
= "##{w[:id]}"
%span.item.monospace
= w[:points]
pts
%span.item.monospace
= w[:wallet]
%span.item
%span.item.monospace
= w[:zld].to_zld
ZLD
%span.item
= RelativeTime.in_words(w[:created])

Expand Down

0 comments on commit db39b49

Please sign in to comment.