<?php

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

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

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

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

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

// 計算利息
if ($profiledata['user_bank_time'])
{
	$bankdays = floor( ( time() - $profiledata['user_bank_time'] ) / 86400 );
	$rpg_bank_result = $bankdays;
	if ( $bankdays >= 1)
	{
		$newbank = $profiledata['user_bank'];
		for ($i=0; $i < $bankdays; $i++)
		{
			$newbank = floor($newbank * (1 + $board_config['bank_fees']));
		}
		$sql = "UPDATE " . USERS_TABLE . " SET user_bank = " . $newbank . ", user_bank_time = user_bank_time + 86400 * " . $bankdays . " WHERE uid = " . intval($userdata['uid']);
		if( !$db->sql_query($sql) )
		{
			message_die(GENERAL_ERROR, "無法更新個人利息!!", "", __LINE__, __FILE__, $sql);
		}
		$profiledata['user_bank'] = $newbank;
		$profiledata['user_bank_time'] = $profiledata['user_bank_time'] + $bankdays * 86400;
	}
}

// 計算貸款利息
if ($profiledata['user_loan_time'])
{
	$bankdays = floor( ( time() - $profiledata['user_loan_time'] ) / 86400 );
	$rpg_bank_result = $loandays;
	if ( $loandays >= 1)
	{
		$newloan = $profiledata['user_loan'];
		for ($i=0; $i < $loandays; $i++)
		{
			$newloan = floor($newloan * (1 + $board_config['bank_loanfees']));
		}
		$sql = "UPDATE " . USERS_TABLE . " SET user_loan = " . $newloan . ", user_loan_time = user_loan_time + 86400 * " . $loandays . " WHERE uid = " . intval($userdata['uid']);
		if( !$db->sql_query($sql) )
		{
			message_die(GENERAL_ERROR, "無法更新個人貸款利息!!", "", __LINE__, __FILE__, $sql);
		}
		$profiledata['user_loan'] = $newloan;
		$profiledata['user_loan_time'] = $profiledata['user_loan_time'] + $loandays * 86400;
	}
}

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

$rpg_bank_result = "";
if ( isset($HTTP_POST_VARS['deposit']) )
{
	$profiledata = get_userdata(intval($userdata['uid']));
	$deposits = $HTTP_POST_VARS['deposits'];
	if ($deposits > 0)
	{
		if ($deposits < 100)
		{
			$rpg_bank_result = "本銀行不接受 100 元以下的存款!!";
		}
		else
		{
			if ($deposits > $profiledata['user_money'])
			{
				$rpg_bank_result = "您沒有　" . $deposits . "　元的現金可以存進銀行!!";
			}
			else
			{
				$sql = "UPDATE " . CONFIG_TABLE . " SET config_value = config_value + " . floor($deposits * $board_config['bank_procedure_deposit']) . " 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 + " . ($deposits - floor($deposits * $board_config['bank_procedure_deposit'])) . ", user_money = user_money - " . $deposits . ", user_bank_time = " . time() . " WHERE uid = " . intval($userdata['uid']);
				if( !$db->sql_query($sql) )
				{
					message_die(GENERAL_ERROR, "無法更新個人資金!!", "", __LINE__, __FILE__, $sql);
				}
				$rpg_bank_result = "存入 " . ($deposits - floor($deposits * $board_config['bank_procedure_deposit'])) . " 成功!! (手續費: " . floor($deposits * $board_config['bank_procedure_deposit']) . ")";
			}
		}
	}
	else
	{
		$rpg_bank_result = "存款金額輸入錯誤!!";
	}
}

if ( isset($HTTP_POST_VARS['withdraw']) )
{
	$profiledata = get_userdata(intval($userdata['uid']));
	$withdraws = $HTTP_POST_VARS['withdraws'];
	if ($withdraws > 0)
	{
		if ($withdraws > $profiledata['user_bank'])
		{
			$rpg_bank_result = "您沒有 " . $withdraws . " 元的存款可以提領!!";
		}
		else
		{
			$sql = "UPDATE " . USERS_TABLE . " SET user_bank = user_bank - " . $withdraws . ", user_money = user_money + " . $withdraws . ", user_bank_time = " . time() . " WHERE uid = " . intval($userdata['uid']);
			if( !$db->sql_query($sql) )
			{
				message_die(GENERAL_ERROR, "無法更新個人資金!!", "", __LINE__, __FILE__, $sql);
			}
			$rpg_bank_result = "提領 " . $withdraws . " 成功!!";
		}
	}
	else
	{
		$rpg_bank_result = "提款金額輸入錯誤!!";
	}
}

