Sign in to follow this  
Followers 0
kemperrr

Adding to mysql mod (with example)

11 posts in this topic

ATTENTION: topic for those who have already studied JS!

Hello, this is the first guide in the forum about mysql usage (with example).

 

First of all you should install mysql module from npm (npm install mysql).

Into the beginning of your source code file (e.g., after the "use strict") add:

var mysql = require('mysql');


var connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'root',
    password : '',
    database : 'gm'
});


connection.connect(function(err) {
    if(err) {
      console.log("Error connecting to the database ... ");
      throw err;
    } else {
      console.log('Database connected!')
    }
  });

So we've created our mysql connection (you'd edit configuration), then we'll implement kill stats.

 

Add this code to playerJoin event (it reads player stats on join):

connection.query("SELECT * FROM users WHERE name = ?", [player.name], function(err, results) {

  if(results.length >= 1) 
  {
    player.kills = results[0].kills;
  }
  else 
  {
    connection.query("INSERT INTO users (name) VALUES (?)", [player.name]);
    player.kills = 0;
  }
});

Also add this code to playerQuit (it saves updated kill stats when player leaves the server):

connection.query("UPDATE users SET kills= ? WHERE name = ?", [player.kills, player.name]);

That's all, we've done our kill stats! ^_^

 

Then you can use stats anywhere in your resource:

mp.events.add("playerChat", (player, text) =>
{
 const str = player.name + "<b>(" + player.kills + ")</b>: " + text;
 mp.players.forEach(_player => { _player.outputChatBox(str); });
});

 

Full code:  https://www.dropbox.com/s/zm92uuzx05ebcdy/gm in forum.zip?dl=0

 

Sorry for my english (google translate :D)

Edited by kemperrr
Added protection against sql injection
7 people like this

Share this post


Link to post
Share on other sites

Why not prepared statements?

I've not tested your code, but I think mysql injections are possible.

Share this post


Link to post
Share on other sites
5 минут назад, Bryan сказал:

Why not prepared statements?

I've not tested your code, but I think mysql injections are possible.

connection.escape() this is a protection

Share this post


Link to post
Share on other sites
2 minutes ago, kemperrr said:

connection.escape() this is a protection

I have not seen at the reading. Nevertheless, I would use prepared statements. :S

Share this post


Link to post
Share on other sites
16 часов назад, Bryan сказал:

I have not seen at the reading. Nevertheless, I would use prepared statements. :S

Added protection against sql injection

connection.query("UPDATE users SET kills= ? WHERE name = ?", [player.kills, player.name]);

Did you mean this?

1 person likes this

Share this post


Link to post
Share on other sites
3 часа назад, Neykuratick сказал:

where i can create mod for server?

VS Code ? xD

Share this post


Link to post
Share on other sites
3 часа назад, Neykuratick сказал:

where i can create mod for server?

VS Code, Notepad++, Sublime Text 3, Visual Studio, Brackets.

The best place for me is Sublime Text 3,

Share this post


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
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.