-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecentGameStatsTable.js
141 lines (126 loc) · 5.44 KB
/
RecentGameStatsTable.js
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
var p1Data = null;
var p2Data = null;
var p1Name = null;
var p2Name = null;
function getRecentGamesForName(playerNum){
//Figure out if this is p1 or p2 and get the summoner name
var summoner_name = (playerNum == 1) ? document.getElementById("summonerNameForRecentGames").value : document.getElementById("comparerSummonerNameForRecentGames").value;
$.getJSON("/GetRecentGamesForName", {summonerName:summoner_name}, function(result){
if(playerNum == 1){ p1Name = summoner_name; p1Data = result; }
else if(playerNum == 2){ p2Name = summoner_name; p2Data = result; }
//Figure out if we have both sets of data, or just one
var both_players_entered = (p1Data != null && p2Data != null ? true : false);
//Create games played count text
var games_played_text = "";
var p1_games, p2_games;
if(p1Data){
p1_games = p1Data['games'].length;
games_played_text = '<p class="p1">' + p1Name + ' has played '+ p1_games +' games recently</p>';
}
if(p2Data){
p2_games = p2Data['games'].length;
games_played_text = games_played_text + '<p class="p2">' + p2Name + ' has played '+ p2_games +' games recently</p>';
}
$('#RecentGameStats').html(games_played_text);
//Clear chart data except headers
$('#RecentGameStatsTable tr').slice(1).remove();
if(both_players_entered){
$('#RecentGameStatsTable th').slice(1).attr("colspan", "2");
}
else{
$('#RecentGameStatsTable th').slice(1).removeAttr("column-span");
}
$('#RecentGameStatsTable th').unbind('click');
$('#RecentGameStatsTable th').on('click', updateRecentStats);
var stat_table = $('#RecentGameStatsTable');
var stat_variables = ['minionsKilled', 'championsKilled', 'numDeaths', 'goldEarned'];
if(both_players_entered){
var min_games = Math.min(p1_games, p2_games);
for (var i = 0; i < min_games; i++) {
var row = $('<tr></tr>');
row.append($('<td></td>').text(i + 1));
for(var j = 0; j < stat_variables.length; j++){
row.append($('<td class=p1></td>').text(p1Data['games'][i]['stats'][stat_variables[j]] || 0));
row.append($('<td class=p2></td>').text(p2Data['games'][i]['stats'][stat_variables[j]] || 0));
}
stat_table.append(row);
};
}
else if(p1Data){
for (var i = 0; i < p1_games; i++) {
var row = $('<tr></tr>');
row.append($('<td></td>').text(i + 1));
for(var j = 0; j < stat_variables.length; j++){
var stats = p1Data
row.append($('<td class=p1></td>').text(p1Data['games'][i]['stats'][stat_variables[j]] || 0));
}
stat_table.append(row);
};
}
else if(p2Data){
for (var i = 0; i < p2_games; i++) {
var row = $('<tr></tr>');
row.append($('<td></td>').text(i + 1));
for(var j = 0; j < stat_variables.length; j++){
row.append($('<td class=p2></td>').text(p2Data['games'][i]['stats'][stat_variables[j]] || 0));
}
stat_table.append(row);
};
}
var summoner_id = (playerNum == 1) ? p1Data['summonerId'] : p2Data['summonerId'];
//Display match history link
$('#matchHistoryLink').removeClass('hide')
.attr('href', "ShowMatchHistory?summonerId=" + summoner_id);
}).fail(function(a,b,c){alert(c);});
}
function updateRecentStats(){
var clicked_col = this.innerHTML;
var col_index = 0;
var table = $('#RecentGameStatsTable');
var headers = $('#RecentGameStatsTable > tbody > tr > th');
$.each(headers, function(i, val){
if(val.innerHTML == clicked_col){
col_index = i;
}
});
//Return if they click the Game# column
if(col_index == 0){
return;
}
var comparison_col = col_index * 2 - 1;
var rows = $('#RecentGameStatsTable > tbody > tr');
var vals = [];
var for_grouped_chart = [];
grouped_header = {'Game':1};
grouped_header[p1Name] = 1;
grouped_header[p2Name] = 1;
for_grouped_chart.push(grouped_header);
var both_players_entered = (p1Data != null && p2Data != null ? true : false);
for(i = 1; i < rows.length; i++){
var row = rows[i];
var cells = row.cells;
var game_num = cells[0].innerHTML;
var val = parseInt(cells[col_index].innerHTML);
vals.push([game_num, val]);
if(both_players_entered){
var obj = {};
obj['Game'] = i;
obj[p1Name] = parseInt(cells[comparison_col].innerHTML);
obj[p2Name] = parseInt(cells[comparison_col+1].innerHTML);
for_grouped_chart.push(obj);
}
}
if(both_players_entered){
GroupedBarChart(for_grouped_chart);
}
$('.chart').html('');
var svg = d3.select(".chart");
var chart = SimpleBarChart(
{
'parent': svg, 'width' : 600, 'height': 600,
'labels': [ "Game", clicked_col ],
'data' : vals, 'mleft' : 50,
'mright' : 20, 'mbottom': 60, 'mtop' : 20
});
chart();
}