2019-01-22 01:30:42 +01:00
|
|
|
from dpnet.netserver import Netserver
|
2020-05-27 14:09:44 +02:00
|
|
|
from giants.masterserver import MasterServer
|
2019-02-06 01:07:00 +01:00
|
|
|
import asyncio
|
2019-01-23 10:28:23 +01:00
|
|
|
|
2020-05-28 11:38:10 +02:00
|
|
|
## stats
|
|
|
|
import pymysql
|
|
|
|
|
2019-01-22 01:30:42 +01:00
|
|
|
|
|
|
|
class Server:
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
self.listen_ip = kwargs.get("ip", "0.0.0.0")
|
|
|
|
self.listen_port = kwargs.get("port", 19711)
|
2021-02-16 12:24:15 +01:00
|
|
|
self.register_with_ms = kwargs.get("register", True)
|
2020-05-28 11:38:10 +02:00
|
|
|
## stats
|
2021-02-16 12:31:04 +01:00
|
|
|
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)
|
2020-05-28 11:38:10 +02:00
|
|
|
|
|
|
|
def db_execute(self, sql, args=None):
|
2021-02-16 12:24:15 +01:00
|
|
|
try:
|
|
|
|
with self.db.cursor() as cursor:
|
|
|
|
cursor.execute(sql, args)
|
|
|
|
except:
|
2021-02-16 12:31:04 +01:00
|
|
|
self.db = self.db_connect()
|
2021-02-16 12:24:15 +01:00
|
|
|
return self.db_execute(sql, args)
|
2020-05-28 11:38:10 +02:00
|
|
|
|
|
|
|
def db_query(self, sql, args=None):
|
2021-02-16 12:24:15 +01:00
|
|
|
try:
|
|
|
|
with self.db.cursor() as cursor:
|
|
|
|
cursor.execute(sql, args)
|
|
|
|
t = cursor.fetchall()
|
|
|
|
return t
|
|
|
|
except:
|
2021-02-16 12:31:04 +01:00
|
|
|
self.db = self.db_connect()
|
2021-02-16 12:24:15 +01:00
|
|
|
return self.db_query(sql, args)
|
|
|
|
|
2020-05-27 14:09:44 +02:00
|
|
|
async def on_connection_received(self, ip, port):
|
2021-02-16 12:24:15 +01:00
|
|
|
version = 1.5
|
2020-05-28 11:38:10 +02:00
|
|
|
map_name = "Players last 24h/last 48h"
|
2021-02-16 12:24:15 +01:00
|
|
|
server_name = "\x03!! V1.5 IS OUT !! \x04 Download on Discord: \x06https://discord.gg/Avj4azU\x01"
|
2020-05-28 11:38:10 +02:00
|
|
|
|
|
|
|
self.db_execute("INSERT INTO multiplayer_stats(ip, date) VALUES (%s, NOW())", (ip,))
|
2021-02-16 12:31:04 +01:00
|
|
|
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() AND ip not in ('192.168.40.1')")[0][0]
|
2020-05-28 11:38:10 +02:00
|
|
|
|
|
|
|
player_count = stats_24
|
|
|
|
max_player_count = stats_48
|
|
|
|
|
2020-05-27 14:09:44 +02:00
|
|
|
return version, server_name, player_count, max_player_count, map_name
|
2019-02-05 01:35:22 +01:00
|
|
|
|
2019-01-22 01:30:42 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2021-02-16 12:24:15 +01:00
|
|
|
server = Server(port=19110)
|
2020-05-27 14:09:44 +02:00
|
|
|
if server.register_with_ms:
|
|
|
|
ms = MasterServer(server)
|
2020-05-27 15:45:58 +02:00
|
|
|
register_thread = ms.register_and_run()
|
2019-02-06 01:07:00 +01:00
|
|
|
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
listen = loop.create_datagram_endpoint(lambda: Netserver(server), local_addr=(server.listen_ip, server.listen_port))
|
|
|
|
transport, protocol = loop.run_until_complete(listen)
|
|
|
|
try:
|
|
|
|
loop.run_forever()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
|
|
|
transport.close()
|
|
|
|
loop.close()
|