Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var mysql = require('mysql');
- var con = mysql.createConnection({
- host: "localhost",
- user: "nodeFrederik",
- password: "node1234",
- database: "nodeFrederik"
- });
- const RANDOM_COIN_LEAST_TIME = 180000; // every 3 minutes
- const RANDOM_COIN_MOST_TIME = 1800000; // every 30 minutes
- var processedIndices = false;
- var locationIndices = [];
- var locations = [
- // RC
- {lat: [56.17215, 56.17250], lon: [10.18807, 10.18872], score: 10},
- // Nygaard
- {lat: [56.17144, 56.17192], lon: [10.18944, 10.19059], score: 5},
- // INCUBA
- {lat: [56.17225, 56.17267], lon: [10.18697, 10.18806], score: 5},
- // FøTeX
- {lat: [56.17078, 56.17112], lon: [10.18800, 10.18932], score: 3},
- // TÅGEKAMMERET
- {lat: [56.16638, 56.16654], lon: [10.19994, 10.20010], score: 2}
- ];
- var preprocressLocations = function() {
- for (var i = 0; i < locations.length; i++) {
- for (var j = 0; j < locations[i].score; j++) {
- locationIndices.push(i);
- }
- }
- processedIndices = true;
- }
- var getRandomCoinPosition = function() {
- if (!processedIndices) {
- preprocressLocations();
- }
- var i = Math.floor(Math.random() * locationIndices.length);
- var L = locations[locationIndices[i]];
- return {
- lat: L.lat[0] + (L.lat[1] - L.lat[0]) * Math.random(),
- lon: L.lon[0] + (L.lon[1] - L.lon[0]) * Math.random()
- }
- }
- var spawnRandomCoin = function() {
- var coinPosition = getRandomCoinPosition();
- console.log('I spawn a coin at (' + coinPosition.lat + ', ' + coinPosition.lon + ')')
- /*** Insert coin into database. ***/
- // INSERT INTO coins (lat, long)
- // Next coin!
- var nextTime = RANDOM_COIN_LEAST_TIME + (RANDOM_COIN_MOST_TIME - RANDOM_COIN_LEAST_TIME) * Math.random();
- setTimeout(spawnRandomCoin, Math.floor(nextTime));
- }
- module.exports = spawnRandomCoin;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement