<?php

define('BODYGUARD_COUNT', 2);
define('BOSS_COUNT', 2);

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

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

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

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

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

$rpg_rob_result = "";

// 取消搶劫
if ( isset($HTTP_POST_VARS['cancel2rob']) )
{
	$sql = "DELETE FROM xoops_pbb_rpg_rob WHERE attack_target <> -1 AND task_over = 0 AND attack_id = " . intval($userdata['uid']);
	if( !$db->sql_query($sql) )
	{
		message_die(GENERAL_ERROR, "無法取消搶劫動作!!", "", __LINE__, __FILE__, $sql);
	}
}

// 取消未確認的保鑣
if ( isset($HTTP_POST_VARS['ClearNotConfirm']) )
{
	$sql = 'DELETE FROM xoops_pbb_rpg_bodyguard WHERE yes_or_no = 0 AND from_id = ' . intval($userdata['uid']);
	if( !$db->sql_query($sql) )
	{
		message_die(GENERAL_ERROR, '無法取消未確認的保鑣!!', '', __LINE__, __FILE__, $sql);
	}
}

// 清除記錄
if ( isset($HTTP_POST_VARS['clear2history']) )
{
	$sql = "DELETE FROM xoops_pbb_rpg_event WHERE event_class <= 2 AND uid = " . intval($userdata['uid']);
	if( !$db->sql_query($sql) )
	{
		message_die(GENERAL_ERROR, "無法清除搶劫記錄!!", "", __LINE__, __FILE__, $sql);
	}

}

// 取得水怪情報
$information = "";
if ( isset($HTTP_POST_VARS['getinformation']) )
{
	if ($profiledata['user_money'] < 500)
	{
		message_die(GENERAL_MESSAGE, '現金不足不足，無法繳交 500 元打聽情報!!' . $returnurl);
	}
	$sql = "UPDATE " . CONFIG_TABLE . " SET config_value = config_value + 500 WHERE config_name = 'rpg_jackpot'";
	if( !$db->sql_query($sql) )
	{
		message_die(GENERAL_ERROR, "無法更新頭彩獎金!!", "", __LINE__, __FILE__, $sql);
	}
	$sql = "UPDATE " . USERS_TABLE . " SET user_money = user_money - 500 WHERE uid = " . intval($userdata['uid']);
	if( !$db->sql_query($sql) )
	{
		message_die(GENERAL_ERROR, "無法更新會員的貨幣!!", "", __LINE__, __FILE__, $sql);
	}
	$tomonster = $HTTP_POST_VARS['tomonster'];
	$profiledata['user_money'] -= 500;
	$sql = "SELECT uid, posts, user_money, user_bank, user_robfail FROM " . USERS_TABLE . " WHERE uname = '" . str_replace("\'", "''", $tomonster) . "' AND uid <> " . ANONYMOUS;
	if( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, '找不到欲查詢的會員!!', '', __LINE__, __FILE__, $sql);
	}
	// 基本資料
	$defense_point = 0;
	$attack_point = 0;
	$monstermoney = 0;
	$monsterbank = 0;
	$monsterrob = 0;
	if ( $row = $db->sql_fetchrow($result) )
	{
		$tomonsterid = $row['uid'];
		$defense_point = $row['posts'];
		if ($defense_point == 0)
		{
			$defense_point = 1;
		}
		$monstermoney = $row['user_money'];
		$monsterbank = $row['user_bank'];
		$monsterrob = $row['user_robfail'];
	}
	$information = '<table class="forumline" width="40%" 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></tr>
		<tr><td><span class="gensmall">搶劫對象</span></td><td><span class="gensmall">' . $tomonster . '</span></td></tr>
		<tr><td><span class="gensmall">現　　金</span></td><td align="right"><span class="gensmall">' . number_format($monstermoney) . '</span></td></tr>
		<tr><td><span class="gensmall">存　　款</span></td><td align="right"><span class="gensmall">' . number_format($monsterbank) . '</span></td></tr>';
	if ($monsterrob > 0)
	{
		$information .= '<tr><td><span class="gensmall">草人詛咒</span></td><td align="center"><span class="gensmall">';
		$robid = floor(($monsterrob - 1) / 4);
		$robcnt = (($monsterrob - 1) % 4) + 1;
		if ($robid <= 7)
		{
			for ($i=0; $i < $robcnt; $i++)
			{
				$information .= '<img src="images/rob' . $robid . '.gif" />';
			}
		}
		else
		{
				$information .= '<img src="images/rob7.gif" />× ' . ($monsterrob - 28);
		}
		$information .= '(' . $monsterrob . ')</span></td></tr>';
	}
	$information .= '<tr><td><span class="gensmall">防禦指數</span></td><td align="right"><span class="gensmall">' .
			$defense_point . '</span></td></tr>';
	$defense_point *= 5;
	$sql = 'SELECT u.uname, u.posts, u.user_robfail FROM xoops_pbb_rpg_bodyguard b, xoops_users u
		WHERE b.to_id = u.uid AND b.yes_or_no = 1 AND b.invite_time >= ' . (time() - 86400 * 3) .
		' AND b.from_id = ' . $tomonsterid;
	if( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, "無法取得防禦資料!!", "", __LINE__, __FILE__, $sql);
	}
	if ( $row = $db->sql_fetchrow($result) )
	{
		$information .= '<tr><td><span class="gensmall">僱用保鑣</span></td><td><span class="gensmall">';
		do
		{
			$information .= $row['uname'] . " (" . $row['posts'] . " -" . $row['user_robfail'] . "%)<br />";
			$defense_point += $row['posts'];
			$monsterrob += $row['user_robfail'];
		}
		while ( $row = $db->sql_fetchrow($result) );
		$information .= "</span></td><tr>";
	}
	$information .= '<tr><td class="row3"><span class="gensmall">總防禦指數</span></td><td class="row3" align="right"><span class="gensmall">' .
			$defense_point . ' (-' . $monsterrob . '%)</span></td></tr>';
	$sql = "SELECT u.uname, u.posts, u.user_robfail FROM xoops_users u, xoops_pbb_rpg_rob r
		WHERE u.uid = r.attack_id AND r.attack_target = " . $tomonsterid . " AND task_over = 0";
	if( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, "無法取得攻擊資料!!", "", __LINE__, __FILE__, $sql);
	}
	$attrob = 0;
	if ( $row = $db->sql_fetchrow($result) )
	{
		$information .= '<tr><td><span class="gensmall">目前搶匪</span></td><td><span class="gensmall">';
		do
		{
			$information .= $row['uname'] . " (" . $row['posts'] . " -" . $row['user_robfail'] . "%)<br />";
			$attack_point += $row['posts'];
			$monsterrob -= $row['user_robfail'];
			$attrob += $row['user_robfail'];
		}
		while ( $row = $db->sql_fetchrow($result) );
		$information .= "</span></td><tr>";
	}
	$information .= '<tr><td class="row3"><span class="gensmall">總攻擊指數</span></td><td class="row3" align="right"><span class="gensmall">' .
			$attack_point . ' (-' .	$attrob . '%)</span></td></tr>';
	$rob_ok = floor($attack_point * 100 / $defense_point);
	if ($rob_ok > 80)
	{
		$rob_ok = 80;
	}
	else if ($rob_ok < 3)
	{
		$rob_ok = 3;
	}
	$rob_ok += $monsterrob;
	$information .= '<tr><td class="row3"><span class="gensmall">預估搶劫成功率至少</span></td><td class="row3" align="right"><span class="gensmall">' .
			$rob_ok . '%</span></td></tr></table>';
}

