Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const clone = require("./utils").clone;
- const config = require("../config");
- const mysql = require("mysql");
- var connection = mysql.createConnection( {
- host : "localhost",
- database : "galactic_imperium",
- user : "root",
- password : ""
- } );
- connection.connect();
- /** Initializes the database in the global variable _db. Doesn’t require or
- * return anything
- */
- module.exports.init = function() {
- _db = {
- players: [],
- areas: [],
- planets: [],
- species: []
- };
- _db_vars = {
- players: [],
- areas: [],
- planets: [],
- species: []
- };
- connection.query( "SELECT * FROM players", function( err, players ) {
- if ( err ) {
- throw err;
- } else {
- for ( var player in players ) {
- _db.players[players[player].login] = players[player];
- }
- }
- } );
- connection.query( "SELECT * FROM areas", function( err, areas ) {
- if ( err ) {
- throw err;
- } else {
- for ( var area in areas ) {
- _db.areas[areas[area].name] = areas[area];
- }
- }
- } );
- connection.query( "SELECT * FROM planets", function( err, planets ) {
- if ( err ) {
- throw err;
- } else {
- for ( var planet in planets ) {
- _db.planets[planets[planet].name] = planets[planet];
- }
- }
- } );
- connection.query( "SELECT * FROM species", function( err, specie ) {
- if ( err ) {
- throw err;
- } else {
- for ( var species in specie ) {
- _db.species[specie[species].name] = specie[species];
- }
- }
- } );
- console.log( "Database initialized !" );
- }
- /** Gets an element in a given array of the database.
- * @param array The name of the array in which to look for `id` in the
- * database. Is a string.
- * @param id The name of the variable to look for in the database. Can be any
- * valid key type.
- */
- module.exports.get = function( array, id ) {
- if ( id ) {
- if ( !_db[array][id] ) {
- _db_vars[array][id] = [3, false];
- }
- _db_vars[array][id][0] = 3;
- return clone( _db[array][id] );
- } else {
- return clone( _db[array] );
- }
- }
- /** Indicates whether some data is stored in a given array and at a given key
- * in the DB.
- * @param array Name of the array in which to look for.
- * @param id The name of the variable to look for in the array.
- */
- module.exports.exists = function( array, id ) {
- if (_db[array][id]) {
- _db_vars[array][id][0] = 3;
- return true
- } else {
- return false;
- }
- }
- /** Exports a value in the database.
- * @param array Name of the DB array.
- * @param id Key of the DB array.
- * @param value Value to set the value of the array of key `id` to.
- */
- module.exports.set = function( array, id, value ) {
- // Clone prevent to change _db content without using set.
- _db[array][id] = clone(value);
- if( !_db_vars[array][id] ) {
- _db_vars[array][id] = [];
- }
- _db_vars[array][id][0] = 3;
- _db_vars[array][id][1] = true;
- }
- /**
- * Write an object in his file.
- * @param array Name of the DB array.
- * @param id Key of the DB array.
- */
- function write( array, id ) {
- if ( _db[array][id] ) {
- if ( _db[array][id].id ) {
- connection.query( "UPDATE " + array + " SET ? WHERE id = " + db[array][id].id, _db[array][id], function( err ) {
- if ( err ) {
- throw err;
- } else {
- if( _db_vars[array][id][0] !== 0 ) {
- _db_vars[array][id][1] = false;
- }
- }
- } );
- } else {
- connection.query( "INSERT INTO " + array + " SET ?", _db[array][id], function( err ) {
- if ( err ) {
- throw err;
- } else {
- if( _db_vars[array][id][0] !== 0 ) {
- _db_vars[array][id][1] = false;
- }
- }
- } );
- }
- }
- }
- /**
- * Write all data form memory to database files.
- */
- module.exports.flush = function() {
- for ( var array in _db ) {
- for ( var id in _db[array] ) {
- --_db_vars[array][id][0];
- if ( _db_vars[array][id][1] ) {
- write( array, id );
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement