Source

Selador / statistik.php

Full commit
  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
<?php

/* statistik.php - Front-end code for the in-game 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/>.
 **/

require_once("php/base/user.inc.php");
require_once("php/base.php");
require_once("php/event.php");
require_once("headers.php");

// 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

?>
<!--<body class="inhalt" onload="start()">-->

<?php

$page = 1;

$query_startpage = false;
$highlightID = 0;

function draw_register ($this_f, $f, $title)
{
	echo '<th><a href="statistik.php?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;


// Then calculate ressources through checking income - this takes into account
// past events, because event_handler might have updated $secspassed
// $me->activevillage->do_income ();

$me->activevillage->update_stamp ();


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


//if ($_REQUEST['f'] == 1)
{
?>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="/scripts/jquery.autocomplete.js"></script>
<link rel="stylesheet" href="/styles/jquery.autocomplete.css" type="text/css" />
<script type="text/javascript" src="/scripts/jquery.bgiframe.min.js"></script>
<script type="text/javascript">

$(document).ready(function() {
	<?php

	if ($f == 1 || $f == 2 )
		$query = 'select user as  element from user';
	else
		$query = 'select tag as element from allies';
	//javascript autocompletion
	//preload all users from the database and fill in the js data array
	if ($res = mysql_query($query)) {
		while ($row =mysql_fetch_array($res)) {
			if (isset($datastr))
				$datastr.=',';
			$datastr .= '"'.$row['element'].'"';
		}
	}

	?>
	data = [<?php echo $datastr; ?>].sort();

	$('#search').autocomplete(data);


	// Break out of frames. This facilitates the search working as expected
	if (top.location.href != location.href) {
	    top.location.href = location.href;
	}
});

</script>
<?php
}

// We include basic design parts here
require_once ("styles/php/base_design-".$_SESSION['theme'].".php");

?>
<div id="contentbox">
<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";
		$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
			$query_startpage = "select ((ranking-1)/".$proseite.")+1 from ranking where uid=".$me->uid;
		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";
				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";
					}
				}
			}
		}
		calcpage ($page);
 		$query = "select A.uid, A.user, A.inactivity, A.userflags, A.aid, round(A.score/1000) as score, B.name as race, C.tag, D.ranking from user A join races B on (A.race=B.raceid) left join allies C on (A.aid=C.aid) join ranking D on (A.uid=D.uid) order by D.ranking asc limit ".($proseite *($page-1)) .", ".$proseite;
 		$header = '<th class="bold">&nbsp;</th><th class="bold">Name</th><th class="bold">Ally</th><th class="bold">Punkte</th></tr>';
 		break;

	case 2: //Heldenstatistiken
		$query1 = "select count(uid) as number from hero_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
			$query_startpage = "select ((ranking-1)/".$proseite.")+1 from hero_ranking where uid=".$me->uid;
		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, A.inactivity, A.userflags, A.aid, round(A.xp/1000) as score, B.name as race, C.tag, D.ranking from user A join races B on (A.race=B.raceid) left join allies C on (A.aid=C.aid) join hero_ranking D on (A.uid=D.uid) order by D.ranking asc limit ".($proseite *($page-1)) .", ".$proseite;
 		$header = '<th class="bold">&nbsp;</th><th class="bold">Name</th><th class="bold">Ally</th><th class="bold">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 class="bold">&nbsp;</th><th class="bold">Ally</th><th class="bold">Punkte</th><th class="bold">Spieler</th><th class="bold">&oslash;</th></tr>';
 		break;


 	case 4: //Kriegsstatistiken
		$query1 = "select count(aid) as number from war_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 (ranking/".$proseite.")+1 from war_ranking where aid=".$me->aid;
		else //wurde nach einer Allianz gescucht?
		{
			if ("" != $_POST['nameToSearch'])
			$query_startpage = "select (R.ranking/".$proseite.")+1, A.aid from allies as A, war_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.ranking-1)/".$proseite.")+1, A.aid from allies as A, war_ranking as R where R.ranking=".(int)($_POST['rankToSearch'])." and R.aid=A.aid";
				}
			}
		}

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




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



echo "<div id=\"forschung\">\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="border"><tr>'.$header;

$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="border" cellspacing="0"><tr>'.$header;
	}
	$count++;
  $index++;
	if ($count % 2 == 0)
		$class = ' class="iterate"';
	else
		$class = '';
  switch ($f)
  {

  	case 1:
			if ($row['uid'] == $me->uid || $row['uid'] == $highlightID)
				echo "<tr  class=\"hervorhebung\" >";
			else
				echo "<tr  ".$class.">";

    	echo "<td>".$row['ranking']."</td><td width=\"130px\" style=\"text-align:left\"><a ";
    	echo " href=\"profile.php?u=".$row['uid']."\">";
    	echo style_username ($row['uid'], $row['user'], $row['inactivity'], $row['userflags'])."</a></td><td style=\"text-align:center;\">";

    	if ($row['tag'])
    		echo "<a href=\"allianz.php?a=".$row['aid']."\">".$row['tag']."</a></td><td>".$row['score']."</td></tr>\n";
    	else
    		echo "&nbsp;</td><td>".$row['score']."</td></tr>\n";
    	break;

  	case 2:
			if ($row['uid'] == $me->uid || $row['uid'] == $highlightID)
				echo "<tr class=\"hervorhebung\" >";
			else
				echo "<tr ".$class.">";

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

    	echo " href=\"profile.php?u=".$row['uid']."\">";
	echo style_username ($row['uid'], $row['user'], $row['inactivity'], $row['userflags'])."</a></td><td style=\"text-align:center;\">";

    	if ($row['tag'])
    		echo "<a href=\"allianz.php?a=".$row['aid']."\">".$row['tag']."</a></td><td  style=\"text-align:center\">".$row['score']."</td></tr>\n";
    	else
    		echo "&nbsp;</td><td style=\"text-align:center\">".$row['score']."</td></tr>\n";
    	break;

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

    	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>\n";
    	break;


	 case 4:
			if ($row['aid'] == $me->aid || ($row['aid'] == $highlightID))
				echo "<tr  class=\"hervorhebung\">";
			else
				echo "<tr ".$class.">";

    	echo "<td>".$row['ranking']."</td><td ><a class=\"dark\" href=\"allianz.php?a=".$row['aid']."\">".$row['tag']."</a></td><td>".$row['score']."</td><td>".round($row['average'])."</td><td>".$row['won']."</td><td>".$row['lost']."</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=\"statistik.php?f=".$f."&page=".($page-1)."\"><img src=\"styles/".$_SESSION['theme']."/pfeil_lang_links.png\" alt=\"Nach links bl&auml;ttern!\"  title=\"Nach links bl&auml;ttern!\"></a>";

}
else
{
	echo "<img src=\"styles/".$_SESSION['theme']."/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=\"statistik.php?f=".$f."&page=".($page+1)."\"><img src=\"styles/".$_SESSION['theme']."/pfeil_lang_rechts.png\" alt=\"Nach rechts bl&auml;ttern!\" title=\"Nach rechts bl&auml;ttern!\"></a>";
else
	echo "<img src=\"styles/".$_SESSION['theme']."/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 "<b>".$search." suchen:</b>&nbsp;&nbsp;<input type=\"text\" size=\"15\" id=\"search\" 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></div>";

mysql_free_result ($res);
require_once("footer.php");
?>