Files
sampy/sampy/client/__init__.py
2020-03-30 00:38:02 +02:00

35 lines
1.1 KiB
Python

import struct
import socket
from . import base
from . import query
from . import player
STATE_UNKNOWN = (0, base.BaseClient)
STATE_QUERY = (1, query.QueryClient)
STATE_PLAYER = (2, player.PlayerClient)
class Client:
def __init__(self, server: "__ServerInstance__", ip: str, port: int):
self.server = server
self.ip = ip
self.port = port
self.set_state(STATE_UNKNOWN)
def set_state(self, state: tuple):
self.state = state
self.client = self.state[1](self.server, self.ip, self.port)
async def on_packet(self, packet: bytes):
if self.state == STATE_UNKNOWN:
# We are currently unaware if this is a player client or query client, but we got a packet that will be our check to know
if packet.startswith(b"SAMP"):
await self.client.on_state_change()
self.set_state(STATE_QUERY)
else:
await self.client.on_state_change()
self.set_state(STATE_PLAYER)
await self.client.on_packet(packet)