-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
119 lines (80 loc) · 2.52 KB
/
main.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
var serverIp='http://jukebox.cmc.im';
var serverPort= 80;
var voteValue=[];
function httpGetAsync(theUrl, callback)
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
callback(JSON.parse(xmlHttp.responseText));
}
xmlHttp.open("GET", theUrl, true); // true for asynchronous
xmlHttp.send(null);
}
function httpPostAsync(theUrl, data)
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.onload = function(){
var status = xmlHttp.status; // HTTP response status, e.g., 200 for "200 OK"
var data = xmlHttp.responseText; // Returned data, e.g., an HTML document.
}
xmlHttp.open("POST", theUrl, true); // true for asynchronous
xmlHttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xmlHttp.send(data);
}
function getVotes(){
httpGetAsync(serverIp+":"+serverPort+"/votes",updateVotes);
}
function getSongs(){
httpGetAsync(serverIp+':'+serverPort+'/data',updateSongs);
}
function voteSong(songnum){
console.log(songnum);
setTimeout(getVotes, 300);
httpPostAsync(serverIp+':'+serverPort+'/song/'+songnum+'/vote',null)
}
function updateVotes(votes){
voteValue=votes;
localUpdate();
}
function updateSongs(songs){
for (var i =0 ; i < songs.length; i++) {
console.log(songs[i][0][0]);
var $input = $(' <div ><input type="button" value="new button" onclick="calculate(event);" /> <input type="button"/></div>');
$input.children(":first").attr('value', songs[i][0][0] + " - " +songs[i][0][1] );
$input.children(":first").attr('id',i);
$input.children(":last").attr('value', 0 );
$input.attr('score',0).show();
$input.attr('value', songs[i][0][0]);
$input.appendTo($(".clist"));
}
}
function calculate(event) {
var num=event.target.id;
voteSong(num);
//updateList();
}
function localUpdate(){
for (var i =0 ; i < voteValue.length; i++) {
$( "#"+i ).parent().children(":last").attr('value', voteValue[i]);
$( "#"+i ).parent().attr('score',voteValue[i]);}
console.log($(".clist"));
$('.clist div').sort(function(a,b) {
var an = a.getAttribute('score');
var bn = b.getAttribute('score');
var cn = a.getAttribute('value');
var dn = b.getAttribute('value');
if(an > bn) {
return -1;
}
if(an < bn) {
return 1;
}
if(an = bn) {
return dn.toUpperCase().localeCompare(cn.toUpperCase());
}
}).appendTo('.clist');
}
getSongs();
getVotes();
longPoll=window.setInterval(getVotes,5000);