87 lines
2.0 KiB
Python
87 lines
2.0 KiB
Python
import struct
|
|
from objects import glob
|
|
from helpers import dataHelper
|
|
|
|
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
|
|
|
|
def query_r():
|
|
packet_data = []
|
|
|
|
rules = get_rules()
|
|
packet_data.append(len(rules)) # len_rules
|
|
|
|
for name, value in rules.items():
|
|
packet_data.append([
|
|
len(name), name,
|
|
len(value), value
|
|
])
|
|
|
|
flat_packet_data = dataHelper.flatten(packet_data)
|
|
|
|
packet = struct.pack(b"<H" + b"c%dsc%ds" * flat_packet_data[0] # using len_rules
|
|
% [len(y) for x in rules.items() for y in x], # array of only rules entries
|
|
*flat_packet_data)
|
|
|
|
return packet
|
|
|
|
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)
|
|
|
|
return packet
|
|
|
|
def get_online_players(): #TODO
|
|
return 0
|
|
|
|
def get_rules(): #TODO
|
|
return {"Rule name sample": "Rule value"}
|
|
|
|
def get_players_scores():
|
|
return {"Sunpy": 64, "username": 123}
|
|
|
|
RESPONSE = { #TODO: c, d & p (https://wiki.sa-mp.com/wiki/Query_Mechanism)
|
|
b"i": query_i,
|
|
b"r": query_r,
|
|
b"c": query_c
|
|
} |