feat: Добавлен отдельный метод исполнения Sq кода
This commit is contained in:
@@ -54,6 +54,7 @@ class PyG2O
|
|||||||
if (!_silent)
|
if (!_silent)
|
||||||
print("[PyG2O] Successfully connected to " + url);
|
print("[PyG2O] Successfully connected to " + url);
|
||||||
|
|
||||||
|
_send({"event": "register_server"});
|
||||||
_send({"event": "init_temp_tokens", "token": _clientTokens});
|
_send({"event": "init_temp_tokens", "token": _clientTokens});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,7 +74,7 @@ class PyG2O
|
|||||||
{
|
{
|
||||||
local request = JSON.parse_ansi(message);
|
local request = JSON.parse_ansi(message);
|
||||||
if (!("uuid" in request) ||
|
if (!("uuid" in request) ||
|
||||||
!("data" in request))
|
!("code" in request))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
_message_call.bindenv(this)(request);
|
_message_call.bindenv(this)(request);
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
|
|
||||||
function _message_call(data)
|
function _message_call(data)
|
||||||
{
|
{
|
||||||
local compile_string = "try { " + data["data"] + " } catch(id) { print(\"[PyG2O] Error white executing the code: \" + id); return null; }";
|
local compile_string = "try { " + data["code"] + " } catch(id) { print(\"[PyG2O] Error white executing the code: \" + id); return null; }";
|
||||||
local result = compilestring(compile_string)();
|
local result = compilestring(compile_string)();
|
||||||
_send({"event": "sq_response", "uuid": data["uuid"], "data": result});
|
_send({"event": "sq_response", "uuid": data["uuid"], "data": result});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ class Server:
|
|||||||
_logger: loguru.Logger = loguru.logger
|
_logger: loguru.Logger = loguru.logger
|
||||||
_static_tokens: list[str] = []
|
_static_tokens: list[str] = []
|
||||||
_temp_tokens: list[str] = []
|
_temp_tokens: list[str] = []
|
||||||
|
_server_connection: WebSocket | None = None
|
||||||
_registered_clients: dict[int, list] = {}
|
_registered_clients: dict[int, list] = {}
|
||||||
_requests: WeakValueDictionary[str, asyncio.Future] = WeakValueDictionary()
|
_requests: WeakValueDictionary[str, asyncio.Future] = WeakValueDictionary()
|
||||||
_topics = TopicWeakDict()
|
_topics = TopicWeakDict()
|
||||||
@@ -74,6 +75,21 @@ class Server:
|
|||||||
except ValueError:
|
except ValueError:
|
||||||
cls._logger.exception('message должен быть типа dict')
|
cls._logger.exception('message должен быть типа dict')
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def sq_execute(cls, code: str) -> asyncio.Future | None:
|
||||||
|
if cls._server_connection is None:
|
||||||
|
return
|
||||||
|
|
||||||
|
request, data = cls._make_request()
|
||||||
|
data['code'] = code
|
||||||
|
data = json.dumps(data)
|
||||||
|
|
||||||
|
# Меняем синтаксис под Squirrel
|
||||||
|
data = data.replace("'", '\\"').replace('True', 'true').replace('False', 'false')
|
||||||
|
|
||||||
|
asyncio.create_task(cls._server_connection.send_text(data))
|
||||||
|
return request
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _make_request(cls):
|
def _make_request(cls):
|
||||||
request_id = str(uuid4())
|
request_id = str(uuid4())
|
||||||
@@ -128,6 +144,8 @@ class Server:
|
|||||||
cls._logger.exception(f'Ошибка декодирования JSON: {e}')
|
cls._logger.exception(f'Ошибка декодирования JSON: {e}')
|
||||||
except WebSocketDisconnect:
|
except WebSocketDisconnect:
|
||||||
cls._logger.info('WebSocket клиент отключился')
|
cls._logger.info('WebSocket клиент отключился')
|
||||||
|
playerid = next((key for key, values in cls._registered_clients.items() if connection in values), None)
|
||||||
|
if playerid is not None: cls._registered_clients[playerid].remove(connection)
|
||||||
except WebSocketException as e:
|
except WebSocketException as e:
|
||||||
cls._logger.exception(f'Ошибка WebSocket подключения: {e}')
|
cls._logger.exception(f'Ошибка WebSocket подключения: {e}')
|
||||||
|
|
||||||
@@ -168,6 +186,10 @@ class Server:
|
|||||||
except KeyError:
|
except KeyError:
|
||||||
cls._registered_clients[playerid] = [connection]
|
cls._registered_clients[playerid] = [connection]
|
||||||
|
|
||||||
|
case {'event': 'register_server'}:
|
||||||
|
if cls._server_connection is None:
|
||||||
|
cls._server_connection = connection
|
||||||
|
|
||||||
case {'event': 'sq_response', 'uuid': uuid, 'data': data}:
|
case {'event': 'sq_response', 'uuid': uuid, 'data': data}:
|
||||||
try:
|
try:
|
||||||
cls._requests[uuid].set_result(data)
|
cls._requests[uuid].set_result(data)
|
||||||
|
|||||||
Reference in New Issue
Block a user