-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathbidding.js
More file actions
116 lines (99 loc) · 3.01 KB
/
bidding.js
File metadata and controls
116 lines (99 loc) · 3.01 KB
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
/* jshint node: true */
'use strict';
// slot is the number of slots to fill
// bids is all the bids from one auction, already sorted by bid price
module.exports = function(slots, bids) {
// the winning bids and the slots filled by the set of bids
var retVal = {
winningBids: [],
primarySlots: [],
secondarySlots: []
};
var slotsFilled = 0; // slot (filling loop) counter
var secondarySlotsFilled = 0; // secondary filling loop
var primaryOverflow = 0;
// if no bids, return nothing
if (bids.length < 1) { return retVal; }
// primary filling loop
while (slotsFilled < slots) {
var highestBid = {};
// check if there are anymore bids
if (bids.length > 0){
// get highest bid
highestBid = bids[0];
}
// else break out and return what we have
else { break; }
// adjust slots if bid is void
var bidSlots = 0;
if (highestBid.void && highestBid.wonSlots) {
bidSlots = highestBid.wonSlots;
}
else if (highestBid.void && !highestBid.wonSlots) {
bidSlots = 0;
}
else { bidSlots = highestBid.slots; }
// add this bid to the list of winning bids
retVal.winningBids.push(highestBid);
// find the number of slots this bid fulfills
slotsFilled += bidSlots;
// fill the slots with the winning bid
var counter = 0;
while (retVal.primarySlots.length < slots &&
counter < bidSlots) {
retVal.primarySlots.push(highestBid);
counter++;
}
if (slotsFilled > slots){
primaryOverflow = slotsFilled - slots;
}
else {
// remove highest bid
bids.splice(0, 1);
}
}
// if no bids, return nothing
if (bids.length < 1) { return retVal; }
// pre-add overflow
var overflowCounter = 0;
var overflowBid = bids[0];
while (retVal.secondarySlots.length < slots &&
overflowCounter < primaryOverflow) {
retVal.secondarySlots.push(overflowBid);
overflowCounter++;
secondarySlotsFilled++;
}
bids.splice(0, 1);
// secondary filling loop
while (secondarySlotsFilled < slots) {
var secondaryHighestBid = {};
// check if there are anymore bids
if (bids.length > 0){
// get highest bid
secondaryHighestBid = bids[0];
}
// else break out and return what we have
else { break; }
// adjust slots if bid is void
var secBidSlots = 0;
if (secondaryHighestBid.void && secondaryHighestBid.wonSlots) {
secBidSlots = secondaryHighestBid.wonSlots;
}
else if (secondaryHighestBid.void && !secondaryHighestBid.wonSlots) {
secBidSlots = 0;
}
else { secBidSlots = secondaryHighestBid.slots; }
// find the number of slots this bid fulfills
secondarySlotsFilled += secBidSlots;
// fill the slots with the winning bid
var secondaryCounter = 0;
while (retVal.secondarySlots.length < slots &&
secondaryCounter < secBidSlots) {
retVal.secondarySlots.push(secondaryHighestBid);
secondaryCounter++;
}
// remove highest bid
bids.splice(0, 1);
}
return retVal;
};