// 搶劫銀行
if ( isset($HTTP_POST_VARS['rob2bank']) )
{
	if ($profiledata['user_money'] < 500)
	{
		message_die(GENERAL_MESSAGE, '現金不足，無法繳交 500 元規費!!' . $returnurl);
	}
	$sql = "UPDATE " . CONFIG_TABLE . " SET config_value = config_value + 500 WHERE config_name = 'rpg_jackpot'";
	if( !$db->sql_query($sql) )
	{
		message_die(GENERAL_ERROR, "無法更新頭彩獎金!!", "", __LINE__, __FILE__, $sql);
	}
	$sql = "UPDATE " . USERS_TABLE . " SET user_money = user_money - 500 WHERE uid = " . intval($userdata['uid']);
	if( !$db->sql_query($sql) )
	{
		message_die(GENERAL_ERROR, "無法更新會員的貨幣!!", "", __LINE__, __FILE__, $sql);
	}
	$sql = 'SELECT max(join_time) AS join_time FROM xoops_pbb_rpg_rob WHERE attack_target = -1 AND attack_id = ' . intval($userdata['uid']);
	if( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, '無法判斷是否可以搶劫銀行!!', '', __LINE__, __FILE__, $sql);
	}
	if ( $row = $db->sql_fetchrow($result) )
	{
		if ($row['join_time'] >= (time() - 86400 * 1))
		{
			message_die(GENERAL_MESSAGE, '嘿嘿嘿....按上一頁回去偷搶是不行的喲!!' . $returnurl);
		}
		else
		{
			$sql = 'SELECT count(*) AS non_start FROM xoops_pbb_rpg_rob WHERE attack_target = -1 AND task_over = 0 AND attack_id = ' . intval($userdata['uid']);
			if( !($result = $db->sql_query($sql)) )
			{
				message_die(GENERAL_ERROR, '無法判斷是否可以搶劫銀行!!', '', __LINE__, __FILE__, $sql);
			}
			if ( $row = $db->sql_fetchrow($result) )
			{
				if ($row['non_start'] > 0)
				{
					message_die(GENERAL_MESSAGE, '嘿嘿嘿....按上一頁回去偷搶是不行的喲!!' . $returnurl);
				}
			}
		}
	}

	$profiledata['user_money'] -= 500;
	$sql = 'INSERT INTO xoops_pbb_rpg_rob VALUES (-1, "銀行", ' . intval($userdata['uid']) . ', "' . $profiledata['uname'] . '", ' . time() . ', 0)';
	if( !$db->sql_query($sql) )
	{
		message_die(GENERAL_ERROR, "無法更新搶劫列表!!", "", __LINE__, __FILE__, $sql);
	}
	$sql = "SELECT COUNT(*) AS rob_count FROM xoops_pbb_rpg_rob WHERE attack_target = -1 AND task_over = 0";
	if( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, "無法計算銀行搶匪人數!!", "", __LINE__, __FILE__, $sql);
	}
	if ( $row = $db->sql_fetchrow($result) )
	{
		if ($row['rob_count'] >= 10)
		{
			$sql = "SELECT attack_id, attack_name FROM xoops_pbb_rpg_rob WHERE attack_target = -1 AND task_over = 0";
			if( !($result = $db->sql_query($sql)) )
			{
				message_die(GENERAL_ERROR, "無法取得銀行搶匪資料!!", "", __LINE__, __FILE__, $sql);
			}
			$robbers = array();
			$robnames = '';
			if ( $row = $db->sql_fetchrow($result) )
			{
				do
				{
					$robbers[] = $row['attack_id'];
					$robnames .= ' ' . $row['attack_name'];
				}
				while ( $row = $db->sql_fetchrow($result) );
			}
			$robbersql = implode(', ', $robbers);
			srand((double)microtime()*1000000);
			$randid = rand(1, 100);
			$robnames = '結果值: ' . $randid . ', 搶匪:' . $robnames;
			if ($randid <= 3)
			{
				// 搶劫成功
				$sql = "SELECT SUM(user_bank) AS bank_amount FROM xoops_users";
				if( !($result = $db->sql_query($sql)) )
				{
					message_die(GENERAL_ERROR, "無法取得銀行總值!!", "", __LINE__, __FILE__, $sql);
				}
				$row = $db->sql_fetchrow($result);
				$bankamount = $row['bank_amount'];
				$sql = 'UPDATE xoops_users SET rpg_rob_cnt = rpg_rob_cnt + 1, rpg_rob_succ = rpg_rob_succ + 1, ' .
					'rpg_rob_money = rpg_rob_money + ' . (floor($bankamount / count($robbers))) .
					', user_money = user_money + ' . (floor($bankamount / count($robbers))) .
					' WHERE uid in (' . $robbersql . ')';
				if( !$db->sql_query($sql) )
				{
					message_die(GENERAL_ERROR, "無法更新搶劫記錄!", "", __LINE__, __FILE__, $sql);
				}
				$sql = 'INSERT INTO xoops_pbb_rpg_event SELECT attack_id AS uid, ' . time() .
					' AS event_time, 0 AS event_class, 1 AS event_type, ' . (floor($bankamount / count($robbers))) .
					' AS event_money, "搶銀行得逞!! ' . $robnames . '" AS event_note ' .
					' FROM xoops_pbb_rpg_rob WHERE attack_target = -1 AND task_over = 0';
				if( !$db->sql_query($sql) )
				{
					message_die(GENERAL_ERROR, "無法更新搶劫記錄檔!!", "", __LINE__, __FILE__, $sql);
				}
				$sql = 'UPDATE xoops_users SET user_bank = FLOOR(user_bank / 2)';
				if( !$db->sql_query($sql) )
				{
					message_die(GENERAL_ERROR, "無法更新銀行存款!!", "", __LINE__, __FILE__, $sql);
				}
			}
			else
			{
				// 搶劫失敗
				$sql = 'UPDATE xoops_users SET user_robfail = user_robfail + 1, rpg_rob_cnt = rpg_rob_cnt + 1, rpg_rob_fail = rpg_rob_fail + 1 WHERE uid in (' . $robbersql . ')';
				if( !$db->sql_query($sql) )
				{
					message_die(GENERAL_ERROR, "無法更新記錄!!", "", __LINE__, __FILE__, $sql);
				}
				$sql = 'INSERT INTO xoops_pbb_rpg_event SELECT attack_id AS uid, ' . time() .
					' AS event_time, 0 AS event_class, 0 AS event_type, 0 AS event_money, "搶銀行失敗, ' .
					$robnames . '" AS event_note FROM xoops_pbb_rpg_rob WHERE attack_target = -1 AND task_over = 0';
				if( !$db->sql_query($sql) )
				{
					message_die(GENERAL_ERROR, "無法更新搶劫記錄檔!!", "", __LINE__, __FILE__, $sql);
				}
				$profiledata['user_robfail'] += 1;
				// 如果搶匪持有特殊道具 - 新匪上任一把火
				$sql = 'SELECT uid, min(own_id) as own_id FROM xoops_pbb_rpg_owns WHERE item_id = 99 AND uid in (' . $robbersql . ') GROUP BY uid';
				if ( $result = $db->sql_query($sql) )
				{
					if ( $row = $db->sql_fetchrow($result) )
					{
						do
						{
							if ($row['uid'] == intval($userdata['uid']))
							{
								$profiledata['user_robfail'] -= 1;
							}
							$sql = 'UPDATE xoops_users SET user_robfail = user_robfail - 1 WHERE user_robfail > 0 AND uid = ' . $row['uid'];
							$db->sql_query($sql);
							$sql = 'DELETE FROM xoops_pbb_rpg_owns WHERE own_id = ' . $row['own_id'];
							$db->sql_query($sql);
						}
						while ( $row = $db->sql_fetchrow($result) );
					}
				}
			}
			$sql = 'UPDATE xoops_pbb_rpg_rob SET task_over = 1, join_time = ' . time() . ' WHERE attack_target = -1 AND task_over = 0';
			if( !$db->sql_query($sql) )
			{
				message_die(GENERAL_ERROR, "無法更新搶劫列表!!", "", __LINE__, __FILE__, $sql);
			}
		}
	}
}

