Selador / stats_extern.php

  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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
<?php

/* stats_extern.php - Front-end code for the per-world external stats page
 *
 * Copyright (C) 2006, 2007, 2008 Kevin Read, Simone Schaefer
 *
 * This file is part of Selador, a browser-based fantasy strategy game
 *
 * This program is distributed under the terms of the GNU Affero General Public License.
 *
 *
 *   Selador is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   any later version.
 *
 *   Selador is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with Selador.  If not, see <http://www.gnu.org/licenses/>.
 **/

// This is unfinished but actually quite close (and also quite important)

@session_start ();
header ("Content-Type: text/html; charset=UTF-8");

require_once ("base_functions.php");
require_once ("base_outside.php");
require_once ("php/base/config.inc.php");

require_once ("php/base/AuthDB.inc.php");
require_once ("php/auth.php");

$webtitle = "Statistik";

if (isset ($_SESSION['world_entered']))
{
  require_once ("land.php");
  die ();
}

if (isset ($_SESSION['authed_user']))
{
	require_once ("portal.php");
	die ();
}

$glob_log = array ();

/* Make sure a file is deleted. Maybe has to wait if is in use atm
function safe_delete ($filename)
{
	global $debug;

	if ($debug)
		echo $filename." ";
	$tries = 0;
	if (file_exists ($filename))
	{
		// echo "ok \n";
		while ((!($res = @unlink ($filename))) && ($tries < 5))
		{
			$tries++;
			usleep (100000);
		}
	}
} */


// $db = new GameDB;
$authdb = new AuthDB;
$world_found = false;

if (isset ($_REQUEST['w']))
{
	$worldname = mysql_real_escape_string ($_REQUEST['w']);
	$query = 'select title from worlds where name="'.$worldname.'"';

	if (!($res = $authdb->query ($query)))
		log_err ("Cannot find worldname ".$worldname.": ".$query.", ".$authdb->error ());
	else
	{
		if ($row = mysql_fetch_array ($res))
		{
			$world_found = true;
			$webtitle = "Statistik f&uuml;r &quot;".$row['title']."&quot;";
		}
	}
}

// Ally-Armee-Ranking:
// select E.ascore,sum(C.amount),round(sum(C.amount)/(E.ascore+0.01)*31.4171) as bla,D.tag from user A, armies B, troops C, allies D, ally_ranking E where A.uid=B.uid and B.troopid=C.troopid and C.unitid!=1000 and A.aid=D.aid and A.aid=E.aid group by A.aid order by bla desc

require_once("header_extern.php");

echo '</head><body>';
echo '<div id="zentriert"><div id="logo"><a href="index.php"><img src="styles/sun/logo.jpg" alt="Startseite"></a></div>';

require_once("menue.php");

?>

<!--<img src="gfx/steinhaus.jpg">-->
<!--<div id="leiste-links" style="background-image:url(styles/sun/leiste-links.png);height:440px">
<div id="angriffe"><br>Anmelden</div>
<div id="rohstoffe" class="inhalt">
<span style="font-size:12px;">Hilfe zu den allgemeinen Benutzerdaten findest du <a href="hilfe_anmeldung.php" target="_blank">hier</a>.<br><br> </span></div></div>
-->

<?php
$page = 1;

$query_startpage = false;
$highlightID = 0;

function draw_register ($this_f, $f, $title)
{
	global $worldname;
	echo '<th><a href="stats_extern.php?w='.$worldname.'&f='.$this_f.'" class="aktiv">';

	if ($this_f == $f)
		echo '<u>'.$title.'</u></a></th>';
	else
		echo $title.'</a></th>';
}

