EK.IceFlake

  • Hero Member
  • "We are the champions my fellahs" - Sufyan/VK.SuFy
  • Posts: 1,761
Passing 'this' to Connect
« on May 2nd, 2017, 01:33 PM »Last edited on May 2nd, 2017, 01:35 PM
I tried a script that saves a player's variables to the database when they disconnect.
Code: [Select]
UXPlayer.newmember("Login", function ()
{
//...
    this.On.Destroyed.Connect(this, function (header, payload)
    {
        Exec("Saving " + this.Name + " to database", function ()
        {
            ::UXPlayerVars.apply(function (v)
            {
                switch (typeof v)
                {
                    case "string": SQLd.Exec("insert or replace into players_vars ([name], [indx], varchar(24) [value]) values ('" + ::SQLite.Escape(this.Name) + "', '" + ::SQLite.Escape(v) + "', '" + this[v] + "');"); break;
                    case "integer": SQLd.Exec("insert or replace into players_vars ([name], [indx], integer [value]) values ('" + ::SQLite.Escape(this.Name) + "', '" + ::SQLite.Escape(v) + "', " + this[v] + ");"); break;
                    case "float": SQLd.Exec("insert or replace into players_vars ([name], [indx], float [value]) values ('" + ::SQLite.Escape(this.Name) + "', '" + ::SQLite.Escape(v) + "', " + this[v] + ");"); break;
                    default: throw "Invalid parameter type";
                }
                return v;
            });
        });
    });
});

And it gives me this error:
[ERR] Squirrel exception caught while destroying player
[ERR] the index 'Name' does not exist
[
=>Location: unknown
=>Line: unknown
=>Function: unknown
]
[INF] Traceback:
[
]
[INF] Locals:
[
]

How should I figure the out the player?
(:edit: I've noticed that this is vulnerable to an SQL injection attack since I didn't escape the value, only the player's name and the variable's name. Let's forget that for now since I solved it)
Re: Passing 'this' to Connect
« Reply #1, on May 2nd, 2017, 03:08 PM »
Solved by messing about with it
Code: [Select]
    this.On.Destroyed.Connect(this, function (header, payload)
    {
        local Entity = this;

        Exec("Saving " + this.Name + " to database", function ()
        {
            local v = "UID";
            ::UXPlayerVars.apply(function (v)
            {
                switch (typeof Entity[v])
                {
                    case "null": break;
                    case "string": SQLd.Exec("insert or replace into players_vars ([name], [indx], varchar(24) [value]) values ('" + ::SQLite.Escape(Entity.Name) + "', '" + ::SQLite.Escape(v) + "', '" + SQLite.Escape(Entity[v]) + "');"); break;
                    case "integer": SQLd.Exec("insert or replace into players_vars ([name], [indx], integer [value]) values ('" + ::SQLite.Escape(Entity.Name) + "', '" + ::SQLite.Escape(v) + "', " + SQLite.Escape(Entity[v]) + ");"); break;
                    case "float": SQLd.Exec("insert or replace into players_vars ([name], [indx], float [value]) values ('" + ::SQLite.Escape(Entity.Name) + "', '" + ::SQLite.Escape(v) + "', " + SQLite.Escape(Entity[v]) + ");"); break;
                    default: throw "Invalid parameter type";
                }
                return v;
            });
        });
    });