lot/lotV2.js
2017-09-08 10:45:53 +01:00

153 lines
3.0 KiB
JavaScript

const jsonfile = require('jsonfile'),
https = require('https'),
UltraSES = require('ultrases'), cron = require('node-cron');
const Sugar = require('sugar-date');
const pusher = require('./pusher');
const file = 'data/data.json';
const m = [];
const newNumberContainer = {};
let numberStore = [];
let resultsObj = {};
function buildArray() {
for (let x = 0; x < 51; x++) {
const l = [];
for (let y = 0; y < 51; y++)
l.push(0);
m[x] = l;
}
}
function sortNumber(a, b) {
return a - b;
}
function dynamicSort(property) {
let sortOrder = 1;
if (property[0] === '-') {
sortOrder = -1;
property = property.substr(1);
}
return function(a, b) {
const result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0;
return result * sortOrder;
};
}
function othercalcV2() {
// Var p = 1 / ((-1)*numberStore.length);
const p = 1.0 / (numberStore.length);
for (let i = 0; i < numberStore.length - 1; i++) {
const thisRow = [];
for (let s = 1; s < 6; s++) {
const cv = numberStore[i][s];
for (let n = 1; n < 6; n++)
if (n !== s) {
const workVal = numberStore[i][n];
// Console.log(workVal);
// m[cv-1][workVal-1]++;
// Console.log('mo', mo);
const tempArray = [cv, workVal].sort((a, b) => {
if (a < b)
return -1;
return a > b ? 1 : 0;
});
const id = tempArray.join('-');
if (thisRow.indexOf(id) === -1) {
const record = newNumberContainer[id] || { 'c': 0, 'v': 0, 'id': id };
record.c++;
record.v += (p * i);
newNumberContainer[id] = record;
thisRow.push(id);
}
}
}
}
let flatArray = Object.keys(newNumberContainer).map((key) => newNumberContainer[key]);
flatArray = flatArray.sort((a, b) => {
if (a.c < b.c)
return 1;
return a.c > b.c ? -1 : 0;
});
let line = [];
let lineI = 0;
while (line.length < 5) {
const pair = flatArray[lineI];
pairS = pair.id.split('-');
if (line.indexOf(pairS[0]) === -1 && line.length < 5)
line.push(pairS[0]);
if (line.indexOf(pairS[1]) === -1 && line.length < 5)
line.push(pairS[1]);
// console.log(line);
lineI++;
}
line = line.sort((a, b) => {
if (~~a < ~~b)
return -1;
return ~~a > ~~b ? 1 : 0;
});
//console.log(line);
return line;
}
function performCalcs() {
buildArray();
//BuildTable();
let line = othercalcV2();
console.log('---------------------------------------');
console.log(`line: ${line}`);
pusher.push(`line: ${line}`);
// Starcalc();
// tripCalc();
}
function GO() {
numberStore = [];
resultsObj = {};
jsonfile.readFile(file, function(err, obj) {
console.log(err);
numberStore = obj;
// Console.log(numberStore);
performCalcs();
//PrepareResults();
// console.log(resultsObj);
});
}
GO();