mirror of
https://github.com/kuitoi/kuitoi-Server.git
synced 2025-08-18 00:35:36 +00:00
__gracefully_kick;
_last_position; car position - client.cars[car_id]['pos'];
This commit is contained in:
parent
f181a82e0e
commit
3b5324d115
@ -37,6 +37,12 @@ class Client:
|
|||||||
self._cars = [None] * 21 # Max 20 cars per player + 1 snowman
|
self._cars = [None] * 21 # Max 20 cars per player + 1 snowman
|
||||||
self._snowman = {"id": -1, "packet": ""}
|
self._snowman = {"id": -1, "packet": ""}
|
||||||
self._connect_time = 0
|
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
|
@property
|
||||||
def _writer(self):
|
def _writer(self):
|
||||||
@ -74,6 +80,10 @@ class Client:
|
|||||||
def cars(self):
|
def cars(self):
|
||||||
return self._cars
|
return self._cars
|
||||||
|
|
||||||
|
@property
|
||||||
|
def last_position(self):
|
||||||
|
return self._last_position
|
||||||
|
|
||||||
def _update_logger(self):
|
def _update_logger(self):
|
||||||
self._log = utils.get_logger(f"{self.nick}:{self.cid}")
|
self._log = utils.get_logger(f"{self.nick}:{self.cid}")
|
||||||
self.log.debug(f"Update logger")
|
self.log.debug(f"Update logger")
|
||||||
@ -378,7 +388,8 @@ class Client:
|
|||||||
"json": car_json,
|
"json": car_json,
|
||||||
"json_ok": bool(car_json),
|
"json_ok": bool(car_json),
|
||||||
"snowman": snowman,
|
"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)
|
await self._send(pkt, to_all=True, to_self=True)
|
||||||
else:
|
else:
|
||||||
|
@ -36,7 +36,8 @@ class Client:
|
|||||||
self._identifiers = []
|
self._identifiers = []
|
||||||
self._cars: List[Optional[Dict[str, int]]] = []
|
self._cars: List[Optional[Dict[str, int]]] = []
|
||||||
self._snowman: Dict[str, Union[int, str]] = {"id": -1, "packet": ""}
|
self._snowman: Dict[str, Union[int, str]] = {"id": -1, "packet": ""}
|
||||||
|
self._last_position = {}
|
||||||
|
async def __gracefully_kick(self): ...
|
||||||
@property
|
@property
|
||||||
def _writer(self) -> StreamWriter: ...
|
def _writer(self) -> StreamWriter: ...
|
||||||
@property
|
@property
|
||||||
@ -55,6 +56,8 @@ class Client:
|
|||||||
def identifiers(self) -> list: ...
|
def identifiers(self) -> list: ...
|
||||||
@property
|
@property
|
||||||
def cars(self) -> List[dict | None]: ...
|
def cars(self) -> List[dict | None]: ...
|
||||||
|
@property
|
||||||
|
def last_position(self): ...
|
||||||
def is_disconnected(self) -> bool: ...
|
def is_disconnected(self) -> bool: ...
|
||||||
async def kick(self, reason: str) -> None: ...
|
async def kick(self, reason: str) -> None: ...
|
||||||
async def send_message(self, message: str | bytes, to_all: bool = True) -> None:...
|
async def send_message(self, message: str | bytes, to_all: bool = True) -> None:...
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
# Licence: FPA
|
# Licence: FPA
|
||||||
# (c) kuitoi.su 2023
|
# (c) kuitoi.su 2023
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import json
|
||||||
|
|
||||||
from core import utils
|
from core import utils
|
||||||
|
|
||||||
@ -22,12 +23,14 @@ class UDPServer(asyncio.DatagramTransport):
|
|||||||
self.port = port
|
self.port = port
|
||||||
self.run = False
|
self.run = False
|
||||||
|
|
||||||
def connection_made(self, transport): ...
|
def connection_made(self, transport):
|
||||||
|
...
|
||||||
|
|
||||||
async def handle_datagram(self, data, addr):
|
async def handle_datagram(self, data, addr):
|
||||||
try:
|
try:
|
||||||
cid = data[0] - 1
|
cid = data[0] - 1
|
||||||
code = data[2:3].decode()
|
code = data[2:3].decode()
|
||||||
|
data = data[2:].decode()
|
||||||
|
|
||||||
client = self.Core.get_client(cid=cid)
|
client = self.Core.get_client(cid=cid)
|
||||||
if client:
|
if client:
|
||||||
@ -39,9 +42,19 @@ class UDPServer(asyncio.DatagramTransport):
|
|||||||
if client._udp_sock != (self.transport, addr):
|
if client._udp_sock != (self.transport, addr):
|
||||||
client._udp_sock = (self.transport, addr)
|
client._udp_sock = (self.transport, addr)
|
||||||
self.log.debug(f"Set UDP Sock for CID: {cid}")
|
self.log.debug(f"Set UDP Sock for CID: {cid}")
|
||||||
ev.call_event("onChangePosition")
|
ev.call_event("onChangePosition", data=data)
|
||||||
if client:
|
sub = data.find("{", 1)
|
||||||
await client._send(data[2:], to_all=True, to_self=False, to_udp=True)
|
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 _:
|
case _:
|
||||||
self.log.debug(f"[{cid}] Unknown code: {code}")
|
self.log.debug(f"[{cid}] Unknown code: {code}")
|
||||||
else:
|
else:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user