-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbids.html
305 lines (248 loc) · 8.67 KB
/
bids.html
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>2013 Bids</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="robots" content="index, follow">
<meta name="description" content="">
<meta name="keywords" content="">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<script src="js/jquery-1.7.1.min.js"></script>
<script src="js/jquery.sortable.js"></script>
<h1>2013 Bids</h1>
<div class="disclaimer">
NOTE: OSB states that you should have NO expectations that this site, or its
computed results, will have any bearing on final assignments.
</div>
<div>
Drag the posts to reach your desired bid list (top is most preferred). You will
need a PIN to save your work; ask Kennon for yours. Upon saving, click the Solve
button to calculate post assignments.
</div>
<div>
To solve for an optimal assignment set, we use the Hungarian algorithm, a
polynomial-time solution to the <a href="http://en.wikipedia.org/wiki/Assignment_problem">
assignment problem</a>. Ties are resolved deterministically but this is a product
of internal workings of the language, and is therefore effectively random.
</div>
<div>
All code can be found on <a href="https://github.com/kennonlee/cacahuates" target="_blank">github.com</a>. Enjoy!
</div>
<h2>Current optimal assignments</h2>
Your assignment will not appear until you have saved your bid list. Don't worry-- you can edit and resave it at any time.
<div id="assignments">'
<p id="spinnertext" class="spinnertext">Calculating...</p>
<p class="spinner"><img src="css/images/spinner.gif"></img></p>
</div>
<div id="compute" class="computebutton" onclick="window.location.reload()">Solve!</div>
<div id="bidcols">
</div>
</div>
<div id="floatspacer" class="floatspacer">
</div>
<h2>God Mode</h2>
<div>Force assignments to whoever you want-- even those who have not yet saved their bid lists. If you try to force too many people to a given post (e.g., assigning 3 people to Moscow), then the algorithm will randomly choose from among the forced assignees.
<div></div>
<div id="godmode">
<div id="godassignments"></div>
<form action="" method="get" id="godform">
</form>
</div>
<script>
function showSaveMsg(name, msg) {
var savemsg = $("#savemsg-" + name);
$(savemsg).html(msg);
$(savemsg).show().delay(2000).fadeOut();
}
function getAssignments(forced, targetElemRef) {
request = $.ajax({
url: "/get_assignments",
type: "get",
data: forced
});
request.done(function(response, textStatus, jqXHR) {
setTimeout(function() {
$("#spinnertext").html("Wait for it...");
setTimeout(function() {
$(targetElemRef).html("");
var assignments = eval('(' + response + ')');
var assignmentList = $("<ul>")
$(assignmentList).appendTo($(targetElemRef));
assignments.sort();
for (var i = 0; i < assignments.length; i++) {
var info = assignments[i];
// var assignmentStr = info[0] + " -> " + info[1] + " (#" + info[2] + " pick)";
var assignmentStr = info[0] + " -> " + info[1];
var rank = info[2];
if (rank < -10) {
$('<li class="forced">' + assignmentStr + "</li>").appendTo($(assignmentList));
}
else {
$("<li>" + assignmentStr + "</li>").appendTo($(assignmentList));
}
}
//alert(response);
// }, 1000 + (Math.random() * 500));
// }, 500 + (Math.random() * 500));
}, (Math.random() * 500));
}, (Math.random() * 500));
});
request.fail(function (jqXHR, textStatus, errorThrown) {
alert("poop");
});
}
function drawBidLists(rankings) {
var people = Object.keys(rankings);
people.sort();
// construct a column for each person
$.each(people, function (index, name) {
var bidcol = $("<div/>").addClass("bidcol");
$("<h2>" + name + "</h2>").appendTo($(bidcol));
// add a list entry for each post
var rankingId = "ranking-" + name;
var ranking = $("<ul/>").attr("id", rankingId).addClass("sortable");
$(ranking).appendTo($(bidcol));
// this will become tied to an ajax request to retrieve stored rankings
var posts = rankings[name];
for (var i = 0; i < posts.length; i++) {
var post = posts[i];
$("<li>" + post + "</li>").appendTo($(ranking));
}
// make it drag sortable
$(ranking).sortable();
$("<div/>").attr("id", "savemsg-" + name).addClass("savemsg").hide().appendTo($(bidcol));
var pinField = $('<input type="password"/>').attr("id", "pin-" + name).addClass("pinfield");
$(pinField).appendTo($(bidcol));
var savebutton = $("<div>Save</div>").attr("id", "savebutton-" + name).addClass("savebutton");
$(savebutton).click(function() {
var optionTexts = [];
$("#" + rankingId + " li").each(function() {
optionTexts.push($(this).text());
});
var pin = $("#pin-" + name).val()
if (pin.length == 0) {
showSaveMsg(name, "enter a PIN!");
return;
}
// alert(name + "(pin " + pin + "): " + optionTexts);
request = $.ajax({
url: "/save",
type: "post",
data: {
name: name,
pin: pin,
ranking: optionTexts
}
});
request.done(function(response, textStatus, jqXHR) {
showSaveMsg(name, response);
});
request.fail(function (jqXHR, textStatus, errorThrown) {
alert("poop");
});
});
$(savebutton).appendTo($(bidcol));
$(bidcol).appendTo($("#bidcols"));
});
}
/**
*
*/
function drawGodMode(people, posts) {
people.sort();
var count = 0;
$('<table id="godtable"/>').appendTo($('#godform'));
$('<tr id="godtr" />').appendTo('#godtable');
$.each(people, function (index, name) {
var rowcount = Math.floor(count / 5);
$('<td id="td-' + count + '"/>').appendTo('#godtr');
var str = '<label for="god-label-' + name + '">' + name + '</label>';
str += '<select id="god-select-' + name + '" name="' + name + '"/>';
$(str).addClass('godselect').appendTo($('#td-' + rowcount)).change(function (eventObj) {
getAssignments($('#godform').serialize(), '#godassignments');
});
$('<option value="">---</option>').appendTo('#god-select-' + name);
$.each(posts, function(index, post) {
$('<option value="' + post + '">' + post + '</option>').appendTo('#god-select-' + name);
});
count++;
});
}
/**
* Randomize array element order in-place.
* Using Fisher-Yates shuffle algorithm.
*
* http://stackoverflow.com/questions/2450954/how-to-randomize-a-javascript-array
*/
function shuffle(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
function init() {
var people = ["Kennon", "Vadim", "Pooja", "Casey", "Byron",
"Matt", "Nick", "Miguel", "DaveG", "Mark",
"Theresa", "Jeremy", "Eric", "Phillip", "DaveP", "Sam",
"Andrew", "Souleymane", "Rich"
];
var posts = ["Abu Dhabi", "Canberra", "Dakar",
"Frankfurt", "Frankfurt RCSO",
"London", "Mexico City", "Montevideo",
"Moscow", "New Delhi", "DC"
];
// pull out forced assignments from url, pass to get_rankings
var urlParams = getUrlParams();
for (var key in urlParams) {
if (urlParams[key] == '') {
delete urlParams[key];
}
}
request = $.ajax({
url: "/get_rankings",
type: "get",
});
request.done(function(response, textStatus, jqXHR) {
var rankings = eval('(' + response + ')');
var missing = '';
for (var i = 0; i < people.length; i++) {
var name = people[i];
if (rankings[name] == null) {
missing += name + ' ';
// shuffle is in-place so use slice() to clone
//rankings[name] = shuffle(posts.slice(0));
rankings[name] = posts.slice(0);
}
}
//alert(missing);
drawBidLists(rankings);
drawGodMode(people, posts);
getAssignments(urlParams, '#assignments');
});
request.fail(function (jqXHR, textStatus, errorThrown) {
alert("poop");
});
}
// http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values
function getUrlParams() {
var urlParams;
var match,
pl = /\+/g, // Regex for replacing addition symbol with a space
search = /([^&=]+)=?([^&]*)/g,
decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
query = window.location.search.substring(1);
urlParams = {};
while (match = search.exec(query))
urlParams[decode(match[1])] = decode(match[2]);
return urlParams;
}
$(init);
</script>
</body>
</html>