if ( isset($HTTP_POST_VARS['loan']) )
{
	$profiledata = get_userdata(intval($userdata['uid']));
	$loan = $HTTP_POST_VARS['loan'];	
	if ($loan > 0)
	{
		if ($loan < 1000)
		{
			$rpg_bank_result = "本銀行不接受 1000 元以下的貸款!!";
		}
		else
		{
			if ($loan > $board_config['bank_loanmax'])
			{
				$rpg_bank_result = "本銀行貸款金額上限為 " . $board_config['bank_loanmax'] . " 元!!";
			}
			else
			{
				if ($loan < ($profiledata['user_money'] + $profiledata['user_bank']))
				{
					$rpg_bank_result = "您的目前的財產超過貸款金額，無法貸款!!";
				}
				else
				{
					$sql = "UPDATE " . CONFIG_TABLE . " SET config_value = config_value + " . floor($loan * $board_config['bank_procedure_loan']) . " WHERE config_name = 'rpg_jackpot'";
					if( !$db->sql_query($sql) )
					{
						message_die(GENERAL_ERROR, "無法更新頭彩獎金!!", "", __LINE__, __FILE__, $sql);
					}
					$sql = "UPDATE " . USERS_TABLE . " SET user_loan = user_loan + " . $loan . ", user_money = user_money + " . ($loan - floor($loan * $board_config['bank_procedure_loan'])) . ", user_loan_time = " . time() . " WHERE uid = " . intval($userdata['uid']);
					if( !$db->sql_query($sql) )
					{
						message_die(GENERAL_ERROR, "無法更新個人資金!!", "", __LINE__, __FILE__, $sql);
					}
					else
					{
						$rpg_bank_result = "貸款 " . ($loan - floor($loan * $board_config['bank_procedure_loan'])) . " 成功!! (手續費: " . floor($loan * $board_config['bank_procedure_loan']) . ")";
					}
				}
			}
		}
	}
	else
	{
		$rpg_bank_result = "貸款金額輸入錯誤!!";
	}
}

if ( isset($HTTP_POST_VARS['loanback']) )
{
	$profiledata = get_userdata(intval($userdata['uid']));
	$loanback = $HTTP_POST_VARS['loanback'];
	if ($profiledata['user_loan'] = 0)
	{
		if ($loanback <= 0)
		{
			$rpg_bank_result = "還款金額輸入錯誤!!";
		}
		else
		{
			if ($loanback < 100)
			{
				$rpg_bank_result = "本銀行不接受 100 元以下的還款!!";
			}
			else
			{
				if ($loanback > $profiledata['user_loan'])
				{
					$rpg_bank_result = "您的還款金額大於您在銀行貸款的金額!!";
				}		
				else
				{
					$sql = "UPDATE " . USERS_TABLE . " SET user_loan = user_loan - " . $loanback . ", user_money = user_money - " . $loanback . ", user_loan_time = " . time() . " WHERE uid = " . intval($userdata['uid']);
					if( !$db->sql_query($sql) )
					{
						message_die(GENERAL_ERROR, "無法更新個人資金!!", "", __LINE__, __FILE__, $sql);
					}
					$rpg_bank_result = "還款 " . $loan . " 成功!!";
				}
			}
		}
	}
	else
	{
		$rpg_bank_result = "您沒有跟銀行貸款，所以不需要還款!!";
	}
}

