maple_clb/client.py
2022-03-11 20:00:30 -07:00

109 lines
3.3 KiB
Python

from asyncio import sleep
from rich import print
from .character import Character
from .client_base import ClientBase
from .opcodes import RecvOps, SendOps
from .packet import iPacket, oPacket, packet_handler
from .packet_helper import Helpers
class Client(ClientBase):
def __init__(self, loop=None, username=None, password=None):
super().__init__(loop)
self._username = username
self._password = password
self._channels = []
self._characters = []
async def begin(self):
while self._loop.is_running() and self._sock.fileno():
action = await self._action_queue.get()
match action:
case SendOps.LOGIN_PASSWORD:
self._loop.create_task(self.do_login())
case _:
...
await sleep(0.5)
@packet_handler(RecvOps.PING)
async def pong(self, ipkt: iPacket):
print("Sending PONG packet")
await sleep(1)
opkt = oPacket(SendOps.PONG)
await self.send_packet(opkt)
@packet_handler(RecvOps.LOGIN_STATUS)
async def login_second(self, ipkt: iPacket):
ipkt.decode_byte()
ipkt.decode_byte()
ipkt.decode_int()
account_id = ipkt.decode_int()
gender = ipkt.decode_byte()
gm_1 = ipkt.decode_byte()
ipkt.decode_short()
gm_2 = ipkt.decode_byte()
account_name = ipkt.decode_string()
new_account = ipkt.decode_byte()
shadow_ban = ipkt.decode_byte()
shadow_ban_duration = ipkt.decode_long()
ipkt.decode_byte()
ipkt.decode_long()
ipkt.decode_int()
ipkt.decode_byte()
ipkt.decode_byte()
ipkt.decode_long()
print(f"Account ID: {account_id} | Gender: {gender} | GM 1: {gm_1} | GM 2: {gm_2} | Account Name: {account_name} | New Account: {new_account} | Shadow Ban: {shadow_ban} ")
await self.send_packet(oPacket(SendOps.SERVERLIST_REQUEST))
@packet_handler(RecvOps.SERVERLIST)
async def serverlist(self, ipkt: iPacket):
if world_id := ipkt.decode_byte() == 0xFF:
if not self._channels:
return
await self.send_packet(oPacket(SendOps.CHARLIST_REQUEST))
return
world_name = ipkt.decode_string()
world_flag = ipkt.decode_byte()
event_message = ipkt.decode_string()
print(world_id, world_name, world_flag, event_message)
ipkt.decode_short()
ipkt.decode_short()
ipkt.decode_byte()
channel_count = ipkt.decode_byte()
for _ in range(channel_count):
self._channels.append({
"name": ipkt.decode_string(),
"load": ipkt.decode_int(),
"world_id": ipkt.decode_byte(),
"channel_id": ipkt.decode_short()
})
ipkt.decode_short()
ipkt.decode_int()
print(self._channels)
@packet_handler(RecvOps.CHARLIST)
async def charlist(self, ipkt: iPacket):
self._characters = Helpers.character_entries(ipkt)
print(self._characters)
async def do_login(self):
opkt = oPacket(SendOps.LOGIN_PASSWORD)
opkt.encode_string(self._username)
opkt.encode_string(self._password)
print(f"Sending login request")
await self.send_packet(opkt)