-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
383 lines (318 loc) · 11.9 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
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
<!doctype html>
<html lang="es">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/bootstrap-table.min.css">
<script src="https://code.jquery.com/jquery-3.6.0.slim.min.js"></script>
<script type="text/javascript" src="data.js"></script>
<title>RUF LINEAS</title>
</head>
<body>
<h1>RUF</h1>
<p>
<button type="button" class="btn-primary" id="nextline-btn">SIGUIENTE LINEA</button>
<div id="nextline" style="display: none">
<table class="table">
<thead>
<tr>
<th scope="col">Nombre</th>
<th scope="col">Cambiar Por</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<div id="nextlinebtns">
<button type="button" class="btn-success" id="acceptline-btn">ACEPTAR</button>
<button type="button" class="btn-danger" id="cancelline-btn">CANCELAR</button>
</div>
</div>
</p>
<p>
<label><input type="checkbox" id="onlyactiveplayers"> Mostrar solo jugadores activos</label>
</p>
<table
id="table"
data-toggle="table"
data-sort-class="table-active"
data-sortable="true"
data-id-field="name"
data-unique-id="name"
>
<thead>
<tr>
<th data-field="name" data-sortable="true">NOMBRE</th>
<th data-field="handler" data-sortable="true" title="handler/cortador" data-formatter="handlerFormatter">🥏 / 🏃</th>
<th data-field="female" data-sortable="true" title="hombre/mujer" data-formatter="femaleFormatter">👩 / 🧔</th>
<th data-field="points" data-sortable="true" title="puntos jugados" data-formatter="pointsFormatter" data-events="pointsEvents">PUNTOS JUGADOS</th>
<th data-field="active" title="judagores disponibles" data-formatter="activeFormatter" data-events="activeEvents">ACTIVO</th>
</tr>
</thead>
</table>
<p>
<button type="button" class="btn-primary" id="addplayer-btn">AGREGAR JUGADOR</button>
</p>
<!--<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js" integrity="sha384-eMNCOe7tC1doHpGoWe/6oMVemdAVTMs2xqW4mwXrXsW0L84Iytr2wi5v2QjrP/xp" crossorigin="anonymous"></script>-->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-cn7l7gDp0eyniUwwAZgrzD06kc/tftFf19TOAs2zVinnD/C7E91j9yyk5//jjpt/" crossorigin="anonymous"></script>
<script src="https://unpkg.com/[email protected]/dist/bootstrap-table.min.js"></script>
<script>
let acceptedLines = [];
function getTable(){
return $('#table');
}
function initPlayersTable(){
const data = [];
// 😊
// https://unicode.org/emoji/charts/full-emoji-list.html#1f3c3
RUF_players.sort((a, b) => a.name.localeCompare(b.name));
for(const item of RUF_players){
data.push({
'name': item.name,
'handler': item.handler,
'female': item.female,
'points': 0,
'active': false,
});
}
const $table = getTable();
// $table.bootstrapTable({data: data});
$table.bootstrapTable('load', data);
}
// function genderFormatter(value, row, index) {
// return value ? '👩' : '🧔';
// }
function femaleFormatter(value, row, index) {
return value ? '👩' : '🧔';
}
function handlerFormatter(value, row, index) {
return value ? '🥏' : '🏃';
}
function activeFormatter(value, row, index) {
return value ? '<a class="deactivate-btn" href="javascript:void(0)" title="Desactivar Jugador"> ✔ </a>' : '<a class="activate-btn" href="javascript:void(0)" title="Activar Jugador"> ❌ </a>';
}
function activatePlayer(playerName, value){
const player = getPlayerData(playerName);
player.active = value;
const $table = getTable();
$table.bootstrapTable('updateCellByUniqueId', {id: playerName, field: 'active', value: player.active});
$('#onlyactiveplayers').change();
}
window.activeEvents = {
'click .deactivate-btn': function (e, value, row, index) {
activatePlayer(row.name, false);
},
'click .activate-btn': function (e, value, row, index) {
activatePlayer(row.name, true);
}
}
function pointsFormatter(value, row, index) {
return `<a class="updatepoints-btn" href="javascript:void(0)" title="Actualizar Puntos">${value}</a>`;
}
// actualizar puntos jugados manualmente
window.pointsEvents = {
'click .updatepoints-btn': function (e, value, row, index) {
const newPoints = prompt('Cambiar puntos jugados: ', value);
if(newPoints !== null && !isNaN(+newPoints)){
const playerName = row.name;
setPointsPlayed(playerName, newPoints);
}
}
}
function setPointsPlayed(playerName, points){
getTable().bootstrapTable('updateCellByUniqueId', {id: playerName, field: 'points', value: +points})
}
function getPlayerData(playerName){
return getTable().bootstrapTable('getRowByUniqueId', playerName);
}
function addPointPlayed(playerName){
const player = getPlayerData(playerName);
if(player){
setPointsPlayed(playerName, getPointsPlayed(playerName) + 1);
}
}
function getPointsPlayed(playerName){
const player = getPlayerData(playerName);
if(player){
return (+player.points) || 0;
}
return null;
}
function hideInactivePlayers(){
const $table = getTable();
$table.bootstrapTable('filterBy', {active: true})
}
function showAllPlayers(){
const $table = getTable();
$table.bootstrapTable('filterBy', {})
}
function suggestLine(handlers, girls){
handlers = +handlers || 0;
girls = +girls || 0;
const line = [];
const activePlayers = getAllActivePlayers();
activePlayers.sort(function(a, b) {
const pointsA = a.points || 0;
const pointsB = b.points || 0;
return pointsA - pointsB;
});
if(activePlayers.length < 7){
alert('No hay suficientes jugadores activos');
return null;
}
let pendingHandlers = handlers;
let pendingGirls = girls;
// aseguramos los handlers requeridos
for(const player of activePlayers){
if(pendingHandlers > 0){
if(player.handler && !line.includes(player)){
line.push(player);
pendingHandlers--;
}
}
else {
break;
}
}
if(pendingHandlers > 0) {
alert('No hay suficientes Handlers!');
}
// aseguramos las chicas requeridas
for(const player of line){
if(player.female){
pendingGirls--;
}
}
for(const player of activePlayers){
if(pendingGirls > 0){
if(player.female && !line.includes(player)){
line.push(player);
pendingGirls--;
}
}
else {
break;
}
}
if(pendingGirls > 0) {
alert('No hay suficientes Chicas!');
}
if(line.length > 7){
alert('Demasiados handles y chicas para solo 7!');
return null;
}
// completamos linea
for(const player of activePlayers){
if(line.length === 7){
break;
}
if(!line.includes(player)){
line.push(player);
}
}
return line;
}
function getAllPlayers(){
return getTable().bootstrapTable('getData');
}
function getAllActivePlayers(){
const activePlayers = getAllPlayers().filter(player => player.active == true);
return activePlayers;
}
function showLine(line){
const $nextLine = $('#nextline');
const $tbody = $nextLine.find('tbody');
$tbody.html('');
const changePlayers = getAllActivePlayers();
changePlayers.sort((a, b) => a.name.localeCompare(b.name));
let changePlayersOptions = '';
changePlayers.forEach(p => changePlayersOptions += `<option value="${p.name}">${p.name}</option>`);
for(const player of line){
$tbody.append(`
<tr>
<td class="name">${player.name}</td>
<td class="change">
<select class="select-player">
${changePlayersOptions}
</select>
<button type="button" class="btn-primary change-player-btn">CAMBIAR JUGADOR</button>
</td>
</tr>
`);
}
$nextLine.show();
$('#nextlinebtns').show();
}
function getSuggestedLinePlayers(){
const names = [];
$('#nextline').find('.name').each(function( index ) {
names.push($( this ).text());
});
return names;
}
$(function() {
initPlayersTable();
$('#onlyactiveplayers').change(function(){
if(this.checked){
hideInactivePlayers();
}
else {
showAllPlayers();
}
});
// sugerir una nueva linea
$('#nextline-btn').click(function(){
const girls = window.prompt('Cuántas Chicas?', '0');
const handlers = window.prompt('Cuantos Handlers?', '0');
const line = suggestLine(handlers, girls);
if(line){
showLine(line);
}
});
$('#cancelline-btn').click(function(){
$('#nextline').hide();
});
// aceptar linea
$('#acceptline-btn').click(function(){
const suggestedLine = getSuggestedLinePlayers();
acceptedLines.push(suggestedLine);
$('#nextlinebtns').hide();
$('.change-player-btn').prop('disabled', true);
for(const name of suggestedLine){
addPointPlayed(name);
}
});
// cambiar jugador de la linea sugerida
$('#nextline').on('click', '.change-player-btn', function(){
const newPlayerName = $(this).closest('.change').find('.select-player').val();
const alreadyInLine = getSuggestedLinePlayers();
if(alreadyInLine.includes(newPlayerName)){
alert(newPlayerName + ' ya está en la línea! elige otro reemplazo');
}
else {
$(this).closest('tr').find('.name').html(newPlayerName);
}
});
// agregar jugador que no exista ya
$('#addplayer-btn').click(function(){
const name = prompt('Nombre: ', '');
if( name ){
const isHandler = confirm('Es handler?');
const isFemale = confirm('Es mujer/niña?');
getTable().bootstrapTable('append', {
name: name,
handler: isHandler,
female: isFemale,
active: true,
points: 0,
});
}
});
});
// TODO: llevar tiempo de juego de cada jugador, para tomar en cuenta eso también a la hora de priorizar la linea
</script>
</body>
</html>