// 搶劫會員
if ( isset($HTTP_POST_VARS['rob2monster']) )
{
	if ( intval($profiledata['posts']) <= 100 )
	{
		$i = 5;
	}
	else
	{
		$i = floor((intval($profiledata['posts']) - 100) / 50) + 6;
	}
	$i += floor( intval($profiledata['user_money']) / 10000 );
	if ($i < intval($profiledata['user_robfail']))
	{
		message_die(GENERAL_MESSAGE, '您的草人太多了(' . $profiledata['user_robfail'] . '), 詛咒太重了，不能參加搶劫!!<br /><br />可攜帶草人數: 100 篇文章以下 5 隻，每 50 篇加 1 隻，每 10000 元現金可再加1隻 (' . $i . ')' . $returnurl);
	}

	$tomonster = $HTTP_POST_VARS['tomonster'];
	if ($profiledata['user_money'] < 100)
	{
		message_die(GENERAL_MESSAGE, '現金不足，無法繳交 100 元規費!!' . $returnurl);
	}
	$sql = "UPDATE " . CONFIG_TABLE . " SET config_value = config_value + 100 WHERE config_name = 'rpg_jackpot'";
	if( !$db->sql_query($sql) )
	{
		message_die(GENERAL_ERROR, "無法更新頭彩獎金!!", "", __LINE__, __FILE__, $sql);
	}
	$sql = "UPDATE " . USERS_TABLE . " SET user_money = user_money - 100 WHERE uid = " . intval($userdata['uid']);
	if( !$db->sql_query($sql) )
	{
		message_die(GENERAL_ERROR, "無法更新會員的貨幣!!", "", __LINE__, __FILE__, $sql);
	}
	$profiledata['user_money'] -= 100;
	$defense_point = 0;
	$monsterrob = 0;
	$monstermoney = 0;
	$monsterbank = 0;
	$sql = "SELECT uid, posts, user_money, user_bank, user_robfail FROM " . USERS_TABLE . " WHERE uname = '" . str_replace("\'", "''", $tomonster) . "' AND uid <> " . ANONYMOUS;
	if( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, '找不到欲搶劫的會員', '', __LINE__, __FILE__, $sql);
	}
	if ( $row = $db->sql_fetchrow($result) )
	{
		$tomonsterid = $row['uid'];
		$defense_point = $row['posts'] * 5;
		if ($defense_point == 0)
		{
			$defense_point = 1;
		}
		$monstermoney = $row['user_money'];
		$monsterrob = $row['user_robfail'];
		if ($monsterrob < 50)
		{
			$monsterbank = floor($row['user_bank'] * $monsterrob * 2 / 100);
		}
		else
		{
			$monsterbank = $row['user_bank'];
		}
	}
	$sql = "SELECT count(*) AS bosscount FROM xoops_pbb_rpg_bodyguard WHERE yes_or_no = 1 AND from_id = " . $tomonsterid .
		" AND invite_time >= " . (time() - 86400 * 3) . " AND to_id = " . intval($userdata['uid']);
	if( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, '無法判斷被搶的會員是否為僱主!!', '', __LINE__, __FILE__, $sql);
	}
	if ( $row = $db->sql_fetchrow($result) )
	{
		if ($row['bosscount'] > 0)
		{
			if (($profiledata['user_money'] + $profiledata['user_bank']) <= 1000)
			{
				$lostmoney = $profiledata['user_money'] + $profiledata['user_bank'];
				$sql = 'UPDATE xoops_users SET user_money = 0, user_bank = 0 WHERE uid = ' . intval($userdata['uid']);
			}
			else if ($profiledata['user_money'] >= $profiledata['user_bank'])
			{
				if ($profiledata['user_money'] >= 10000)
				{
					$lostmoney = floor($profiledata['user_money'] / 10);
					$sql = 'UPDATE xoops_users SET user_money = user_money - ' . $lostmoney . ' WHERE uid = ' . intval($userdata['uid']);
				}
				else if ($profiledata['user_money'] >= 1000)
				{
					$lostmoney = 1000;
					$sql = 'UPDATE xoops_users SET user_money = user_money - 1000 WHERE uid = ' . intval($userdata['uid']);
				}
				else
				{
					$sql = 'UPDATE xoops_users SET user_money = user_money - ' . $profiledata['user_money'] .
					', user_bank = user_bank - (1000 - ' . $profiledata['user_money'] . ') WHERE uid = ' . intval($userdata['uid']);
					$lostmoney = 1000;
				}
			}
			else
			{
				if ($profiledata['user_bank'] >= 10000)
				{
					$lostmoney = floor($profiledata['user_bank'] / 10);
					$sql = 'UPDATE xoops_users SET user_bank = user_bank - ' . $lostmoney . ' WHERE uid = ' . intval($userdata['uid']);
				}
				else if ($profiledata['user_bank'] >= 1000)
				{
					$lostmoney = 1000;
					$sql = 'UPDATE xoops_users SET user_bank = user_bank - 1000 WHERE uid = ' . intval($userdata['uid']);
				}
				else
				{
					$sql = 'UPDATE xoops_users SET user_bank = user_bank - ' . $profiledata['user_bank'] .
					', user_money = user_money - (1000 - ' . $profiledata['user_bank'] . ') WHERE uid = ' . intval($userdata['uid']);
					$lostmoney = 1000;
				}
			}
			if( !$db->sql_query($sql) )
			{
				message_die(GENERAL_ERROR, "不顧江湖道義的處罰意外失敗!!", "", __LINE__, __FILE__, $sql);
			}
			$sql = "UPDATE " . CONFIG_TABLE . " SET config_value = config_value + " . $lostmoney. " WHERE config_name = 'rpg_jackpot'";
			if( !$db->sql_query($sql) )
			{
				message_die(GENERAL_ERROR, "無法更新頭彩獎金!!", "", __LINE__, __FILE__, $sql);
			}
			message_die(GENERAL_MESSAGE, "不顧江湖道義，居然想搶僱主的錢!!沒收您 " . $lostmoney . " 元!!" . $returnurl);
		}
	}

	$sql = 'SELECT max(join_time) AS max_time FROM xoops_pbb_rpg_rob WHERE attack_target <> -1 AND attack_id = ' . intval($userdata['uid']);
	if( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, '無法判斷是否可以搶劫其他會員!!', '', __LINE__, __FILE__, $sql);
	}
	if ( $row = $db->sql_fetchrow($result) )
	{
		// 如果在二小時內不能搶劫
		if ($row['max_time'] >= (time() - 86400 / 12))
		{
			message_die(GENERAL_MESSAGE, '您必須等到 ' . (create_date($userdata['user_dateformat'], ($row['max_time'] + 86400 / 12), $board_config['board_timezone'])) . ' 才可以搶劫其他會員!!' . $returnurl);
		}
		/*
		else
		{
			$sql = 'SELECT count(*) AS non_start FROM xoops_pbb_rpg_rob WHERE attack_target <> -1 AND task_over = 0 AND attack_id = ' . intval($userdata['uid']);
			if( !($result = $db->sql_query($sql)) )
			{
				message_die(GENERAL_ERROR, '無法判斷是否可以搶劫銀行!!', '', __LINE__, __FILE__, $sql);
			}
			if ( $row = $db->sql_fetchrow($result) )
			{
				if ($row['non_start'] > 0)
				{
					message_die(GENERAL_MESSAGE, '您必須等到銀行搶匪集團成軍 2 小時後，才可以搶劫!!' . $returnurl);
				}
			}
		}
		*/
	}
	$sql = 'SELECT max(join_time) AS max_time FROM xoops_pbb_rpg_rob WHERE attack_target = ' . $tomonsterid . ' AND attack_id = ' . intval($userdata['uid']);
	if( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, '無法判斷是否可以搶劫這位會員!!', '', __LINE__, __FILE__, $sql);
	}
	if ( $row = $db->sql_fetchrow($result) )
	{
		if ($row['max_time'] >= (time() - 86400 / 2))
		{
			message_die(GENERAL_MESSAGE, '您必須等到 ' . (create_date($userdata['user_dateformat'], ($row['max_time'] + 86400 / 2), $board_config['board_timezone'])) . ' 才可以搶劫這位會員!!' . $returnurl);
		}
	}


	$sql = 'INSERT INTO xoops_pbb_rpg_rob VALUES (' . $tomonsterid . ', "' . str_replace("\'", "''", $tomonster) .
		'", ' . intval($userdata['uid']) . ', "' . $profiledata['uname'] . '", ' . time() . ', 0)';
	if( !$db->sql_query($sql) )
	{
		message_die(GENERAL_ERROR, "無法更新搶劫列表!!", "", __LINE__, __FILE__, $sql);
	}
	$sql = "SELECT COUNT(*) AS rob_count FROM xoops_pbb_rpg_rob WHERE attack_target = " . $tomonsterid . " AND task_over = 0";
	if( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, "無法計算搶匪人數!!", "", __LINE__, __FILE__, $sql);
	}
	if ( $row = $db->sql_fetchrow($result) )
	{
		if ($row['rob_count'] >= 5)
		{
			$sql = "SELECT attack_id, attack_name FROM xoops_pbb_rpg_rob WHERE attack_target = " . $tomonsterid . " AND task_over = 0";
			if( !($result = $db->sql_query($sql)) )
			{
				message_die(GENERAL_ERROR, "無法取得搶匪資料!!", "", __LINE__, __FILE__, $sql);
			}
			$robbers = array();
			$robnames = '';
			if ( $row = $db->sql_fetchrow($result) )
			{
				do
				{
					$robbers[] = $row['attack_id'];
					$robnames .= ' ' . $row['attack_name'];
				}
				while ( $row = $db->sql_fetchrow($result) );
			}
			$robbersql = implode(', ', $robbers);
			$attack_point = 0;
			$attack_rob = 0;
			$sql = 'SELECT SUM(posts) AS att_point, SUM(user_robfail) as att_robfail FROM xoops_users WHERE uid IN (' . $robbersql . ')';
			if( !($result = $db->sql_query($sql)) )
			{
				message_die(GENERAL_ERROR, "無法取得攻擊資料!!", "", __LINE__, __FILE__, $sql);
			}
			if ( $row = $db->sql_fetchrow($result) )
			{
				$attack_point = $row['att_point'];
				$attack_rob = $row['att_robfail'];
			}
			$sql = 'SELECT SUM(u.posts) AS def_point, SUM(u.user_robfail) AS def_robfail FROM xoops_pbb_rpg_bodyguard b, xoops_users u
				WHERE b.to_id = u.uid AND b.yes_or_no = 1 AND b.invite_time >= ' . (time() - 86400 * 3) .
				' AND b.from_id = ' . $tomonsterid;
			if( !($result = $db->sql_query($sql)) )
			{
				message_die(GENERAL_ERROR, "無法取得防禦資料!!", "", __LINE__, __FILE__, $sql);
			}
			if ( $row = $db->sql_fetchrow($result) )
			{
				$defense_point += $row['def_point'];
				$monsterrob += $row['def_robfail'];
			}
			$rob_ok = floor($attack_point * 100 / $defense_point);
			if ($rob_ok > 80)
			{
				$rob_ok = 80;
			}
			else if ($rob_ok < 3)
			{
				$rob_ok = 3;
			}
			$rob_ok = $rob_ok + $monsterrob - $attack_rob;
			srand((double)microtime()*1000000);
			$randid = rand(1, 100);
			$robnames = '機率: ' . $rob_ok . '%, 結果值: ' . $randid . ', 搶匪:' . $robnames;
			if ($randid <= $rob_ok)
			{
				// 搶劫成功
				$monstermoney += $monsterbank;
				$moneypercent = 30 + $monsterrob; // 被搶者將失去 30% 的現金，由搶匪均分
				if ($moneypercent > 100) $moneypercent = 100;
				$sql = 'UPDATE xoops_users SET rpg_rob_cnt = rpg_rob_cnt + 1, rpg_rob_succ = rpg_rob_succ + 1, ' .
					'rpg_rob_money = rpg_rob_money + ' . (floor($monstermoney * $moneypercent / 100 / count($robbers))) .
					', user_money = user_money + ' . (floor($monstermoney * $moneypercent / 100 / count($robbers))) .
					' WHERE uid in (' . $robbersql . ')';
				if( !$db->sql_query($sql) )
				{
					message_die(GENERAL_ERROR, "無法更新搶劫記錄!!", "", __LINE__, __FILE__, $sql);
				}
				$sql = 'INSERT INTO xoops_pbb_rpg_event SELECT attack_id AS uid, ' . time() .
					' AS event_time, 1 AS event_class, 1 AS event_type, ' . (floor($monstermoney * $moneypercent / 100 / count($robbers))) .
					' AS event_money, "搶' . $tomonster . '得逞!! ' . $robnames . '" AS event_note ' .
					' FROM xoops_pbb_rpg_rob WHERE attack_target = ' . $tomonsterid . ' AND task_over = 0';
				if( !$db->sql_query($sql) )
				{
					message_die(GENERAL_ERROR, "無法更新搶劫記錄檔!!", "", __LINE__, __FILE__, $sql);
				}
				$sql = 'INSERT INTO xoops_pbb_rpg_event VALUES (' . $tomonsterid . ', ' . time() . ', 2, 0, ' .
					(floor($monstermoney * $moneypercent / 100)) . ', "你被搶劫啦!! ' . $robnames . '")';
				if( !$db->sql_query($sql) )
				{
					message_die(GENERAL_ERROR, "無法更新被搶記錄檔!!", "", __LINE__, __FILE__, $sql);
				}
				$sql = 'UPDATE xoops_users SET rpg_robbed_cnt = rpg_robbed_cnt + 1, rpg_robbed_money = rpg_robbed_money + ' .
					(floor($monstermoney * $moneypercent / 100)) . ', user_bank = user_bank - ' . $monsterbank .
					', user_money = FLOOR((user_money + ' . $monsterbank . ') * (100 - ' . $moneypercent .
					') / 100) WHERE uid = ' . $tomonsterid;
				if( !$db->sql_query($sql) )
				{
					message_die(GENERAL_ERROR, "無法更新被搶現金!!", "", __LINE__, __FILE__, $sql);
				}
				PM_User($tomonsterid, $tomonsterid, "被搶通知", "被搶金額: " . floor($monstermoney * $moneypercent / 100) . "
					銀行強迫提款: " . $monsterbank . " \n " . $robnames);
			}
			else
			{
				// 搶劫失敗
				$sql = 'UPDATE xoops_users SET rpg_rob_cnt = rpg_rob_cnt + 1, rpg_rob_fail = rpg_rob_fail + 1, user_robfail = user_robfail + 1 WHERE uid in (' . $robbersql . ')';
				if( !$db->sql_query($sql) )
				{
					message_die(GENERAL_ERROR, "無法更新記錄!!", "", __LINE__, __FILE__, $sql);
				}
				$sql = 'INSERT INTO xoops_pbb_rpg_event SELECT attack_id AS uid, ' . time() .
					' AS event_time, 1 AS event_class, 0 AS event_type, 0 AS event_money, "搶' .
					$tomonster . '失敗!! ' . $robnames . '" AS event_note FROM xoops_pbb_rpg_rob WHERE attack_target = ' .
					$tomonsterid . ' AND task_over = 0';
				if( !$db->sql_query($sql) )
				{
					message_die(GENERAL_ERROR, "無法更新搶劫記錄檔!!", "", __LINE__, __FILE__, $sql);
				}
				$profiledata['user_robfail'] += 1;
				// 如果搶匪持有特殊道具 - 新匪上任一把火
				$sql = 'SELECT uid, min(own_id) as own_id FROM xoops_pbb_rpg_owns WHERE item_id = 99 AND uid in (' . $robbersql . ') GROUP BY uid';
				if ( $result = $db->sql_query($sql) )
				{
					if ( $row = $db->sql_fetchrow($result) )
					{
						do
						{
							if ($row['uid'] == intval($userdata['uid']))
							{
								$profiledata['user_robfail'] -= 1;
							}
							$sql = 'UPDATE xoops_users SET user_robfail = user_robfail - 1 WHERE user_robfail > 0 AND uid = ' . $row['uid'];
							$db->sql_query($sql);
							$sql = 'DELETE FROM xoops_pbb_rpg_owns WHERE own_id = ' . $row['own_id'];
							$db->sql_query($sql);
						}
						while ( $row = $db->sql_fetchrow($result) );
					}
				}
			}
			$sql = 'UPDATE xoops_pbb_rpg_rob SET task_over = 1, join_time = ' . time() . ' WHERE attack_target = ' .
				$tomonsterid . ' AND task_over = 0';
			if( !$db->sql_query($sql) )
			{
				message_die(GENERAL_ERROR, "無法更新搶劫列表!!", "", __LINE__, __FILE__, $sql);
			}
		}
	}
}


