function CheckPlayerPosition(table) {
var tablesizesArray = new Array();
console.log(table.attr('class'));
$(document).find('table.' + table.attr('class') + '').each(function () {
var tablenumber = parseFloat($(this).data('counttable'));
var tablesize = parseFloat($(this).data('sizetable'));
var parentTable = $(this);
var counttr = 1;
var countcaption = 1;
var countplace = 1;
if (tablesize > 0) {
tablesizesArray[tablenumber] = tablesize - 2;
if (tablenumber > 1) {
var prevtablesizes = 0;
$(tablesizesArray).each(function (i, j) {
if (i < tablenumber && j > 0) {
prevtablesizes += parseFloat(j);
}
});
var countplace = prevtablesizes + 1;
} else {
var countplace = 1;
}
$(parentTable).find('tr.bar').each(function () {
var countscoretd = $(this).find('td.scorecell').length;
var parenttr = $(this);
//Проставляем позицию в таблице
$(this).find('.position').html(' ' + counttr + ' ');
$(this).attr('data-startposition', counttr);
var playerid = $(this).find('.player-name').data('playerid');
if ($(playerid).length) {
playerid = 0;
}
$(this).find('td.scorecell').remove();
for (y = 1; y <= tablesize - 2; y++) {
if ($(parentTable).hasClass('group-table')) {
var row = $(this).find('.place_in_table');
}
if ($(parentTable).hasClass('prize-table')) {
var row = $(this).find('.tour_place');
}
if (counttr == y) {
$('<td data-playerid="' + playerid + '" width="100" class="blackcell scorecell" id="pc1001"> </td>').insertBefore(row);
}
else {
$('<td data-playerid="' + playerid + '" class="score scorecell" data-cellid="' + (y) + '"> <div id="' + counttr + '" class="match autosize-textarea"><input class="autosize-textarea-editor" data-playerid="" data-againstplayercell="' + (y) + '" type="text" value=""></div></td>').insertBefore(row);
}
}
counttr++;
//добавляем номера мест от 1 до конца в призовую таблицу
if ($(parentTable).hasClass('prize-table')) {
$(parenttr).attr('data-place', countplace);
}
countplace++;
});
}
});
}
function draganddrop(table) {
//console.log(table.attr('class'));
$("tr.bar").draggable({
appendTo: "body",
helper: "clone",
cursor: "move",
revert: "invalid",
stop: function (event, ui) {
//console.log(event);
//console.log($(ui.helper[0]).children().attr('id'));
//CheckPlayerValue();
CheckPlayerPosition(table);
}
});
initDroppable($("tr.bar"));
function initDroppable($elements) {
$elements.droppable({
activeClass: "ui-state-default",
hoverClass: "ui-drop-hover",
accept: ":not(.ui-sortable-helper)",
over: function (event, ui) {
var $this = $(this);
},
drop: function (event, ui) {
var $this = $(this);
var li1 = $('<tr class="bar">').append(ui.draggable.html());
var linew1 = $(this).after(li1);
var li2 = $('<tr class="bar">').append($(this).html());
var linew2 = $(ui.draggable).after(li2);
//$(li2).parent().parent().addClass('fffffffffffffffffffffffff');
CheckPlayerPosition($(li2).parent().parent());
$(ui.draggable).remove();
$(this).remove();
if ($(li1).find('td.player-name').find('input').length > 0) {
var input = $(li1).find('td.player-name').find('input');
createAutocomplete(input, table);
}
if ($(li2).find('input').find('td.player-name').length > 0) {
var input = $(li2).find('td.player-name').find('input');
createAutocomplete(input, table);
}
initDroppable($("tr.bar"));
$("tr.bar").draggable({
appendTo: "body",
helper: "clone",
cursor: "move",
revert: "invalid",
stop: function (event, ui) {
CheckPlayerPosition(table);
}
});
}
});
}
}
function calculatewins(ThisValue, score, ParentTr, ParentTd) {
var RateWithPointsArr = [];
var winsarr = [];
$(document).find('tr.bar').each(function() {
var wins = new Array();
var i = 0;
var TR = $(this);
var sum = 0;
$(TR).find('td.score input').each(function() {
var inputvalue = $(this).val();
if ($(this).hasClass('win')) {
wins[i] = [$(this).data('againstplayercell')];
i++;
}
if (inputvalue > 0) {
sum += parseFloat(inputvalue);
}
});
TR.find('div.score').text(wins.length);
TR.find('div.winplayers').text('выиграно: ' + wins.length + '; набрано очков: ' + sum);
//$('<div>выиграно: '+wins.length+'; набрано очков: '+sum+'</div').insertAfter(TR.find('div.score'));
});
$(document).find('td div.score').each(function() {
var parentTd = $(this).parent();
var inputvalue = $(this).text();
var tddataid = parentTd.data('id');
if ($(parentTd).length > 0 && parseFloat(inputvalue) >= 0) {
winsarr.push({parentTd: parentTd, wins: inputvalue});
//winsarr.sort(sortByWins);
}
});
var RateWithPointsArr = [];
$(winsarr).each(function(i, j) {
var datacelldataid = j.parentTd.data('id');
var summ = 0;
for (x = 1; x <= winsarr.length; x++) {
var index = (x) % winsarr.length;
if (j.parentTd != winsarr[index]['parentTd']) {
if (j.wins == winsarr[index]['wins']) {
var againstcell = winsarr[index]['parentTd'].parent().data('startposition');
var PlayerScoreVSPlayersWithSameWins = j.parentTd.parent().find('td.score').find('input[data-againstplayercell=' + againstcell + ']').val();
summ += +parseFloat(PlayerScoreVSPlayersWithSameWins);
}
}
}
RateWithPointsArr.push({id: datacelldataid, summ: summ, wins: j.wins, parentTd: j.parentTd});
});
//RateWithPointsArr.sort(sortByRate);
$(RateWithPointsArr).each(function(i, j) {
for (y = 1; y <= RateWithPointsArr.length; y++) {
var index = (y) % RateWithPointsArr.length;
if (j.id != RateWithPointsArr[index]['id']) {
if (j.wins == RateWithPointsArr[index]['wins'] && j.summ == RateWithPointsArr[index]['summ']) {
var thiscell = j.parentTd.parent().data('startposition');
var againstcell = RateWithPointsArr[index]['parentTd'].parent().data('startposition');
var againstplayerscore = winsarr[index]['parentTd'].parent().find('td.score').find('input[data-againstplayercell=' + thiscell + ']').val();
var thisplayerscore = j.parentTd.parent().find('td.score').find('input[data-againstplayercell=' + againstcell + ']').val();
//console.log('againstplayerscore' + againstplayerscore);
//console.log('thisplayerscore' + thisplayerscore);
if (thisplayerscore > againstplayerscore) {
j.summ += +1;
//console.log(j.id);
}
}
}
}
});
RateWithPointsArr.sort(function(a, b) {
if (a.wins > b.wins) {
return -1;
}
else if (a.wins < b.wins) {
return 1;
}
else if (a.summ > b.summ) {
return -1;
}
else if (a.summ < b.summ) {
return 1;
}
else {
return 0;
}
});
$(RateWithPointsArr).each(function(i, j) {
j.parentTd.find('div.place').text('место: '+(i + 1));
});
console.log(RateWithPointsArr);
}