Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var mysql = require('mysql');
  2. var con = mysql.createConnection({
  3.     host: "localhost",
  4.     user: "nodeFrederik",
  5.     password: "node1234",
  6.     database: "nodeFrederik"
  7. });
  8.  
  9. const RANDOM_COIN_LEAST_TIME = 180000; // every 3 minutes
  10. const RANDOM_COIN_MOST_TIME = 1800000; // every 30 minutes
  11.  
  12. var processedIndices = false;
  13. var locationIndices = [];
  14. var locations = [
  15.     // RC
  16.     {lat: [56.17215, 56.17250], lon: [10.18807, 10.18872], score: 10},
  17.     // Nygaard
  18.     {lat: [56.17144, 56.17192], lon: [10.18944, 10.19059], score: 5},
  19.     // INCUBA
  20.     {lat: [56.17225, 56.17267], lon: [10.18697, 10.18806], score: 5},
  21.     // FøTeX
  22.     {lat: [56.17078, 56.17112], lon: [10.18800, 10.18932], score: 3},
  23.     // TÅGEKAMMERET
  24.     {lat: [56.16638, 56.16654], lon: [10.19994, 10.20010], score: 2}
  25. ];
  26.  
  27. var preprocressLocations = function() {
  28.     for (var i = 0; i < locations.length; i++) {
  29.         for (var j = 0; j < locations[i].score; j++) {
  30.             locationIndices.push(i);
  31.         }
  32.     }
  33.  
  34.     processedIndices = true;
  35. }
  36.  
  37. var getRandomCoinPosition = function() {
  38.     if (!processedIndices) {
  39.         preprocressLocations();
  40.     }
  41.  
  42.     var i = Math.floor(Math.random() * locationIndices.length);
  43.     var L = locations[locationIndices[i]];
  44.     return {
  45.         lat: L.lat[0] + (L.lat[1] - L.lat[0]) * Math.random(),
  46.         lon: L.lon[0] + (L.lon[1] - L.lon[0]) * Math.random()
  47.     }
  48. }
  49.  
  50. var spawnRandomCoin = function() {
  51.     var coinPosition = getRandomCoinPosition();
  52.  
  53.     console.log('I spawn a coin at (' + coinPosition.lat + ', ' + coinPosition.lon + ')')
  54.    
  55.     /*** Insert coin into database. ***/
  56.     // INSERT INTO coins (lat, long)
  57.  
  58.     // Next coin!
  59.     var nextTime = RANDOM_COIN_LEAST_TIME + (RANDOM_COIN_MOST_TIME - RANDOM_COIN_LEAST_TIME) * Math.random();
  60.     setTimeout(spawnRandomCoin, Math.floor(nextTime));
  61. }
  62.  
  63. module.exports = spawnRandomCoin;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement