<?php

include($phpbb_root_path . 'rpg/function_rpg.'.$phpEx);

function GetList($listfield, $displayname)
{
	global $db;

	$htmllist = '';
	$sql = 'SELECT uname, ' . $listfield . ' FROM xoops_users ORDER BY 2 DESC LIMIT 0, 10';
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, '無法取得' . $displayname, '!!', __LINE__, __FILE__, $sql);
	}
	if ( $row = $db->sql_fetchrow($result) )
	{
		$i = 1;
		$htmllist = '<table class="forumline" width="90%" cellspacing="1" cellpadding="3" border="0">
			<tr><td align="center" class="catLeft"><b><span class="gen">#</span></b></td>
			<td align="center" class="catLeft"><b><span class="gen">會員名稱</span></b></td>
			<td align="center" class="catLeft"><b><span class="gen">' . $displayname. '</span></b></td></tr>';
		do
		{
			$htmllist .= '<tr><td class="row3" align="center"><span class="gensmall">' . $i .
					'</span></td><td><span class="gensmall">' . $row['uname'] .
					'</span></td><td align="right"><span class="gensmall">' . number_format($row[$listfield]) .
					'</span></td></tr>';
			$i++;
		}
		while ( $row = $db->sql_fetchrow($result) );
		$htmllist .= '</table>';
	}
	return $htmllist;
}


if ( !defined('IN_PHPBB') )
{
	die("Hacking attempt");
	exit;
}

$returnurl = '<br /><br /><a href="rpg.php?action=stat">返回上一頁</a>';

$template->set_filenames(array(
	'menu' => 'rpg_header.tpl',
	'body' => 'rpg_stat.tpl')
);

// 取得個人資料
$profiledata = get_userdata(intval($userdata['uid']));
$itemerror = '';

if ( CheckRefresh(99, intval($userdata['uid']), 10) )
{
	message_die(GENERAL_MESSAGE, '為了避免系統負荷過重，請間隔 10 秒再重新整理!!' . $returnurl);
}



$cashlist = GetList('user_money', '持有現金');
$banklist = GetList('user_bank', '銀行存款');
$robcount = GetList('rpg_rob_cnt', '搶劫次數');
$robbedcount = GetList('rpg_robbed_cnt', '被搶次數');
$robsucc = GetList('rpg_rob_succ', '成功次數');
$robfail = GetList('rpg_rob_fail', '失敗次數');
$robmoney = GetList('rpg_rob_money', '搶劫金額');
$robbedmoney = GetList('rpg_robbed_money', '被搶金額');
$dollcount = GetList('user_robfail', '詛咒草人');
$eol_kill = GetList('rpg_eol_kill', '砍人次數');
$eol_killed = GetList('rpg_eol_killed', '陣亡次數');

// 最近成功
$eventsucc = '';
$sql = 'SELECT event_time, event_money, event_note FROM xoops_pbb_rpg_event
	WHERE event_class <= 1 AND event_type = 1
	GROUP BY event_time, event_money, event_note
	ORDER BY event_time DESC
	LIMIT 0, 10';
if ( !($result = $db->sql_query($sql)) )
{
	message_die(GENERAL_ERROR, '無法取得最近搶劫成功列表!!', __LINE__, __FILE__, $sql);
}
if ( $row = $db->sql_fetchrow($result) )
{
	$i = 1;
	$eventsucc = '<table class="forumline" width="95%" cellspacing="1" cellpadding="3" border="0">
			<tr><td align="center" class="catLeft"><b><span class="gen">#</span></b></td>
			<td align="center" class="catLeft"><b><span class="gen">時間</span></b></td>
			<td align="center" class="catLeft"><b><span class="gen">每人所得</span></b></td>
			<td align="center" class="catLeft"><b><span class="gen">最近搶劫成功事件</span></b></td></tr>';
	do
	{
		$eventsucc .= '<tr><td class="row3" align="center"><span class="gensmall">' . $i .
				'</span></td><td align="center"><span class="gensmall">' .
				($row['event_time'] ? create_date($userdata['user_dateformat'], $row['event_time'], $board_config['board_timezone']) : $lang['Never_last_logon']) .
				'</span></td><td align="right"><span class="gensmall">' . number_format($row['event_money']) .
				'</span></td><td><span class="gensmall">' . $row['event_note'] .
				'</span></td></tr>';
		$i++;
	}
	while ( $row = $db->sql_fetchrow($result) );
	$eventsucc .= '</table>';
}

// 最近失敗
$eventfail = '';
$sql = 'SELECT event_time, event_money, event_note FROM xoops_pbb_rpg_event
	WHERE event_class <= 1 AND event_type = 0
	GROUP BY event_time, event_money, event_note
	ORDER BY event_time DESC
	LIMIT 0, 10';
if ( !($result = $db->sql_query($sql)) )
{
	message_die(GENERAL_ERROR, '無法取得最近搶劫失敗列表!!', __LINE__, __FILE__, $sql);
}
if ( $row = $db->sql_fetchrow($result) )
{
	$i = 1;
	$eventfail = '<table class="forumline" width="95%" cellspacing="1" cellpadding="3" border="0">
			<tr><td align="center" class="catLeft"><b><span class="gen">#</span></b></td>
			<td align="center" class="catLeft"><b><span class="gen">時間</span></b></td>
			<td align="center" class="catLeft"><b><span class="gen">最近搶劫失敗事件</span></b></td></tr>';
	do
	{
		$eventfail .= '<tr><td class="row3" align="center"><span class="gensmall">' . $i .
				'</span></td><td align="center"><span class="gensmall">' .
				($row['event_time'] ? create_date($userdata['user_dateformat'], $row['event_time'], $board_config['board_timezone']) : $lang['Never_last_logon']) .
				'</span></td><td><span class="gensmall">' . $row['event_note'] .
				'</span></td></tr>';
		$i++;
	}
	while ( $row = $db->sql_fetchrow($result) );
	$eventfail .= '</table>';
}

// 便便數量
$bosscount = '';
$sql = 'select u.uname, count(*) as itemcount from xoops_pbb_rpg_owns o, xoops_users u
	where o.item_id = 105 and o.uid = u.uid group by u.uname order by 2 desc limit 0, 10';
if ( !($result = $db->sql_query($sql)) )
{
	message_die(GENERAL_ERROR, '無法取得便便列表!!', __LINE__, __FILE__, $sql);
}
if ( $row = $db->sql_fetchrow($result) )
{
	$i = 1;
	$bosscount = '<table class="forumline" width="90%" cellspacing="1" cellpadding="3" border="0">
		<tr><td align="center" class="catLeft"><b><span class="gen">#</span></b></td>
		<td align="center" class="catLeft"><b><span class="gen">會員名稱</span></b></td>
		<td align="center" class="catLeft"><b><span class="gen">祝福能量</span></b></td></tr>';
	do
	{
		$bosscount .= '<tr><td class="row3" align="center"><span class="gensmall">' . $i .
				'</span></td><td><span class="gensmall">' . $row['uname'] .
				'</span></td><td align="right"><span class="gensmall">' . $row['itemcount'] .
				'</span></td></tr>';
		$i++;
	}
	while ( $row = $db->sql_fetchrow($result) );
	$bosscount .= '</table>';
}

// 僱主人數
/*
$bosscount = '';
$sql = 'SELECT u.uname, COUNT(b.to_id) as boss_count FROM xoops_pbb_rpg_bodyguard b, xoops_users u
	WHERE b.yes_or_no = 1 AND b.to_id = u.uid AND b.invite_time > ' . (time() - 86400 * 3) .
	' GROUP BY u.uname ORDER BY 2 DESC LIMIT 0, 10';
if ( !($result = $db->sql_query($sql)) )
{
	message_die(GENERAL_ERROR, '無法取得僱主人數列表!!', __LINE__, __FILE__, $sql);
}
if ( $row = $db->sql_fetchrow($result) )
{
	$i = 1;
	$bosscount = '<table class="forumline" width="90%" cellspacing="1" cellpadding="3" border="0">
		<tr><td align="center" class="catLeft"><b><span class="gen">#</span></b></td>
		<td align="center" class="catLeft"><b><span class="gen">會員名稱</span></b></td>
		<td align="center" class="catLeft"><b><span class="gen">僱主人數</span></b></td></tr>';
	do
	{
		$bosscount .= '<tr><td class="row3" align="center"><span class="gensmall">' . $i .
				'</span></td><td><span class="gensmall">' . $row['uname'] .
				'</span></td><td align="right"><span class="gensmall">' . $row['boss_count'] .
				'</span></td></tr>';
		$i++;
	}
	while ( $row = $db->sql_fetchrow($result) );
	$bosscount .= '</table>';
}
*/

// 保鑣薪資
$bgsalary = '';
$sql = 'SELECT u.uname, SUM(b.bodyguard_salary) as bg_salary FROM xoops_pbb_rpg_bodyguard b, xoops_users u
	WHERE b.yes_or_no = 1 AND b.to_id = u.uid AND b.invite_time > ' . (time() - 86400 * 3) .
	' GROUP BY u.uname ORDER BY 2 DESC LIMIT 0, 10';
if ( !($result = $db->sql_query($sql)) )
{
	message_die(GENERAL_ERROR, '無法取得保鑣薪資列表!!', __LINE__, __FILE__, $sql);
}
if ( $row = $db->sql_fetchrow($result) )
{
	$i = 1;
	$bgsalary = '<table class="forumline" width="90%" cellspacing="1" cellpadding="3" border="0">
		<tr><td align="center" class="catLeft"><b><span class="gen">#</span></b></td>
		<td align="center" class="catLeft"><b><span class="gen">會員名稱</span></b></td>
		<td align="center" class="catLeft"><b><span class="gen">保鑣薪資總額</span></b></td></tr>';
	do
	{
		$bgsalary .= '<tr><td class="row3" align="center"><span class="gensmall">' . $i .
				'</span></td><td><span class="gensmall">' . $row['uname'] .
				'</span></td><td align="right"><span class="gensmall">' . $row['bg_salary'] .
				'</span></td></tr>';
		$i++;
	}
	while ( $row = $db->sql_fetchrow($result) );
	$bgsalary .= '</table>';
}

// 產生資料頁面
$page_title = "水怪大陸 RPG 首頁";
include($phpbb_root_path . 'includes/page_header.'.$phpEx);

$template->assign_vars(array(
	'USERNAME' => $profiledata['uname'],
	'AVATAR_IMG' => GetAvatar($profiledata),
	'RPG_CASH_LIST' => $cashlist,
	'RPG_BANK_LIST' => $banklist,
	'RPG_ROB_COUNT' => $robcount,
	'RPG_ROBBED_COUNT' => $robbedcount,
	'RPG_ROB_SUCC' => $robsucc,
	'RPG_ROB_FAIL' => $robfail,
	'RPG_ROB_MONEY' => $robmoney,
	'RPG_ROBBED_MONEY' => $robbedmoney,
	'RPG_DOLL_COUNT' => $dollcount,
	'RPG_EVENT_SUCC' => $eventsucc,
	'RPG_EVENT_FAIL' => $eventfail,
	'RPG_BOSS_COUNT' => $bosscount,
	'RPG_BG_SALARY' => $bgsalary,
	'RPG_EOL_KILL' => $eol_kill,
	'RPG_EOL_KILLED' => $eol_killed,
	'L_NOWMENU' => 'ＲＰＧ英雄榜') );

$template->pparse('menu');
$template->pparse('body');

include($phpbb_root_path . 'includes/page_tail.'.$phpEx);

?>