This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.py
199 lines (156 loc) · 5.75 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env python3
# SPDX-License-Identifier: MIT
# Copyright (c) 2021-2024 scmanjarrez. All rights reserved.
# This work is licensed under the terms of the MIT license.
import sqlite3 as sql
from contextlib import closing
DB = "paimon.db"
def setup_db() -> None:
with closing(sql.connect(DB)) as db:
with closing(db.cursor()) as cur:
cur.executescript(
"""
CREATE TABLE IF NOT EXISTS users (
uid TEXT PRIMARY KEY,
resin_warn INTEGER DEFAULT 1,
resin INTEGER DEFAULT 150,
teapot_warn INTEGER DEFAULT 1,
teapot INTEGER DEFAULT 2200,
teapot_max INTEGER DEFAULT 0,
parametric_warn INTEGER DEFAULT 1,
expedition_warn INTEGER DEFAULT 1,
updates INTEGER DEFAULT 240
);
"""
)
def cached(uid: str) -> int:
with closing(sql.connect(DB)) as db:
with closing(db.cursor()) as cur:
cur.execute(
"SELECT EXISTS ("
"SELECT 1 "
"FROM users "
"WHERE uid = ?"
")",
[uid],
)
return cur.fetchone()[0]
def add_user(uid: str) -> None:
with closing(sql.connect(DB)) as db:
with closing(db.cursor()) as cur:
cur.execute("INSERT INTO users (uid) VALUES (?)", [uid])
db.commit()
def resin(uid: str) -> int:
with closing(sql.connect(DB)) as db:
with closing(db.cursor()) as cur:
cur.execute("SELECT resin FROM users WHERE uid = ?", [uid])
return cur.fetchone()[0]
def set_resin(uid: str, value: int) -> None:
with closing(sql.connect(DB)) as db:
with closing(db.cursor()) as cur:
cur.execute(
"UPDATE users SET resin = ? WHERE uid = ?", [value, uid]
)
db.commit()
def resin_warn(uid: str) -> int:
with closing(sql.connect(DB)) as db:
with closing(db.cursor()) as cur:
cur.execute("SELECT resin_warn FROM users WHERE uid = ?", [uid])
return cur.fetchone()[0]
def toggle_resin_warn(uid: str) -> None:
with closing(sql.connect(DB)) as db:
with closing(db.cursor()) as cur:
cur.execute(
"UPDATE users "
"SET resin_warn = -resin_warn "
"WHERE uid = ?",
[uid],
)
db.commit()
def teapot(uid: str) -> int:
with closing(sql.connect(DB)) as db:
with closing(db.cursor()) as cur:
cur.execute("SELECT teapot FROM users WHERE uid = ?", [uid])
return cur.fetchone()[0]
def set_teapot(uid: str, value: int) -> None:
with closing(sql.connect(DB)) as db:
with closing(db.cursor()) as cur:
cur.execute(
"UPDATE users SET teapot = ? WHERE uid = ?", [value, uid]
)
db.commit()
def teapot_max(uid: str) -> int:
with closing(sql.connect(DB)) as db:
with closing(db.cursor()) as cur:
cur.execute("SELECT teapot_max FROM users WHERE uid = ?", [uid])
return cur.fetchone()[0]
def set_teapot_max(uid: str, value: int) -> None:
with closing(sql.connect(DB)) as db:
with closing(db.cursor()) as cur:
cur.execute(
"UPDATE users SET teapot_max = ? WHERE uid = ?",
[value, uid],
)
db.commit()
def teapot_warn(uid: str) -> int:
with closing(sql.connect(DB)) as db:
with closing(db.cursor()) as cur:
cur.execute("SELECT teapot_warn FROM users WHERE uid = ?", [uid])
return cur.fetchone()[0]
def toggle_teapot_warn(uid: str) -> None:
with closing(sql.connect(DB)) as db:
with closing(db.cursor()) as cur:
cur.execute(
"UPDATE users "
"SET teapot_warn = -teapot_warn "
"WHERE uid = ?",
[uid],
)
db.commit()
def parametric_warn(uid: str) -> int:
with closing(sql.connect(DB)) as db:
with closing(db.cursor()) as cur:
cur.execute(
"SELECT parametric_warn FROM users WHERE uid = ?", [uid]
)
return cur.fetchone()[0]
def toggle_parametric_warn(uid: str) -> None:
with closing(sql.connect(DB)) as db:
with closing(db.cursor()) as cur:
cur.execute(
"UPDATE users "
"SET parametric_warn = -parametric_warn "
"WHERE uid = ?",
[uid],
)
db.commit()
def expedition_warn(uid: str) -> int:
with closing(sql.connect(DB)) as db:
with closing(db.cursor()) as cur:
cur.execute(
"SELECT expedition_warn FROM users WHERE uid = ?", [uid]
)
return cur.fetchone()[0]
def toggle_expedition_warn(uid: str) -> None:
with closing(sql.connect(DB)) as db:
with closing(db.cursor()) as cur:
cur.execute(
"UPDATE users "
"SET expedition_warn = -expedition_warn "
"WHERE uid = ?",
[uid],
)
db.commit()
def updates(uid: str) -> int:
with closing(sql.connect(DB)) as db:
with closing(db.cursor()) as cur:
cur.execute("SELECT updates FROM users WHERE uid = ?", [uid])
return cur.fetchone()[0]
def set_updates(uid: str, value: int) -> None:
with closing(sql.connect(DB)) as db:
with closing(db.cursor()) as cur:
cur.execute(
"UPDATE users SET updates = ? WHERE uid = ?",
[value, uid],
)
db.commit()