// 聘請保鑣
if ( isset($HTTP_POST_VARS['getbodyguard']) )
{
	$towho = $HTTP_POST_VARS['towho'];
	$howmuch = $HTTP_POST_VARS['howmuch'];

	if (BODYGUARD_COUNT > 0)
	{
		$sql = "SELECT COUNT(*) AS bodycount  FROM xoops_pbb_rpg_bodyguard WHERE from_id = " . intval($userdata['uid']) .
			" AND yes_or_no <= 1 AND invite_time > " . (time() - 86400 * 3);
		if ( $result = $db->sql_query($sql) )
		{
			if ( $row = $db->sql_fetchrow($result) )
			{
				if ($row['bodycount'] >= BODYGUARD_COUNT)
				{
					message_die(GENERAL_MESSAGE, '您最多只能聘請 ' . BODYGUARD_COUNT . ' 位保鑣!!' . $returnurl);
				}
			}
		}
	}

	if (($howmuch < 1) || ($howmuch > $profiledata['user_bank']))
	{
		message_die(GENERAL_MESSAGE, '匯款金額輸入錯誤或存款不足!!' . $returnurl);
	}
	if ($howmuch < 100)
	{
		message_die(GENERAL_MESSAGE, '本銀行不接受 100 元以下的匯款!!' . $returnurl);
	}
	$sql = "SELECT uid FROM " . USERS_TABLE . " WHERE uname = '" . str_replace("\'", "''", $towho) . "' AND uid <> " . ANONYMOUS;
	if( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, '找不到欲聘請的保鑣!!', '', __LINE__, __FILE__, $sql);
	}
	if ( $row = $db->sql_fetchrow($result) )
	{
		$towhoid = $row['uid'];

		if (BOSS_COUNT > 0)
		{
			$sql = "SELECT COUNT(*) AS bosscount FROM xoops_pbb_rpg_bodyguard WHERE to_id = " . $towhoid .
				" AND yes_or_no <= 1 AND invite_time > " . (time() - 86400 * 3);
			if ( $result = $db->sql_query($sql) )
			{
				if ( $row = $db->sql_fetchrow($result) )
				{
					if ($row['bosscount'] >= BOSS_COUNT)
					{
						message_die(GENERAL_MESSAGE, '保鑣: ' . $towho . ' 已經有 ' . BOSS_COUNT . ' 位以上僱主!!' . $returnurl);
					}
				}
			}
		}

		$sql = "UPDATE " . CONFIG_TABLE . " SET config_value = config_value + " . (floor($howmuch * 0.01)) . " WHERE config_name = 'rpg_jackpot'";
		if( !$db->sql_query($sql) )
		{
			message_die(GENERAL_ERROR, "無法更新頭彩獎金!!", "", __LINE__, __FILE__, $sql);
		}
		$sql = "UPDATE " . USERS_TABLE . " SET user_bank = user_bank - " . (floor($howmuch * 0.01)) .
			" WHERE uid = " . intval($userdata['uid']);
		if( !$db->sql_query($sql) )
		{
			message_die(GENERAL_ERROR, "無法更新手續費!!", "", __LINE__, __FILE__, $sql);
		}
		$profiledata['user_bank'] -= floor($howmuch * 0.01);
		$sql = "DELETE FROM xoops_pbb_rpg_bodyguard WHERE from_id = " . intval($userdata['uid']) .
			" AND to_id = " . $towhoid;
		if( !$db->sql_query($sql) )
		{
			message_die(GENERAL_ERROR, '聘請保鑣程序發生錯誤!!', '', __LINE__, __FILE__, $sql);
		}
		if ($towhoid == intval($userdata['uid']))
		{
			$rpg_rob_result = "不可以聘請自己當保鑣，扣手續費 " . floor($howmuch * 0.01) . " 元!!";
		}
		else
		{
			$sql = "INSERT INTO xoops_pbb_rpg_bodyguard VALUES (" . intval($userdata['uid']) . ", " . $towhoid .
				", " . (floor($howmuch - $howmuch * 0.01)) . ", 0, " . time() . ")";
			if( !$db->sql_query($sql) )
			{
				message_die(GENERAL_ERROR, '聘請保鑣程序發生錯誤!!', '', __LINE__, __FILE__, $sql);
			}
			$rpg_rob_result = "<font color=red>聘請作業完成!!<br />一旦 " . $towho . " 上任，即會進行扣款作業。<br />支付保鑣的佣金是從您的銀行存款中扣除，若扣款時存款不足，保鑣會拒絕上任!!</font><br /><br />";
		}
	}
}

