forked from MasterCash/ant-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatamanager.py
107 lines (93 loc) · 2.84 KB
/
datamanager.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
from queue import Empty
from multiprocessing import SimpleQueue
import signal
import sqlite3
from time import sleep
class Data:
state: int
id: int
power: int
alliance: str or None
name: str
loc: tuple[int, int]
class DataManager:
file_name = "db.sqlite"
def __init__(self) -> None:
con = self.getCon()
with con:
con.execute('''CREATE TABLE IF NOT EXISTS ant_hills
(id INTEGER PRIMARY KEY NOT NULL,
power INTEGER NOT NULL,
x INTEGER NOT NULL, y INTEGER NOT NULL,
time TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
UNIQUE (x, y))''')
con.execute('''CREATE TABLE IF NOT EXISTS id_names
(id INTEGER NOT NULL REFERENCES ant_hills (id) ON DELETE CASCADE ON UPDATE CASCADE,
name TEXT NOT NULL,
time TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY(id, name))''')
con.execute('''CREATE TABLE IF NOT EXISTS id_alliances
(id INTEGER NOT NULL REFERENCES ant_hills (id) ON DELETE CASCADE ON UPDATE CASCADE,
name TEXT,
time TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY(id, name))''')
con.close()
def getCon(self):
return sqlite3.connect(self.file_name)
def getIdFromName(self, name):
con = self.getCon()
with con:
cur = con.execute('''
SELECT id FROM id_names WHERE name IS ?
''', (name,))
result = cur.fetchone()
return result[0] if result != None else None
def getDataFromId(self, uid):
con = self.getCon()
with con:
cur = con.execute('''
SELECT * FROM ant_hills WHERE id IS ?
''', (str(uid),))
result = cur.fetchone()
return result
def sendData(self, uid, name, alliance, power, x, y):
con = self.getCon()
if uid is None or not uid:
uid = "None"
if name is None or not name:
name = "None"
if power is None:
power = 0
if x is None:
x = -1
if y is None:
y = -1
try:
with con:
con.execute('''
INSERT OR REPLACE INTO ant_hills (id, power, x, y)
VALUES (?, ?, ?, ?)
''',(uid, power, x, y))
con.execute('''
INSERT OR REPLACE INTO id_names (id, name)
VALUES (?, ?)
''', (uid, name))
con.execute('''
INSERT OR REPLACE INTO id_alliances (id, name)
VALUES (?, ?)
''', (uid, alliance))
except:
print(f'error entering: {uid}, {name}, {alliance}, {power}, {x}, {y}')
con.close()
def collectData(killswitch, dataQueue: SimpleQueue):
signal.signal(signal.SIGINT, signal.SIG_IGN)
database = DataManager()
while not (killswitch.value and dataQueue.empty()):
try:
if not dataQueue.empty():
(uid, name, alliance, power, x, y)= dataQueue.get()
database.sendData(uid, name, alliance, power, x, y)
sleep(5)
except Empty:
pass
dataQueue.close()