scitor 2 years ago
commit
e37dab6b42
3 changed files with 153 additions and 0 deletions
  1. 12
    0
      README.md
  2. 127
    0
      index.js
  3. 14
    0
      package.json

+ 12
- 0
README.md View File

@@ -0,0 +1,12 @@
1
+# Better NPC orders
2
+
3
+this mod is used to replace the default npc order cronjob
4
+on the screeps.space server
5
+
6
+## terminals
7
+
8
+each terminal:
9
+- buys always buys power
10
+- buys or sells (50:50) energy/hydrogen/oxygen
11
+- buys one of the other base minerals with 50% chance
12
+- sells one of the other base minerals

+ 127
- 0
index.js View File

@@ -0,0 +1,127 @@
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
+    var getGametime = function () {
11
+        return storage.env.get(storage.env.keys.GAMETIME).then(data => parseInt(data));
12
+    };
13
+
14
+    var qSequence = function (collection, fn) {
15
+        return _.reduce(collection, (promise, element, key) => promise.then(() => fn(element, key)), q.when());
16
+    };
17
+
18
+    var handler;
19
+    if (config.cronjobs) {
20
+        config.cronjobs.recreateNpcOrders = [5 * 60, function () {
21
+            var gameTime;
22
+
23
+            var sellMinerals = ['Z', 'K', 'U', 'L', 'X'];
24
+            var mineralPrice = {
25
+                energy: 1000,
26
+                H: 3000,
27
+                O: 3000,
28
+                Z: 6000,
29
+                K: 6000,
30
+                U: 6000,
31
+                L: 6000,
32
+                X: 18000,
33
+                power: 120000
34
+            };
35
+
36
+            return getGametime()
37
+                .then(data => gameTime = data)
38
+                .then(() => db['rooms.objects'].find({$and: [{type: 'terminal'}, {user: {$eq: null}}]}))
39
+                .then(terminals => qSequence(terminals, terminal => {
40
+                    return db.rooms.findOne({_id: terminal.room})
41
+                        .then(room => {
42
+                            if (room.status != 'normal') {
43
+                                return;
44
+                            }
45
+                            if (room.nextNpcMarketOrder && room.nextNpcMarketOrder > gameTime) {
46
+                                return;
47
+                            }
48
+
49
+                            var orders = [{
50
+                                created: gameTime,
51
+                                active: true,
52
+                                type: 'buy',
53
+                                amount: 100000,
54
+                                remainingAmount: 100000,
55
+                                totalAmount: 100000,
56
+                                resourceType: 'power',
57
+                                price: mineralPrice['power']/6,
58
+                                roomName: terminal.room
59
+                            }];
60
+
61
+                            ['O','H','energy'].forEach(function(mineral) {
62
+                                let sell = Math.random() > 0.5;
63
+                                orders.push({
64
+                                    created: gameTime,
65
+                                    active: true,
66
+                                    type: sell ? 'sell' : 'buy',
67
+                                    amount: 400000,
68
+                                    remainingAmount: 400000,
69
+                                    totalAmount: 400000,
70
+                                    resourceType: mineral,
71
+                                    price: sell ? mineralPrice[mineral] : mineralPrice[mineral]/6,
72
+                                    roomName: terminal.room
73
+                                });
74
+                            });
75
+
76
+                            if (Math.random() > 0.5)
77
+                            sellMinerals.forEach(function(mineral) {
78
+                                orders.push({
79
+                                    created: gameTime,
80
+                                    active: true,
81
+                                    type: 'buy',
82
+                                    amount: 100000,
83
+                                    remainingAmount: 100000,
84
+                                    totalAmount: 100000,
85
+                                    resourceType: mineral,
86
+                                    price: mineralPrice[mineral]/6,
87
+                                    roomName: terminal.room
88
+                                });
89
+                            });
90
+
91
+                            return db.transactions.findEx({$and: [{from:terminal.room}, {time: {$gt: gameTime-5000}}]}, {sort:{time:-1}})
92
+                            .then(history => {
93
+                                    var sellMineral, amount = 100000;
94
+                                    if (history.length > 0) {
95
+                                        let minerals = history.filter(h=> (h.resourceType != 'O' &&
96
+                                                                           h.resourceType != 'H' &&
97
+                                                                           h.resourceType != 'energy'
98
+                                        ));
99
+                                        if (minerals.length > 0) {
100
+                                            sellMineral = minerals[0].resourceType;
101
+                                            minerals.forEach(m=>{ if (m.resourceType==sellMineral) amount += m.amount });
102
+                                        }
103
+                                    } else
104
+                                        sellMineral = sellMinerals[Math.floor(Math.random() * sellMinerals.length)];
105
+
106
+                                    orders.push({
107
+                                        created: gameTime,
108
+                                        active: true,
109
+                                        type: 'sell',
110
+                                        amount: amount,
111
+                                        remainingAmount: amount,
112
+                                        totalAmount: amount,
113
+                                        resourceType: sellMineral,
114
+                                        price: mineralPrice[sellMineral],
115
+                                        roomName: terminal.room
116
+                                    });
117
+                            })
118
+                            .then(() => db['market.orders'].removeWhere({roomName: room._id}))
119
+                            .then(() => db['market.orders'].insert(orders))
120
+                            .then(() => db.rooms.update({_id: room._id}, {$set: {nextNpcMarketOrder: gameTime + Math.round(5000 * (0.8 + 0.4 * Math.random()))}}));
121
+                        })
122
+                }))
123
+        }];
124
+    }
125
+
126
+};
127
+

+ 14
- 0
package.json View File

@@ -0,0 +1,14 @@
1
+{
2
+  "name": "better-npc-orders",
3
+  "version": "1.0.0",
4
+  "description": "",
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
+}