Advertisement
Guest User

Untitled

a guest
Aug 27th, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. const clone  = require("./utils").clone;
  3. const config = require("../config");
  4. const mysql  = require("mysql");
  5.  
  6. var connection = mysql.createConnection( {
  7.         host     : "localhost",
  8.         database : "galactic_imperium",
  9.         user     : "root",
  10.         password : ""
  11.     } );
  12. connection.connect();
  13.  
  14. /** Initializes the database in the global variable _db. Doesn’t require or
  15.  *  return anything
  16.  */
  17. module.exports.init = function() {
  18.     _db = {
  19.         players: [],
  20.         areas: [],
  21.         planets: [],
  22.         species: []
  23.     };
  24.  
  25.     _db_vars = {
  26.         players: [],
  27.         areas: [],
  28.         planets: [],
  29.         species: []
  30.     };
  31.  
  32.     connection.query( "SELECT * FROM players", function( err, players ) {
  33.         if ( err ) {
  34.             throw err;
  35.         } else {
  36.             for ( var player in players ) {
  37.                 _db.players[players[player].login] = players[player];
  38.             }
  39.         }
  40.     } );
  41.  
  42.     connection.query( "SELECT * FROM areas", function( err, areas ) {
  43.         if ( err ) {
  44.             throw err;
  45.         } else {
  46.             for ( var area in areas ) {
  47.                 _db.areas[areas[area].name] = areas[area];
  48.             }
  49.         }
  50.     } );
  51.  
  52.     connection.query( "SELECT * FROM planets", function( err, planets ) {
  53.         if ( err ) {
  54.             throw err;
  55.         } else {
  56.             for ( var planet in planets ) {
  57.                 _db.planets[planets[planet].name] = planets[planet];
  58.             }
  59.         }
  60.     } );
  61.  
  62.     connection.query( "SELECT * FROM species", function( err, specie ) {
  63.         if ( err ) {
  64.             throw err;
  65.         } else {
  66.             for ( var species in specie ) {
  67.                 _db.species[specie[species].name] = specie[species];
  68.             }
  69.         }
  70.     } );
  71.  
  72.     console.log( "Database initialized !" );
  73. }
  74.  
  75. /** Gets an element in a given array of the database.
  76.  * @param array The name of the array in which to look for `id` in the
  77.  *  database. Is a string.
  78.  * @param id The name of the variable to look for in the database. Can be any
  79.  *  valid key type.
  80.  */
  81. module.exports.get = function( array, id ) {
  82.     if ( id ) {
  83.         if ( !_db[array][id] ) {
  84.             _db_vars[array][id] = [3, false];
  85.         }
  86.  
  87.         _db_vars[array][id][0] = 3;
  88.  
  89.         return clone( _db[array][id] );
  90.     } else {
  91.         return clone( _db[array] );
  92.     }
  93. }
  94.  
  95. /** Indicates whether some data is stored in a given array and at a given key
  96.  *  in the DB.
  97.  * @param array Name of the array in which to look for.
  98.  * @param id The name of the variable to look for in the array.
  99.  */
  100. module.exports.exists = function( array, id ) {
  101.     if (_db[array][id]) {
  102.         _db_vars[array][id][0] = 3;
  103.  
  104.         return true
  105.     } else {
  106.         return false;
  107.     }
  108. }
  109.  
  110. /** Exports a value in the database.
  111.  * @param array Name of the DB array.
  112.  * @param id Key of the DB array.
  113.  * @param value Value to set the value of the array of key `id` to.
  114.  */
  115. module.exports.set = function( array, id, value ) {
  116.     // Clone prevent to change _db content without using set.
  117.     _db[array][id] = clone(value);
  118.  
  119.     if( !_db_vars[array][id] ) {
  120.         _db_vars[array][id] = [];
  121.     }
  122.  
  123.     _db_vars[array][id][0] = 3;
  124.     _db_vars[array][id][1] = true;
  125. }
  126.  
  127. /**
  128.  * Write an object in his file.
  129.  * @param array Name of the DB array.
  130.  * @param id Key of the DB array.
  131.  */
  132. function write( array, id ) {
  133.     if ( _db[array][id] ) {
  134.         if ( _db[array][id].id ) {
  135.             connection.query( "UPDATE " + array + " SET ? WHERE id = " + db[array][id].id, _db[array][id], function( err ) {
  136.                 if ( err ) {
  137.                     throw err;
  138.                 } else {
  139.                     if( _db_vars[array][id][0] !== 0 ) {
  140.                         _db_vars[array][id][1] = false;
  141.                     }
  142.                 }
  143.             } );
  144.         } else {
  145.             connection.query( "INSERT INTO " + array + " SET ?", _db[array][id], function( err ) {
  146.                 if ( err ) {
  147.                     throw err;
  148.                 } else {
  149.                     if( _db_vars[array][id][0] !== 0 ) {
  150.                         _db_vars[array][id][1] = false;
  151.                     }
  152.                 }
  153.             } );
  154.         }
  155.     }
  156. }
  157.  
  158. /**
  159.  * Write all data form memory to database files.
  160.  */
  161. module.exports.flush = function() {
  162.     for ( var array in _db ) {
  163.         for ( var id in _db[array] ) {
  164.             --_db_vars[array][id][0];
  165.  
  166.             if ( _db_vars[array][id][1] ) {
  167.                 write( array, id );
  168.             }
  169.         }
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement