Jump to content
RAGE Multiplayer Community

Recommended Posts

Posted (edited)

Hey, 

I scripted a simple login script with mysql and javascript. I have a running NodeJs, apache2, phpmyadmin and mysql(Mariadb)

Here is my client side script 

Spoiler
hashCode = function(str) {
    var hash = 0, i, chr, len;
    if (str.length === 0) return hash;
    for (i = 0, len = str.length; i < len; i++) {
        chr = str.charCodeAt(i);
        hash = ((hash << 5) - hash) + chr;
        hash |= 0;
    }
    return hash;
};

let mysql = require("mysql");

var mysql = mysql.createConnection({
    host: "localhost",
    user: "user",
    password: "pw",
    database: "gtav",
})

connection.connect(function(e){
    if(e)
    {
        console.log("No connection to database!" + e);
    }
    else
    {
        console.log("connection successfull!");
    }
})

mp.events.add("registerLogin.server",(player,username,password,type) => {
    if(type == 0){
        connection.query("SELECT * FROM userdata WHERE Username = ?",[username],function(err,res){
            if(res.length > 0){
                player.outputChatBox("Der Angegeben Name ist bereits Vergeben!");
            }
            else{
                connection.query("SELECT * FROM userdata WHERE SocialClub = ?",[player.socialClub],function(err,res){
                    if(res.length > 0)
                    {
                        player.outputChatBox("Du hast bereits einen Account!");
                    }
                    else{
                        connection.query("INSERT INTO userdata set Username = ?, Passwort = ?, SocialClub = ?, Money = ?",[username,hashCode(password),socialClub,1000], function(err,res){
                            if(!err)
                            {
                                player.outputChatBox("Du hast dich erfolgreich registriert!");
                                setPlayerDatasAfterlogin(player,username);
                            }
                        })
                    }
                })
            }
        })
    }
    else{
        connection.query("SELECT * FROM userdata WHERE Username = ? AND Passwort = ?",[username,hashCode(password)],function(err,res){
            if(res.length > 0)
            {
                player.outputChatBox("You have beenn logged in!");
                setPlayerDatasAfterlogin(player,username);
            }
            else
            {
                player.outputChatBox("Username or Password is wrong!");
            }
        })
    }
})

function setPlayerDatasAfterlogin(player,username){
    connection.query("SELECT * FROM userdata WHERE Username = ?",[username],function(e,r){
        player.setVariable("Money",r[0].Money);
    })
    player.spawn(new mp.Vector3(436.491, -982.172, 30.699));
    player.call("registerLogin.destroy");
}

 

But, whatever i do i only get an error in return 

Spoiler

Starting RAGE Multiplayer server...

[=========================================================]

        MaxPlayers:             10
        Sync rate:              40
        Name:                   TestServer [Test]
        Gamemode:               freeroam
        Streaming distance:     500
        Announcement:           disabled
        Voice chat:             disabled
        Address:                127.0.0.1:22005
        Connection limits:      disabled
        Encryption:             disabled
        NodeJS:                 enabled
        C#:                     disabled

[=========================================================]


[N] Starting network... - OK: (IPv4-only) at 127.0.0.1:22005
(node:3485) ExperimentalWarning: The ESM module loader is experimental.
Loading packages
        "gtav" loaded
Error: /opt/ragemp/ragemp-srv/packages/gtav/server.js:14
var mysql = mysql.createConnection({
    ^

SyntaxError: Identifier 'mysql' has already been declared
    at new Script (vm.js:80:7)
    at createScript (vm.js:264:10)
    at Proxy.runInThisContext (vm.js:316:10)
    at Module._compile (internal/modules/cjs/loader.js:670:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:718:10)
    at Module.load (internal/modules/cjs/loader.js:605:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:544:12)
    at Function.Module._load (internal/modules/cjs/loader.js:536:3)
    at Module.require (internal/modules/cjs/loader.js:643:17)
    at require (internal/modules/cjs/helpers.js:22:18)
[P] Plugin bridge.so loaded!
Started HTTP server at 22006 port.

 

Can you help me out?

 

Edited by Heizungauf6
Link to post
Share on other sites
43 minutes ago, Heizungauf6 said:

Hey, 

I scripted a simple login script with mysql and javascript. I have a running NodeJs, apache2, phpmyadmin and mysql(Mariadb)

Here is my client side script 

  Hide contents

hashCode = function(str) {
    var hash = 0, i, chr, len;
    if (str.length === 0) return hash;
    for (i = 0, len = str.length; i < len; i++) {
        chr = str.charCodeAt(i);
        hash = ((hash << 5) - hash) + chr;
        hash |= 0;
    }
    return hash;
};

let mysql = require("mysql");

var mysql = mysql.createConnection({
    host: "localhost",
    user: "user",
    password: "pw",
    database: "gtav",
})

connection.connect(function(e){
    if(e)
    {
        console.log("No connection to database!" + e);
    }
    else
    {
        console.log("connection successfull!");
    }
})

mp.events.add("registerLogin.server",(player,username,password,type) => {
    if(type == 0){
        connection.query("SELECT * FROM userdata WHERE Username = ?",[username],function(err,res){
            if(res.length > 0){
                player.outputChatBox("Der Angegeben Name ist bereits Vergeben!");
            }
            else{
                connection.query("SELECT * FROM userdata WHERE SocialClub = ?",[player.socialClub],function(err,res){
                    if(res.length > 0)
                    {
                        player.outputChatBox("Du hast bereits einen Account!");
                    }
                    else{
                        connection.query("INSERT INTO userdata set Username = ?, Passwort = ?, SocialClub = ?, Money = ?",[username,hashCode(password),socialClub,1000], function(err,res){
                            if(!err)
                            {
                                player.outputChatBox("Du hast dich erfolgreich registriert!");
                                setPlayerDatasAfterlogin(player,username);
                            }
                        })
                    }
                })
            }
        })
    }
    else{
        connection.query("SELECT * FROM userdata WHERE Username = ? AND Passwort = ?",[username,hashCode(password)],function(err,res){
            if(res.length > 0)
            {
                player.outputChatBox("You have beenn logged in!");
                setPlayerDatasAfterlogin(player,username);
            }
            else
            {
                player.outputChatBox("Username or Password is wrong!");
            }
        })
    }
})

function setPlayerDatasAfterlogin(player,username){
    connection.query("SELECT * FROM userdata WHERE Username = ?",[username],function(e,r){
        player.setVariable("Money",r[0].Money);
    })
    player.spawn(new mp.Vector3(436.491, -982.172, 30.699));
    player.call("registerLogin.destroy");
}

 

But, whatever i do i only get an error in return 

  Hide contents


Starting RAGE Multiplayer server...

[=========================================================]

        MaxPlayers:             10
        Sync rate:              40
        Name:                   TestServer [Test]
        Gamemode:               freeroam
        Streaming distance:     500
        Announcement:           disabled
        Voice chat:             disabled
        Address:                127.0.0.1:22005
        Connection limits:      disabled
        Encryption:             disabled
        NodeJS:                 enabled
        C#:                     disabled

[=========================================================]


[N] Starting network... - OK: (IPv4-only) at 127.0.0.1:22005
(node:3485) ExperimentalWarning: The ESM module loader is experimental.
Loading packages
        "gtav" loaded
Error: /opt/ragemp/ragemp-srv/packages/gtav/server.js:14
var mysql = mysql.createConnection({
    ^

SyntaxError: Identifier 'mysql' has already been declared
    at new Script (vm.js:80:7)
    at createScript (vm.js:264:10)
    at Proxy.runInThisContext (vm.js:316:10)
    at Module._compile (internal/modules/cjs/loader.js:670:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:718:10)
    at Module.load (internal/modules/cjs/loader.js:605:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:544:12)
    at Function.Module._load (internal/modules/cjs/loader.js:536:3)
    at Module.require (internal/modules/cjs/loader.js:643:17)
    at require (internal/modules/cjs/helpers.js:22:18)
[P] Plugin bridge.so loaded!
Started HTTP server at 22006 port.

 

Can you help me out?

 

let mysql = require("mysql");

var mysql = mysql.createConnection({
    host: "localhost",
    user: "user",
    password: "pw",
    database: "gtav",
})

Im not super familiar with this. But i guess its because you defined mysql twice. Once you have the mysql let and then again mysql as a var. As your failure says it has been declared twice. 

Link to post
Share on other sites
Posted (edited)
vor einer Stunde schrieb R1Z:
let mysql = require("mysql");

var mysql = mysql.createConnection({
    host: "localhost",
    user: "user",
    password: "pw",
    database: "gtav",
})

Im not super familiar with this. But i guess its because you defined mysql twice. Once you have the mysql let and then again mysql as a var. As your failure says it has been declared twice. 

Yeah, i also figured this out, but i dont know how to fix it 😕

Edited by Heizungauf6
Link to post
Share on other sites
4 hours ago, Heizungauf6 said:

Yeah, i also figured this out, but i dont know how to fix it 😕

Don't use the same variable name?

let mysql

var mysql

you can't use the same name, so just do

let mysql

var mysqlConnection

Link to post
Share on other sites
vor 7 Stunden schrieb MrPancakers:

Don't use the same variable name?

let mysql

var mysql

you can't use the same name, so just do

let mysql

var mysqlConnection

But when I change var MySQL to var MySQLconnect the log just says: can’t find module “MySQL”

Link to post
Share on other sites
vor 9 Minuten schrieb Heathen:

Why are you naming the variable the same name as the module? Plus, do you have the mysql module installed?

Yeah, thats it i forgot to install the module xD. But how do i install it on Debian 10?

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...