-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
215 lines (168 loc) · 8.86 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<title>Fantasy League Solver</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css2?family=Lato:wght@400;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.4.1/semantic.min.css"
integrity="sha512-8bHTC73gkZ7rZ7vpqUQThUDhqcNFyYi2xgDgPDHc+GXVGHXq+xPjynxIopALmOPqzo9JZj0k6OqqewdGO3EsrQ=="
crossorigin="anonymous" />
<link href="assets/css/style.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="logo"></div>
<div class="options ui fluid formation">
<h3>Formation</h3>
<div class="opt-players">
<p>You can either let the algorithm select the 15 players who will get you the maximum number of points, or specify a
formation and the algorithm will select the lowest cost players as subs, ensuring the maximum points are gained from only outfield players.
</p>
</div>
<br />
<select id="team-formation" class="ui dropdown formation-select" name="team-formation"
placeholder="Team Formation">
<option value="all" selected>Select the best overall team (including subs)</option>
<option value="343">3 4 3</option>
<option value="352">3 5 2</option>
<option value="442">4 4 2</option>
<option value="451">4 5 1</option>
<option value="433">4 3 3</option>
</select>
</div>
<div class="options ui fluid positive">
<h3>Players you always want</h3>
<div class="opt-players">
<p>I would like to have these players in my team:</p>
</div>
<br />
<select id="player-initial-positive" class="ui search selection dropdown player-select" name="positive-players" multiple=""
placeholder="Search for a player">
</select>
</div>
<div class="options ui fluid negative">
<h3>Players you never want</h3>
<div class="opt-players">
<p>I don't want these players in my team:</p>
</div>
<br />
<select id="player-initial-negative" class="ui search selection dropdown player-select" name="negative-players" multiple=""
placeholder="Search for a player">
</select>
</div>
<div class="result">
<div class="pitch"></div>
<div class="player-price-summary">
<p>Players in <span class="grey-player-area">Grey</span> are cheap players (less than or equal to £4.5m). At least one goalkeeper is always cheap, and depending on your preferred formation, others can be too.</p>
</div>
<div class="points-summary">
<p>This team generated <span id="total_points"></span> points last season.</p>
</div>
</div>
<div class="clear"></div>
<br/>
<div class="info" style="margin-top: 20vh;">
<p>Our algorithm analyses historic scores and optimises the team
selection to gain the maximum number of points.
</p>
<p>We predict the total number of points for those with either no known scores (e.g. players from newly promoted teams).
If the player played in the 2019/20 season, we take those points as the predicted points.
</p>
<p>Not all players have been added. If you want to add more, you can do it yourself by submitting changes to the code at <a href="https://github.com/eamonnmag/fantasy-league-optimizer" target="_new">GitHub</a> :)</p>
</div>
</div>
<div class="footer">
<p>Built by <a href="https://www.linkedin.com/in/eamonnmaguire1986/" target="_new">Eamonn Maguire</a> | <a target="_new" href="https://twitter.com/antarcticdesign">@antarcticdesign</a>.</p>
<p>Open Source and available on <a href="https://github.com/eamonnmag/fantasy-league-optimizer" target="_new">GitHub</a></p>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"
integrity="sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg=="
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.4.1/semantic.min.js"
integrity="sha512-dqw6X88iGgZlTsONxZK9ePmJEFrmHwpuMrsUChjAw1mRUhUITE5QU9pkcSox+ynfLhL15Sv2al5A0LVyDCmtUw=="
crossorigin="anonymous"></script>
<script src="assets/js/solver.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/6.1.1/d3.min.js"
integrity="sha512-5xkNvFVCctXwOszeifE8pzjyDFlHvHDCKIrhwmuSbCtTHqc7IhA6/1tTNYXE8WmYvwP5KFQegCS1QRR4poYgjg=="
crossorigin="anonymous"></script>
<script src="assets/js/fantasy-solve.js" type="text/javascript"></script>
<script>
var players;
function output_result(result) {
var counts = { 'GKP': 0, 'DEF': 0, 'MID': 0, 'FWD': 0 };
// reset
if(result.feasible) {
d3.select('.pitch').html('');
console.log(result);
Object.keys(result).forEach(function (r) {
if (r in players) {
var player = players[r];
var player_elem = d3.select('.pitch').append('div').attr('class', function () {
counts[player.position] += 1;
return player.position.toLowerCase() + ' ' + player.position.toLowerCase() + counts[player.position];
})
player_elem.append('h4').attr('class', player.cost <= 4.5 ? 'low-cost' : '').text(player.name);
player_elem.append('p').attr('class', 'team').html(player.team + ' £' + player.cost + 'm');
}
});
d3.select('#total_points').text(result.result);
} else {
d3.select('.pitch').html('<p>No optimal team can be found. Try relaxing the constraints.</p>');
}
}
function build_player_lookup(players){
var player_lookup = {};
players.forEach(function(p) {
player_lookup[p.name + '_' + p.team + '_' + p.position] = p;
});
return player_lookup;
}
d3.json('assets/js/players.json').then(function (response) {
players = build_player_lookup(response.players);
var result = fantasySolver.solve(players);
output_result(result);
var for_select = [];
Object.keys(players).forEach(function (p_key) {
for_select.push({ 'name': players[p_key].name + ' - ' + players[p_key].team + ' - £' + players[p_key].cost + 'm', 'value': p_key })
});
$('.formation-select').dropdown();
$('.player-select')
.dropdown({
action: 'combo',
values: for_select.sort(function(a, b){return a.name < b.name ? -1 : 1})
});
function recalculate() {
var positive_values = [];
var negative_values = [];
setTimeout(function() {
d3.selectAll('.options.positive a.ui.label.transition.visible').each(function (d) {
positive_values.push(d3.select(this).attr('data-value'));
});
d3.selectAll('.options.negative a.ui.label.transition.visible').each(function (d) {
negative_values.push(d3.select(this).attr('data-value'));
});
var formation = d3.select('#team-formation').node().value;
var result = fantasySolver.runWithInitialPlayers(positive_values, negative_values, formation);
console.log(result);
output_result(result);
}, 250);
d3.selectAll('a.ui.label.transition.visible i.delete.icon').on("click", function(d) {
recalculate();
})
}
d3.select("#player-initial-positive")
.on("change", function () {
recalculate();
});
d3.select("#player-initial-negative")
.on("change", function () {
recalculate();
});
d3.select("#team-formation")
.on("change", function () {
recalculate();
});
});
</script>
</body>
</html>