diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 0000000..c8543bc --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,53 @@ +{ + "parserOptions": { + "ecmaVersion": 6, + "sourceType": "module", + "ecmaFeatures": { + "jsx": false + } + }, + "env": { + "browser": true, + "node": true, + "es6": true + }, + "rules": { + "arrow-spacing": "error", + "block-scoped-var": "error", + "block-spacing": "error", + "brace-style": ["error", "stroustrup", {}], + "camelcase": "error", + "comma-dangle": ["error", "never"], + "comma-spacing": ["error", { "before": false, "after": true }], + "comma-style": [1, "last"], + "consistent-this": [1, "_this"], + "curly": [1, "multi"], + "eol-last": 1, + "eqeqeq": 1, + "func-names": 1, + "indent": ["error", 2, { "SwitchCase": 1 }], + "lines-around-comment": ["error", { "beforeBlockComment": true, "allowArrayStart": true }], + "max-len": [1, 120, 2], // 2 spaces per tab, max 80 chars per line + "new-cap": 1, + "newline-before-return": "error", + "no-array-constructor": 1, + "no-inner-declarations": [1, "both"], + "no-mixed-spaces-and-tabs": 1, + "no-multi-spaces": 2, + "no-new-object": 1, + "no-shadow-restricted-names": 1, + "object-curly-spacing": ["error", "always"], + "padded-blocks": ["error", { "blocks": "never", "switches": "always" }], + "prefer-const": "error", + "prefer-template": "error", + "one-var": 0, + "quote-props": ["error", "always"], + "quotes": [1, "single"], + "radix": 1, + "semi": [1, "always"], + "space-before-blocks": [1, "always"], + "space-infix-ops": 1, + "vars-on-top": 1 + } + +} diff --git a/app.js b/app.js new file mode 100644 index 0000000..5f2e70c --- /dev/null +++ b/app.js @@ -0,0 +1,33 @@ +const loader = require('./libs/loader'); +const lot = require('./libs/lot'); +const lotv2 = require('./libs/lotV2'); +const pusher = require('./libs/pusher'); + + + +function run(data) { + const v1 = lot.calculate(data); + const v2 = lotv2.calculate(data); + + const msg = + +`V2: ${v2} stars: ${v1.mainstars} +V1: ${v1.mainline} stars: ${v1.mainstars} +Other: ${v1.otherLine} stars: ${v1.otherStars} +`; + + pusher.push(msg); + console.log(msg); + loader.save(data); +} + +function go() { + loader.load(run); +} + + +cron.schedule('10 13 * * 2,5', function() { + go(); + // console.log('tick'); + return -1; +}); diff --git a/data/data.json b/data/data.json index 319c348..cdd6e55 100644 --- a/data/data.json +++ b/data/data.json @@ -1 +1 @@ -[["2017-08-29",4,12,15,32,38,1,5],["2017-08-25",1,5,7,15,47,9,12],["2017-08-22",3,10,12,17,27,3,5],["2017-08-18",2,24,39,42,45,2,8],["2017-08-15",10,14,30,35,46,4,10],["2017-08-11",18,28,39,46,48,5,12],["2017-08-08",15,25,26,40,41,4,5],["2017-08-04",29,30,36,40,41,2,9],["2017-08-01",14,21,24,29,30,8,10],["2017-07-28",5,9,29,31,41,2,4],["2017-07-25",12,14,43,44,48,2,11],["2017-07-21",1,8,9,26,49,5,9],["2017-07-18",1,25,27,41,45,5,7],["2017-07-14",11,14,20,21,47,7,10],["2017-07-11",14,22,26,42,50,8,10],["2017-07-07",11,20,35,37,45,3,6],["2017-07-04",10,22,25,37,49,5,8],["2017-06-30",17,35,39,47,50,6,8],["2017-06-27",9,17,21,28,45,1,3],["2017-06-23",3,4,21,31,38,3,7],["2017-06-20",11,18,26,43,44,8,10],["2017-06-16",15,17,38,41,42,9,12],["2017-06-13",3,12,22,27,49,4,11],["2017-06-09",9,20,27,39,43,10,11],["2017-06-06",20,22,25,37,40,3,7],["2017-06-02",8,10,24,33,42,3,9],["2017-05-30",7,12,27,38,48,6,9],["2017-05-26",5,7,26,36,39,2,10],["2017-05-23",8,15,25,27,42,1,4],["2017-05-19",9,11,12,19,30,4,9],["2017-05-16",8,11,15,20,30,3,8],["2017-05-12",2,20,28,29,44,3,9],["2017-05-05",3,7,30,35,43,1,3],["2017-05-02",6,19,23,25,27,11,12],["2017-04-28",14,20,25,30,39,2,8],["2017-04-25",9,11,19,32,43,3,9],["2017-04-21",2,13,16,22,49,4,5],["2017-04-18",17,22,31,38,45,5,12],["2017-04-14",4,14,20,23,33,6,10],["2017-04-11",5,21,22,31,49,2,8],["2017-04-07",2,10,19,35,50,6,7],["2017-04-04",1,9,24,33,34,2,6],["2017-03-31",17,24,26,28,45,4,12],["2017-03-28",9,13,31,33,46,6,10],["2017-03-24",2,17,21,27,34,5,9],["2017-03-21",1,20,23,44,47,4,11],["2017-03-17",6,10,19,29,36,3,9],["2017-03-14",3,5,21,36,44,3,6],["2017-03-10",31,36,38,47,49,8,11],["2017-03-07",6,37,41,48,50,4,5],["2017-03-03",2,11,29,30,47,1,12],["2017-02-28",10,20,31,35,42,2,12],["2017-02-24",2,4,13,22,43,8,9],["2017-02-21",13,19,41,45,49,3,4],["2017-02-17",19,25,33,36,48,2,9],["2017-02-14",2,10,24,40,44,3,10],["2017-02-10",7,21,26,35,43,2,9],["2017-02-07",4,10,31,38,44,8,10],["2017-02-03",3,4,15,46,50,5,9],["2017-01-31",3,4,17,23,44,6,9],["2017-01-27",17,20,28,45,48,5,9],["2017-01-24",1,5,7,17,23,3,8],["2017-01-20",10,17,27,31,49,3,5],["2017-01-17",4,16,25,43,47,2,10],["2017-01-13",3,7,16,26,50,4,7],["2017-01-06",10,14,18,21,49,9,11],["2017-01-03",19,23,27,34,49,1,11],["2016-12-27",3,22,25,32,39,7,12],["2016-12-23",4,16,20,39,46,2,5],["2016-12-20",13,15,17,21,27,2,3],["2016-12-16",6,10,30,41,45,4,11],["2016-12-13",14,28,29,48,50,2,8],["2016-12-09",6,12,28,37,40,1,5],["2016-12-06",1,7,11,27,45,4,9],["2016-12-02",6,11,23,31,32,2,12],["2016-11-25",5,11,17,23,28,10,11],["2016-11-22",5,10,33,34,47,2,10],["2016-11-18",3,12,27,32,47,6,8],["2016-11-15",7,15,36,42,48,3,7],["2016-11-11",3,22,23,38,49,2,9],["2016-11-08",5,9,47,49,50,8,9],["2016-11-04",7,11,12,44,48,3,7],["2016-11-01",6,10,20,29,37,1,9],["2016-10-28",10,16,19,23,43,2,8],["2016-10-25",3,15,20,21,28,1,5],["2016-10-21",9,20,23,44,48,8,10],["2016-10-18",2,17,21,25,27,6,9],["2016-10-14",7,13,19,31,34,1,11],["2016-10-11",5,12,20,24,37,4,11],["2016-10-07",2,12,20,27,30,9,11],["2016-10-04",4,7,14,34,38,4,11],["2016-09-30",17,35,37,43,44,2,10],["2016-09-27",6,9,13,39,41,2,12],["2016-09-23",14,16,39,42,49,3,9],["2016-09-20",1,4,10,41,45,6,9],["2016-09-16",5,8,14,22,32,2,11],["2016-09-13",4,7,16,35,44,1,3],["2016-09-09",4,11,15,17,28,1,3],["2016-09-06",12,24,30,34,45,7,10],["2016-09-02",8,16,32,40,50,2,11],["2016-08-30",13,16,29,35,42,6,8],["2016-08-26",1,9,28,30,48,7,8],["2016-08-23",2,23,31,37,45,4,8],["2016-08-19",5,6,22,25,34,5,7],["2016-08-16",2,26,34,37,46,4,8],["2016-08-12",2,15,28,36,50,2,11],["2016-08-09",12,19,43,44,45,5,10],["2016-08-05",16,19,24,35,37,2,7],["2016-08-02",2,8,27,38,46,2,6],["2016-07-29",1,21,26,40,50,2,4],["2016-07-26",3,10,13,45,49,2,11],["2016-07-22",7,24,26,37,45,5,9],["2016-07-19",19,24,28,36,37,3,6],["2016-07-15",2,11,13,14,21,1,8],["2016-07-12",3,7,26,33,41,2,10],["2016-07-08",8,12,18,33,44,4,8],["2016-07-05",1,10,29,38,48,3,4],["2016-07-01",2,11,13,40,50,1,10],["2016-06-28",26,32,37,43,49,4,5],["2016-06-24",11,19,27,28,39,3,10],["2016-06-21",5,17,32,35,49,1,5],["2016-06-17",1,7,18,27,43,4,8],["2016-06-14",13,34,39,42,50,9,11],["2016-06-10",14,21,35,42,43,7,9],["2016-06-07",19,26,35,45,49,2,5],["2016-06-03",7,23,31,33,39,6,10],["2016-05-31",6,12,26,30,48,6,7],["2016-05-27",13,25,27,43,46,4,8],["2016-05-24",16,17,23,34,37,6,9],["2016-05-20",9,14,30,41,45,4,9],["2016-05-17",15,27,32,36,39,3,10],["2016-05-13",7,15,28,31,42,10,11],["2016-05-10",2,26,27,40,49,5,10],["2016-05-06",32,34,40,45,48,1,10],["2016-05-03",8,23,24,34,38,3,7],["2016-04-29",4,5,25,28,43,6,11],["2016-04-26",10,17,31,32,42,2,5],["2016-04-22",17,26,32,34,43,2,10],["2016-04-19",11,14,15,27,44,2,7],["2016-04-15",13,14,32,37,48,1,7],["2016-04-12",1,5,9,22,38,2,10],["2016-04-08",6,8,26,43,49,6,10],["2016-04-05",3,5,9,19,40,2,5],["2016-04-01",2,16,23,25,49,6,9],["2016-03-29",1,4,19,25,36,3,11],["2016-03-25",12,19,36,42,43,5,8],["2016-03-22",12,15,26,42,49,5,8],["2016-03-18",14,19,21,24,49,5,11],["2016-03-15",5,7,10,34,44,2,10],["2016-03-11",1,21,26,40,43,6,9],["2016-03-08",1,8,9,14,23,1,7],["2016-03-04",6,9,14,23,40,1,5],["2016-03-01",4,7,13,28,37,10,11],["2016-02-26",5,13,15,33,50,9,11],["2016-02-23",23,25,32,37,42,1,11],["2016-02-19",13,14,30,32,39,3,9],["2016-02-16",3,10,22,37,50,6,10],["2016-02-12",3,20,28,31,49,2,5],["2016-02-09",6,9,13,28,37,4,5],["05-Feb-2016",3,27,32,41,46,4,8],["02-Feb-2016",36,21,10,6,9,6,2],["29-Jan-2016",29,32,23,1,5,1,7],["26-Jan-2016",15,40,24,48,38,2,9],["22-Jan-2016",27,10,30,47,12,9,8],["19-Jan-2016",2,30,38,43,46,7,2],["15-Jan-2016",43,38,19,10,46,1,11],["12-Jan-2016",2,10,30,44,1,1,8],["08-Jan-2016",35,33,26,40,5,3,8],["05-Jan-2016",6,10,31,36,39,6,10],["01-Jan-2016",4,37,38,39,44,4,7]] +[["2017-09-01",3,7,8,14,4,5,8],["2017-08-29",4,12,15,32,38,1,5],["2017-08-25",1,5,7,15,47,9,12],["2017-08-22",3,10,12,17,27,3,5],["2017-08-18",2,24,39,42,45,2,8],["2017-08-15",10,14,30,35,46,4,10],["2017-08-11",18,28,39,46,48,5,12],["2017-08-08",15,25,26,40,41,4,5],["2017-08-04",29,30,36,40,41,2,9],["2017-08-01",14,21,24,29,30,8,10],["2017-07-28",5,9,29,31,41,2,4],["2017-07-25",12,14,43,44,48,2,11],["2017-07-21",1,8,9,26,49,5,9],["2017-07-18",1,25,27,41,45,5,7],["2017-07-14",11,14,20,21,47,7,10],["2017-07-11",14,22,26,42,50,8,10],["2017-07-07",11,20,35,37,45,3,6],["2017-07-04",10,22,25,37,49,5,8],["2017-06-30",17,35,39,47,50,6,8],["2017-06-27",9,17,21,28,45,1,3],["2017-06-23",3,4,21,31,38,3,7],["2017-06-20",11,18,26,43,44,8,10],["2017-06-16",15,17,38,41,42,9,12],["2017-06-13",3,12,22,27,49,4,11],["2017-06-09",9,20,27,39,43,10,11],["2017-06-06",20,22,25,37,40,3,7],["2017-06-02",8,10,24,33,42,3,9],["2017-05-30",7,12,27,38,48,6,9],["2017-05-26",5,7,26,36,39,2,10],["2017-05-23",8,15,25,27,42,1,4],["2017-05-19",9,11,12,19,30,4,9],["2017-05-16",8,11,15,20,30,3,8],["2017-05-12",2,20,28,29,44,3,9],["2017-05-05",3,7,30,35,43,1,3],["2017-05-02",6,19,23,25,27,11,12],["2017-04-28",14,20,25,30,39,2,8],["2017-04-25",9,11,19,32,43,3,9],["2017-04-21",2,13,16,22,49,4,5],["2017-04-18",17,22,31,38,45,5,12],["2017-04-14",4,14,20,23,33,6,10],["2017-04-11",5,21,22,31,49,2,8],["2017-04-07",2,10,19,35,50,6,7],["2017-04-04",1,9,24,33,34,2,6],["2017-03-31",17,24,26,28,45,4,12],["2017-03-28",9,13,31,33,46,6,10],["2017-03-24",2,17,21,27,34,5,9],["2017-03-21",1,20,23,44,47,4,11],["2017-03-17",6,10,19,29,36,3,9],["2017-03-14",3,5,21,36,44,3,6],["2017-03-10",31,36,38,47,49,8,11],["2017-03-07",6,37,41,48,50,4,5],["2017-03-03",2,11,29,30,47,1,12],["2017-02-28",10,20,31,35,42,2,12],["2017-02-24",2,4,13,22,43,8,9],["2017-02-21",13,19,41,45,49,3,4],["2017-02-17",19,25,33,36,48,2,9],["2017-02-14",2,10,24,40,44,3,10],["2017-02-10",7,21,26,35,43,2,9],["2017-02-07",4,10,31,38,44,8,10],["2017-02-03",3,4,15,46,50,5,9],["2017-01-31",3,4,17,23,44,6,9],["2017-01-27",17,20,28,45,48,5,9],["2017-01-24",1,5,7,17,23,3,8],["2017-01-20",10,17,27,31,49,3,5],["2017-01-17",4,16,25,43,47,2,10],["2017-01-13",3,7,16,26,50,4,7],["2017-01-06",10,14,18,21,49,9,11],["2017-01-03",19,23,27,34,49,1,11],["2016-12-27",3,22,25,32,39,7,12],["2016-12-23",4,16,20,39,46,2,5],["2016-12-20",13,15,17,21,27,2,3],["2016-12-16",6,10,30,41,45,4,11],["2016-12-13",14,28,29,48,50,2,8],["2016-12-09",6,12,28,37,40,1,5],["2016-12-06",1,7,11,27,45,4,9],["2016-12-02",6,11,23,31,32,2,12],["2016-11-25",5,11,17,23,28,10,11],["2016-11-22",5,10,33,34,47,2,10],["2016-11-18",3,12,27,32,47,6,8],["2016-11-15",7,15,36,42,48,3,7],["2016-11-11",3,22,23,38,49,2,9],["2016-11-08",5,9,47,49,50,8,9],["2016-11-04",7,11,12,44,48,3,7],["2016-11-01",6,10,20,29,37,1,9],["2016-10-28",10,16,19,23,43,2,8],["2016-10-25",3,15,20,21,28,1,5],["2016-10-21",9,20,23,44,48,8,10],["2016-10-18",2,17,21,25,27,6,9],["2016-10-14",7,13,19,31,34,1,11],["2016-10-11",5,12,20,24,37,4,11],["2016-10-07",2,12,20,27,30,9,11],["2016-10-04",4,7,14,34,38,4,11],["2016-09-30",17,35,37,43,44,2,10],["2016-09-27",6,9,13,39,41,2,12],["2016-09-23",14,16,39,42,49,3,9],["2016-09-20",1,4,10,41,45,6,9],["2016-09-16",5,8,14,22,32,2,11],["2016-09-13",4,7,16,35,44,1,3],["2016-09-09",4,11,15,17,28,1,3],["2016-09-06",12,24,30,34,45,7,10],["2016-09-02",8,16,32,40,50,2,11],["2016-08-30",13,16,29,35,42,6,8],["2016-08-26",1,9,28,30,48,7,8],["2016-08-23",2,23,31,37,45,4,8],["2016-08-19",5,6,22,25,34,5,7],["2016-08-16",2,26,34,37,46,4,8],["2016-08-12",2,15,28,36,50,2,11],["2016-08-09",12,19,43,44,45,5,10],["2016-08-05",16,19,24,35,37,2,7],["2016-08-02",2,8,27,38,46,2,6],["2016-07-29",1,21,26,40,50,2,4],["2016-07-26",3,10,13,45,49,2,11],["2016-07-22",7,24,26,37,45,5,9],["2016-07-19",19,24,28,36,37,3,6],["2016-07-15",2,11,13,14,21,1,8],["2016-07-12",3,7,26,33,41,2,10],["2016-07-08",8,12,18,33,44,4,8],["2016-07-05",1,10,29,38,48,3,4],["2016-07-01",2,11,13,40,50,1,10],["2016-06-28",26,32,37,43,49,4,5],["2016-06-24",11,19,27,28,39,3,10],["2016-06-21",5,17,32,35,49,1,5],["2016-06-17",1,7,18,27,43,4,8],["2016-06-14",13,34,39,42,50,9,11],["2016-06-10",14,21,35,42,43,7,9],["2016-06-07",19,26,35,45,49,2,5],["2016-06-03",7,23,31,33,39,6,10],["2016-05-31",6,12,26,30,48,6,7],["2016-05-27",13,25,27,43,46,4,8],["2016-05-24",16,17,23,34,37,6,9],["2016-05-20",9,14,30,41,45,4,9],["2016-05-17",15,27,32,36,39,3,10],["2016-05-13",7,15,28,31,42,10,11],["2016-05-10",2,26,27,40,49,5,10],["2016-05-06",32,34,40,45,48,1,10],["2016-05-03",8,23,24,34,38,3,7],["2016-04-29",4,5,25,28,43,6,11],["2016-04-26",10,17,31,32,42,2,5],["2016-04-22",17,26,32,34,43,2,10],["2016-04-19",11,14,15,27,44,2,7],["2016-04-15",13,14,32,37,48,1,7],["2016-04-12",1,5,9,22,38,2,10],["2016-04-08",6,8,26,43,49,6,10],["2016-04-05",3,5,9,19,40,2,5],["2016-04-01",2,16,23,25,49,6,9],["2016-03-29",1,4,19,25,36,3,11],["2016-03-25",12,19,36,42,43,5,8],["2016-03-22",12,15,26,42,49,5,8],["2016-03-18",14,19,21,24,49,5,11],["2016-03-15",5,7,10,34,44,2,10],["2016-03-11",1,21,26,40,43,6,9],["2016-03-08",1,8,9,14,23,1,7],["2016-03-04",6,9,14,23,40,1,5],["2016-03-01",4,7,13,28,37,10,11],["2016-02-26",5,13,15,33,50,9,11],["2016-02-23",23,25,32,37,42,1,11],["2016-02-19",13,14,30,32,39,3,9],["2016-02-16",3,10,22,37,50,6,10],["2016-02-12",3,20,28,31,49,2,5],["2016-02-09",6,9,13,28,37,4,5],["05-Feb-2016",3,27,32,41,46,4,8],["02-Feb-2016",36,21,10,6,9,6,2],["29-Jan-2016",29,32,23,1,5,1,7],["26-Jan-2016",15,40,24,48,38,2,9],["22-Jan-2016",27,10,30,47,12,9,8],["19-Jan-2016",2,30,38,43,46,7,2],["15-Jan-2016",43,38,19,10,46,1,11],["12-Jan-2016",2,10,30,44,1,1,8],["08-Jan-2016",35,33,26,40,5,3,8],["05-Jan-2016",6,10,31,36,39,6,10],["01-Jan-2016",4,37,38,39,44,4,7]] diff --git a/libs/loader.js b/libs/loader.js new file mode 100644 index 0000000..8311161 --- /dev/null +++ b/libs/loader.js @@ -0,0 +1,84 @@ +const jsonfile = require('jsonfile'); +const https = require('https'); +const Sugar = require('sugar-date'); + +const file = 'data/data.json'; + +function resultsQuery(callback) { + const options = { + 'host': 'euromillions.p.mashape.com', + 'path': '/ResultsService/FindLast', + + 'headers': { + 'accept': 'text/plain', + 'X-Mashape-Key': '5A0H980jK6mshSFL24ZmfiRrNHV2p1d1fhQjsngtx8QWuO9oe4' + + }, + 'method': 'GET' + }; + + + https.request(options).on('response', function (response) { + let data = ''; + response.on('data', function (chunk) { + data += chunk; + }); + response.on('end', function () { + //console.log(data); + callback(JSON.parse(data)); + }); + }).end(); +} + +function processDataV2(data) { + const newArray = []; + const date = /(\/Date\()([0-9]+)([\s\S]+)/.exec(data.Date); + + // console.log(data); + + //date = /(\/Date\()([0-9]+)([\s\S]+)/.exec(data.Date); + const newdate = Sugar.Date.format(new Date(parseInt(date[2])), '%Y-%m-%d'); + + newArray.push(newdate); + + newArray.push(data.Num1); + newArray.push(data.Num2); + newArray.push(data.Num3); + newArray.push(data.Num4); + newArray.push(data.Num5); + + newArray.push(data.Star1); + newArray.push(data.Star2); + + return newArray; +} + + +function load(cb) { + jsonfile.readFile(file, (err, obj) => { + if (err) console.error(err); + + const lotData = obj; + + resultsQuery( (a) => { + const latest = processDataV2(a); + + const dateExists = lotData.filter((item) => { + if (item[0] === latest[0]) return item; + }); + + if (dateExists.length === 0) + lotData.unshift(latest); + + // return the data + cb(lotData); + }); + }); +} + +function save(data) { + jsonfile.writeFileSync(file, data); +} + +module.exports.load = load; +module.exports.save = save; diff --git a/libs/lot.js b/libs/lot.js new file mode 100644 index 0000000..f3fc16e --- /dev/null +++ b/libs/lot.js @@ -0,0 +1,294 @@ +#!/usr/bin/env node + +const m = [], + prev = [], + mo = [], + star = [], + trip = [], + results = []; +const line = []; + +let re = []; +const resultsObj = {}; + + + + +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 buildArray() { + for (let x = 0; x < 51; x++) { + const l = []; + for (let y = 0; y < 51; y++) + l.push(0); + + m[x] = l; + } +} + + +function othercalc() { + let i = 0; + // var p = 1 / ((-1)*re.length); + const p = 1.0 / (re.length); + // console.log('P', p); + + for (i = 0; i < re.length - 1; i++) + // console.log((p * i)); + // console.log(re[i]); + for (let s = 1; s < 6; s++) { + const cv = re[i][s]; + // console.log("row: " + cv); + for (let n = 1; n < 6; n++) + if (n !== s) { + workVal = re[i][n]; + // console.log(workVal); + // m[cv-1][workVal-1]++; + let flag = false; + for (var t = 0; t < mo.length; t++) + if (mo[t].a === cv && mo[t].b === workVal) { + if (typeof mo[t].c === 'undefined' || mo[t].c === null) + mo[t].c = 0; + + mo[t].c++; + flag = true; + // mo[t].v++; + /* + if (i < (re.length/2)) + mo[t].v = mo[t].v - (p * i) + else + */ + + // original weighting + //mo[t].v = mo[t].v + (p * i); + + //reverse weighting.. + mo[t].v = mo[t].v + (1 - (p * i)); + // console.log(mo[t], 1 - (p * i)); + //; + //mo[t].v = mo[t].v + 1; + // ; + } + + if (!flag) + mo.push({ + 'a': cv, + 'b': workVal, + 'v': 1 + }); + } + } + + + console.log('******************************************'); + mo.sort(dynamicSort('v')); + + //for (var t = (mo.length - 20); t < mo.length; t++) { + var t = mo.length - 1; + while (line.length < 5) { + if (line.indexOf(mo[t].a) == -1 && line.length < 5) + line.push(mo[t].a); + + if (line.indexOf(mo[t].b) == -1 && line.length < 5) + line.push(mo[t].b); + + t = t - 1; + } + + // console.log(line); + + console.log(line.sort()); + + resultsObj.mainline = line.sort(); + resultsObj.otherLine = []; + resultsObj.otherLine.push(mo[mo.length - 1].a); + resultsObj.otherLine.push(mo[mo.length - 1].b); + + for (var t = mo.length - 1; t > (mo.length - 3); t--) + // if (mo[t].v >= 3) { + if (mo[t].v > 1) + console.log(`${mo[t].a }, ${ mo[t].b }, ${ mo[t].v}`); + + + // buildTable(); +} + +function tripCalc() { + const blist = [[0, 1, 2], [0, 1, 3], [0, 1, 4], [0, 2, 3], [0, 2, 4], [0, 3, 4], [1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]; + const p = 1.0 / (re.length); + let a, b, c, cur; + //var trip = []; + //console.log + for (i = 0; i < re.length - 1; i++) + + for (let t = 0; t < blist.length; t++) { + cur = blist[t]; + + a = re[i][cur[0] + 1]; + b = re[i][cur[1] + 1]; + c = re[i][cur[2] + 1]; + + + // console.log(a + ", " + b + "," + c); + + + let found = false; + for (y = 0; y < trip.length; y++) { + const nums = trip[y].nums; + + if (!found) + if ((nums.indexOf(a) !== -1) && (nums.indexOf(b) !== -1) && (nums.indexOf(c) !== -1)) { + /* console.log("nums:" + nums); + console.log("want: " + a + ", " + b + "," + c); + console.log("A:" + nums.indexOf(a)); + console.log("B:" + nums.indexOf(b)); + console.log("C:" + nums.indexOf(c)); +*/ + + trip[y].cnt = trip[y].cnt + (p * i); + // trip[y].cnt = trip[y].cnt + 1; + + // console.log(">>>" + trip[y].cnt); + found = true; + } + } + + if (!found) + + //trip.push({nums:[a,b,c],cnt:(p * i)}); + trip.push({ + 'nums': [a, b, c], + 'cnt': 1 + }); + } + + + let max = 0; + for (y = 0; y < trip.length; y++) + if (trip[y].cnt > max) max = trip[y].cnt; + + //console.log(`Max: ${ max}`); + for (y = 0; y < trip.length; y++) + if (trip[y].cnt === max) { + // console.log(`${trip[y].cnt }, ${ trip[y].nums}`); + + resultsObj.otherLine = resultsObj.otherLine.concat(trip[y].nums).sort(); + } + + + + // console.log(JSON.stringify(trip)); +} + +function starcalc() { + let i = 0; + const p = 1 / re.length; + for (i = 0; i < re.length - 1; i++) + // console.log(re[i]); + for (let s = 6; s < 8; s++) { + const cv = re[i][s]; + // console.log("row: " + cv); + for (let n = 6; n < 8; n++) + if (n !== s) { + workVal = re[i][n]; + // console.log(workVal); + // m[cv-1][workVal-1]++; + let flag = false; + for (var t = 0; t < mo.length; t++) + if (mo[t].a === cv && mo[t].b === workVal) { + flag = true; + mo[t].v++; + + mo[t].v = mo[t].v + (p * i); + } + + if (!flag) + mo.push({ + 'a': cv, + 'b': workVal, + 'v': 1 + }); + } + } + + + // console.log('******************************************'); + mo.sort(dynamicSort('v')); + + var t = mo.length - 1; + + const starOutput = []; + starOutput.push(mo[t].a); + starOutput.push(mo[t].b); + + + resultsObj.mainstars = []; + resultsObj.mainstars.push(starOutput.sort()[0]); + resultsObj.mainstars.push(starOutput.sort()[1]); + + resultsObj.otherStars = []; + resultsObj.otherStars.push(mo[mo.length - 3].a); + resultsObj.otherStars.push(mo[mo.length - 3].b); + + + + //console.log('Stars: ' + mo[t].a + ', ' + mo[t].b); + + /* console.log(`Stars: ${ starOutput.sort()[0] }, ${ starOutput.sort()[1]}`); + for (var t = mo.length - 6; t < mo.length; t++) + if (mo[t].v >= 3) + console.log(`${mo[t].a }, ${ mo[t].b }, ${ mo[t].v}`); +*/ + + + // buildTable(); +} + +function performCalcs() { + buildArray(); + //buildTable(); + othercalc(); + console.log('---------------------------------------'); + starcalc(); + + tripCalc(); +} + +function prepareResults() { + resultsObj.mainline = resultsObj.mainline.sort(sortNumber); + resultsObj.otherLine = resultsObj.otherLine.sort(sortNumber); + + resultsObj.mainstars = resultsObj.mainstars.sort(sortNumber); + resultsObj.otherStars = resultsObj.otherStars.sort(sortNumber); + + return resultsObj; +} + +function calculate(data) { + re = data; + performCalcs(); + + prepareResults(); + + console.log(resultsObj); + + return resultsObj; +} + +module.exports.calculate = calculate; diff --git a/libs/lotV2.js b/libs/lotV2.js new file mode 100644 index 0000000..880a8f5 --- /dev/null +++ b/libs/lotV2.js @@ -0,0 +1,114 @@ + +const m = []; + +const newNumberContainer = {}; + +let numberStore = []; + +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 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(); + const line = othercalcV2(); + console.log('---------------------------------------'); + console.log(`line: ${line}`); + // Starcalc(); + + // tripCalc(); + return line; +} + +function calculate(data) { + numberStore = data; + + return performCalcs(); +} + +module.exports.calculate = calculate; + diff --git a/libs/pusher.js b/libs/pusher.js new file mode 100644 index 0000000..4142705 --- /dev/null +++ b/libs/pusher.js @@ -0,0 +1,26 @@ +/** + * + * User: Martin Donnelly + * Date: 2016-04-08 + * Time: 16:35 + * + */ +var Pushover = require('node-pushover'), dateFormat = require('dateformat'); +var push = new Pushover({ + 'token': 'aqnos2j4v4pjpry3hvnxq9646eup23', + 'user': 'BE2vgFxdHJw91lVGMRYvZDDmVa5cCM' +}); + + +var logger = require('log4js').getLogger(); + +const prefix = process.env.NODE_ENV === 'production' ? 'Production' : 'Dev'; +const title = `mdLot ${ prefix } Alert`; +module.exports = { + + 'push' : function(contents) { + var now = new Date(); + var msg = `Updated at ${ dateFormat(now, 'dddd, mmmm dS, yyyy, HH:MM:ss') }\n\n${ contents }`; + push.send(title, msg); + } +}; diff --git a/lot.js b/lot.js index 2ba66f3..28eb4dc 100644 --- a/lot.js +++ b/lot.js @@ -1,44 +1,42 @@ #!/usr/bin/env node -var jsonfile = require('jsonfile'), - https = require('https'), - UltraSES = require('ultrases'),cron = require('node-cron'); +const jsonfile = require('jsonfile'), + https = require('https'), + UltraSES = require('ultrases'), cron = require('node-cron'); -var Sugar = require('sugar-date'); +const Sugar = require('sugar-date'); -var file = 'data/data.json'; -var m = [], - prev = [], - mo = [], - star = [], - trip = [], - results = [], - line = []; +const file = 'data/data.json'; +const m = [], + prev = [], + mo = [], + star = [], + trip = [], + results = []; +let line = []; -var re = []; -var resultsObj = {}; +let re = []; +let resultsObj = {}; - -var mailer = new UltraSES({ - aws: { - accessKeyId: 'AKIAJWJS75F7WNCGK64A', - secretAccessKey: '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', - "region": "eu-west-1" - }, - defaults: { - from: 'Martin Donnelly ' - } +const mailer = new UltraSES({ + 'aws': { + 'accessKeyId': 'AKIAJWJS75F7WNCGK64A', + 'secretAccessKey': '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', + 'region': 'eu-west-1' + }, + 'defaults': { + 'from': 'Martin Donnelly ' + } }); - function $(elm) { - return document.getElementById(elm); + return document.getElementById(elm); } function resultsQuery(callback) { - /* var options = { + /* var options = { host: 'nunofcguerreiro.com', path: '/api-euromillions-json', port: 443, @@ -51,424 +49,409 @@ function resultsQuery(callback) { method: 'GET' };*/ - var options = { - host: 'euromillions.p.mashape.com', - path: '/ResultsService/FindLast', - //port: 443, - headers: { - accept: "text/plain", - "X-Mashape-Key": "5A0H980jK6mshSFL24ZmfiRrNHV2p1d1fhQjsngtx8QWuO9oe4" - /* 'Content-Type': 'application/json', + const options = { + 'host': 'euromillions.p.mashape.com', + 'path': '/ResultsService/FindLast', + //port: 443, + 'headers': { + 'accept': 'text/plain', + 'X-Mashape-Key': '5A0H980jK6mshSFL24ZmfiRrNHV2p1d1fhQjsngtx8QWuO9oe4' + + /* 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(data)*/ - }, - method: 'GET' - }; + }, + 'method': 'GET' + }; - /* - unirest.get("https://euromillions.p.mashape.com/ResultsService/FindLast") - .header("X-Mashape-Key", "5A0H980jK6mshSFL24ZmfiRrNHV2p1d1fhQjsngtx8QWuO9oe4") - .header("Accept", "text/plain") - .end(function (result) { - console.log(result.status, result.headers, result.body); + /* + unirest.get("https://euromillions.p.mashape.com/ResultsService/FindLast") + .header("X-Mashape-Key", "5A0H980jK6mshSFL24ZmfiRrNHV2p1d1fhQjsngtx8QWuO9oe4") + .header("Accept", "text/plain") + .end(function (result) { + console.log(result.status, result.headers, result.body); + }); + */ + + https.request(options).on('response', function (response) { + let data = ''; + response.on('data', function (chunk) { + data += chunk; }); - */ - - https.request(options).on('response', function (response) { - var data = ''; - response.on("data", function (chunk) { - data += chunk; - }); - response.on('end', function () { - //console.log(data); - callback(JSON.parse(data)); - }); - }).end(); + response.on('end', function () { + //console.log(data); + callback(JSON.parse(data)); + }); + }).end(); } function sortNumber(a, b) { - return a - b; + return a - b; } function dynamicSort(property) { - var sortOrder = 1; - if (property[0] === "-") { - sortOrder = -1; - property = property.substr(1); - } - return function (a, b) { - var result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0; - return result * sortOrder; - } + 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 buildArray() { - for (var x = 0; x < 51; x++) { - var l = []; - for (var y = 0; y < 51; y++) { - l.push(0); - } - m[x] = l; - } + for (let x = 0; x < 51; x++) { + const l = []; + for (let y = 0; y < 51; y++) + l.push(0); + + m[x] = l; + } } function buildTable() { - var area = $('area'); - // clean area. - /* while (area.lastChild) { + const area = $('area'); + // clean area. + /* while (area.lastChild) { area.removeChild(area.lastChild); } */ - var table = document.createElement('table'); - var row = document.createElement('tr'); + const table = document.createElement('table'); + var row = document.createElement('tr'); + var cell = document.createElement('td'); + var p = document.createTextNode('---'); + cell.appendChild(p); + row.appendChild(cell); + for (var x = 1; x < 50; x++) { var cell = document.createElement('td'); - var p = document.createTextNode('---') + var p = document.createTextNode(x); cell.appendChild(p); row.appendChild(cell); - for (var x = 1; x < 50; x++) { - var cell = document.createElement('td'); - var p = document.createTextNode(x) - cell.appendChild(p); - row.appendChild(cell); - } - table.appendChild(row); - /** + } + table.appendChild(row); + + /** * */ - for (var y = 0; y < 51; y++) { - var row = document.createElement('tr'); - var cell = document.createElement('td'); - var p = document.createTextNode('[' + parseInt(y + 1) + ']') - cell.appendChild(p); - row.appendChild(cell); - for (var x = 1; x < 51; x++) { - var cell = document.createElement('td'); - var p = document.createTextNode(m[y][x - 1]) - cell.appendChild(p); - row.appendChild(cell); - } - table.appendChild(row); + for (let y = 0; y < 51; y++) { + var row = document.createElement('tr'); + var cell = document.createElement('td'); + var p = document.createTextNode(`[${ parseInt(y + 1) }]`); + cell.appendChild(p); + row.appendChild(cell); + for (var x = 1; x < 51; x++) { + var cell = document.createElement('td'); + var p = document.createTextNode(m[y][x - 1]); + cell.appendChild(p); + row.appendChild(cell); } - area.appendChild(table); + table.appendChild(row); + } + area.appendChild(table); } function calc() { - var i = 0; - for (i = 0; i < re.length - 1; i++) { - console.log(re[i]); - for (var s = 1; s < 6; s++) { - var cv = re[i][s]; - console.log("row: " + cv); - for (var n = 1; n < 6; n++) { - if (n != s) { - workVal = re[i][n]; - console.log(workVal); - m[cv - 1][workVal - 1]++; - } - } + let i = 0; + for (i = 0; i < re.length - 1; i++) { + console.log(re[i]); + for (let s = 1; s < 6; s++) { + const cv = re[i][s]; + console.log(`row: ${ cv}`); + for (let n = 1; n < 6; n++) + if (n != s) { + workVal = re[i][n]; + console.log(workVal); + m[cv - 1][workVal - 1]++; } } - buildTable(); + } + buildTable(); } function othercalc() { - var i = 0; - // var p = 1 / ((-1)*re.length); - var p = 1.0 / (re.length); - console.log('P', p); + let i = 0; + // var p = 1 / ((-1)*re.length); + const p = 1.0 / (re.length); + console.log('P', p); - for (i = 0; i < re.length - 1; i++) { - console.log((p * i)); - // console.log(re[i]); - for (var s = 1; s < 6; s++) { - var cv = re[i][s]; - // console.log("row: " + cv); - for (var n = 1; n < 6; n++) { - if (n != s) { - workVal = re[i][n]; - // console.log(workVal); - // m[cv-1][workVal-1]++; - var flag = false; - for (var t = 0; t < mo.length; t++) { - if (mo[t].a == cv && mo[t].b == workVal) { - if (typeof mo[t].c == 'undefined' || mo[t].c == null) - { - mo[t].c = 0; - } - mo[t].c++; - flag = true; - // mo[t].v++; - /* + for (i = 0; i < re.length - 1; i++) { + console.log((p * i)); + // console.log(re[i]); + for (let s = 1; s < 6; s++) { + const cv = re[i][s]; + // console.log("row: " + cv); + for (let n = 1; n < 6; n++) + if (n != s) { + workVal = re[i][n]; + // console.log(workVal); + // m[cv-1][workVal-1]++; + let flag = false; + for (var t = 0; t < mo.length; t++) + if (mo[t].a == cv && mo[t].b == workVal) { + if (typeof mo[t].c === 'undefined' || mo[t].c == null) + mo[t].c = 0; + + mo[t].c++; + flag = true; + // mo[t].v++; + /* if (i < (re.length/2)) mo[t].v = mo[t].v - (p * i) else */ - // original weighting - //mo[t].v = mo[t].v + (p * i); + // original weighting + //mo[t].v = mo[t].v + (p * i); - //reverse weighting.. - mo[t].v = mo[t].v + (1-(p * i)); - console.log(mo[t], 1-(p*i)); - //; - //mo[t].v = mo[t].v + 1; - // ; - - - } - } - if (!flag) { - mo.push({ - a: cv, - b: workVal, - v: 1 - }); - } - } + //reverse weighting.. + mo[t].v = mo[t].v + (1 - (p * i)); + console.log(mo[t], 1 - (p * i)); + //; + //mo[t].v = mo[t].v + 1; + // ; } + + if (!flag) + mo.push({ + 'a': cv, + 'b': workVal, + 'v': 1 + }); } } + } - console.log("******************************************"); - mo.sort(dynamicSort("v")); + console.log('******************************************'); + mo.sort(dynamicSort('v')); - //for (var t = (mo.length - 20); t < mo.length; t++) { - var t = mo.length - 1; - while (line.length < 5) { - if (line.indexOf(mo[t].a) == -1 && line.length < 5) { - line.push(mo[t].a); - } - if (line.indexOf(mo[t].b) == -1 && line.length < 5) { - line.push(mo[t].b); - } - t = t - 1; - } + //for (var t = (mo.length - 20); t < mo.length; t++) { + var t = mo.length - 1; + while (line.length < 5) { + if (line.indexOf(mo[t].a) == -1 && line.length < 5) + line.push(mo[t].a); - // console.log(line); + if (line.indexOf(mo[t].b) == -1 && line.length < 5) + line.push(mo[t].b); - console.log(line.sort()); + t = t - 1; + } - resultsObj.mainline = line.sort(); - resultsObj.otherLine = []; - resultsObj.otherLine.push(mo[mo.length - 1].a); - resultsObj.otherLine.push(mo[mo.length - 1].b); + // console.log(line); - for (var t = mo.length - 1; t > (mo.length - 3); t--) { - // if (mo[t].v >= 3) { - if (mo[t].v > 1) { - console.log(mo[t].a + ', ' + mo[t].b + ", " + mo[t].v) - } - } - // buildTable(); + console.log(line.sort()); + + resultsObj.mainline = line.sort(); + resultsObj.otherLine = []; + resultsObj.otherLine.push(mo[mo.length - 1].a); + resultsObj.otherLine.push(mo[mo.length - 1].b); + + for (var t = mo.length - 1; t > (mo.length - 3); t--) + // if (mo[t].v >= 3) { + if (mo[t].v > 1) + console.log(`${mo[t].a }, ${ mo[t].b }, ${ mo[t].v}`); + + + // buildTable(); } function tripCalc() { - var blist = [[0, 1, 2], [0, 1, 3], [0, 1, 4], [0, 2, 3], [0, 2, 4], [0, 3, 4], [1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]; - var p = 1.0 / (re.length); - var a, b, c, cur; - //var trip = []; - //console.log - for (i = 0; i < re.length - 1; i++) { + const blist = [[0, 1, 2], [0, 1, 3], [0, 1, 4], [0, 2, 3], [0, 2, 4], [0, 3, 4], [1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]; + const p = 1.0 / (re.length); + let a, b, c, cur; + //var trip = []; + //console.log + for (i = 0; i < re.length - 1; i++) - for (var t = 0; t < blist.length; t++) { - cur = blist[t]; + for (let t = 0; t < blist.length; t++) { + cur = blist[t]; - a = re[i][cur[0] + 1]; - b = re[i][cur[1] + 1]; - c = re[i][cur[2] + 1]; + a = re[i][cur[0] + 1]; + b = re[i][cur[1] + 1]; + c = re[i][cur[2] + 1]; - // console.log(a + ", " + b + "," + c); + // console.log(a + ", " + b + "," + c); - var found = false; - for (y = 0; y < trip.length; y++) { - var nums = trip[y].nums; + let found = false; + for (y = 0; y < trip.length; y++) { + const nums = trip[y].nums; - if (!found) { - if ((nums.indexOf(a) != -1) && (nums.indexOf(b) != -1) && (nums.indexOf(c) != -1)) { - /* console.log("nums:" + nums); + if (!found) + if ((nums.indexOf(a) != -1) && (nums.indexOf(b) != -1) && (nums.indexOf(c) != -1)) { + /* console.log("nums:" + nums); console.log("want: " + a + ", " + b + "," + c); console.log("A:" + nums.indexOf(a)); console.log("B:" + nums.indexOf(b)); console.log("C:" + nums.indexOf(c)); */ - trip[y].cnt = trip[y].cnt + (p * i); - // trip[y].cnt = trip[y].cnt + 1; + trip[y].cnt = trip[y].cnt + (p * i); + // trip[y].cnt = trip[y].cnt + 1; - // console.log(">>>" + trip[y].cnt); - found = true; - } - } + // console.log(">>>" + trip[y].cnt); + found = true; + } + } - } + if (!found) - if (!found) { - - //trip.push({nums:[a,b,c],cnt:(p * i)}); - trip.push({ - nums: [a, b, c], - cnt: 1 - }); - - } - } - - - - } - - var max = 0; - for (y = 0; y < trip.length; y++) { - if (trip[y].cnt > max) max = trip[y].cnt; - } - console.log("Max: " + max); - for (y = 0; y < trip.length; y++) { - if (trip[y].cnt == max) { - console.log(trip[y].cnt + ", " + trip[y].nums); - - resultsObj.otherLine = resultsObj.otherLine.concat(trip[y].nums).sort(); - - } + //trip.push({nums:[a,b,c],cnt:(p * i)}); + trip.push({ + 'nums': [a, b, c], + 'cnt': 1 + }); } - // console.log(JSON.stringify(trip)); + let max = 0; + for (y = 0; y < trip.length; y++) + if (trip[y].cnt > max) max = trip[y].cnt; + + console.log(`Max: ${ max}`); + for (y = 0; y < trip.length; y++) + if (trip[y].cnt == max) { + console.log(`${trip[y].cnt }, ${ trip[y].nums}`); + + resultsObj.otherLine = resultsObj.otherLine.concat(trip[y].nums).sort(); + } + + + + // console.log(JSON.stringify(trip)); } function starcalc() { - var i = 0; - var p = 1 / re.length; - for (i = 0; i < re.length - 1; i++) { - // console.log(re[i]); - for (var s = 6; s < 8; s++) { - var cv = re[i][s]; - // console.log("row: " + cv); - for (var n = 6; n < 8; n++) { - if (n != s) { - workVal = re[i][n]; - // console.log(workVal); - // m[cv-1][workVal-1]++; - var flag = false; - for (var t = 0; t < mo.length; t++) { - if (mo[t].a == cv && mo[t].b == workVal) { - flag = true; - mo[t].v++; + let i = 0; + const p = 1 / re.length; + for (i = 0; i < re.length - 1; i++) + // console.log(re[i]); + for (let s = 6; s < 8; s++) { + const cv = re[i][s]; + // console.log("row: " + cv); + for (let n = 6; n < 8; n++) + if (n != s) { + workVal = re[i][n]; + // console.log(workVal); + // m[cv-1][workVal-1]++; + let flag = false; + for (var t = 0; t < mo.length; t++) + if (mo[t].a == cv && mo[t].b == workVal) { + flag = true; + mo[t].v++; - mo[t].v = mo[t].v + (p * i); - } - } - if (!flag) { - mo.push({ - a: cv, - b: workVal, - v: 1 - }); - } - } + mo[t].v = mo[t].v + (p * i); } + + if (!flag) + mo.push({ + 'a': cv, + 'b': workVal, + 'v': 1 + }); } } - console.log("******************************************"); - mo.sort(dynamicSort("v")); - var t = mo.length - 1; + console.log('******************************************'); + mo.sort(dynamicSort('v')); - var starOutput = []; - starOutput.push(mo[t].a); - starOutput.push(mo[t].b); + var t = mo.length - 1; + + const starOutput = []; + starOutput.push(mo[t].a); + starOutput.push(mo[t].b); - resultsObj.mainstars = []; - resultsObj.mainstars.push(starOutput.sort()[0]); - resultsObj.mainstars.push(starOutput.sort()[1]); + resultsObj.mainstars = []; + resultsObj.mainstars.push(starOutput.sort()[0]); + resultsObj.mainstars.push(starOutput.sort()[1]); - resultsObj.otherStars = []; - resultsObj.otherStars.push(mo[mo.length - 3].a); - resultsObj.otherStars.push(mo[mo.length - 3].b); + resultsObj.otherStars = []; + resultsObj.otherStars.push(mo[mo.length - 3].a); + resultsObj.otherStars.push(mo[mo.length - 3].b); - //console.log('Stars: ' + mo[t].a + ', ' + mo[t].b); + //console.log('Stars: ' + mo[t].a + ', ' + mo[t].b); - console.log('Stars: ' + starOutput.sort()[0] + ', ' + starOutput.sort()[1]); - for (var t = mo.length - 6; t < mo.length; t++) { - if (mo[t].v >= 3) { - console.log(mo[t].a + ', ' + mo[t].b + ", " + mo[t].v) - } - } + console.log(`Stars: ${ starOutput.sort()[0] }, ${ starOutput.sort()[1]}`); + for (var t = mo.length - 6; t < mo.length; t++) + if (mo[t].v >= 3) + console.log(`${mo[t].a }, ${ mo[t].b }, ${ mo[t].v}`); - // buildTable(); + + + // buildTable(); } function performCalcs() { - buildArray(); - //buildTable(); - othercalc(); - console.log("---------------------------------------"); - starcalc(); + buildArray(); + //buildTable(); + othercalc(); + console.log('---------------------------------------'); + starcalc(); - tripCalc(); + tripCalc(); } function prepareResults() { - resultsObj.mainline = resultsObj.mainline.sort(sortNumber); - resultsObj.otherLine = resultsObj.otherLine.sort(sortNumber); + resultsObj.mainline = resultsObj.mainline.sort(sortNumber); + resultsObj.otherLine = resultsObj.otherLine.sort(sortNumber); - resultsObj.mainstars = resultsObj.mainstars.sort(sortNumber); - resultsObj.otherStars = resultsObj.otherStars.sort(sortNumber); + resultsObj.mainstars = resultsObj.mainstars.sort(sortNumber); + resultsObj.otherStars = resultsObj.otherStars.sort(sortNumber); } function saveData() { - jsonfile.writeFileSync(file, re); + jsonfile.writeFileSync(file, re); } function sendEmail() { - var email = { - to: 'martind2000@gmail.com', - subject: 'Todays numbers' - }; + const email = { + 'to': 'martind2000@gmail.com', + 'subject': 'Todays numbers' + }; - var template = { file: './jade/output.jade', locals: resultsObj }; - mailer.sendTemplate(email, template, function(err){ + const template = {'file': './jade/output.jade', 'locals': resultsObj}; + mailer.sendTemplate(email, template, function(err) { if(err) throw err; - console.log('compiled template email sent'); - }); - + console.log('compiled template email sent'); + }); } function processData(data) { newArray.push(data.drawns[0].date); - numbers = data.drawns[0].numbers.split(' '); - for (var i = 0; i < numbers.length; ++i) { - numbers[i] = parseInt(numbers[i]); - } - - stars = data.drawns[0].stars.split(' '); - for (var i = 0; i < stars.length; ++i) { - stars[i] = parseInt(stars[i]); - } - newArray = newArray.concat(numbers); - newArray = newArray.concat(stars); + numbers = data.drawns[0].numbers.split(' '); + for (var i = 0; i < numbers.length; ++i) + numbers[i] = parseInt(numbers[i]); - re.unshift(newArray); + stars = data.drawns[0].stars.split(' '); + for (var i = 0; i < stars.length; ++i) + stars[i] = parseInt(stars[i]); + + newArray = newArray.concat(numbers); + newArray = newArray.concat(stars); + re.unshift(newArray); } function processDataV2(data) { - var newArray = []; - var numbers, stars, date ; + const newArray = []; + let numbers, stars, date; console.log(data); date = /(\/Date\()([0-9]+)([\s\S]+)/.exec(data.Date); - var newdate = Sugar.Date.format(new Date(parseInt(date[2])), '%Y-%m-%d'); + const newdate = Sugar.Date.format(new Date(parseInt(date[2])), '%Y-%m-%d'); newArray.push(newdate); @@ -483,58 +466,135 @@ function processDataV2(data) { re.unshift(newArray); console.log(newArray); - } function processDataV2B(data) { - var newArray = []; - var numbers=[], stars=[], date ; + let newArray = []; + const numbers = [], stars = []; + let date; console.log(data); -date = /(\/Date\()([0-9]+)([\s\S]+)/.exec(data.Date); + date = /(\/Date\()([0-9]+)([\s\S]+)/.exec(data.Date); date = date[2]; - date = parseInt(date); + date = parseInt(date); - date = new Date(date); + date = new Date(date); console.log(dateFormat(date)); - date = dateFormat(date,"yyyy-dd-mm" ); + date = dateFormat(date, 'yyyy-dd-mm' ); - numbers.push(parseInt(data.Num1)); - numbers.push(parseInt(data.Num2)); - numbers.push(parseInt(data.Num3)); - numbers.push(parseInt(data.Num4)); - numbers.push(parseInt(data.Num5)); + numbers.push(parseInt(data.Num1)); + numbers.push(parseInt(data.Num2)); + numbers.push(parseInt(data.Num3)); + numbers.push(parseInt(data.Num4)); + numbers.push(parseInt(data.Num5)); - stars.push(parseInt(data.Star1)); - stars.push(parseInt(data.Star2)); + stars.push(parseInt(data.Star1)); + stars.push(parseInt(data.Star2)); - newArray.push(date); - newArray = newArray.concat(numbers); - newArray = newArray.concat(stars); + newArray.push(date); + newArray = newArray.concat(numbers); + newArray = newArray.concat(stars); - re.unshift(newArray); + re.unshift(newArray); - console.log(newArray); + console.log(newArray); + + return newArray; } +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 GO() { - re = []; - resultsObj = {}; + re = []; + resultsObj = {}; - jsonfile.readFile(file, function (err, obj) { - console.log(err); - re = obj; + jsonfile.readFile(file, function (err, obj) { + console.log(err); + re = obj; - /* performCalcs(); + /* performCalcs(); prepareResults(); @@ -542,22 +602,21 @@ function GO() { */ - resultsQuery(function (a) { - //processData(a); - processDataV2(a); + resultsQuery(function (a) { + //processData(a); + processDataV2(a); - performCalcs(); + performCalcs(); - prepareResults(); + prepareResults(); - console.log(resultsObj); + console.log(resultsObj); - sendEmail(); - - saveData(); - }); + // sendEmail(); + saveData(); }); + }); } @@ -567,8 +626,8 @@ function GO() { //sendEmail(); -cron.schedule('10 13 * * 2,5', function(){ +cron.schedule('10 13 * * 2,5', function() { GO(); - // console.log('tick'); - return -1; + // console.log('tick'); + return -1; }); diff --git a/lotV2.js b/lotV2.js index 4d53fbe..8bf51a0 100644 --- a/lotV2.js +++ b/lotV2.js @@ -4,16 +4,12 @@ const jsonfile = require('jsonfile'), const Sugar = require('sugar-date'); -const file = 'data/data.json'; -const m = [], - prev = [], - mo = [], - star = [], - trip = [], - results = [], - line = []; +const pusher = require('./pusher'); -let newNumberContainer = {}; +const file = 'data/data.json'; +const m = []; + +const newNumberContainer = {}; let numberStore = []; let resultsObj = {}; @@ -21,9 +17,9 @@ let resultsObj = {}; function buildArray() { for (let x = 0; x < 51; x++) { const l = []; - for (let y = 0; y < 51; y++) { + for (let y = 0; y < 51; y++) l.push(0); - } + m[x] = l; } } @@ -38,14 +34,16 @@ function dynamicSort(property) { 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; - } + const result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0; + + return result * sortOrder; + }; } function othercalcV2() { - // Var p = 1 / ((-1)*numberStore.length); + // Var p = 1 / ((-1)*numberStore.length); const p = 1.0 / (numberStore.length); for (let i = 0; i < numberStore.length - 1; i++) { @@ -54,25 +52,24 @@ function othercalcV2() { for (let s = 1; s < 6; s++) { const cv = numberStore[i][s]; - for (let n = 1; n < 6; n++) { + for (let n = 1; n < 6; n++) if (n !== s) { - let workVal = numberStore[i][n]; + 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; - }); + 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}; + const record = newNumberContainer[id] || { 'c': 0, 'v': 0, 'id': id }; record.c++; record.v += (p * i); newNumberContainer[id] = record; @@ -80,52 +77,55 @@ function othercalcV2() { 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; - }); + if (a.c < b.c) + return 1; + + return a.c > b.c ? -1 : 0; + }); let line = []; let lineI = 0; while (line.length < 5) { - let pair = flatArray[lineI]; + const pair = flatArray[lineI]; pairS = pair.id.split('-'); - if (line.indexOf(pairS[0]) === -1 && line.length < 5) { + if (line.indexOf(pairS[0]) === -1 && line.length < 5) line.push(pairS[0]); - } - if (line.indexOf(pairS[1]) === -1 && line.length < 5) { + + if (line.indexOf(pairS[1]) === -1 && line.length < 5) line.push(pairS[1]); - } - // console.log(line); + + // console.log(line); lineI++; } line = line.sort((a, b) => { - if (~~a < ~~b) { - return -1; - } - return ~~a > ~~b ? 1 : 0; - }); + if (~~a < ~~b) + return -1; - console.log(line); + return ~~a > ~~b ? 1 : 0; + }); + + //console.log(line); + + return line; } function performCalcs() { buildArray(); //BuildTable(); - othercalcV2(); + let line = othercalcV2(); console.log('---------------------------------------'); + console.log(`line: ${line}`); + pusher.push(`line: ${line}`); // Starcalc(); // tripCalc(); @@ -137,20 +137,16 @@ function GO() { resultsObj = {}; jsonfile.readFile(file, function(err, obj) { - console.log(err); - numberStore = obj; + console.log(err); + numberStore = obj; - // Console.log(numberStore); - performCalcs(); + // Console.log(numberStore); + performCalcs(); - //PrepareResults(); + //PrepareResults(); - // console.log(resultsObj); - - - - - }); + // console.log(resultsObj); + }); } GO(); diff --git a/package.json b/package.json index 7532d42..cf3f63a 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,10 @@ "lot": "./lot.js" }, "dependencies": { + "dateformat": "^2.0.0", "jsonfile": "^2.4.0", + "log4js": "^2.3.3", + "node-pushover": "^0.2.2", "sugar": "^2.0.1", "sugar-date": "^2.0.0" } diff --git a/pusher.js b/pusher.js new file mode 100644 index 0000000..4142705 --- /dev/null +++ b/pusher.js @@ -0,0 +1,26 @@ +/** + * + * User: Martin Donnelly + * Date: 2016-04-08 + * Time: 16:35 + * + */ +var Pushover = require('node-pushover'), dateFormat = require('dateformat'); +var push = new Pushover({ + 'token': 'aqnos2j4v4pjpry3hvnxq9646eup23', + 'user': 'BE2vgFxdHJw91lVGMRYvZDDmVa5cCM' +}); + + +var logger = require('log4js').getLogger(); + +const prefix = process.env.NODE_ENV === 'production' ? 'Production' : 'Dev'; +const title = `mdLot ${ prefix } Alert`; +module.exports = { + + 'push' : function(contents) { + var now = new Date(); + var msg = `Updated at ${ dateFormat(now, 'dddd, mmmm dS, yyyy, HH:MM:ss') }\n\n${ contents }`; + push.send(title, msg); + } +};