changes for player type, install return instance, fixes

This commit is contained in:
əlemi 2022-05-04 23:35:46 +02:00
parent 98d3967dfe
commit 2885c3c270
No known key found for this signature in database
GPG key ID: BBCBFE5D7244634E
3 changed files with 25 additions and 19 deletions

View file

@ -1,16 +1,24 @@
import uuid import uuid
import datetime import datetime
from enum import Enum
from typing import Dict, List from typing import Dict, List
from aiocraft.mc.definitions import Item from aiocraft.mc.definitions import Player
from aiocraft.mc.proto import PacketPlayerInfo from aiocraft.mc.proto import PacketPlayerInfo
from ..scaffold import Scaffold from ..scaffold import Scaffold
from ..events import ConnectedEvent from ..events import ConnectedEvent
class ActionType(Enum): # TODO move this in aiocraft
ADD_PLAYER = 0
UPDATE_GAMEMODE = 1
UPDATE_LATENCY = 2
UPDATE_DISPLAY_NAME = 3
REMOVE_PLAYER = 4
class GameTablist(Scaffold): class GameTablist(Scaffold):
tablist : Dict[uuid.UUID, dict] tablist : Dict[uuid.UUID, Player]
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
@ -25,18 +33,18 @@ class GameTablist(Scaffold):
async def tablist_update(packet:PacketPlayerInfo): async def tablist_update(packet:PacketPlayerInfo):
for record in packet.data: for record in packet.data:
uid = record['UUID'] uid = record['UUID']
if packet.action != 0 and uid not in self.tablist: if packet.action != ActionType.ADD_PLAYER.value and uid not in self.tablist:
continue # TODO this happens kinda often but doesn't seem to be an issue? continue # TODO this happens kinda often but doesn't seem to be an issue?
if packet.action == 0: if packet.action == ActionType.ADD_PLAYER.value:
self.tablist[uid] = record record['joinTime'] = datetime.datetime.now()
self.tablist[uid]['joinTime'] = datetime.datetime.now() self.tablist[uid] = Player(**record) # TODO have it be a Player type inside packet
elif packet.action == 1: elif packet.action == ActionType.UPDATE_GAMEMODE.value:
self.tablist[uid]['gamemode'] = record['gamemode'] self.tablist[uid].gamemode = record['gamemode']
elif packet.action == 2: elif packet.action == ActionType.UPDATE_LATENCY.value:
self.tablist[uid]['ping'] = record['ping'] self.tablist[uid].ping = record['ping']
elif packet.action == 3: elif packet.action == ActionType.UPDATE_DISPLAY_NAME.value:
self.tablist[uid]['displayName'] = record['displayName'] self.tablist[uid].displayName = record['displayName']
elif packet.action == 4: elif packet.action == ActionType.REMOVE_PLAYER.value:
self.tablist.pop(uid, None) self.tablist.pop(uid, None)

View file

@ -49,5 +49,4 @@ class CallbacksHolder:
async def join_callbacks(self): async def join_callbacks(self):
await asyncio.gather(*list(self._tasks.values())) await asyncio.gather(*list(self._tasks.values()))
self._tasks.clear()

View file

@ -159,7 +159,7 @@ class Treepuncher(
await super().stop() await super().stop()
self.logger.info("Treepuncher stopped") self.logger.info("Treepuncher stopped")
def install(self, module: Type[Addon]) -> Type[Addon]: def install(self, module: Type[Addon]) -> Addon:
m = module(self) m = module(self)
if isinstance(m, Provider): if isinstance(m, Provider):
self.notifier.add_provider(m) self.notifier.add_provider(m)
@ -167,7 +167,7 @@ class Treepuncher(
self.modules.append(m) self.modules.append(m)
else: else:
raise ValueError("Given type is not an addon") raise ValueError("Given type is not an addon")
return module return m
async def _work(self): async def _work(self):
try: try:
@ -189,13 +189,12 @@ class Treepuncher(
await self.join() await self.join()
except OSError as e: except OSError as e:
self.logger.error("Connection error : %s", str(e)) self.logger.error("Connection error : %s", str(e))
except AuthException as e:
self.logger.error("Auth exception : [%s|%d] %s (%s)", e.endpoint, e.code, e.data, e.kwargs)
break
if self._processing: # don't sleep if Treepuncher is stopping if self._processing: # don't sleep if Treepuncher is stopping
await asyncio.sleep(self.cfg.getfloat('reconnect_delay', fallback=5)) await asyncio.sleep(self.cfg.getfloat('reconnect_delay', fallback=5))
except AuthException as e:
self.logger.error("Auth exception : [%s|%d] %s (%s)", e.endpoint, e.code, e.data, e.kwargs)
except Exception: except Exception:
self.logger.exception("Unhandled exception") self.logger.exception("Unhandled exception")