if ( isset($HTTP_POST_VARS['remit']) )
{
	$profiledata = get_userdata(intval($userdata['uid']));
	$towho = $HTTP_POST_VARS['towho'];
	$remits = $HTTP_POST_VARS['remits'];
	$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) )
	{
		if ($remits > $profiledata['user_bank'])
		{
			$rpg_bank_result = "存款不足，請先在銀行存夠欲匯款的金額後再匯款!!";
		}
		else
		{
			if ($remits > 0)
			{
				if ($remits < 100)
				{
					$rpg_bank_result = "本銀行不接受 100 元以下的匯款!!";
				}
				else
				{
					$sql = "UPDATE " . CONFIG_TABLE . " SET config_value = config_value + " . floor($remits * $board_config['bank_procedure_remit']) . " 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 + " . ($remits - floor($remits * $board_config['bank_procedure_remit'])) . ", user_bank_time = " . time() . " WHERE uid = " . intval($row['uid']);
					if( !$db->sql_query($sql) )
					{
						message_die(GENERAL_ERROR, "無法更新匯款對象銀行存款!!", "", __LINE__, __FILE__, $sql);
					}
					$sql = "UPDATE " . USERS_TABLE . " SET user_bank = user_bank - " . $remits . ", user_bank_time = " . time() . " WHERE uid = " . intval($userdata['uid']);
					if( !$db->sql_query($sql) )
					{
						message_die(GENERAL_ERROR, "無法更新個人銀行存款!!", "", __LINE__, __FILE__, $sql);
					}
					$rpg_bank_result = "匯出 " . ($remits - floor($remits * $board_config['bank_procedure_remit'])) . " 成功!! (手續費: " . floor($remits * $board_config['bank_procedure_remit']) . ")";
					$sql = 'INSERT INTO xoops_pbb_rpg_event VALUES (' . intval($userdata['uid']) . ', ' . time() . ', 3, 0, ' . ($remits - floor($remits * 0.01)) . ', "匯出對象: ' . $towho . '")';
					if( !$db->sql_query($sql) )
					{
						message_die(GENERAL_ERROR, "無法更新匯款記錄!!", "", __LINE__, __FILE__, $sql);
					}
					$sql = 'INSERT INTO xoops_pbb_rpg_event VALUES (' . intval($row['uid']) . ', ' . time() . ', 3, 1, ' . ($remits - floor($remits * $board_config['bank_procedure_remit'])) . ', "匯入金主: ' . $profiledata['uname'] . '")';
					if( !$db->sql_query($sql) )
					{
						message_die(GENERAL_ERROR, "無法更新匯款記錄!!", "", __LINE__, __FILE__, $sql);
					}
				}
			}
			else
			{
				$rpg_bank_result = "匯款金額輸入錯誤!!";
			}
		}
	}
}

// 取得匯款記錄
$rpg_bank_history = "";
$sql = 'SELECT * FROM xoops_pbb_rpg_event WHERE event_class = 3 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_bank_history = '<table width="80%" cellpadding="3" cellspacing="1" border="0" class="forumline">
	<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_bank_history .= '
		<tr>
		<td align="center" class="row3"><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_bank_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_bank_history . '</span></table><br />
	<form name="ClearHistory" action="rpg.php" method="post">
	<input type="hidden" name="action" value="bank" />
	<input type="submit" name="clear2history" value="清除所有匯款記錄" class="mainoption" />
	</form>
	</td>
	</tr>';
}


if ( isset($HTTP_POST_VARS['deposit']) || isset($HTTP_POST_VARS['withdraw']) || isset($HTTP_POST_VARS['remit']))
{
	$profiledata = get_userdata(intval($userdata['uid']));
}

$rpg_bank_amount = 0;
$sql = 'SELECT sum(user_bank) as bank_amount FROM ' . USERS_TABLE;
if( !($result = $db->sql_query($sql)) )
{
	message_die(GENERAL_ERROR, '無法取得銀行總值!!', '', __LINE__, __FILE__, $sql);
}
if ( $row = $db->sql_fetchrow($result) )
{
	$rpg_bank_amount = $row['bank_amount'];
}

// 產生資料頁面
$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_LOAN' => number_format($profiledata['user_loan']),
	'RPG_BANK_FEES' => 100 * $board_config['bank_fees'],
	'RPG_BANK_LOANFEES' => 100 * $board_config['bank_loanfees'],
	'RPG_PROCEDURE_DEPOSIT' => 100 * $board_config['bank_procedure_deposit'],
	'RPG_PROCEDURE_LOAN' => 100 * $board_config['bank_procedure_loan'],
	'RPG_PROCEDURE_REMIT' => 100 * $board_config['bank_procedure_remit'],
	'RPG_BANK_LOANMAX' => number_format($board_config['bank_loanmax']),
	'RPG_BANK_TIME' => $profiledata['user_bank_time'] ? create_date($userdata['user_dateformat'], $profiledata['user_bank_time'], $board_config['board_timezone']) : $lang['Never_last_logon'],
	'RPG_LOAN_TIME' => $profiledata['user_loan_time'] ? create_date($userdata['user_dateformat'], $profiledata['user_loan_time'], $board_config['board_timezone']) : $lang['Never_last_logon'],
	'RPG_BANK_AMOUNT' => number_format($rpg_bank_amount),
	'RPB_BANK_HISTORY' => $rpg_bank_history,
	'RPG_BANK_RESULT' => $rpg_bank_result . '<hr>',
	'L_NOWMENU' => '水怪大陸銀行') );

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

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

?>