summaryrefslogtreecommitdiffstats
path: root/pages/scores.php
blob: 3f252ecce255006ad340d4aae513f1a4d4938a53 (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php
htmlHeader(
	array('stats'), 'Scores', 
	'Score history for Pathery', 
	array('scores', 'dateformat')
);

//The date to display
$dateDisplay = date('Y-m-d', strtotime("-1 days"));

//This is only used in a strtotime function, so this should be safe.
if (isset($_GET['date'])) {
	$dateLookup = $_GET['date'];
	if (!strtotime($dateLookup)) $dateLookup = $dateDisplay;
} else $dateLookup = $dateDisplay;


$jsYear = date("Y");
//The part where you face palm...
$jsMonth = date("n") - 1;
$jsDay = date("j");
$jsDate = "$jsYear,$jsMonth,$jsDay";
?>

<script type="text/javascript" src="js/jquery.keystrokes.min.js"></script>
<script type="text/javascript" src="js/jquery.hashchange.min.js"></script>

<script type="text/javascript">
playerWallColor = '<?PHP echo isset($wallColor) ? $wallColor : ''; ?>';
playerWallEmblem = '<?PHP echo isset($wallEmblem) ? $wallEmblem : ''; ?>';
</script>

<script>
var startOfPathery = new Date(2011,2,13);

var serverToday = new Date(<? echo $jsDate; ?>);
var serverNow = new Date(<? echo time(); ?>*1000);

var pointerTime = new Date();
pointerTime.setTime(serverToday.getTime())

var pointerMapID = -1;
var goToPointer = true;
var goToScorePointer = true;

$(function(){
  // Bind the event.
  $(window).hashchange( function(){
	 handleHashChange();
  })
  // Trigger the event (useful on page load).
  $(window).hashchange();
});

function handleHashChange() {
	var hash = window.location.hash.substring(1);
	if (hash !== '') {
		hashParts = hash.split('_')
		dateSplit = hashParts[0].split("-");
		if (typeof(hashParts[1]) !== 'undefined') {
			pointerMapID = hashParts[1];
		}
		if (typeof(hashParts[2]) !== 'undefined' && hashParts[2]-0 > 0) {
			pointerPage = hashParts[2];
		} else pointerPage = 1;
		if (typeof(hashParts[3]) !== 'undefined') {
			pointerSolutionID = hashParts[3];
		}
		//+1 because i'm gonna use prev.
		pointerTime = new Date(dateSplit[0],dateSplit[1]-1,dateSplit[2]-0+1);
	} 
	getMapIDs('prev');
}


function addDay(dateObj) {
	var d = dateObj;
	d.setDate(d.getDate()+1);
	return d;
}
function minusDay(dateObj) {
	var d = dateObj;
	d.setDate(d.getDate()-1);
	return d;
}

//Contra
$(document).bind('keystrokes', {
keys: ['arrow up', 'arrow up', 'arrow down', 'arrow down', 'arrow left', 'arrow right', 'arrow left', 'arrow right', 'b', 'a', 'enter']
}, function(){
alert('You unlocked awesomeness!');
});


function displayMapScores(mapID) {
	pointerMapID = mapID;
	updateHash();
	if (!$('#'+mapID+',dspScore').length) {
		var newDiv = $('<div/>', {
			'id': mapID+',dspScore',
			html: ''
		});
		$('#scoreDisplay').empty();
		newDiv.prependTo('#scoreDisplay');
	}

	//Set the 'selectedMap' class
	$('.selectedMap').removeClass('selectedMap');
	var currentMapDiv = $('#thumb_'+mapID);
	currentMapDiv.addClass('selectedMap');
	
	//Hack: display the "current map" div for currently running maps
	//Relies on the fact that the #thumb_<mapId> div has a single child with the 'current' class..
	var showCurrentMapMessage = currentMapDiv.children().hasClass('currentlyRunning');
	$('#currentlyRunningMessage').toggle(showCurrentMapMessage);
	
	$("#mapDisplay").fadeOut('fast');
	
	scoresShowPage(pointerPage, mapID);
	
	//Create a fake DOM element to determine the margin-left of the map, to get the target width
	var fakeMapDom = $('<div class="mapCol"></div>');
	var mapMargin = parseInt(fakeMapDom.css('margin-left'));
	var targetWidth = $('.mapNavCon').width() - $('#scoreDisplay').width() - mapMargin;
	displayMap(mapID, "mapDisplay", targetWidth);
}

function updateHash() {
	var tmp = document.location + '';
	tmp = tmp.split("#");
	var anchorTag = tmp[0]+'#'+pointerDate;
	anchorTag += "_"+pointerMapID;
	if (pointerPage) anchorTag += "_"+pointerPage;
	else anchorTag += "_";
	if (pointerSolutionID) anchorTag += "_"+pointerSolutionID;
	else anchorTag += "_";
	document.location=anchorTag;
}

var pointerPage = 1, pointerSolutionID
function saveScoreLocation(page, solutionID) {
	pointerPage = page;
	pointerSolutionID = solutionID;
	updateHash();
	//console.log("SaveScoreLoc");
}

var pointerDate;
function getMapIDs(request) {
	switch (request) {
		case "next":
		if (pointerTime.getTime() < serverToday.getTime()) {
			addDay(pointerTime);
			pointerDate = pointerTime.format("yyyy-mm-dd");
		} else return;
		break;
		case "prev":
		if (pointerTime.getTime() > startOfPathery.getTime()) {
			minusDay(pointerTime);
			pointerDate = pointerTime.format("yyyy-mm-dd");
		} else return;
		break;
	}
	var timeDisplayPrefix = '';
	if (pointerTime.getTime() >= serverToday.getTime()) {
		$('#mapNavRight').removeClass('mapNavActive');
		$('#mapNavRight').addClass('mapNavDisabled');
		timeDisplayPrefix = '<i title="(Server Time)">Today </i>';
	} else {
		$('#mapNavRight').addClass('mapNavActive');
		$('#mapNavRight').removeClass('mapNavDisabled');
	}
	if (pointerTime.getTime() <= startOfPathery.getTime()) {
		$('#mapNavLeft').removeClass('mapNavActive');
		$('#mapNavLeft').addClass('mapNavDisabled');
		timeDisplayPrefix = '<i title="The start!">Pathery Started </i>';
	} else {
		$('#mapNavLeft').addClass('mapNavActive');
		$('#mapNavLeft').removeClass('mapNavDisabled');
	}
	
	$('#displayDate').html(timeDisplayPrefix+pointerTime.format("dddd, mmmm dS, yyyy"));
	
	var urlString = "a/mapsbydate/"+pointerDate+'.js';
	updateHash();
	$.ajax({
		type: "GET",  
		url: urlString,
		cache: true,
		data: '', 
		fail: function() { alert("error"); },
		complete: function(data) {getMapIDsResponse(data.responseText);}
	});
}

var mapsInNav = new Array();
function getMapIDsResponse(response) {
	var json = decryptJSON(response);
	
	json.reverse();
	
	//Delete removed maps
	$("#mapNavigation").hide(0);
	//console.log("going through mapsInNav", mapsInNav);
	mapsInNav = $.grep(mapsInNav, function(mapID, key) {
		//console.log("del: compare:", key, mapID, json, jQuery.inArray(mapID, json));
		if (jQuery.inArray(mapID, json) < 0 && typeof(mapID) != 'undefined') {
			removeMapFromNav(mapID);
			return false;
		} else return true;
	});
	
	//Add non-existing maps
	//console.log("going through json", json);
	
	var lastMapID = 0;
	$.each(json, function(key, mapID) {
		//console.log("add: compare:", mapID, mapsInNav);
		if (jQuery.inArray(mapID, mapsInNav) == -1) {
			addMapToNav(mapID);
			lastMapID = mapID;
		}
	});
	if (goToPointer == true && pointerMapID < 0) {
		pointerMapID = lastMapID;
	}
	
	//Don't queue animations...
	if ($('#mapNavigation').is(':animated')) {
		$("#mapNavigation").show(0);
	} else $("#mapNavigation").fadeIn('300');
	
	if (goToPointer == true) {
		goToPointer = false;
		displayMapScores(pointerMapID);
	}
}

function addMapToNav(mapID) {

	var $newdiv1 = $("<div id='thumb_"+mapID+"'/>");
	if (pointerMapID + '' == mapID + '') $newdiv1.addClass('selectedMap');
	$newdiv1.addClass('mapThumbnailOuter');

	$("#mapNavigation").prepend($newdiv1);

	var URLString = 'a/map/'+mapID+'.js';
	$.ajax({
		type: "GET",  
		url: URLString,
		cache: true,
		data: '', 
		fail: function() { alert("error"); },
		complete: function(data) {
			var map = decryptJSON(data.responseText);
			var mapDate = new Date(map.dateExpires*1000);
			isActive = mapDate.getTime() > serverToday.getTime();
			
			$("#thumb_"+mapID).html(formatMapThumbForNav(data.responseText)).show();
			//$("#thumb_"+mapID).html(mapThumbnailHTML(map, 120, isActive)).show();
		}
	});
	mapsInNav.unshift(mapID);
}

function formatMapThumbForNav(json) {
	var map = decryptJSON(json);
	var mapDate = new Date(map.dateExpires*1000);
	var mapClass = '';
	var toolTip = 'Inactive map';

	var isCurrentlyRunning = mapDate.getTime() > serverNow.getTime();
	
	if (isCurrentlyRunning) {
		mapClass = ' currentlyRunning';
		toolTip = 'This map can still be played.';
	}

	var r = '';
	r += "<div class='mapThumbnail"+mapClass+"' title='"+toolTip+"'; onclick='displayMapScores("+map.ID+")'>";
	r += mapThumbnailHTML(map, 120, isActive);
	r += '</div>';
	return r;
}

function removeMapFromNav(mapID) {
	//console.log("remove map:", mapID, mapsInNav.indexOf(mapID));
	$("#thumb_"+mapID).hide('fast', function(){ $("#thumb_"+mapID).remove(); });
}

</script>
<body>

<?php
echo soundManager2();
topbar($Links);
?>

<div class="wrapper">

	<div id='displayDate'>
	Loading...
	</div>

	<div class='mapNavCon'>
		<a id='mapNavLeft' class='mapNavLeft mapNavActive unselectable' href='javascript:getMapIDs("prev");'>&lt;</a>
		<a id='mapNavRight' class='mapNavRight mapNavActive unselectable' href='javascript:getMapIDs("next");'>&gt;</a>
		<div class='mapNavCenter'>
			<div id='mapNavigation'></div>
		</div>
	</div>
	<div id="currentlyRunningMessage"><span>This map hasn't completed yet, so points/solutions are not available</span></div>
	<div style='clear:both'></div>
	<div>
		<div class='scoreCol' id='scoreDisplay'></div>
		<div class="mapCol" id='mapDisplay' style='display:none; float:left;'></div>
	</div>
	
	
	<div style='clear:both'></div>
</div>

<!--
<script src="sounds/script/soundmanager.js"></script>
<script type="text/javascript">soundManagerInit();</script>
-->
<?php
htmlFooter();
?>