From db39b4977871d6d6f41797f35dae2b32d161804d Mon Sep 17 00:00:00 2001 From: Yegor Bugayenko Date: Wed, 5 Feb 2020 23:18:53 +0300 Subject: [PATCH] #67 safe points --- objects/karma.rb | 10 ++++++++-- objects/projects.rb | 2 +- objects/reviews.rb | 2 +- objects/withdrawals.rb | 4 ++-- views/karma.haml | 10 +++++++--- 5 files changed, 19 insertions(+), 9 deletions(-) diff --git a/objects/karma.rb b/objects/karma.rb index f40c295..d486fd7 100644 --- a/objects/karma.rb +++ b/objects/karma.rb @@ -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? diff --git a/objects/projects.rb b/objects/projects.rb index 8df68d5..1c42133 100644 --- a/objects/projects.rb +++ b/objects/projects.rb @@ -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 diff --git a/objects/reviews.rb b/objects/reviews.rb index 0c22152..99148b4 100644 --- a/objects/reviews.rb +++ b/objects/reviews.rb @@ -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 diff --git a/objects/withdrawals.rb b/objects/withdrawals.rb index 1afaba2..0d69021 100644 --- a/objects/withdrawals.rb +++ b/objects/withdrawals.rb @@ -38,7 +38,7 @@ 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) @@ -46,7 +46,7 @@ def pay(wallet, points, wts, keygap) 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 diff --git a/views/karma.haml b/views/karma.haml index 6e9ee5e..20b07d0 100644 --- a/views/karma.haml +++ b/views/karma.haml @@ -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])