// 接受或是拒絕當保鑣
if ( isset($HTTP_POST_VARS['reject']) || isset($HTTP_POST_VARS['accept']) )
{
	$mark_list = ( !empty($HTTP_POST_VARS['mark']) ) ? $HTTP_POST_VARS['mark'] : 0;
	if ( isset($mark_list) && !is_array($mark_list) )
	{
		$mark_list = array();
	}
	if ( count($mark_list) )
	{
		$mark_list_sql = implode(', ', $mark_list);
		if (isset($HTTP_POST_VARS['accept']) )
		{
			$accept_list = array();
			$reject_list = array();
			$sql = "SELECT b.from_id, b.bodyguard_salary, u.user_bank FROM xoops_pbb_rpg_bodyguard b, xoops_users u
				WHERE u.uid = b.from_id AND b.yes_or_no = 0 AND b.from_id in (" . $mark_list_sql .
				") AND b.to_id = " . intval($userdata['uid']);
			if( !($result = $db->sql_query($sql)) )
			{
				message_die(GENERAL_ERROR, '無法取得僱主給薪列表!!', '', __LINE__, __FILE__, $sql);
			}
			if ( $row = $db->sql_fetchrow($result) )
			{
				$body_salary = 0;
				do
				{
					if ($row['bodyguard_salary'] <= $row['user_bank'])
					{
						$sql = "UPDATE xoops_users SET user_bank = user_bank - " . $row['bodyguard_salary'] .
							", user_bank_time = " . time() . " WHERE uid = " . $row['from_id'];
						if( !$db->sql_query($sql) )
						{
							message_die(GENERAL_ERROR, '僱主扣款發生錯誤!!', '', __LINE__, __FILE__, $sql);
						}
						$accept_list[] = $row['from_id'];
						$body_salary += $row['bodyguard_salary'];
					}
					else
					{
						$reject_list[] = $row['from_id'];
					}
				}
				while ( $row = $db->sql_fetchrow($result) );
				if (count($accept_list) > 0)
				{
					$sql = "UPDATE xoops_pbb_rpg_bodyguard SET yes_or_no = 1, invite_time = " . time() .
						" WHERE yes_or_no = 0 AND from_id in (" . (implode(', ', $accept_list)) .
						") AND to_id = " . intval($userdata['uid']);
					if( !$db->sql_query($sql) )
					{
						message_die(GENERAL_ERROR, '保鑣上任發生錯誤!!', '', __LINE__, __FILE__, $sql);
					}
					$sql = "UPDATE xoops_users SET user_bank = user_bank + " . $body_salary .
						", user_bank_time = " . time() . " WHERE uid = " . intval($userdata['uid']);
					if( !$db->sql_query($sql) )
					{
						message_die(GENERAL_ERROR, '保鑣付款發生錯誤!!', '', __LINE__, __FILE__, $sql);
					}
				}
				if (count($reject_list) > 0)
				{
					$sql = "UPDATE xoops_pbb_rpg_bodyguard SET yes_or_no = 2
						WHERE yes_or_no = 0 AND from_id in (" . (implode(', ', $reject_list)) .
						") AND to_id = " . intval($userdata['uid']);
					if( !$db->sql_query($sql) )
					{
						message_die(GENERAL_ERROR, '保鑣拒絕發生錯誤!!', '', __LINE__, __FILE__, $sql);
					}
				}
			}
		}
		else
		{
			$sql = "UPDATE xoops_pbb_rpg_bodyguard SET yes_or_no = 2 WHERE yes_or_no = 0 AND from_id in (" .
				$mark_list_sql . ") AND to_id = " . intval($userdata['uid']);
			if( !$db->sql_query($sql) )
			{
				message_die(GENERAL_ERROR, '保鑣拒絕發生錯誤!!', '', __LINE__, __FILE__, $sql);
			}
		}
	}
	else
	{
		$rpg_rob_result = "請勾選欲上任或拒絕的僱主";
	}
}

// 取得保鑣列表和防禦指數
$rob_defense = $profiledata['posts'] * 5;
$bodyguardlist = "";
$sql = 'SELECT u.uname, u.posts, b.bodyguard_salary, b.invite_time, b.yes_or_no FROM xoops_users u, xoops_pbb_rpg_bodyguard b
	WHERE u.uid = b.to_id and b.from_id = ' . intval($userdata['uid']) . ' ORDER BY b.invite_time DESC';
if( !($result = $db->sql_query($sql)) )
{
	message_die(GENERAL_ERROR, '無法取得保鑣列表!!', '', __LINE__, __FILE__, $sql);
}
if ( $row = $db->sql_fetchrow($result) )
{
	$i = 1;
	$clearolddata = false;
	$bodyguardlist = '<table class="forumline" width="60%" 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>
		<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
	{
		$bodyguardlist .= '<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['posts'] .
					'</span></td><td align="right"><span class="gensmall">' . $row['bodyguard_salary'] .
					'</span></td><td align="center"><span class="gensmall">' .
					($row['invite_time'] ? create_date($userdata['user_dateformat'], $row['invite_time'], $board_config['board_timezone']) : $lang['Never_last_logon']) .
					'</span></td><td><span class="gensmall">';
		if ($row['yes_or_no'])
		{
			if ($row['yes_or_no'] == 2)
			{
					$bodyguardlist .= '拒絕</span></td></tr>';
			}
			else
			{
				if ($row['invite_time'] > (time() - 86400 * 3))
				{
					$bodyguardlist .= '執勤中</span></td></tr>';
					$rob_defense += $row['posts'];
				}
				else
				{
					$bodyguardlist .= '任期已滿</span></td></tr>';
					if ($row['invite_time'] <= (time() - 86400 * 5))
					{
						$clearolddata = true;
					}
				}
			}
		}
		else
		{
			$bodyguardlist .= '未確認</span></td></tr>';
		}
		$i++;
	}
	while ( $row = $db->sql_fetchrow($result) );
	$bodyguardlist .= '</table>';
	if ($clearolddata)
	{
		$sql = 'DELETE FROM xoops_pbb_rpg_bodyguard WHERE from_id = ' . intval($userdata['uid']) .
			' AND invite_time <= ' . (time() - 86400 * 5);
		if( !$db->sql_query($sql) )
		{
			message_die(GENERAL_ERROR, '無法清除過期保鑣!!', '', __LINE__, __FILE__, $sql);
		}
	}
}
if ($bodyguardlist != "")
{
	$bodyguardlist = '總防禦指數: ' . $rob_defense . '<br />' . $bodyguardlist .
		'<form name="ClearNotConfirm" action="rpg.php" method="post"><input type="hidden" name="action" value="rob" />
			<input type="submit" name="ClearNotConfirm" value="清除未確認的保鑣" class="mainoption" /></form>';
}
else
{
	$bodyguardlist = '總防禦指數: ' . $rob_defense;
}

// 取得僱主列表
$bosslist = "";
$sql = 'SELECT u.uname, u.posts, b.bodyguard_salary, b.from_id, b.invite_time, b.yes_or_no from xoops_users u, xoops_pbb_rpg_bodyguard b
	where u.uid = b.from_id and b.to_id = ' . intval($userdata['uid']) . ' order by b.invite_time DESC';
if( !($result = $db->sql_query($sql)) )
{
	message_die(GENERAL_ERROR, '無法取得僱主列表!!', '', __LINE__, __FILE__, $sql);
}
if ( $row = $db->sql_fetchrow($result) )
{
	$clearolddata = false;
	$i = 1;
	$needcheck = false;
	$bosslist = '<table class="forumline" width="60%" 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>
		<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
	{
		$bosslist .= '<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['posts'] * 5) .
				'</span></td><td align="right"><span class="gensmall">' . number_format($row['bodyguard_salary']) .
				'</span></td><td align="center"><span class="gensmall">' .
		                 ($row['invite_time'] ? create_date($userdata['user_dateformat'], $row['invite_time'], $board_config['board_timezone']) : $lang['Never_last_logon']) .
		                 '</span></td>';
		if ($row['yes_or_no'])
		{
			if ($row['yes_or_no'] == 2)
			{
					$bosslist .= '<td><span class="gensmall">已拒絕</span></td></tr>';
					if ($row['invite_time'] <= (time() - 86400 * 5))
					{
						$clearolddata = true;
					}
			}
			else
			{
				if ($row['invite_time'] > (time() - 86400 * 3))
				{
					$bosslist .= '<td><span class="gensmall">執勤中</span></td></tr>';
				}
				else
				{
					$bosslist .= '<td><span class="gensmall">任期已滿</span></td></tr>';
					if ($row['invite_time'] <= (time() - 86400 * 5))
					{
						$clearolddata = true;
					}
				}
			}
		}
		else
		{
			$bosslist .= '<td align="center"><span class="gensmall"><input type="checkbox" name="mark[]2" value="' . $row['from_id'] . '" /></span></td></tr>';
			$needcheck = true;
		}
		$i++;
	}
	while ( $row = $db->sql_fetchrow($result) );
	if ($needcheck)
	{
		$bosslist = '<form name="GetBoss" action="rpg.php" method="post"><input type="hidden" name="action" value="rob" />' .
				$bosslist . '</table><br /><input type="submit" name="accept" value="上任" class="mainoption" />&nbsp;&nbsp;&nbsp;' .
				'<input type="submit" name="reject" value="拒絕" class="mainoption" /></form>';
	}
	else
	{
		$bosslist .= '</table>';
	}
	$bosslist = '<tr><td align="right" valign="top" nowrap="nowrap"><b><span class="gen">僱主列表:&nbsp;</span></b></td>
			<td width="100%"><span class="gen">' . $bosslist . '</span></td></tr>';
	if ($clearolddata)
	{
		$sql = 'DELETE FROM xoops_pbb_rpg_bodyguard WHERE to_id = ' . intval($userdata['uid']) .
			' AND invite_time <= ' . (time() - 86400 * 5) . ' AND yes_or_no in (1, 2)';
		if( !$db->sql_query($sql) )
		{
			message_die(GENERAL_ERROR, '無法清除過期僱主!!', '', __LINE__, __FILE__, $sql);
		}
	}
}

// 判斷是否可以搶劫銀行
$robbank = "";
$sql = 'SELECT max(join_time) AS join_time FROM xoops_pbb_rpg_rob WHERE attack_target = -1 AND attack_id = ' . intval($userdata['uid']);
if( !($result = $db->sql_query($sql)) )
{
	message_die(GENERAL_ERROR, '無法判斷是否可以搶劫銀行!!', '', __LINE__, __FILE__, $sql);
}
$robbank = '<form name="GoRobBank" action="rpg.php" method="post">
		<input type="hidden" name="action" value="rob" />
		<input type="submit" name="rob2bank" value="確定" class="mainoption" /></form>';
if ( $row = $db->sql_fetchrow($result) )
{
	// 如果在二十四小時內不能搶劫
	if ( $row['join_time'] >= (time() - 86400 * 1))
	{
		$robbank = '您必須等到 ' . (create_date($userdata['user_dateformat'], ($row['join_time'] + 86400 * 1), $board_config['board_timezone'])) . ' 才可以行搶銀行.';
	}
	else
	{
		$sql = 'SELECT count(*) AS non_start FROM xoops_pbb_rpg_rob WHERE attack_target = -1 AND task_over = 0 AND attack_id = ' . intval($userdata['uid']);
		if( !($result = $db->sql_query($sql)) )
		{
			message_die(GENERAL_ERROR, '無法判斷是否可以搶劫銀行!!', '', __LINE__, __FILE__, $sql);
		}
		if ( $row = $db->sql_fetchrow($result) )
		{
			// 如果在二十四小時內不能搶劫
			if ($row['non_start'] > 0)
			{
				$robbank = '您必須等到銀行搶匪集團成軍 24 小時後，才可以搶劫銀行!!';
			}
		}
	}
}

// 銀行同伙
$sql = 'SELECT u.uname, u.posts, r.join_time from xoops_users u, xoops_pbb_rpg_rob r
	WHERE u.uid = r.attack_id AND r.attack_target = -1 AND task_over = 0 ORDER BY r.join_time DESC';
if( !($result = $db->sql_query($sql)) )
{
	message_die(GENERAL_ERROR, '無法取得銀行搶匪列表!!', '', __LINE__, __FILE__, $sql);
}
if ( $row = $db->sql_fetchrow($result) )
{
	$i = 1;
	$robbank .= '<br /><table class="forumline" width="60%" 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
	{
		$robbank .= '<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['posts'] .
				'</span></td><td align="center"><span class="gensmall">' .
				($row['join_time'] ? create_date($userdata['user_dateformat'], $row['join_time'], $board_config['board_timezone']) : $lang['Never_last_logon']) .
				'</span></td></tr>';
		$i++;
	}
	while ( $row = $db->sql_fetchrow($result) );
	$robbank .= '</table>';
}

// 判斷是否可以搶劫會員
$robmonster = '<form name="GoRobMonster" action="rpg.php" method="post"><input type="hidden" name="action" value="rob" />對象: <input type="text" class="post" style="width:90px" name="tomonster" size="10" maxlength="25" value="' . $profiledata['uname'] . '" />&nbsp;&nbsp;<input type="submit" name="rob2monster" value="確定" class="mainoption" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" name="getinformation" value="取得情報($500)" class="mainoption" /></form>' . $information;
$sql = 'SELECT max(join_time) AS max_time FROM xoops_pbb_rpg_rob WHERE attack_target <> -1 AND attack_id = ' . intval($userdata['uid']);
if( !($result = $db->sql_query($sql)) )
{
	message_die(GENERAL_ERROR, '無法判斷是否可以搶劫水怪!!', '', __LINE__, __FILE__, $sql);
}
if ( $row = $db->sql_fetchrow($result) )
{
	// 如果在二小時內不能搶劫
	if ($row['max_time'] >= (time() - 86400 / 12))
	{
		$robmonster = '您必須等到 ' . (create_date($userdata['user_dateformat'], ($row['max_time'] + 86400 / 12), $board_config['board_timezone'])) . ' 才可以搶劫其他會員!!';
	}
	/*
	else
	{
		$sql = 'SELECT count(*) AS non_start FROM xoops_pbb_rpg_rob WHERE attack_target <> -1 AND task_over = 0 AND attack_id = ' . intval($userdata['uid']);
		if( !($result = $db->sql_query($sql)) )
		{
			message_die(GENERAL_ERROR, '無法判斷是否可以搶劫會員!!', '', __LINE__, __FILE__, $sql);
		}
		if ( $row = $db->sql_fetchrow($result) )
		{
			if ($row['non_start'] > 0)
			{
				$robmonster = '您必須等到銀行搶匪集團成軍 2 小時後，才可以搶劫!!';
			}
		}
	}
	*/
}
// 搶匪同伙
$sametarget = array();
$sql = 'SELECT attack_target FROM xoops_pbb_rpg_rob WHERE task_over = 0 AND attack_target <> -1 AND attack_id = ' . intval($userdata['uid']);
if( !($result = $db->sql_query($sql)) )
{
	message_die(GENERAL_ERROR, '無法取得搶劫對象列表!!', '', __LINE__, __FILE__, $sql);
}
if ( $row = $db->sql_fetchrow($result) )
{
	do
	{
		$sametarget[] = $row['attack_target'];
	}
	while ( $row = $db->sql_fetchrow($result) );
}

if (count($sametarget))
{
	$sql = 'SELECT r.attack_target, r.target_name, u.uname, u.posts, r.join_time FROM xoops_users u, xoops_pbb_rpg_rob r
		WHERE u.uid = r.attack_id AND r.attack_target IN (' . (implode(', ', $sametarget)) .
		') AND task_over = 0 ORDER BY r.attack_target, r.join_time';
	if( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, '無法取得會員搶匪列表!!', '', __LINE__, __FILE__, $sql);
	}
	if ( $row = $db->sql_fetchrow($result) )
	{
		$i = 1;
		$attackpoint = 0;
		$attack_target = $row['attack_target'];
		$robmonster .= '<table class="forumline" width="60%" 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>
			<td align="center" class="catLeft"><b><span class="gen">加入時間</span></b></td></tr>';
		do
		{
			if ($i == 1)
			{
				$attackpoint += $row['posts'];
				$robmonster .= '<tr><td class="row3" align="center"><span class="gensmall">' . $i .
						'</span></td><td><span class="gensmall">' . $row['target_name'] .
						'</span></td><td><span class="gensmall">' . $row['uname'] .
						'</span></td><td align="right"><span class="gensmall">' . $row['posts'] .
						'</span></td><td align="center"><span class="gensmall">' .
						($row['join_time'] ? create_date($userdata['user_dateformat'], $row['join_time'], $board_config['board_timezone']) : $lang['Never_last_logon']) .
						'</span></td></tr>';
			}
			else
			{
				if ($attack_target <> $row['attack_target'])
				{
					$attack_target = $row['attack_target'];
					$i = 1;
					$robmonster .= '<tr><td class="row3"></td><td class="row3" colspan="2" align="center"><span class="gensmall">總攻擊指數</span></td>
							<td class="row3" align="right"><span class="gensmall">' . $attackpoint . '</span></td><td class="row3" ></td></tr>
							<tr><td class="row3" align="center"><span class="gensmall">' . $i . '</span></td><td><span class="gensmall">' .
							$row['target_name'] . '</span></td><td><span class="gensmall">' . $row['uname'] .
							'</span></td><td align="right"><span class="gensmall">' . $row['posts'] .
							'</span></td><td align="center"><span class="gensmall">' .
							($row['join_time'] ? create_date($userdata['user_dateformat'], $row['join_time'], $board_config['board_timezone']) : $lang['Never_last_logon']) .
							'</span></td></tr>';
					$attackpoint = $row['posts'];
				}
				else
				{
					$attackpoint += $row['posts'];
					$robmonster .= '<tr><td class="row3" align="center"><span class="gensmall">' . $i . '</span></td><td></td><td><span class="gensmall">' .
							$row['uname'] . '</span></td><td align="right"><span class="gensmall">' . $row['posts'] .
							'</span></td><td align="center"><span class="gensmall">' .
							($row['join_time'] ? create_date($userdata['user_dateformat'], $row['join_time'], $board_config['board_timezone']) : $lang['Never_last_logon']) .
							'</span></td></tr>';
				}
			}
			$i++;
		}
		while ( $row = $db->sql_fetchrow($result) );
		$robmonster .= '<tr><td class="row3"></td><td class="row3" colspan="2" align="center"><span class="gensmall">總攻擊指數</span></td>
				<td class="row3" align="right"><span class="gensmall">' . $attackpoint . '</span></td><td class="row3" ></td></tr></table><br />
				<form name="CancelRob" action="rpg.php" method="post"><input type="hidden" name="action" value="rob" />
				<input type="submit" name="cancel2rob" value="取消以上搶劫動作" class="mainoption" /></form>';
	}
}

// 取得搶匪列表和攻擊指數
$rob_attack = 0;
$robberlist = "";
$sql = 'SELECT u.uname, u.posts, r.join_time from xoops_users u, xoops_pbb_rpg_rob r
	where u.uid = r.attack_id and r.task_over = 0 and r.attack_target = ' . intval($userdata['uid']) . ' order by r.join_time';
if( !($result = $db->sql_query($sql)) )
{
	message_die(GENERAL_ERROR, '無法取得搶匪列表!!', '', __LINE__, __FILE__, $sql);
}
if ( $row = $db->sql_fetchrow($result) )
{
	$i = 1;
	$robberlist = '<table class="forumline" width="60%" 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
	{
		$robberlist .= '<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['posts'] .
				'</span></td><td align="center"><span class="gensmall">' .
				($row['join_time'] ? create_date($userdata['user_dateformat'], $row['join_time'], $board_config['board_timezone']) : $lang['Never_last_logon']) .
				'</span></td></tr>';
		$rob_attack += $row['posts'];
		$i++;
	}
	while ( $row = $db->sql_fetchrow($result) );
	$robberlist .= '</table>';
}
$robberlist = "以下搶匪即將對您發動攻擊!!總攻擊指數: " . $rob_attack . "<br />" . $robberlist;

// 取得苦主排行榜
$targetlist = "";
$sql = 'SELECT r.target_name, u.posts, u.user_money, COUNT(*) as user_def FROM xoops_pbb_rpg_rob r, xoops_users u
	WHERE r.attack_target <> -1 AND r.task_over = 0 AND u.uid = r.attack_target
	GROUP BY r.target_name, u.posts, u.user_money
	ORDER BY 4 DESC';
if( !($result = $db->sql_query($sql)) )
{
	message_die(GENERAL_ERROR, '無法取得苦主排行榜!!', '', __LINE__, __FILE__, $sql);
}
if ( $row = $db->sql_fetchrow($result) )
{
	$i = 1;
	$targetlist = '<table class="forumline" width="60%" 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>
		<td align="center" class="catLeft"><b><span class="gen">搶匪人數</span></b></td></tr>';
	do
	{
		$targetlist .= '<tr><td class="row3" align="center"><span class="gensmall">' . $i .
				'</span></td><td><span class="gensmall">' . $row['target_name'] .
				'</span></td><td align="right"><span class="gensmall">' . $row['posts'] .
				'</span></td><td align="right"><span class="gensmall">' . number_format($row['user_money']) .
				'</span></td><td align="right"><span class="gensmall">' . $row['user_def'] . '</span></td></tr>';
		$i++;
	}
	while ( $row = $db->sql_fetchrow($result) );
	$targetlist .= '</table>';
}

// 取得歷史記錄
$rpg_rob_history = "";
$sql = 'SELECT * FROM xoops_pbb_rpg_event WHERE event_class <= 2 AND uid = ' . intval($userdata['uid']) . ' ORDER BY event_time DESC LIMIT 0, 50';
if( !($result = $db->sql_query($sql)) )
{
	message_die(GENERAL_ERROR, '無法取得搶劫記錄!!', '', __LINE__, __FILE__, $sql);
}
if ( $row = $db->sql_fetchrow($result) )
{
	$i = 1;
	$rpg_rob_history = '<table class="forumline" width="100%" 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
	{
		$rpg_rob_history .= '<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) );
	$rpg_rob_history = '<tr><td align="right" valign="top" nowrap="nowrap"><b><span class="gen">搶劫記錄:&nbsp;</span></b></td>
			<td width="100%" valign="middle"><span class="gen">' . $rpg_rob_history . '</table></span><br />
			<form name="ClearHistory" action="rpg.php" method="post"><input type="hidden" name="action" value="rob" />
			<input type="submit" name="clear2history" value="清除所有搶劫記錄" class="mainoption" /></form></td></tr>';
}

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

$template->assign_vars(array(
	'USERNAME' => $profiledata['uname'],
	'AVATAR_IMG' => GetAvatar($profiledata),
	'RPG_MONEY' => number_format($profiledata['user_money']),
	'RPG_BANK' => number_format($profiledata['user_bank']),
	'RPG_ROB_DEFENSE_P' => $bodyguardlist,
	'RPG_ROB_BOSS_P' => $bosslist,
	'RPG_ROB_ATTACK_P' => $robberlist,
	'RPG_TARGET_LIST' => $targetlist,
	'RPG_ROB_BANK' => $robbank,
	'RPG_ROB_MONSTER' => $robmonster,
	'RPB_ROB_HISTORY' => $rpg_rob_history,
	'RPG_ROB_RESULT' => $rpg_rob_result,
	'L_NOWMENU' => '搶匪有限公司') );

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

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

?>