-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTeamsJS_Jquery.js
More file actions
159 lines (149 loc) · 4.28 KB
/
TeamsJS_Jquery.js
File metadata and controls
159 lines (149 loc) · 4.28 KB
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
var teams_list = null;
$( document ).ready(function() {
get_teams(function(kala){
teams_list = kala;
});
$("#teamas").change(function(){
var team = "";
$("#teamas option:selected").each(function(){
team = $(this).text();
});
if (team != ""){
// console.log(team);
update_team(team);
}
}).trigger("change");
});
function get_teams(callback){
$.getJSON("http://pinq.kapsi.fi/github/workspace/index.php", {cmd : "list_teams"})
.done(function(data){
make_list(data);
callback(data);
}).fail(function(){
$("#team_name").text("Tietokanta ei ole käytössä")
});
};
function make_list(data_list){
$("#teamas").append("<option> </option>");
$.each(data_list, function(i,val){
$("#teamas").append("<option>"+i+"</option>");
});
};
function update_team(name){
// console.log teams_list[name]);
$("#team_text").text(name);
name = name_chek(name);
update_names(name);
update_games(name);
var results = calculate_wins(name);
update_horzbar(results[0].length,results[1].length,results[2].length);
};
function update_names(team_name){
$("#players")
.find('option')
.remove()
.end();
$("#players").append("<option> </option>");
$.each(teams_list[team_name], function(i,val){
if( i == "players"){
$.each(val, function(i,val){
// console.log(val.name + " #"+val.number);
$("#players").append("<option>" + val.name + " #" + val.number + "</option>");
});
}
});
};
function update_games(team_name){
$("#game")
.find('option')
.remove()
.end();
$("#game").append("<option> <option>");
$.each(teams_list[team_name], function(i,val){
if( i == "matches"){
$.each(val, function(i,val){
// console.log(val.home.name +" " + val.away.name);
$("#game").append("<option>" + val.home.name + " - " + val.away.name + "</option>");
});
}
});
};
function name_chek(mystring){
return mystring.replace(/'/g, "'")
}
function calculate_wins(team_name){
var wins = [];
var loses = [];
var even = [];
$.each(teams_list[team_name], function(i,val){
if( i == "matches"){
$.each(val, function(i,val){
var home = Number(val.home.results.first) + Number(val.home.results.second);
var away = Number(val.away.results.first) + Number(val.away.results.second);
if (val.home.name == team_name && home < away){
wins.push(val.id);
// console.log(val.id);
}
else if(val.away.name == team_name && home > away){
wins.push(val.id);
}
else if (home == away){
even.push(val.id);
}
else{
loses.push(val.id);
}
});
}
});
// console.log(" voitot: " + wins.length + " häviöt: " + loses.length + " tasapelit: " + even.length);
return [wins,loses,even];
};
function update_horzbar(win,loses,even){
// console.log(typeof(win),typeof(loses),even);
if (side_bar != null){
side_bar.destroy();
}
var element2 = document.getElementById("results").getContext("2d");
var side_bar = new Chart(element2,{
type: "horizontalBar",
data: {
labels:["Pelit"],
datasets:[{
label:"Voitot",
backgroundColor: "rgb(0,200,0,0.6)",
data:[win]
},{
label:"Häviöt",
backgroundColor: "rgba(200,0,0,0.6)",
data:[loses]
},{
label:"Tasapelit",
data:[even]
}]
},
options: {
tooltips: {
mode: 'index',
intersect: false
},
responsive: false,
scales: {
xAxes: [{
stacked: true,
}],
yAxes: [{
stacked: true
}]
},
elements: {
rectangle: {
borderWidth: 2,
}
},
legend: {
position: 'right',
}
}
});
}