summaryrefslogtreecommitdiffstats
path: root/js/scores.js
blob: 0c1383f0a2e6c083ee13b64f7d7517f3a95875dc (plain)
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
201
202
203
204
205
206
207
208
209
210
211
var scorePages = new Object;
var currentPage = new Object; 

function scoresRequestPage(mapid, page) {
	// console.log('scoresRequestPage', mapid, page);
	if (typeof(ajax) != 'object') {
		setTimeout(function() {scoresRequestPage(mapid, page)},200);
		return false;
	}
	// Ask again if it's busy.
	switch (ajax.xmlhttp.readyState) {
		case 4:
		case 0:
		break;
		default:
			setTimeout(function() {scoresRequestPage(mapid, page)},300);
			return false;
	}
	
	//console.log("Requesting Page", mapid, page, ajax.inUse);
	
	// == Additionally, check for achievements.
	ajax.requestFile = "do.php?r=reqScorePage&mapid="+mapid+"&reqPage="+page;
	ajax.onCompletion = scoresRequestPageDone; // Specify function to be executed on response.
	//ajax.onLoading = '';
	//ajax.onLoaded = '';
	//ajax.onFail = '';
   ajax.runAJAX();// Do it!
	return true;
}
function scoresRequestLoading() {
	//Signify that it's working in some manner?
	//console.log("I'm loading teh page...");
}
function scoresRequestPageDone() {
	//console.log("Request Page Done", ajax.response);
	var JO = decryptJSON(ajax.response);
	if (JO == undefined) 
		return;
	scoresUpdatePage(JO.mapid, JO.page, scoresFormatPage(JO));
	if (JO.notificationtext != undefined) {
		showNotification(JO.notificationtext);
		if (checkSound(JO.mapid)) {
			soundManager.setVolume('achieve', 50);
			setTimeout("soundManager.play('achieve');", 350);
		}
	}
}
function scoresUpdatePage(mapid, page, html) {
	// console.log("Updating Page", mapid, page);
	scoresPreparePage(mapid, page);
	scorePages[mapid][page].html = html;
	//if (scorePages[mapid][page] == currentPage[mapid]) {
	if (page == currentPage[mapid]) {
		scoresShowPage(mapid, page);
	}
}
function scoresShowMyPage(mapid) {
	//Future use
}

function scoresShowPage(mapid, page) {
	mapid = mapid - 0;
	page = page - 0;
	// console.log('scoresShowPage', mapid, page);
	if (scoresPreparePage(mapid, page) == false) {
		currentPage[mapid] = page;
		// currentPage[mapid] = scorePages[mapid][page];
		//Signify some sort of loadingness?
		//Request Page
		scoresRequestPage(mapid, page);
		return;
	}
	
	if (typeof(scorePages[mapid][page].html) === 'undefined') {
		scoresRequestPage(mapid, page);
		currentPage[mapid] = page;
		return;
	}
	//	console.log('scorpages', scorePages[mapid][page].html);
	updateDsp(mapid, 'dspScore', scorePages[mapid][page].html);
	//Also; update the page
	// console.log('here', scorePages[mapid][page], currentPage[mapid], scorePages[mapid][page] != currentPage[mapid]);
	
	if (page != currentPage[mapid]) {
		scoresRequestPage(mapid, page);
	}
	currentPage[mapid] = page;
	//currentPage[mapid] = scorePages[mapid][page];
}

