Merge branch 'feature/improve-player-list' into temp-develop

feature/embed-uploads
Tristan Gosselin-Hane 7 years ago
commit 510a7d47c0
No known key found for this signature in database
GPG Key ID: 86886FC44C72EAA1
  1. 22
      webhook-bridge.py

@ -39,9 +39,10 @@ BOT_USERNAME = ""
NEXT_MESSAGE_TIME = datetime.now(timezone.utc) NEXT_MESSAGE_TIME = datetime.now(timezone.utc)
PREVIOUS_MESSAGE = "" PREVIOUS_MESSAGE = ""
PLAYER_LIST = bidict() PLAYER_LIST = bidict()
PREVIOUS_PLAYER_LIST = bidict()
ACCEPT_JOIN_EVENTS = True
TAB_HEADER = "" TAB_HEADER = ""
TAB_FOOTER = "" TAB_FOOTER = ""
LAST_CONNECTION_TIME = datetime.now(timezone.utc)
def mc_uuid_to_username(uuid): def mc_uuid_to_username(uuid):
@ -174,7 +175,9 @@ def main():
def handle_disconnect(): def handle_disconnect():
logging.info('Disconnected.') logging.info('Disconnected.')
global PLAYER_LIST global PLAYER_LIST, PREVIOUS_PLAYER_LIST, ACCEPT_JOIN_EVENTS
PREVIOUS_PLAYER_LIST = PLAYER_LIST.copy()
ACCEPT_JOIN_EVENTS = False
PLAYER_LIST = bidict() PLAYER_LIST = bidict()
connection.disconnect(immediate=True) connection.disconnect(immediate=True)
time.sleep(15) time.sleep(15)
@ -261,6 +264,7 @@ def main():
TAB_FOOTER = json.loads(header_footer_packet.footer)["text"] TAB_FOOTER = json.loads(header_footer_packet.footer)["text"]
def handle_tab_list(tab_list_packet): def handle_tab_list(tab_list_packet):
global ACCEPT_JOIN_EVENTS
logging.debug("Processing tab list packet") logging.debug("Processing tab list packet")
for action in tab_list_packet.actions: for action in tab_list_packet.actions:
if isinstance(action, clientbound.play.PlayerListItemPacket.AddPlayerAction): if isinstance(action, clientbound.play.PlayerListItemPacket.AddPlayerAction):
@ -273,7 +277,7 @@ def main():
if action.name not in UUID_CACHE.inv: if action.name not in UUID_CACHE.inv:
UUID_CACHE.inv[action.name] = action.uuid UUID_CACHE.inv[action.name] = action.uuid
# Initial tablist backfill # Initial tablist backfill
if LAST_CONNECTION_TIME + timedelta(seconds=2.5) < datetime.now(timezone.utc): if ACCEPT_JOIN_EVENTS:
webhook_payload = { webhook_payload = {
'username': username, 'username': username,
'avatar_url': "https://visage.surgeplay.com/face/160/{}".format(player_uuid), 'avatar_url': "https://visage.surgeplay.com/face/160/{}".format(player_uuid),
@ -285,6 +289,15 @@ def main():
if config.es_enabled: if config.es_enabled:
es_connection(uuid=action.uuid, reason=ConnectionReason.CONNECTED, count=len(PLAYER_LIST)) es_connection(uuid=action.uuid, reason=ConnectionReason.CONNECTED, count=len(PLAYER_LIST))
return return
else:
# The bot's name is sent last after the initial back-fill
if action.name == BOT_USERNAME:
ACCEPT_JOIN_EVENTS = True
if config.es_enabled:
diff = set(PREVIOUS_PLAYER_LIST.keys()) - set(PLAYER_LIST.keys())
for uuid in diff:
es_connection(uuid=uuid, reason=ConnectionReason.DISCONNECTED, count=len(PLAYER_LIST))
if config.es_enabled: if config.es_enabled:
es_connection(uuid=action.uuid, reason=ConnectionReason.SEEN) es_connection(uuid=action.uuid, reason=ConnectionReason.SEEN)
if isinstance(action, clientbound.play.PlayerListItemPacket.RemovePlayerAction): if isinstance(action, clientbound.play.PlayerListItemPacket.RemovePlayerAction):
@ -305,8 +318,7 @@ def main():
del PLAYER_LIST[action.uuid] del PLAYER_LIST[action.uuid]
def handle_join_game(join_game_packet): def handle_join_game(join_game_packet):
global PLAYER_LIST, LAST_CONNECTION_TIME global PLAYER_LIST
LAST_CONNECTION_TIME = datetime.now(timezone.utc)
logging.info('Connected.') logging.info('Connected.')
PLAYER_LIST = bidict() PLAYER_LIST = bidict()

Loading…
Cancel
Save