This repository has been archived on 2020-03-29. You can view files and clone it, but cannot push or open issues or pull requests.
sampy_archive/handlers/serverPingHandler.py

87 lines
2.0 KiB
Python
Raw Normal View History

2018-09-24 11:37:21 +02:00
import struct
from objects import glob
2018-09-26 08:41:50 +02:00
from helpers import dataHelper
2018-09-24 11:37:21 +02:00
def handle(client, data):
ping_ip = ".".join((str(x) for x in struct.unpack(b"<BBBB", data[4:8])))
print("![{}] -> Pinging to {}".format(client.addr, ping_ip))
if len(data) == 11: # We should add our data ontop
if data[-1:] in RESPONSE:
data += RESPONSE[data[-1:]]()
return data
def query_i():
len_hostname = len(glob.config["server"]["hostname"])
len_mode = len(glob.config["server"]["mode"])
len_language = len(glob.config["server"]["language"])
packet = struct.pack(b"<?HHI%dsI%dsI%ds" % (len_hostname, len_mode, len_language),
False,
get_online_players(),
glob.config["server"]["max_players"],
len_hostname,
glob.config["server"]["hostname"].encode(),
len_mode,
glob.config["server"]["mode"].encode(),
len_language,
glob.config["server"]["language"].encode())
return packet
2018-09-24 13:40:46 +02:00
def query_r():
packet_data = []
rules = get_rules()
packet_data.append(len(rules)) # len_rules
for name, value in rules.items():
2018-09-26 08:41:50 +02:00
packet_data.append([
len(name), name,
len(value), value
])
2018-09-24 13:40:46 +02:00
2018-09-26 08:41:50 +02:00
flat_packet_data = dataHelper.flatten(packet_data)
2018-09-24 13:40:46 +02:00
2018-09-26 08:41:50 +02:00
packet = struct.pack(b"<H" + b"c%dsc%ds" * flat_packet_data[0] # using len_rules
2018-09-26 09:40:17 +02:00
% [len(y) for x in rules.items() for y in x], # array of only rules entries
2018-09-24 13:40:46 +02:00
*flat_packet_data)
return packet
2018-09-26 09:42:18 +02:00
def query_c():
packet_data = []
players_scores = get_players_scores()
packet_data.append(len(players_scores))
for name, value in players_scores.items():
packet_data.append([
len(name), name,
value
])
flat_packet_data = dataHelper.flatten(packet_data)
packet = struct.pack(b"<H" + b"c%dscI" * flat_packet_data[0]
% [len(x) for x in players_scores.keys()],
*flat_packet_data)
2018-09-26 09:43:05 +02:00
return packet
2018-09-26 09:42:18 +02:00
2018-09-24 11:37:21 +02:00
def get_online_players(): #TODO
return 0
2018-09-24 13:40:46 +02:00
def get_rules(): #TODO
return {"Rule name sample": "Rule value"}
2018-09-26 09:42:18 +02:00
def get_players_scores():
return {"Sunpy": 64, "username": 123}
2018-09-24 13:40:46 +02:00
RESPONSE = { #TODO: c, d & p (https://wiki.sa-mp.com/wiki/Query_Mechanism)
b"i": query_i,
2018-09-26 09:42:18 +02:00
b"r": query_r,
b"c": query_c
2018-09-24 11:37:21 +02:00
}