-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvote.py
63 lines (55 loc) · 2.4 KB
/
vote.py
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
import time
from fastapi import HTTPException
class Vote:
def __init__(self, redis):
self.redis = redis
self.yes_vote_redis = None
self.no_vote_redis = None
def process_vote(self, opinion, ip):
if opinion:
return self.yes_vote(ip)
return self.no_vote(ip)
def yes_vote(self, ip):
lua_script = """
if redis.call("EXISTS", "last_time_tocht_completed") == 1 and tonumber(ARGV[1]) - tonumber(redis.call("GET", "last_time_tocht_completed")) < 10 then
return false
end
if redis.call("EXISTS", "vote_start_time") == 0 then
redis.call("SET", "vote_start_time", ARGV[1])
end
local vote_start_time = tonumber(redis.call("GET", "vote_start_time"))
local delta_time = ARGV[1] - vote_start_time
local vote_value = 1 + 0.0000225 * delta_time^2
if vote_value >= 5 then
vote_value = 5
end
redis.call("SET", "last_time_voted", ARGV[1])
redis.call("INCR", "total_times_voted")
redis.call("SADD", "vote_participants", ARGV[2])
redis.call("INCRBYFLOAT", "vote", vote_value)
return true
"""
if self.yes_vote_redis == None:
self.yes_vote_redis = self.redis.register_script(lua_script)
result = self.yes_vote_redis(args=[time.time(), ip])
print(result)
if result != True:
raise HTTPException(status_code=422, detail="Vote can't be started at this moment, wait for 10s.")
def no_vote(self, ip):
lua_script = """
if redis.call("EXISTS", "vote_start_time") == 0 then
return "Tocht not started yet."
end
redis.call("INCR", "total_times_voted")
redis.call("SET", "last_time_voted", ARGV[1])
redis.call("SADD", "vote_participants", ARGV[2])
if tonumber(redis.call("GET", "vote")) < 1 then
return "Vote is < 1. You can't decrease the vote below 0."
end
redis.call("INCRBYFLOAT", "vote", -1)
"""
if self.no_vote_redis == None:
self.no_vote_redis = self.redis.register_script(lua_script)
result = self.no_vote_redis(args=[time.time(), ip])
if result != None:
raise HTTPException(status_code=422, detail=result.decode())