forked from simonlc/tetr.js
-
Notifications
You must be signed in to change notification settings - Fork 27
/
ranking.js
200 lines (196 loc) · 5.36 KB
/
ranking.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
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
function XMLHTTP(url,obj,fun)
{
var xmlhttp=null;
var handler=function(){
fun(xmlhttp);
}
if (window.XMLHttpRequest)
{// code for all new browsers
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{// code for IE5 and IE6
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp!=null)
{
xmlhttp.onreadystatechange=handler;
xmlhttp.open("POST",url,true);
xmlhttp.send(JSON.stringify(obj));
}
else
{
alert("Your browser does not support XMLHTTP.");
}
}
function handleranking(xmlhttp)
{
if (xmlhttp.readyState==4)
{// 4 = "loaded"
if (xmlhttp.status==200)
{
var res;
try
{
res = JSON.parse(xmlhttp.responseText);
if(res.msg==="fail")
{
$setText(leaderboard, "error: "+res.info);
}
else if(res.msg==="ok")
{
leaderboard.innerHTML = "";
var ranks = res.ranks;
var now = (new Date()).getTime();
var gettimedifftext = function(t)
{
t=Math.round(t/(60*1000));
if(t<100)
return t<=0?"!":(t+"m");
else if(t<99*60)
return Math.round(t/60)+"h";
else
return Math.round(t/(60*24))+"d";
}
var gettimetext = function(t)
{
var hs=Math.round(t/10);
var s=~~(hs/100);
var m=~~(s/60);
hs-=s*100;
s-=m*60;
if(m==0)
return s+"."+((hs>9?"":"0")+hs);
else if(m<10)
return m+":"+((s>9?"":"0")+s)+"."+((hs>9?"":"0")+hs);
else
return m+":"+((s>9?"":"0")+s)+"."+(~~(hs/10)); //
}
var getlinetxt = function(l)
{
if(l<1000)
return l+"L";
else
return l+"";
}
var getscoretext = function(s)
{
var arr=s.split("");
for(var i=arr.length-1-3;i>=0;i-=3){
arr[i]+=" ";
}
return arr.join("");
}
for (var i=0;i<ranks.length;i++) {
var div = document.createElement('div');
var spanname = document.createElement('span');
var spanlines = document.createElement('span');
var spantime = document.createElement('span');
var spandate = document.createElement('span');
var spanscore = document.createElement('span');
$setText(spanname,ranks[i].name);
spanname.style.width = "6em";
spanname.style["text-align"] = "left";
$setText(spanlines,ranks[i].lines + "L");
spanlines.style.width = "2.5em";
$setText(spantime,gettimetext(ranks[i].time));
spantime.style.width = "4em";
$setText(spandate,gettimedifftext(now-ranks[i].date));
spandate.style.width = "2.5em";
spandate.style["font-weight"] = "normal";
if(res.mode==="score")
{
$setText(spanscore,scorestring(ranks[i].score, 7));
spanscore.style.width = "15em";
spanscore.classList.add("rank-score");
}
div.appendChild(spanname);
div.appendChild(spanlines);
div.appendChild(spantime);
div.appendChild(spandate);
if(res.mode==="score")
{
div.appendChild(spanscore);
}
if(typeof res.ranks[i].replay==="string")
{
div.classList.add("has-replay");
var replaykey=ranks[i].replay;
var closure=function(a){return a}
div.addEventListener("click",function(k){
return function(){
fetchreplay(k);
}
}(replaykey));
}
leaderboard.appendChild(div);
if(res.mode==="score" && i+1===5)
{
break;
}
}
}
else
{
$setText(leaderboard, "Problem retrieving leaderboard data\n" +
xmlhttp.status + "\n" + xmlhttp.readyState + "\n" + xmlhttp.statusText + "\n" +
xmlhttp.responseText
);
}
}
catch(e)
{
$setText(leaderboard, "Problem retrieving leaderboard data\n" +
e.toString()
);
}
}
}
}
function handlereplay(xmlhttp)
{
if (xmlhttp.readyState==4)
{// 4 = "loaded"
if (xmlhttp.status==200)
{
var res;
try
{
res = JSON.parse(xmlhttp.responseText);
if(res.msg==="fail")
{
alert("error: "+res.info);
}
else if(res.msg==="ok")
{
showreplaydata(res.replay);
}
else
{
console.log("Problem retrieving leaderboard data\n" +
xmlhttp.status + "\n" + xmlhttp.readyState + "\n" + xmlhttp.statusText + "\n" +
xmlhttp.responseText
);
}
}
catch(e)
{
console.log("Problem retrieving leaderboard data\n" +
e.toString()
);
}
}
}
}
var hostaddr=
"http://farter.cn:8888";
//"http://localhost:8888";
function submitscore(obj)
{
console.log(obj);
XMLHTTP(hostaddr, obj, handleranking);
}
function fetchreplay(key)
{
XMLHTTP(hostaddr, {req:"replay", replaykey:key}, handlereplay);
}