function calcpage (&$page)
{
	global $_REQUEST, $max_pages, $query_startpage, $highlightID;
	if (isset($_REQUEST['page']))
	{
		$page = (int) $_REQUEST['page'];
		if ($page < 1)
		{
			$page = 1;
		}
		if ($page > $max_pages)
		{
			$page = $max_pages;
		}
	}
	else
	{
		if (!$query_startpage)
			$page = 1;
		else
		{
			if (!($res = mysql_query ($query_startpage)))
			{
				log_err ("stats: Cannot find startpage. Query: ".$query_startpage.", ".mysql_error ());
				$page = 1;
			}
			else
			{
				if (!($row = mysql_fetch_array ($res)))
				{
					$page = 1;
					log_err ("stats: No rows returned for startpage. Query: ".$query_startpage.", ".mysql_error ());
				}
				else
				{
					$page = floor($row[0]);
					if (isset($row[1]))
					{
						$highlightID = $row[1];
					}
				}
			}
		}
	}
}

// Handle past events
// $me->event_handler (true, $me->activevillageid, $gui);
$proseite = 40;

if ($world_found)
{
	if (isset ($_REQUEST['f']))
	{
		$f = $_REQUEST['f'];
	}
	else
		$f = 1;

	?>
	<div id="reiter">
	<table class="inhalt" cellspacing="0" width="98%"><tr>

	<?php

	draw_register (1, $f, "Spieler");
	draw_register (2, $f, "Helden");
	draw_register (3, $f, "Allianzen");
	draw_register (4, $f, "Kriege");
	?>
	</tr>
	</table></div>
	<form action="<?php echo $_SERVER['PHP_SELF']."?f=".$_REQUEST['f'] ?>" method="POST">
	<?php

	echo "<input type=\"hidden\" name=\"f\" value=\"".$f."\">";
	switch ($f)
	{
		case 1: //Spielerstatistiken
			$query1 = "select count(uid) as number from ranking where worldname=\"".$worldname."\"";
			$result = mysql_query ($query1);
			$row = mysql_fetch_array ($result);
			mysql_free_result ($result);

			$max_pages = ceil ($row['number'] / $proseite);
			if (!isset($_POST['searchName']) && !isset($_POST['searchRank'])) // wenn keine Suche nach einem Spieler gestartet wurde (name oder Rang), wird die eigene uid genommen
				$page = 1;
			else
			{
				if (isset ($_POST['searchName'])) // wird nach einem Spieler gesucht?
				{
					if ($_POST['nameToSearch'] != "") //wurde ein Spielername eingegeben?
						$query_startpage = "select ((R.ranking-1)/".$proseite.")+1, U.uid from user as U, ranking as R where U.user='".mysql_real_escape_string($_POST['nameToSearch'])."' AND U.uid=R.uid AND R.worldname=\"".$worldname."\"";
					else
					{
						if (0 != (int)$_POST['rankToSearch']) // wurde ein Rang eingegeben?
						{
							$query_startpage = "select ((R.ranking-1)/".$proseite.")+1, U.uid from user as U, ranking as R where R.ranking=".(int)($_POST['rankToSearch'])." and R.uid=U.uid AND R.worldname=\"".$worldname."\"";
						}
					}
				}
			}
			calcpage ($page);
			$query = "select A.uid, A.user, D.allytag as tag, D.score, D.ranking from user A join ranking D on (A.uid=D.uid and D.worldname=\"".$worldname."\") order by D.ranking asc limit ".($proseite *($page-1)) .", ".$proseite;
			$header = '<th>&nbsp;</th><th>Name</th><th width="80" style="text-align:center;">Ally</th><th>Punkte</th></tr>';
			break;

		case 2: //Heldenstatistiken
			$query1 = "select count(uid) as number from ranking";
			$result = mysql_query ($query1);
			$row = mysql_fetch_array ($result);
			mysql_free_result ($result);

			$max_pages = ceil ($row['number'] / $proseite);
			if (!isset($_POST['searchName']) && !isset($_POST['searchRank'])) // wenn keine Suche nach einem Spieler gestartet wurde (name oder Rang), wird die eigene uid genommen
				$page=1;
			else
			{
				if (isset ($_POST['searchName'])) // wird nach einem Spieler gesucht?
				{
					if ($_POST['nameToSearch'] != "") //wurde ein Spielername eingegeben?
						$query_startpage = "select ((R.ranking-1)/".$proseite.")+1, U.uid from user as U, hero_ranking as R where U.user='".mysql_real_escape_string($_POST['nameToSearch'])."' AND U.uid=R.uid";
					else
					{
						if (0 != (int)$_POST['rankToSearch']) // wurde ein Rang eingegeben?
						{
							$query_startpage = "select ((R.ranking-1)/".$proseite.")+1, U.uid from user as U, hero_ranking as R where R.ranking=".(int)($_POST['rankToSearch'])." and R.uid=U.uid";
						}
					}
				}
			}
			calcpage ($page);
			$query = "select A.uid, A.user, D.xp as score, D.allytag as tag, D.hero_ranking as ranking from user A join ranking D on (A.uid=D.uid and D.worldname=\"".$worldname."\") order by D.hero_ranking asc limit ".($proseite *($page-1)) .", ".$proseite;
			$header = '<th>&nbsp;</th><th>Name</th><th width="80" style="text-align:center;">Ally</th><th>Erfahrung</th></tr>';
			break;

		case 3: //Allianzstatistiken
			$query1 = "select count(aid) as number from ally_ranking";
			$result = mysql_query ($query1);
			$row = mysql_fetch_array ($result);
			mysql_free_result ($result);

			$max_pages = ceil ($row['number'] / $proseite);
			if ($me->aid && (!isset($_POST['searchName']))) // ist der Spieler selbst in einer Allianz? hat er keine suche eingeben?
				$query_startpage = "select (aranking/".$proseite.")+1 from ally_ranking where aid=".$me->aid;
			else //wurde nach einer Allianz gescucht?
			{
				if ("" != $_POST['nameToSearch'])
				$query_startpage = "select (R.aranking/".$proseite.")+1, A.aid from allies as A, ally_ranking as R where A.tag='".mysql_real_escape_string($_POST['nameToSearch'])."' AND A.aid=R.aid";
				else
				{
					if (0 != (int)$_POST['rankToSearch']) // wurde ein Rang eingegeben?
					{
						$query_startpage = "select ((R.aranking-1)/".$proseite.")+1, A.aid from allies as A, ally_ranking as R where R.aranking=".(int)($_POST['rankToSearch'])." and R.aid=A.aid";
					}

				}
			}
			calcpage ($page);
			$query = "select A.aid, A.aranking, A.ascore, A.players, B.tag from ally_ranking A, allies B where A.aid=B.aid order by A.aranking asc limit ".($proseite *($page-1)) .", ".$proseite;
			$header = '<th>&nbsp;</th><th>Ally</th><th>Punkte</th><th>Spieler</th><th>Durchschnitt</th></tr>';
			break;


		case 4: //Kriegsstatistiken
			$query1 = "select count(aid) as number from war_stats";
			$result = mysql_query ($query1);
			$row = mysql_fetch_array ($result);
			mysql_free_result ($result);

			$max_pages = ceil ($row['number'] / $proseite);
			if ($me->aid && (!isset($_POST['searchName']))) // ist der Spieler selbst in einer Allianz? hat er keine suche eingeben?
				$query_startpage = "select (aranking/".$proseite.")+1 from ally_ranking where aid=".$me->aid;
			else //wurde nach einer Allianz gescucht?
			{
				if ("" != $_POST['nameToSearch'])
				$query_startpage = "select (R.aranking/".$proseite.")+1, A.aid from allies as A, ally_ranking as R where A.tag='".mysql_real_escape_string($_POST['nameToSearch'])."' AND A.aid=R.aid";
				else
				{
					if (0 != (int)$_POST['rankToSearch']) // wurde ein Rang eingegeben?
					{
						$query_startpage = "select ((R.aranking-1)/".$proseite.")+1, A.aid from allies as A, ally_ranking as R where R.aranking=".(int)($_POST['rankToSearch'])." and R.aid=A.aid";
					}
				}
			}

			calcpage ($page);
			$query = "select A.aid, A.score, (A.score / (lost + won)) as average,  A.lost, A.won, B.tag from war_stats A, allies B where A.aid=B.aid order by A.score desc limit ".($proseite *($page-1)) .", ".$proseite;
			$header = '<th>&nbsp;</th><th>Ally</th><th>Punkte</th><th>Durchschnitt</th><th>verl./gew.</th></tr>';
			break;




		default:
			echo "Gibts noch nicht :(";
			exit ();
			break;
	}



	echo "<div id=\"forschung\"><br>\n";

	if (!($res = mysql_query ($query)))
	{
		log_err ("Cannot read stats. Query: ".$query.", ".mysql_error ());
		echo "Ein Datenbankfehler ist aufgetreten</div></body></html>";
		exit ();
	}

	$display_rows = min ($proseite, mysql_num_rows($res));


	echo "<div id=\"seite-links\">";

	echo '<table class="inhalt" cellspacing="0"><tr>'.$header;
	echo "<tr style=\"height:1px\"><td colspan=\"5\" ><img src=\"styles/sun/line_thin.gif\" style=\"height:1px\"></td></tr>\n";

	$count = 1;
	$index = 0;

	while ($row = mysql_fetch_array ($res))
	{
		if ($count == ($proseite/2 + 1))
		{
	/*		if ($page != 1)
				echo "<tr><td colspan=\"5\"><a href=\"statistik.php?f=".$f."&page=".($page-1)."\">Nach links bl&auml;ttern!</a></td></tr>"; */
			echo "</table></div>";
			echo "<div id=\"seite-rechts\">";
			echo '<table class="inhalt" cellspacing="0"><tr>'.$header;
			echo "<tr><td colspan=\"5\"><img src=\"styles/sun/line_thin.gif\"></td></tr>\n";
		}
		$count++;
		$index++;

		switch ($f)
		{
			case 1:
				if ($row['uid'] == $me->uid || $row['uid'] == $highlightID)
					echo "<tr class=\"rundum\" id=\"hervorhebung\" style=\"text-align:left\">";
				else
					echo "<tr class=\"rundum\" style=\"text-align:left\">";

				echo "<td>".$row['ranking']."</td><td width=\"120px\" style=\"text-align:left\"><a ";

				if ($row['inactivity'])
					echo "class=\"inactive\"";
				else
					echo "class=\"dark\"";

				echo " href=\"profile_extern.php?u=".$row['uid']."\">".$row['user']."</a></td><td style=\"text-align:center;\">";

				if ($row['tag'])
					echo $row['tag']."</td><td>".$row['score']."</td></tr><tr><td colspan=\"5\"><img src=\"styles/sun/line_thin.gif\"></td></tr>\n";
				else
					echo "&nbsp;</td><td>".$row['score']."</td></tr><tr><td colspan=\"6\"><img src=\"styles/sun/line_thin.gif\"></td></tr>\n";
				break;

			case 2:
				if ($row['uid'] == $me->uid || $row['uid'] == $highlightID)
					echo "<tr class=\"rundum\" id=\"hervorhebung\" style=\"text-align:left\">";
				else
					echo "<tr class=\"rundum\" style=\"text-align:left\">";

				echo "<td>".$row['ranking']."</td><td width=\"120px\" style=\"text-align:left\"><a ";

				if ($row['inactivity'])
					echo "class=\"inactive\"";
				else
					echo "class=\"dark\"";

				echo " href=\"profile_extern.php?u=".$row['uid']."\">".$row['user']."</a></td><td style=\"text-align:center;\">";

				if ($row['tag'])
					echo $row['tag']."</td><td  style=\"text-align:center\">".$row['score']."</td></tr><tr><td colspan=\"5\"><img src=\"styles/sun/line_thin.gif\"></td></tr>\n";
				else
					echo "&nbsp;</td><td style=\"text-align:center\">".$row['score']."</td></tr><tr><td colspan=\"6\"><img src=\"styles/sun/line_thin.gif\"></td></tr>\n";
				break;

			case 3:
				if ($row['aid'] == $me->aid || ($row['aid'] == $highlightID))
					echo "<tr id=\"hervorhebung\" style=\"text-align:left\">";
				else
					echo "<tr style=\"text-align:left\">";

				echo "<td>".$row['aranking']."</td><td width=\"70px\" style=\"text-align:left\"><a class=\"dark\" href=\"allianz.php?a=".$row['aid']."\">".$row['tag']."</a></td><td>".$row['ascore']."</td><td>".$row['players']."</td><td>".(int)($row['ascore']/$row['players'])."</td></tr><tr><td colspan=\"7\"><img src=\"styles/sun/line_thin.gif\"></td></tr>\n";
				break;


		 case 4:
				if ($row['aid'] == $me->aid || ($row['aid'] == $highlightID))
					echo "<tr id=\"hervorhebung\" style=\"text-align:left\">";
				else
					echo "<tr style=\"text-align:left\">";

				echo "<td>".$row['aranking']."</td><td width=\"70px\" style=\"text-align:left\"><a class=\"dark\" href=\"allianz.php?a=".$row['aid']."\">".$row['tag']."</a></td><td>".$row['score']."</td><td>".round($row['average'])."</td><td>".$row['lost']."/".$row['won']."</td></tr><tr><td colspan=\"7\"><img src=\"styles/sun/line_thin.gif\"></td></tr>\n";
				break;
		}
	}
	echo "</table></div></div>\n";
	echo "<div id=\"leiste-unten\">";
	// if (($count <= ($proseite/2 + 1)) && ($page != 1))
	if ($page != 1)
	{
		echo "<a href=\"stats_extern.php?w=".$worldname."&f=".$f."&page=".($page-1)."\"><img src=\"styles/sun/pfeil_lang_links.png\" alt=\"Nach links bl&auml;ttern!\"  title=\"Nach links bl&auml;ttern!\"></a>";

	}
	else
	{
		echo "<img src=\"styles/sun/pfeil_lang_links_grau.png\" alt=\"Nach links bl&auml;ttern!\" title=\"Nach links bl&auml;ttern!\">";
	}
	echo "&nbsp;&nbsp;&nbsp;&nbsp";
	if ($page != $max_pages)
			echo "<a href=\"stats_extern.php?w=".$worldname."&f=".$f."&page=".($page+1)."\"><img src=\"styles/sun/pfeil_lang_rechts.png\" alt=\"Nach rechts bl&auml;ttern!\" title=\"Nach rechts bl&auml;ttern!\"></a>";
	else
		echo "<img src=\"styles/sun/pfeil_lang_rechts_grau.png\" alt=\"Nach rechts bl&auml;ttern!\"  title=\"Nach rechts bl&auml;ttern!\">";
	if ($f == 1)
		$search = "Spieler";
	if ($f == 3 || $f == 4)
		$search = "Allianz";
	echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp";
	echo '<input type="hidden" name="w" value="'.$worldname.'">';
	echo "<b>".$search." suchen:</b>&nbsp;&nbsp;<input type=\"text\" size=\"15\" value=\"".$_POST['nameToSearch']."\" name=\"nameToSearch\">&nbsp;&nbsp;";
	echo "<b>&nbsp;&nbsp;oder Rang suchen:</b>&nbsp;&nbsp; <input type=\"text\" size=\"4\" name=\"rankToSearch\" value=\"".(int)$_POST['rankToSearch']."\">&nbsp;&nbsp;<input type=\"submit\" value=\"Los geht's!\" name=\"searchName\">";

	echo "</div></form>";

	//print_r($_SESSION);
	mysql_free_result ($res);
}
else
	echo "fail";
?>

</body></html>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.