clean and add exception

This commit is contained in:
Amazed 2021-02-16 12:31:04 +01:00
parent 0ffcb98d83
commit e10b15bfed

View File

@ -5,7 +5,6 @@ import asyncio
## stats ## stats
import pymysql import pymysql
num_players_alltime = 0
class Server: class Server:
def __init__(self, **kwargs): def __init__(self, **kwargs):
@ -13,14 +12,18 @@ class Server:
self.listen_port = kwargs.get("port", 19711) self.listen_port = kwargs.get("port", 19711)
self.register_with_ms = kwargs.get("register", True) self.register_with_ms = kwargs.get("register", True)
## stats ## stats
self.db = pymysql.connect(host='192.168.40.2', user="gck", passwd="gck", db="gck", autocommit=True) self.db = self.db_connect()
@classmethod
def db_connect(cls):
return pymysql.connect(host='192.168.40.2', user="gck", passwd="gck", db="gck", autocommit=True)
def db_execute(self, sql, args=None): def db_execute(self, sql, args=None):
try: try:
with self.db.cursor() as cursor: with self.db.cursor() as cursor:
cursor.execute(sql, args) cursor.execute(sql, args)
except: except:
self.db = pymysql.connect(host='192.168.40.2', user="gck", passwd="gck", db="gck", autocommit=True) self.db = self.db_connect()
return self.db_execute(sql, args) return self.db_execute(sql, args)
def db_query(self, sql, args=None): def db_query(self, sql, args=None):
@ -30,18 +33,17 @@ class Server:
t = cursor.fetchall() t = cursor.fetchall()
return t return t
except: except:
self.db = pymysql.connect(host='192.168.40.2', user="gck", passwd="gck", db="gck", autocommit=True) self.db = self.db_connect()
return self.db_query(sql, args) return self.db_query(sql, args)
async def on_connection_received(self, ip, port): async def on_connection_received(self, ip, port):
version = 1.5 version = 1.5
map_name = "Players last 24h/last 48h" map_name = "Players last 24h/last 48h"
server_name = "\x03!! V1.5 IS OUT !! \x04 Download on Discord: \x06https://discord.gg/Avj4azU\x01" server_name = "\x03!! V1.5 IS OUT !! \x04 Download on Discord: \x06https://discord.gg/Avj4azU\x01"
self.db_execute("INSERT INTO multiplayer_stats(ip, date) VALUES (%s, NOW())", (ip,)) self.db_execute("INSERT INTO multiplayer_stats(ip, date) VALUES (%s, NOW())", (ip,))
stats_24 = self.db_query("SELECT COUNT(DISTINCT ip) FROM multiplayer_stats WHERE date + INTERVAL 1 DAY > NOW()")[0][0] stats_24 = self.db_query("SELECT COUNT(DISTINCT ip) FROM multiplayer_stats WHERE date + INTERVAL 1 DAY > NOW() AND ip not in ('192.168.40.1')")[0][0]
stats_48 = self.db_query("SELECT COUNT(DISTINCT ip) FROM multiplayer_stats WHERE date + INTERVAL 2 DAY > NOW()")[0][0] stats_48 = self.db_query("SELECT COUNT(DISTINCT ip) FROM multiplayer_stats WHERE date + INTERVAL 2 DAY > NOW() AND ip not in ('192.168.40.1')")[0][0]
player_count = stats_24 player_count = stats_24
max_player_count = stats_48 max_player_count = stats_48