diff --git a/include/client/main.nut b/include/client/main.nut
index 197cb6e..f126483 100644
--- a/include/client/main.nut
+++ b/include/client/main.nut
@@ -3,7 +3,5 @@ CLIENT_PASSWORD <- "";
addEventHandler("onPacket", function(data){
local id = data.readUInt8();
if (id == 250)
- {
CLIENT_PASSWORD = data.readString();
- }
})
diff --git a/include/pyg2o.xml b/include/pyg2o.xml
index 2c40932..ca82b48 100644
--- a/include/pyg2o.xml
+++ b/include/pyg2o.xml
@@ -1,5 +1,6 @@
-
-
-
-
\ No newline at end of file
+
+
+
+
+
diff --git a/include/server/events.nut b/include/server/events.nut
index 6afefd6..3498dfd 100644
--- a/include/server/events.nut
+++ b/include/server/events.nut
@@ -450,7 +450,7 @@ addEventHandler("onPlayerEquipSpell", function(playerid, slotId, instance)
addEventHandler("onPlayerJoin", function(playerid)
{
- client_password = _globalInstance.generateClientPassword();
+ local client_password = _globalInstance.generateClientPassword();
local packet = Packet();
packet.writeUInt8(250);
diff --git a/include/server/main.nut b/include/server/main.nut
index 6e5f60f..4d7fa35 100644
--- a/include/server/main.nut
+++ b/include/server/main.nut
@@ -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]});
+});
diff --git a/include/server/messages.nut b/include/server/messages.nut
index 2ba6a42..91a020d 100644
--- a/include/server/messages.nut
+++ b/include/server/messages.nut
@@ -3,28 +3,5 @@ function _message_call(data)
{
local compile_string = "try { " + data["data"] + " } catch(id) { print(\"[PyG2O] Error white executing the code: \" + id + \"\\nCode: " + data["data"] + "\"); return null; }";
local result = compilestring(compile_string)();
- local className = _getClassName(result);
- if (className == "Vec3")
- {
- data["data"] = {};
- data["data"]["x"] <- result.x;
- data["data"]["y"] <- result.y;
- data["data"]["z"] <- result.z;
- }
- else if (className == "Vec2i")
- {
- data["data"] = {};
- data["data"]["x"] <- result.x;
- data["data"]["y"] <- result.y;
- }
- else if (className != null)
- {
- data["data"] = {};
- data["data"]["obj_name"] <- className;
- data["data"]["obj_data"] <- _serializeObject(result);
- }
- else
- data["data"] = result;
-
- _send("result", data["data"], data["uuid"]);
+ _send({"uuid": data["uuid"], "data": result});
}