-
Notifications
You must be signed in to change notification settings - Fork 4
/
database.py
140 lines (113 loc) · 3.3 KB
/
database.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import config
import datetime
from sqlalchemy import create_engine, BigInteger, Boolean, Column, String, Integer, DateTime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, scoped_session
def start() -> scoped_session:
engine = create_engine(config.DB_URI)
BASE.metadata.bind = engine
BASE.metadata.create_all(engine)
return scoped_session(sessionmaker(bind=engine, autoflush=False))
try:
BASE = declarative_base()
SESSION = start()
except AttributeError as e:
print("DB_URI is not configured!")
raise e
class Ranking(BASE):
__tablename__ = "ranking"
_id = Column(Integer, primary_key=True, autoincrement=True)
user_id = Column(Integer)
user_name = Column(String)
score = Column(Integer)
chat = Column(BigInteger)
time = Column(DateTime)
def __init__(
self,
user_id,
user_name,
score,
chat,
time):
self.user_id = user_id
self.user_name = user_name
self.score = score
self.chat = chat
self.time = time
class Total(BASE):
__tablename__ = "total"
_id = Column(Integer, primary_key=True, autoincrement=True)
user_id = Column(BigInteger)
user_name = Column(String)
score = Column(Integer)
chat = Column(BigInteger)
def __init__(
self,
user_id,
user_name,
score,
chat):
self.user_id = user_id
self.user_name = user_name
self.score = score
self.chat = chat
class Perpetual(BASE):
__tablename__ = "perpetual"
chat_id = Column(BigInteger, primary_key=True)
set_by = Column(BigInteger)
def __init__(
self,
chat_id,
set_by):
self.chat_id = chat_id
self.set_by = set_by
Perpetual.__table__.create(checkfirst=True)
Total.__table__.create(checkfirst=True)
Ranking.__table__.create(checkfirst=True)
def get_week_table(chat_id):
try:
return SESSION.query(Ranking).filter(Ranking.time >= datetime.datetime.now() - datetime.timedelta(days=7), Ranking.chat == chat_id).all()
except:
return []
finally:
SESSION.close()
def get_total_table():
try:
return SESSION.query(Total).all()
except:
return []
finally:
SESSION.close()
def inc_or_new_user(user_id, user_name, score, chat, time):
user = SESSION.query(Total).filter(
Total.user_id == user_id, Total.chat == chat).first()
if user:
user.user_name = user_name
user.score += score
else:
user = Total(user_id, user_name, score, chat)
SESSION.add(user)
entry = Ranking(user_id, user_name, score, chat, time)
SESSION.add(entry)
SESSION.commit()
SESSION.close()
return entry, user
def perpetual_get_status(chat_id):
try:
return SESSION.query(Perpetual).get(chat_id)
except:
return False
finally:
SESSION.close()
def perpetual_toggle_status(chat_id, set_by):
try:
entry = SESSION.query(Perpetual).get(chat_id)
SESSION.delete(entry)
return False
except:
entry = Perpetual(chat_id, set_by)
SESSION.add(entry)
return True
finally:
SESSION.commit()
SESSION.close()