feat: Squirrel изменения для PyG2O

This commit is contained in:
AURUMVORXX
2025-09-02 20:40:00 +05:00
parent fdfb01f825
commit 0e3d061f4b
5 changed files with 23 additions and 92 deletions

View File

@@ -1,5 +1,6 @@
_globalInstance <- -1;
local _clientTokens = [];
class PyG2O
{
@@ -9,9 +10,7 @@ class PyG2O
_reconnect_attempts = 0;
_max_reconnect_attempts = 0;
_constantsInitialized = false;
constructor(url, silent = false, max_reconnect_attempts = 0)
constructor(url, silent = false, headers = {}, max_reconnect_attempts = 0)
{
_url = url;
_max_reconnect_attempts = max_reconnect_attempts;
@@ -20,6 +19,7 @@ class PyG2O
_connection = WebsocketClient();
_connection.silent = _silent;
_connection.setUrl(_url);
_connection.headers = headers;
_connection.onOpen = _onOpen.bindenv(this);
_connection.onClose = _onClose.bindenv(this);
@@ -43,71 +43,16 @@ class PyG2O
print("[PyG2O] Stopped connection");
}
function _send(type, data, uuid = "none")
function _send(data, uuid = "none")
{
local sendData = {
"data": data
}
if (uuid != "none")
sendData["uuid"] <- uuid;
data["uuid"] <- uuid;
_connection.send(JSON.dump_ansi(sendData, 2));
}
function _initializeConstants()
{
if (_constantsInitialized)
return;
_send("init_constants", getconsttable());
}
function _getClassName(object)
{
if (object instanceof DamageDescription)
return "DamageDescription";
else if (object instanceof ItemGround)
return "ItemGround";
else if (object instanceof Vec3)
return "Vec3";
else if (object instanceof Vec2i)
return "Vec2i";
return null;
}
function _serializeObject(object)
{
local cls = object.getclass();
local tab = {};
if (object instanceof DamageDescription)
{
tab["_flags"] <- object.flags;
tab["_damage"] <- object.damage;
tab["_item_instance"] <- object.item_instance;
tab["_distance"] <- object.distance;
tab["_spell_id"] <- object.spell_id;
tab["_spell_level"] <- object.spell_level;
tab["_node"] <- object.node;
}
else if (object instanceof ItemGround)
{
tab["_id"] <- object.id;
tab["_instance"] <- object.instance;
tab["_amount"] <- object.amount;
tab["_world"] <- object.world;
tab["_virtualWorld"] <- object.virtualWorld;
tab["_position"] <- object.getPosition();
tab["_rotation"] <- object.getRotation();
}
return tab;
_connection.send(JSON.dump_ansi(data, 2));
}
function _onOpen(url)
{
_initializeConstants();
if (!_silent)
print("[PyG2O] Successfully connected to " + url);
}
@@ -148,3 +93,13 @@ class PyG2O
return result;
}
}
addEventHandler("onPlayerJoin", function(playerid){
new_token = _globalInstance.generateClientPassword();
_globalInstance.send({"create_temp_token": new_token})
_clientTokens[playerid] = new_token;
});
addEventHandler("onPlayerDisconnect", function(playerid, reason){
_globalInstance.send({"remove_temp_token": _clientTokens[playerid]});
});