-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matchmaking.js
83 lines (80 loc) · 2.75 KB
/
Matchmaking.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
//Made by Alex Daffurn-Lewis
/*
* This function expects an array of bids in the form:
* [
* {address: address, type: type, minQuantity: minQuantity, maxQuantity: maxQuantity, price: price},
* ...
* ]
* Where type is either "offer" or "demand"
* It will return an array of matched offers and demands in the form:
* [
* {offerAddress: addressOfOffer, demandAddress: addressOfDemand, quantity: quantity, price: price},
* ...
* ]
*/
module.exports = function(bids) {
var offers = new Array();
var demands = new Array();
var matches = new Array();
var bidQuantity;
var bidPrice;
/*
* If the bid is an offer we ask them for the minimum price they will want for their goods, we then set their price range to [price, inf)
* If the bid is a demand we ask them for the maximum price they are willing to pay for the goods, we then set their price range to [0, price]
*/
for (var i = 0; i < bids.length; i++) {
if(bids[i].type == "offer") {
offers.push({address: bids[i].address, type: bids[i].type, minQuantity: bids[i].minQuantity,
maxQuantity: bids[i].maxQuantity, minPrice: bids[i].price, maxPrice: Number.MAX_SAFE_INTEGER, status: "incomplete"});
} else {
demands.push({address: bids[i].address, type: bids[i].type, minQuantity: bids[i].minQuantity,
maxQuantity: bids[i].maxQuantity, minPrice: 0, maxPrice: bids[i].price, status: "incomplete"});
}
}
for (var i = 0; i < offers.length; i++) {
for (var j = 0; j < demands.length; j++) {
if(offers[i].status == "complete") {
break;
} else if (demands[j].status == "complete") {
continue;
}
bidQuantity = calcQuantity(offers[i], demands[j]);
bidPrice = calcPrice(offers[i], demands[j]);
if(bidQuantity > 0 && bidPrice > 0) {
updateStatus(offers[i]);
updateStatus(demands[j]);
matches.push({offerAddress: offers[i].address, demandAddress: demands[j].address, quantity: bidQuantity, price: bidPrice})
}
}
}
return matches;
}
/*
* Checks if the two ranges of both bids meet returning 0 if they don't.
* If they do then it returns the HIGHEST value that lies in both of the ranges.
*/
function calcQuantity(offer, demand) {
if(demand.maxQuantity < offer.minQuantity || offer.maxQuantity < demand.minQuantity) {
return 0;
}
if (offer.maxQuantity < demand.maxQuantity) {
return offer.maxQuantity;
}
return demand.maxQuantity;
}
/*
* Checks if the two ranges of both bids meet returning 0 if they do not.
* If they do then it returns the LOWEST value that lies in both of the ranges.
*/
function calcPrice(offer, demand) {
if(demand.maxPrice < offer.minPrice || offer.maxPrice < demand.minPrice) {
return 0;
}
if (offer.minPrice < demand.minPrice) {
return demand.minPrice;
}
return offer.minPrice;
}
function updateStatus(bid) {
bid.status = "complete";
}