This repository has been archived by the owner on Feb 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_base.py
80 lines (64 loc) · 2.9 KB
/
data_base.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
import json
from threading import Lock
def is_string(_str):
return type(_str) is str or type(_str) is unicode
class DataBase:
__db = {}
__db_mutex = {}
def __check_types(self, ip, port, query_port, hostname, modname, players, max_players, last_update, version, passw):
if is_string(ip) \
and type(port) is int and type(query_port) is int and is_string(hostname) \
and type(players) is int and type(max_players) is int and type(last_update) is int\
and is_string(modname) and is_string(version) and type(passw) is bool:
return True
return False
def __id(self, addr, port):
return addr + ':' + str(port)
def if_exists(self, id):
return id in self.__db
def __update(self, ip, port, query_port, hostname, modname, players, max_players, last_update, version, passw):
id = self.__id(ip, port)
with self.__db_mutex[id]:
self.__db[id]['query_port'] = query_port
self.__db[id]['hostname'] = hostname
self.__db[id]['modname'] = modname
self.__db[id]['players'] = players
self.__db[id]['max_players'] = max_players
self.__db[id]['last_update'] = last_update
self.__db[id]['version'] = version
self.__db[id]['passw'] = passw
def add(self, ip, port, query_port, hostname, modname, players, max_players, last_update, version='<0.5.0', passw=False):
if not self.__check_types(ip, port, query_port, hostname, modname, players, max_players,
last_update, version, passw):
return
id = self.__id(ip, port)
if self.if_exists(id):
return
self.__db[id] = {}
self.__db_mutex[id] = Lock()
self.__update(ip, port, query_port, hostname, modname, players, max_players, last_update, version, passw)
return id
# todo: need partial update support
def update(self, ip, port, query_port, hostname, modname, players, max_players, last_update, version='<0.5.0', passw=False):
id = self.__id(ip, port)
if not self.if_exists(id):
return
if not self.__check_types(ip, port, query_port, hostname, modname, players, max_players,
last_update, version, passw):
return
self.__update(ip, port, query_port, hostname, modname, players, max_players, last_update, version, passw)
def resetTimer(self, id):
with self.__db_mutex[id]:
self.__db[id]['last_update'] = 0
@property
def db(self):
return self.__db
def delete(self, id):
with self.__db_mutex[id]:
if id not in self.__db:
return
self.__db.pop(id)
self.__db_mutex.pop(id)
def update_time(self, id, sec):
with self.__db_mutex[id]:
self.__db[id]['last_update'] += sec