scitor 2 years ago
commit
ce3edae973
Signed by: scitor (C4CB001A60CA5028)
3 changed files with 123 additions and 0 deletions
  1. 6
    0
      README.md
  2. 103
    0
      index.js
  3. 14
    0
      package.json

+ 6
- 0
README.md View File

@@ -0,0 +1,6 @@
1
+# Market history
2
+
3
+this is a simple mod that calculates the price history once a day, for the past day.
4
+
5
+it only has a $gt check, so it counts the first 5 minutes of the new day
6
+

+ 103
- 0
index.js View File

@@ -0,0 +1,103 @@
1
+module.exports = function (config) {
2
+  const common = config.common,
3
+    C = config.common.constants,
4
+    storage = common.storage,
5
+    _ = require('lodash'),
6
+    engine = config.engine,
7
+    db = storage.db,
8
+    q = require('q');
9
+
10
+  if (config.cronjobs) {
11
+    var getGametime = function () {
12
+      return storage.env.get(storage.env.keys.GAMETIME).then(data => parseInt(data));
13
+    };
14
+
15
+    var qSequence = function (collection, fn) {
16
+      return _.reduce(collection, (promise, element, key) => promise.then(() => fn(element, key)), q.when());
17
+    };
18
+
19
+    /* https://gist.github.com/derickbailey/b7c6615ecf1fe9ad60a8 */
20
+    var standardDeviation = function (values) {
21
+      var avg = average(values);
22
+
23
+      var squareDiffs = values.map(function (value) {
24
+        var diff = value - avg;
25
+        return diff * diff;
26
+      });
27
+      var avgSquareDiff = average(squareDiffs);
28
+
29
+      return Math.sqrt(avgSquareDiff);
30
+    };
31
+
32
+    var average = function (data) {
33
+      var sum = data.reduce(function (sum, value) {
34
+        return sum + value;
35
+      }, 0);
36
+
37
+      return sum / data.length;
38
+    };
39
+
40
+    var formatDate = function (d) {
41
+      var month = '' + (d.getMonth() + 1),
42
+          day = '' + d.getDate(),
43
+          year = d.getFullYear();
44
+
45
+      if (month.length < 2) month = '0' + month;
46
+      if (day.length < 2) day = '0' + day;
47
+
48
+      return [year, month, day].join('-');
49
+    };
50
+
51
+    config.cronjobs.marketHistory = [5 * 60, function () {
52
+      var gameTime;
53
+      var resources = ['energy', 'H', 'O', 'Z', 'U', 'K', 'L', 'X', 'power'];
54
+      var tmpdate = new Date();
55
+      tmpdate.setDate(tmpdate.getDate() - 1);
56
+      var yesterday = formatDate(tmpdate);
57
+
58
+      return getGametime()
59
+        .then(data => gameTime = data)
60
+        .then(() => qSequence(resources, resource => {
61
+          return db['market.stats'].find({date: yesterday, resourceType: resource})
62
+            .then(stats => {
63
+              if (stats.length > 0)
64
+                return;
65
+
66
+              let historyEntry = {
67
+                date: yesterday,
68
+                resourceType: resource,
69
+                avgPrice: 0.0,
70
+                stddevPrice: 0.0,
71
+                transactions: 0,
72
+                volume: 0
73
+              };
74
+
75
+              return db.transactions.findEx({$and: [{resourceType: resource}, {time: {$gt: gameTime - 43200}}]}, {sort: {time: -1}})
76
+                .then(history => {
77
+                  if (history.length > 0) {
78
+                    let orders = history.filter(h => (
79
+                        h.order != undefined
80
+                    ));
81
+                    if (orders.length < 1)
82
+                      return;
83
+
84
+                    historyEntry.transactions = orders.length;
85
+
86
+                    let prices = [];
87
+                    orders.forEach(o=> {
88
+                      historyEntry.volume += o.amount;
89
+                      prices.push(o.order.price);
90
+                    });
91
+
92
+                    if (prices.length > 0) {
93
+                      historyEntry.avgPrice = average(prices);
94
+                      historyEntry.stddevPrice = standardDeviation(prices);
95
+                    }
96
+                  }
97
+                })
98
+                .then(() => db['market.stats'].insert(historyEntry));
99
+            })
100
+        }))
101
+    }];
102
+  }
103
+};

+ 14
- 0
package.json View File

@@ -0,0 +1,14 @@
1
+{
2
+  "name": "market-history",
3
+  "version": "1.0.0",
4
+  "description": "adds price history once a day, for the past day",
5
+  "main": "index.js",
6
+  "scripts": {
7
+    "test": "echo \"Error: no test specified\" && exit 1"
8
+  },
9
+  "author": "scitor <scitor@el337.de>",
10
+  "license": "ISC",
11
+  "dependencies": {
12
+    "lodash": "^4.17.5"
13
+  }
14
+}