function scoresPreparePage (mapid, page) {
	if (typeof(scorePages[mapid]) != 'object') {
		scorePages[mapid] = new Object;
		scorePages[mapid][page] = new Object;
		return false;
	}
	if (typeof(scorePages[mapid][page]) != 'object') {
		scorePages[mapid][page] = new Object;
		return false;
	}
	return true;
}
function scoresFormatPage(JO) {
	// console.log("Formating page");
	
	var p = "<table class='score'>";
	if (JO.pageCount > 1) {
		var navi = scoresFormatPageNavi(JO);
		p = p+"<tr><td colspan='3' style='text-align:center;'>"+navi+"</td></tr>";
	}
	p = p+"<tr title='Updated "+JO.updateTime+"'>";
	p = p+"<th>Rank</th>";
	p = p+"<th style='width:125px; overflow:hidden; text-align:left; padding-left:42px;'>Name</th>";
	p = p+"<th>Moves</th>";
	p = p+"</tr>";
	
	var showedLastUser = true;
	var x = 0;
	
	var previousI = 0;
	
	for (var i in JO.users) {
		var u = JO.users[i];
		var scoredLocalTime = new Date();
		scoredLocalTime.setTime(scoredLocalTime.getTime() - u.secondsSinceScored * 1000);
		
		var styleClass = '';
		
		if (previousI != i + 1) 
			if (previousI < i - 1 && previousI != 0)
				styleClass = 'border-top: 6px solid #777799;';

		p = p+ "<tr style='"+styleClass+" background-color: "+u.background+"; color:"+u.displayColor+";' title='Scored "+scoredLocalTime.format("h:MM:ss TT")+"'>";
		p = p+ "<td style='text-align:left;'>";
		
		if (u.medal == 'gold') {
			p = p+ "<img title='Current Champion' src='../images/MedalGoldCAR.png' style='width:32px;height:32px;vertical-align:middle'>";
		} else if (u.medal == 'silver') {
			p = p+ "<img title='Tied Top Score' src='../images/MedalSilverCAR.png' style='width:32px;height:32px;vertical-align:middle'>";
		} else {
			p = p+ "<img src='../images/blank.png' style='width:32px;height:32px;vertical-align:middle'>";
		}
		p = p+ i+ "</td>";
		p = p+ "<td style='vertical-align: middle;'>";
		p = p+ "	<div class='grid_td' style='float:left; width:35px; height:35px; background:"+u.wallColor+" url(images/marks/"+u.wallEmblem+");'>";
		p = p+ "		<div style='background-color:transparent;' class='grid_td_inner grid_td_rocks'>";
		p = p+ "		</div>";
		p = p+ "	</div>";
		p = p+ "";
		p = p+ "<span style='float:left; padding: 5px 0px 3px 3px; width:120px; overflow:hidden; text-overflow: ellipsis; white-space:nowrap; '><a href='achievements?id="+u.ID+"' style='color:"+u.displayColor+"'>"+u.display+"</a></span>";
		p = p+ "</td>";
		p = p+ "<td style='text-align:right;'>"+u.moves+"</td>";
		p = p+ "</tr>";
		
		previousI = i;
	}
	
	//p = p+"<tr><th colspan='3' style='text-align:center;'>"+navi+"</th></tr>";
	p = p+"</table>";
	
	var nextPage = JO.page - 0 + 1;
	var prevPage = JO.page - 1;
	
	if (JO.pageCount > 1) {
		if (JO.page < JO.pageCount) {
			p = p+"<a href='javascript:scoresShowPage("+JO.mapid+", "+nextPage+")' class='rightBtn scoreActive'>&gt;</a>";
		} else
			p = "<div class='rightBtn scoreDisabled'>&gt;</div>"+p;
		if (JO.page > 1) {
			p = "<a href='javascript:scoresShowPage("+JO.mapid+", "+prevPage+")' class='leftBtn scoreActive'>&lt;</a>"+p;
		} else 
			p = "<div class='leftBtn scoreDisabled'>&lt;</div>"+p;
	}
	p = "<div class='scoreContainer1'>"+p+"</div>";
	
	//console.log(p);
	//console.log('jo page', JO.page);
	//console.log('jo pagecount', JO.pageCount); 
	//javascript:scoresShowPage($mapid, $x)
	//p = JO.navi+p;
	return p;
}
function scoresFormatPageNavi(JO) {
	var r = '';
	var showedPrevPage = true;
	for (var i = 1; i <= JO.pageCount; i++) {
		if ( 
				(JO.page > i - 3 && JO.page < i + 3) 
				|| (i == 1) 
				|| (i == JO.pageCount)
				|| (i == JO.userPage)
			) {
			
			if (!showedPrevPage) 
				r = r + ' ... ';
			if (JO.page == i) {
				r = r + " <b style='color:#aaaabb; font-size:150%;'>" + i + "</b>";
			} else if (i == JO.userPage) {
				r = r + " <a href='javascript:scoresShowPage("+JO.mapid+", "+i+")' style='color:#99cc99;' title='You are on this page'><i>" + i + '</i></a>';
			} else {
				r = r + " <a href='javascript:scoresShowPage("+JO.mapid+", "+i+")'>" + i + '</a>';
			}
			showedPrevPage = true;
		} else {
			showedPrevPage = false;
		}
	}

	return r;
}