__gracefully_kick;

_last_position;
car position - client.cars[car_id]['pos'];
This commit is contained in:
Maxim Khomutov 2023-07-21 04:55:25 +03:00
parent f181a82e0e
commit 3b5324d115
3 changed files with 33 additions and 6 deletions

View File

@ -37,6 +37,12 @@ class Client:
self._cars = [None] * 21 # Max 20 cars per player + 1 snowman
self._snowman = {"id": -1, "packet": ""}
self._connect_time = 0
self._last_position = {}
ev.register_event("onServerStopped", self.__gracefully_kick)
async def __gracefully_kick(self, _):
await self.kick("Server shutdown!")
@property
def _writer(self):
@ -74,6 +80,10 @@ class Client:
def cars(self):
return self._cars
@property
def last_position(self):
return self._last_position
def _update_logger(self):
self._log = utils.get_logger(f"{self.nick}:{self.cid}")
self.log.debug(f"Update logger")
@ -378,7 +388,8 @@ class Client:
"json": car_json,
"json_ok": bool(car_json),
"snowman": snowman,
"over_spawn": (snowman and allow_snowman) or over_spawn
"over_spawn": (snowman and allow_snowman) or over_spawn,
"pos": {}
}
await self._send(pkt, to_all=True, to_self=True)
else:

View File

@ -36,7 +36,8 @@ class Client:
self._identifiers = []
self._cars: List[Optional[Dict[str, int]]] = []
self._snowman: Dict[str, Union[int, str]] = {"id": -1, "packet": ""}
self._last_position = {}
async def __gracefully_kick(self): ...
@property
def _writer(self) -> StreamWriter: ...
@property
@ -55,6 +56,8 @@ class Client:
def identifiers(self) -> list: ...
@property
def cars(self) -> List[dict | None]: ...
@property
def last_position(self): ...
def is_disconnected(self) -> bool: ...
async def kick(self, reason: str) -> None: ...
async def send_message(self, message: str | bytes, to_all: bool = True) -> None:...

View File

@ -5,6 +5,7 @@
# Licence: FPA
# (c) kuitoi.su 2023
import asyncio
import json
from core import utils
@ -22,12 +23,14 @@ class UDPServer(asyncio.DatagramTransport):
self.port = port
self.run = False
def connection_made(self, transport): ...
def connection_made(self, transport):
...
async def handle_datagram(self, data, addr):
try:
cid = data[0] - 1
code = data[2:3].decode()
data = data[2:].decode()
client = self.Core.get_client(cid=cid)
if client:
@ -39,9 +42,19 @@ class UDPServer(asyncio.DatagramTransport):
if client._udp_sock != (self.transport, addr):
client._udp_sock = (self.transport, addr)
self.log.debug(f"Set UDP Sock for CID: {cid}")
ev.call_event("onChangePosition")
if client:
await client._send(data[2:], to_all=True, to_self=False, to_udp=True)
ev.call_event("onChangePosition", data=data)
sub = data.find("{", 1)
last_pos_data = data[sub:]
try:
last_pos = json.loads(last_pos_data)
client._last_position = last_pos
_, car_id = client._get_cid_vid(data)
client._cars[car_id]['pos'] = last_pos
except Exception as e:
self.log.debug(f"Cannot parse position packet: {e}")
self.log.debug(f"data: {data}, sup: {sub}")
self.log.debug(f"last_pos_data: {last_pos_data}")
await client._send(data, to_all=True, to_self=False, to_udp=True)
case _:
self.log.debug(f"[{cid}] Unknown code: {code}")
else: