-
Notifications
You must be signed in to change notification settings - Fork 2
/
FantasyRandomizer.html
427 lines (378 loc) · 12.2 KB
/
FantasyRandomizer.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>TTownWarriors Generator - Rohan Thomare </title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.3/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.3/JSXTransformer.js"></script>
</head>
<script type="text/jsx">
var Table = React.createClass ({
getInitialState: function() {
data = this.props.data
return {data}
},
render: function() {
var headerColumns = this.props.headers.map(function(header, index) {
return (
<th>{header}</th>
);
});
var rows = this.state.data.map(function(row, rowIndex) {
var rowElement = row.map(function(cell, columnIndex) {
if (columnIndex == 0) {
return (
<th><b>{cell}</b></th>
);
} else {
return (
<td>{cell}</td>
);
}
});
return (
<tr>{rowElement}</tr>
);
});
var bodyElement = (
<tbody>
{rows}
</tbody>
);
var headerElement = (
<thead>
<tr>
{headerColumns}
</tr>
</thead>
);
return (
<table className="table">
{headerElement}
{bodyElement}
</table>
);
}
});
</script>
<script type="text/jsx">
var divArray = [];
var peopleArray = [];
var divisionInitialData = [
{'Division':'Divison1'},
{'Division':'Divison2'},
{'Division':'Divison3'},
];
var peopleInitialData = [
{'Name':'Person1'},
{'Name':'Person2'},
{'Name':'Person3'},
{'Name':'Person4'},
{'Name':'Person5'},
{'Name':'Person6'},
{'Name':'Person7'},
{'Name':'Person8'},
{'Name':'Person9'},
{'Name':'Person10'},
{'Name':'Person11'},
{'Name':'Person12'},
];
var divisionUpdate = function(divisionsData){
divArray = []
divisionsData.map(function(dictionary, index) {
divArray.push(dictionary['Division']);
});
}
divisionUpdate(divisionInitialData);
var peopleUpdate = function(peoplesData){
peopleArray = []
peoplesData.map(function(dictionary, index) {
peopleArray.push(dictionary['Name']);
});
}
peopleUpdate(peopleInitialData);
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
function pp(object){
alert(JSON.stringify(object));
}
var createDivisions = function(divisions, people) {
//initlize the divisions hash with the correct information
var divisionMap = {}
var numDivisions = divisions.length
var MAX_PEOPLE_IN_DIVISION = Math.floor(people.length/divisions.length);
divisions.map(function(divisionName,index) {
divisionMap[divisionName] = [];
});
//initalize peopleLeft array
var peopleLeft = people.map(function(person, index){
return person;
});
var tries = 0;
while(peopleLeft.length > 0) {
tries += 1;
if (tries > numDivisions * 4) {
MAX_PEOPLE_IN_DIVISION += 1;
}
var person = peopleLeft[0];
var randomDivIndex = divisions[getRandomInt(0,100) % numDivisions];
var division = divisionMap[randomDivIndex];
if (division.length >= MAX_PEOPLE_IN_DIVISION){
continue;
}
tries = 0;
division.push(person);
peopleLeft.shift();
}
return divisionMap;
}
var digestDivisions = function(divisions){
var tableHeaders = ['Divisions'];
var tableRows = [];
for (division in divisions) {
players = divisions[division];
tableHeaders.push(division);
players.map(function(player,index) {
if (index >= tableRows.length) {
tableRows.push([(index+1).toString()]);
}
tableRows[index].push(player);
})
}
return {
'headers': tableHeaders,
'rows' : tableRows,
}
}
var updateTable = function() {
React.unmountComponentAtNode(document.getElementById('divisionsTable'));
divisionMap = createDivisions(divArray,peopleArray);
divsionTable = digestDivisions(divisionMap);
React.render(
<Table headers={divsionTable.headers} data={divsionTable.rows}/>,
document.getElementById('divisionsTable')
);
}
updateTable();
</script>
<script type="text/jsx">
//people array
var createDraftOrder = function(people) {
var draftOrder = [];
//initalize peopleLeft array
var peopleLeft = people.map(function(person, index){
return person;
});
while(peopleLeft.length > 0) {
var randomPersonIndex = getRandomInt(0,100) % peopleLeft.length;
var person = peopleLeft[randomPersonIndex];
draftOrder.push(person);
peopleLeft.splice(randomPersonIndex,1);
}
return draftOrder;
}
var digestDraft = function(order) {
var tableHeaders = ['#','Name'];
var tableRows = [];
order.map(function(person, index) {
tableRows.push([(index+1).toString(), person]);
});
return {
'headers': tableHeaders,
'rows' : tableRows,
}
}
var updateDraft = function() {
React.unmountComponentAtNode(document.getElementById('draftTable'));
draftOrder = createDraftOrder(peopleArray);
draftTable = digestDraft(draftOrder);
React.render(
<Table headers={draftTable.headers} data={draftTable.rows}/>,
document.getElementById('draftTable')
);
}
updateDraft();
</script>
<script type="text/jsx">
var ExpandingListItem = React.createClass ({
getInitialState: function() {
if (this.props.data) {
return {'data': this.props.data}
}
return {'data': {}}
},
textStateChange: function(key, value){
currentState = this.state.data;
value ? (currentState[key] = value) : console.log("Skipping empty feild placement in state");
this.setState({'data': currentState});
},
update: function(key, value) {
//send data back to parent
this.textStateChange(key,value);
this.props.update(this.props.index, currentState);
},
render: function() {
var expandingListItem = this;
var inputs = this.props.dataKeys.map( function(key, index) {
var onBlurCallBack = function(e) {
expandingListItem.update(key, e.target.value);
};
var updateCallBack = function(e) {
expandingListItem.textStateChange(key, e.target.value);
};
return (
<input type="text"
onChange={updateCallBack}
onBlur={onBlurCallBack}
className="form-control"
placeholder={'Enter ' + key + ' Here'}
value={expandingListItem.state.data[key]}/>
);
});
//<div className="panel-heading">Panel heading without title</div>
// <div className="panel panel-default">
return (
<div className="panel-body">
{inputs}
</div>
);
}
});
var ExpandingList = React.createClass ({
getInitialState: function (){
items = [];
if (this.props.data && this.props.data.length > 0) {
this.props.data.map(function(item, index) {
items.push(item);
});
} else {
items.push({});
}
return {'items': items};
},
addItem: function() {
//grab state object to be reassigned
currentItems = this.state.items;
currentItems.push({});
this.setState({'items':currentItems});
this.props.update(currentItems);
},
updateItem: function(index, data) {
currentItems = this.state.items;
currentItems[index] = data;
this.setState({'items':currentItems});
this.props.update(currentItems);
},
removeItem: function() {
currentItems = this.state.items;
currentItems.pop();
this.setState({'items':currentItems});
this.props.update(currentItems);
},
render: function() {
//access variable of the React instance of TabList
var list = this;
//walk through tabs and generate a tab based on if it is active
var itemNodes = list.state.items.map(function(item, index) {
return (
<ExpandingListItem key={index}
index={index}
data={item}
dataKeys={list.props.dataKeys}
update={list.updateItem}/>
);
});
if (list.state.items.length > 1) {
var controls = (
<div className="btn-group" role="group" aria-label="First group">
<button onClick={list.addItem} type="button" className="btn btn-default">
<span className="glyphicon glyphicon-plus" aria-hidden="true"></span>
</button>
<button type="button" onClick={list.removeItem} className="btn btn-default">
<span className="glyphicon glyphicon-minus" aria-hidden="true"></span>
</button>
</div>
);
} else {
var controls = (
<div className="btn-group" role="group" aria-label="First group">
<button onClick={list.addItem} type="button" className="btn btn-default">
<span className="glyphicon glyphicon-plus" aria-hidden="true"></span>
</button>
</div>
);
}
return (
<div className="nav nav-tabs">
{itemNodes}
{controls}
</div>
);
}
});
React.render(
<ExpandingList data={divisionInitialData}
dataKeys={['Division']}
update={divisionUpdate}/>,
document.getElementById('division')
);
React.render(
<ExpandingList data={peopleInitialData}
dataKeys={['Name']}
update={peopleUpdate}/>,
document.getElementById('people')
);
var ClickButton = React.createClass({
render: function(){
return (
<button type="button" onClick={this.props.click} className="btn btn-success">
{this.props.children}
</button>
);
}
});
React.render(
<ClickButton click={updateTable}>
Generate
</ClickButton>,
document.getElementById('generate')
);
React.render(
<ClickButton click={updateDraft}>
Reorder
</ClickButton>,
document.getElementById('reorder')
);
</script>
<body style="margin-left: 10px;">
<h2>TTownWarriors Fantasy Football Generator</h2>
<h6>Created by Rohan Thomare on 9/6/15. Copyright © 2015</h6>
<div class="row">
<div class="col-xs-6">
<span id='divisionsTable'></span>
<div id="generate"></div>
</div>
<div class="col-xs-6">
<span id='draftTable'></span>
<div id="reorder"></div>
</div>
</div>
<div class="row">
<div class="col-xs-6">
<h3>Edit Divisions</h3>
<div id="division"></div>
</div>
<div class="col-xs-6">
<h3>Edit People</h3>
<div id="people"></div>
</div>
</div>
</body>