-
Notifications
You must be signed in to change notification settings - Fork 1
/
DrinkInterface.js
244 lines (225 loc) · 7.63 KB
/
DrinkInterface.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
var priceHistoryInterface = require('./database/PriceHistoryInterface');
var drinkDatabaseInterface = require('./database/DrinkInterface');
var consumptionInterface = require('./database/ConsumptionInterface');
var guestInterface = require('./database/GuestInterface');
var balanceInterface = require('./database/BalanceInterface');
var priceCalculator = require('./PriceCalculator');
var broadcasts = require('./connection/broadcasts.js');
var currentPrices = {};
var m = module.exports = {};
var priceEntryCache = {};
priceCalculator.registerPriceObserver(m);
m.getPriceEntry = function (error, callBack) {
priceHistoryInterface.getLatestEntry(error, function cb(entry) {
drinkDatabaseInterface.getAllDrinks(error, function cb(drinks) {
if (!entry) {
entry = {
'drinks': {}
};
}
for (var i in drinks) {
if (entry.drinks && !entry.drinks[i]) {
entry.drinks[i] = {
'id': i,
'price': (drinks[i].priceMax + drinks[i].priceMin) / 2
};
}
}
callBack(entry);
});
})
};
//Method from Observer Interface
m.onPriceUpdate = function(drinksWithPrices){
currentPrices = drinksWithPrices;
};
m.getLatestXPriceHistoryEntries = function (options ,error, callBack) {
priceHistoryInterface.getLatestXPriceHistoryEntries(options.x,error, function cb(history) {
drinkDatabaseInterface.getAllDrinks(error, function cb(drinks) {
if (!history) {
history = {
'drinks': {}
};
}
for (var i in drinks) {
if (history.drinks && !history.drinks[i]) {
history.drinks[i] = {
'id': i,
'price': (drinks[i].priceMax + drinks[i].priceMin) / 2
};
}
}
callBack(history);
});
})
};
m.getPriceHistory = function (options ,error, callBack) {
priceHistoryInterface.getPriceHistory(error, function cb(history) {
drinkDatabaseInterface.getAllDrinks(error, function cb(drinks) {
var numberOfDrinks = 0;
for (var i in drinks) {
numberOfDrinks++;
}
var dataPoints = reduceDataPointsForAllDrinks(history, numberOfDrinks);
callBack(dataPoints);
});
})
};
reduceDataPointsForAllDrinks = function(history, numberOfDrinks){
var drinks = {};
var reducedDataPoints = {};
for(var i in history){
for(var drink in history[i].drinks){
if(!drinks[drink]){
drinks[drink] = [];
}
drinks[drink].push([history[i].time, history[i].drinks[drink].price]);
}
}
for(var drink in drinks){
reducedDataPoints[drink]=douglasPeucker(drinks[drink], 0.01);
}
return reducedDataPoints;
};
function douglasPeucker(points, epsilon) {
var i,
maxIndex = 0,
maxDistance = 0,
perpendicularDistance,
leftRecursiveResults, rightRecursiveResults,
filteredPoints;
// find the point with the maximum distance
for (i = 1; i < points.length - 1; i++) {
perpendicularDistance = findPerpendicularDistance(points[i], [points[1], points[points.length - 1]]);
if (perpendicularDistance > maxDistance) {
maxIndex = i;
maxDistance = perpendicularDistance;
}
}
// if max distance is greater than epsilon, recursively simplify
if (maxDistance >= epsilon) {
leftRecursiveResults = douglasPeucker(points.slice(1, maxIndex), epsilon);
rightRecursiveResults = douglasPeucker(points.slice(maxIndex), epsilon);
filteredPoints = leftRecursiveResults.concat(rightRecursiveResults);
} else {
filteredPoints = points;
}
return filteredPoints;
};
function findPerpendicularDistance(point, line) {
var pointX = point[0],
pointY = point[1],
lineStart = {
x: line[0][0],
y: line[0][1]
},
lineEnd = {
x: line[1][0],
y: line[1][1]
},
slope = (lineEnd.y - lineStart.y) / (lineEnd.x - lineStart.x),
intercept = lineStart.y - (slope * lineStart.x),
result;
result = Math.abs(slope * pointX - pointY + intercept) / Math.sqrt(Math.pow(slope, 2) + 1);
return result;
}
m.buyDrinks = function (data, callBack) {
var price = 0;
var drinks = [];
var l = 0;
for (var i in data.drinks) {
if (data.drinks[i].type == 'drink') {
l++;
getPriceOfDrink(data.priceID, data.drinks[i].drinkID, function cb(obj) {
l--;
price += obj * data.drinks[i].quantity;
drinks.push(data.drinks[i]);
data.price = price;
if (l < 1) {
m.buy(data, drinks, callBack);
}
});
} else {
l++;
balanceInterface.addBalance({
'balance': data.drinks[i].balance,
'guest': data.guestID
}, function cb(bo) {
l--;
if (l < 1) {
m.buy(data, drinks, callBack);
}
});
}
}
if (l < 1) {
m.buy(data, drinks, callBack);
}
};
m.buy = function (data, drinks, callBack) {
guestInterface.getGuest(data.guestID, function error(err) {
console.log(err);
}, function cb(obj) {
if (data.price < obj.balance) {
addConsumption(drinks, data.guestID, data.priceID, callBack);
} else {
callBack(false);
}
});
};
m.getAllDrinks = function (error, cb) {
drinkDatabaseInterface.getAllDrinks(function err(err) {
error(err)
}, cb);
};
m.addDrink = function (drink, cb) {
drinkDatabaseInterface.addDrink(drink, function callBack(obj) {
broadcasts.get('drinkUpdate')();
broadcasts.get('priceUpdate')();
cb(obj);
});
};
m.removeDrink = function (drinkID, cb) {
drinkDatabaseInterface.deleteDrink(drinkID, function err(err) {
cb(false);
console.log(err);
}, function callBack(obj) {
broadcasts.get('drinkUpdate')();
cb(obj);
});
};
m.setPrice = function (data) {
priceCalculator.setPrice(data.drinkID, data.price);
};
m.setDrink = function (data, cb) {
drinkDatabaseInterface.setDrinkInfo(data.drinkID, data, function error(err) {
cb(false);
console.log(err);
}, function callBack(obj) {
broadcasts.get('drinkUpdate')();
cb(obj);
});
};
m.triggerStockCrash = function (decision) {
priceCalculator.triggerStockCrash(decision);
};
getPriceOfDrink = function (priceID, drinkID, callBack) {
//TODO: implement price entry cache, so you don't have to make a database request when several drinks are bought with the same priceID
priceHistoryInterface.getPricesForID(priceID, function error(err) {
console.log(err)
}, function cb(obj) {
callBack(obj.drinks[drinkID].price);
});
};
addConsumption = function (drinks, guestID, priceID, callBack) {
var saved = 0;
var time = new Date().getTime();
for (var i in drinks) {
consumptionInterface.addConsumption({guest: guestID, priceID: priceID, drink: drinks[i].drinkID, quantity: drinks[i].quantity, time: time}, function cb(obj) {
saved++;
if (drinks.length == saved) {
callBack(true);
}
});
}
};