<?php

define('MAX_OWNS', 50);

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

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

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

// 使用道具
if (isset($HTTP_GET_VARS['itemid']) || isset($HTTP_POST_VARS['itemid']))
{
	$ownid = intval(( isset($HTTP_GET_VARS['ownid']) ) ? $HTTP_GET_VARS['ownid'] : $HTTP_POST_VARS['ownid']);
	$itemid = intval(( isset($HTTP_GET_VARS['itemid']) ) ? $HTTP_GET_VARS['itemid'] : $HTTP_POST_VARS['itemid']);

	if (isset($HTTP_GET_VARS['addition1']) || isset($HTTP_POST_VARS['addition1']))
	{
		if ( CheckRefresh(0, intval($userdata['uid']), 10) )
		{
			message_die(GENERAL_MESSAGE, '為了避免系統負荷過重，請間隔 10 秒再使用道具!!' . $returnurl);
		}

		$add1 = (isset($HTTP_GET_VARS['addition1']))? $HTTP_GET_VARS['addition1'] : $HTTP_POST_VARS['addition1'];
		if (isset($HTTP_GET_VARS['addition2']) || isset($HTTP_POST_VARS['addition2']))
		{
			$add2 = (isset($HTTP_GET_VARS['addition2']))? $HTTP_GET_VARS['addition2'] : $HTTP_POST_VARS['addition2'];
		}
		else
		{
			$add2 = '';
		}
		UseAdditionItem($itemid, $ownid, $add1, $add2);
	}
	else
	{
		$sql = 'SELECT * FROM xoops_pbb_rpg_items WHERE item_id = ' . $itemid;
		if ( !($result = $db->sql_query($sql)) )
		{
			message_die(GENERAL_ERROR, '無法取得商品詳細資料!!', '', __LINE__, __FILE__, $sql);
		}
		if ( $row = $db->sql_fetchrow($result) )
		{
			$gen_simple_header = TRUE;
			$page_title = "使用道具";
			$dialogcontent = '';
			if (($row['item_addition1'] != '') && ($row['item_addition2'] != ''))
			{
				$dialogcontent .= $row['item_addition1'] . '<br /><input type="text" name="addition1" maxlength="12" class="post" /><br />';
				$dialogcontent .= $row['item_addition2'] . '<br /><input type="text" name="addition2" class="post" /><br />';
				$inputsumit = '<input type="submit" name="dialogsubmit" value="確定" class="liteoption"
					onClick="GoBack2MainForm(this.form.addition1.value, this.form.addition2.value); return false;"/>';
			}
			else
			{
				$dialogcontent .= $row['item_addition1'] . '<br /><input type="text" name="addition1" maxlength="12" class="post" /><br />';
				$inputsumit = '<input type="submit" name="dialogsubmit" value="確定" class="liteoption"
					onClick="GoBack2MainForm(this.form.addition1.value, \'\'); return false;"/>';
			}
			$dialogtitle = $row['item_name'];

			include($phpbb_root_path . 'includes/page_header.'.$phpEx);
			$template->set_filenames(array('dialogbody' => 'rpg_dialog.tpl'));
			$template->assign_vars(array(
				'S_DIALOG_ACTION' => 'rpg.php?action=items&itemid=' . $itemid . '&ownid=' . $ownid,
				'L_DIALOG_TITLE' => $dialogtitle,
				'L_DIALOG_SUBMIT' => $inputsumit,
				'L_DIALOG_CONTENT' => $dialogcontent));
			$template->pparse('dialogbody');
			include($phpbb_root_path . 'includes/page_tail.'.$phpEx);
		}
		exit;
	}
}

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

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

// 打工
// 新的應徵方式: 沒人作時直接上工，有人作時加入排班
if ( isset($HTTP_GET_VARS['job']) || isset($HTTP_POST_VARS['job']) )
{
	$sql = 'SELECT COUNT(*) as already_work FROM xoops_pbb_rpg_jobs WHERE worker_id = ' . $profiledata['uid'] .
		' AND (start_day + 43200) >= ' . time();
	if ( $result = $db->sql_query($sql) )
	{
		$row = $db->sql_fetchrow($result);
		if ($row['already_work'] > 0)
		{
			$itemerror = '一人不可兼任數職，請將機會讓給別人!!';
		}
	}

	if ( $itemerror == '' )
	{
		$sql = 'SELECT COUNT(*) as already_join FROM xoops_pbb_rpg_joinjobs WHERE uid = ' . $profiledata['uid'];
		if ( $result = $db->sql_query($sql) )
		{
			$row = $db->sql_fetchrow($result);
			if ($row['already_join'] > 0)
			{
				$itemerror = '您已經加入排班了!!';
			}
		}
	}
	if ( $itemerror == '' )
	{
		$jobid = intval(( isset($HTTP_GET_VARS['job']) ) ? $HTTP_GET_VARS['job'] : $HTTP_POST_VARS['job']);
		$sql = 'SELECT * FROM xoops_pbb_rpg_jobs WHERE job_id = ' . $jobid;
		$result = $db->sql_query($sql);
		if ( $row = $db->sql_fetchrow($result) )
		{
			if (($row['start_day'] == 0) && ($row['worker_id'] == 0) )
			{
				// 沒人作，直接上工
				if ($profiledata['user_money'] < $row['apply_fee'])
				{
					$itemerror = '現金不足，繳不出報名費!!';
				}
				else
				{
					$sql = 'update xoops_users set user_money = user_money - ' . $row['apply_fee'] . ' where uid = ' . $profiledata['uid'];
					$db->sql_query($sql);
					$sql = 'update xoops_pbb_rpg_jobs set start_day = ' . time() . ', worker_id = ' . $profiledata['uid'] .
						', worker_name = "' . $profiledata['uname'] . '", job_salary = 0 where job_id = ' . $jobid;
					$db->sql_query($sql);
					$profiledata['user_money'] -= $row['apply_fee'];
				}
			}
			else
			{
				if ($profiledata['user_money'] < $row['apply_fee'])
				{
					$itemerror = '現金不足，繳不出報名費!!';
				}
				else
				{
					$sql = 'update xoops_users set user_money = user_money - ' . $row['apply_fee'] . ' where uid = ' . $profiledata['uid'];
					$db->sql_query($sql);
					$sql = 'insert into xoops_pbb_rpg_joinjobs values (' . $profiledata['uid'] . ', ' . $jobid . ', 0, ' . time() . ')';
					$db->sql_query($sql);
					$profiledata['user_money'] -= $row['apply_fee'];
				}

			}
		}
	}
}

// 買道具
if ( isset($HTTP_GET_VARS['buy']) || isset($HTTP_POST_VARS['buy']) )
{
	$sql = 'SELECT COUNT(*) as take_count FROM xoops_pbb_rpg_owns WHERE uid = ' . $profiledata['uid'];
	$take_count = 0;
	if ( $result = $db->sql_query($sql) )
	{
		if ( $row = $db->sql_fetchrow($result) )
		{
			$take_count = $row['take_count'];
		}
	}
	$itemerror = '';
	while( list($item_id, $item_count) = @each($HTTP_POST_VARS['buy_count']) )
	{
		$item_count = intval($item_count);
		$take_count += $item_count;
		if ( $take_count > MAX_OWNS )
		{
			message_die(GENERAL_MESSAGE, '一個人最多只能攜帶' . MAX_OWNS . '項物品(含武器、防具、魔法技能)，您目前有' .	($take_count - $item_count) . '項物品。' . $returnurl);
		}

		if ( $item_count < 0 )
		{
			message_die(GENERAL_ERROR, '單項購買數量不得為負值!!', '', __LINE__, __FILE__, $sql);
		}
		else if ( $item_count > 0 )
		{
			$sql = 'SELECT * FROM xoops_pbb_rpg_items WHERE in_which_store = 0 AND item_id = ' . $item_id;
			if ( !($result = $db->sql_query($sql)) )
			{
				message_die(GENERAL_ERROR, '無法取得商品詳細資料!!', '', __LINE__, __FILE__, $sql);
			}
			if ( $row = $db->sql_fetchrow($result) )
			{
				$sql = '';
				$job_salary = $row['item_price'] * $item_count;
				if ($row['item_spec_price'] > 0)
				{
					if ($profiledata['user_money'] >=
						floor( ($profiledata['user_money'] + $profiledata['user_bank']) * $row['item_spec_price'] / 100 * $item_count ))
					{
						if ( $profiledata['user_money'] < $row['item_price'] * $item_count )
						{
							$itemerror = '現金不足!!';
						}
						else
						{
							$sql = 'UPDATE xoops_users SET user_money = user_money - ' . ($row['item_price'] * $item_count) .
								' WHERE uid = ' . intval($userdata['uid']);
						}
					}
					else
					{
						$job_salary = floor( ($profiledata['user_money'] + $profiledata['user_bank']) * $row['item_spec_price'] / 100 * $item_count);
						if ( $job_salary < $row['item_price'] * $item_count )
						{
							$itemerror = '資產不足!!';
						}
						else
						{
							$remainpercent = 100 - ( $row['item_spec_price'] * $item_count );
							if ( $remainpercent < 0 )
							{
								$itemerror = '資產不足!!';
							}
							else
							{
								$sql = 'UPDATE xoops_users SET user_money = FLOOR(user_money * ' . $remainpercent .
									' / 100), user_bank = FLOOR(user_bank * ' . $remainpercent .
									' / 100) WHERE uid = ' . intval($userdata['uid']);
							}
						}
					}
				}
				else
				{
					if ($profiledata['user_money'] < $row['item_price'] * $item_count)
					{
						$itemerror = '現金不足!!';
					}
					else
					{
						$sql = 'UPDATE xoops_users SET user_money = user_money - ' . ( $row['item_price'] * $item_count ) .
							' WHERE uid = ' . intval($userdata['uid']);
					}
				}
				if ($sql != '')
				{
					if ( !$db->sql_query($sql) )
					{
						message_die(GENERAL_ERROR, '無法進行扣款作業!!', '', __LINE__, __FILE__, $sql);
					}
					for ($i=1; $i<=$item_count; $i++)
					{
						$sql = 'INSERT INTO xoops_pbb_rpg_owns (uid, item_id) VALUES (' . intval($userdata['uid']) . ', ' . $item_id . ')';
						if ( !$db->sql_query($sql) )
						{
							message_die(GENERAL_ERROR, '商品無法售出!!', '', __LINE__, __FILE__, $sql);
						}
					}
					$sql = 'UPDATE xoops_pbb_rpg_jobs SET job_salary = job_salary + FLOOR(' . $job_salary .
						' * profit_percent / 100) WHERE in_which_store = 0 AND (start_day + 43200) >= ' . time() .
						' AND worker_id > 0';
					if ( !$db->sql_query($sql) )
					{
						message_die(GENERAL_ERROR, '無法分配打工盈餘!!', '', __LINE__, __FILE__, $sql);
					}
					$profiledata = get_userdata(intval($userdata['uid']));
				}
				else
				{
					$itemerror .= ' - ' . $row['item_name'] . ' × ' . $item_count;
				}
			}
			else
			{
				message_die(GENERAL_ERROR, '無法取得商品詳細資料!!', '', __LINE__, __FILE__, $sql);
			}
		}
		if ($itemerror != '') break;
	}
}

// 賣道具
if ( isset($HTTP_GET_VARS['sell']) || isset($HTTP_POST_VARS['sell']) )
{
	$ownid = intval(( isset($HTTP_GET_VARS['sell']) ) ? $HTTP_GET_VARS['sell'] : $HTTP_POST_VARS['sell']);
	$sql = 'SELECT * FROM xoops_pbb_rpg_owns WHERE own_id = ' . $ownid . ' AND uid = ' . intval($userdata['uid']);
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, '無法取得攜帶道具列表!!', '', __LINE__, __FILE__, $sql);
	}
	if ( $row = $db->sql_fetchrow($result) )
	{
		$itemid = $row['item_id'];
	}
	else
	{
		message_die(GENERAL_ERROR, '找不到欲賣出的道具!!', '', __LINE__, __FILE__, $sql);
	}
	$sql = 'SELECT * FROM xoops_pbb_rpg_items WHERE item_id = ' . $itemid;
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, '無法取得商品詳細資料!!', '', __LINE__, __FILE__, $sql);
	}
	if ( $row = $db->sql_fetchrow($result) )
	{
		$sql = 'DELETE FROM xoops_pbb_rpg_owns WHERE own_id = ' . $ownid . ' AND uid = ' . intval($userdata['uid']);
		if ( !$db->sql_query($sql) )
		{
			message_die(GENERAL_ERROR, '無法賣出道具!!', '', __LINE__, __FILE__, $sql);
		}
		$sql = 'UPDATE xoops_users SET user_money = user_money + ' . floor($row['item_price'] * 0.6) .
			' WHERE uid = ' . intval($userdata['uid']);
		if ( !$db->sql_query($sql) )
		{
			message_die(GENERAL_ERROR, '無法更新現金!!', '', __LINE__, __FILE__, $sql);
		}
		$profiledata['user_money'] += floor($row['item_price'] * 0.6);
	}
	else
	{
		message_die(GENERAL_ERROR, '無法取得商品詳細資料!!', '', __LINE__, __FILE__, $sql);
	}
}

// 使用道具 - 不用其它資訊
if ( isset($HTTP_GET_VARS['use']) || isset($HTTP_POST_VARS['use']) )
{
	if ( CheckRefresh(0, intval($userdata['uid']), 10) )
	{
		message_die(GENERAL_MESSAGE, '為了避免系統負荷過重，請間隔 10 秒再使用道具!!' . $returnurl);
	}

	$ownid = intval(( isset($HTTP_GET_VARS['use']) ) ? $HTTP_GET_VARS['use'] : $HTTP_POST_VARS['use']);
	$sql = 'SELECT * FROM xoops_pbb_rpg_owns WHERE own_id = ' . $ownid . ' AND uid = ' . intval($userdata['uid']);
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, '無法取得攜帶道具列表!!', '', __LINE__, __FILE__, $sql);
	}
	if ( $row = $db->sql_fetchrow($result) )
	{
		$itemid = $row['item_id'];
	}
	else
	{
		message_die(GENERAL_ERROR, '找不到欲使用的道具!!', '', __LINE__, __FILE__, $sql);
	}
	$haveused = false;
	switch ($itemid)
	{
		// 拔釘器
		case 1:
			if ( isset($HTTP_GET_VARS['all']) || isset($HTTP_POST_VARS['all']) )
			{
				$sql = 'SELECT count(*) as item_count FROM xoops_pbb_rpg_owns WHERE item_id = 1 AND uid = ' . intval($userdata['uid']);
				$result = $db->sql_query($sql);
				$row = $db->sql_fetchrow($result);
				$sql = 'UPDATE xoops_users SET user_robfail = user_robfail - ' . intval($row['item_count']) . ' WHERE uid = ' . intval($userdata['uid']);
				$db->sql_query($sql);
				$sql = 'UPDATE xoops_users SET user_robfail = 0 WHERE user_robfail < 0 AND uid = ' . intval($userdata['uid']);
				$db->sql_query($sql);
				$sql = 'DELETE FROM xoops_pbb_rpg_owns WHERE item_id = 1 AND uid = ' . intval($userdata['uid']);
				$db->sql_query($sql);
				$profiledata['user_robfail'] -= intval($row['item_count']);
				if ($profiledata['user_robfail'] < 0) $profiledata['user_robfail'] = 0;
			}
			else
			{
				$sql = 'UPDATE xoops_users SET user_robfail = user_robfail - 1
					WHERE user_robfail > 0 AND uid = ' . intval($userdata['uid']);
				if ($profiledata['user_robfail'] > 0)
				{
					$profiledata['user_robfail']--;
				}
				if ( !$db->sql_query($sql) )
				{
					message_die(GENERAL_ERROR, '嗚....拔不出釘子!!', '', __LINE__, __FILE__, $sql);
				}
			}
			$haveused = true;
			break;
		// 便便
		case 101:
			$sql = 'UPDATE xoops_users SET rpg_cur_mp = rpg_max_mp WHERE uid = ' . intval($userdata['uid']);
			if ( !$db->sql_query($sql) )
			{
				message_die(GENERAL_ERROR, '嗚....無法回復 MP !!', '', __LINE__, __FILE__, $sql);
			}
			$haveused = true;
			break;
		// 羽毛
		case 104:
			$sql = 'UPDATE xoops_users SET rpg_cur_hp = rpg_max_hp WHERE uid = ' . intval($userdata['uid']);
			if ( !$db->sql_query($sql) )
			{
				message_die(GENERAL_ERROR, '嗚....無法回復 HP !!', '', __LINE__, __FILE__, $sql);
			}
			$haveused = true;
			break;
		// 祝福
		case 105:
			$sql = 'UPDATE xoops_users SET rpg_cur_hp = rpg_max_hp, rpg_cur_mp = rpg_max_mp WHERE uid = ' . intval($userdata['uid']);
			if ( !$db->sql_query($sql) )
			{
				message_die(GENERAL_ERROR, '嗚....無法回復 HP / MP !!', '', __LINE__, __FILE__, $sql);
			}
			$haveused = true;
			break;
	}
	if ($haveused)
	{
		$sql = 'DELETE FROM xoops_pbb_rpg_owns WHERE own_id = ' . $ownid . ' AND uid = ' . intval($userdata['uid']);
		if ( !$db->sql_query($sql) )
		{
			message_die(GENERAL_ERROR, '無法更新道具清單!!', '', __LINE__, __FILE__, $sql);
		}
	}
}

// 取得工作列表
$no_need_to_join = false;
$sql = 'select count(*) as have_join from xoops_pbb_rpg_jobs where worker_id = ' . $profiledata['uid'];
$result = $db->sql_query($sql);
if ($row = $db->sql_fetchrow($result))
{
	$no_need_to_join = $row['have_join'] > 0;
}

if ( !($no_need_to_join) )
{
	$sql = 'select count(*) as have_join from xoops_pbb_rpg_joinjobs where uid = ' . $profiledata['uid'];
	$result = $db->sql_query($sql);
	if ($row = $db->sql_fetchrow($result))
	{
		$no_need_to_join = $row['have_join'] > 0;
	}
}

if ( $no_need_to_join )
{
	$sql = 'select job_id, count(*) as join_count from xoops_pbb_rpg_joinjobs where in_which_store = 0 group by job_id';
	$result = $db->sql_query($sql);
	if ($row = $db->sql_fetchrow($result))
	{
		$join_count = array();
		do
		{
			$join_count[$row['job_id']] = $row['join_count'];
		}
		while ($row = $db->sql_fetchrow($result));
	}
}



$jobslist = '';
$sql = 'select * from xoops_pbb_rpg_jobs where in_which_store = 0 order by apply_fee desc';
$result = $db->sql_query($sql);
if ( $row = $db->sql_fetchrow($result) )
{
	$i = 1;
	$jobslist = '<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>
			<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
	{
		$jobslist .= '<tr><td class="row3" align="center"><span class="gensmall">' . $i .
				'</span></td><td><span class="gensmall">' . $row['job_name'] .
				'</span></td><td align="right"><span class="gensmall">' . number_format($row['apply_fee']) .
				'</span></td><td align="right"><span class="gensmall">' . $row['profit_percent'] .
				'</span></td><td><span class="gensmall">';
		if ( ((time() > ($row['start_day'] + 43200)) && ($row['start_day'] > 0)) || ($row['start_day'] == 0) )
		{
			$jobslist .= '缺人中</span></td><td></td><td></td>';
			$jobaction = '應徵';
		}
		else
		{
			$jobslist .= $row['worker_name'] . '</span></td><td align="center"><span class="gensmall">嘿嘿....不告訴你!!' .
//					(create_date($userdata['user_dateformat'], ($row['start_day'] + 43200), $board_config['board_timezone'])) .
					'</span></td><td align="right"><span class="gensmall">' . $row['job_salary'] . '</span></td>';
			$jobaction = '排班';
		}
		$jobslist .= '<td align="center"><span class="gensmall">' .
				($no_need_to_join ? (empty($join_count[$row['job_id']]) ? 'N/A' : '排班人數:' . $join_count[$row['job_id']]) : '<a href=rpg.php?action=items&job=' . $row['job_id'] . '>' . $jobaction . '</a>') .
				'</span></td></tr>';

		// 發薪時間
		if ((time() > ($row['start_day'] + 43200)) && ($row['start_day'] > 0))
		{
			$sql = 'update xoops_users set user_money = user_money + ' . $row['job_salary'] . ' where uid = ' . $row['worker_id'];
			$db->sql_query($sql);
			if ($row['worker_id'] == $profiledata['uid'])
				$profiledata['user_money'] += $row['job_salary'];
			$sql = 'update xoops_pbb_rpg_jobs set start_day = 0, worker_id = null, worker_name = null, job_salary = 0 where job_id = ' . $row['job_id'];
			$db->sql_query($sql);
			$sql = 'select j.uid, u.uname from xoops_pbb_rpg_joinjobs j, xoops_users u where j.job_id = ' . $row['job_id'] .
				' and j.uid = u.uid order by j.join_time limit 0, 1';
			$result2 = $db->sql_query($sql);
			if ( $row2 = $db->sql_fetchrow($result2) )
			{
				$sql = "update xoops_pbb_rpg_jobs set start_day = " . time() . ", worker_id = " . $row2['uid'] .
					", worker_name = '" . $row2['uname'] . "', job_salary = 0 where job_id = " . $row['job_id'];
				$db->sql_query($sql);
				$sql = "delete from xoops_pbb_rpg_joinjobs where uid = " . $row2['uid'];
				$db->sql_query($sql);
			}
		}
		$i++;
	}
	while ( $row = $db->sql_fetchrow($result) );
	$jobslist .= '</table><span class="gensmall">若沒人上工，則直接錄取；若有人上工，可加入排班，但須預繳報名費。<br />到任時，系統會從排班列表中取出下一位排班者接任。已有工作者不得加入排班並連任。(後續無人接任不在此限)</span>';
}

// 取得商品列表
$itemlist = '';
$sql = 'SELECT * FROM xoops_pbb_rpg_items WHERE in_which_store = 0 ORDER BY item_id';
if ( !($result = $db->sql_query($sql)) )
{
	message_die(GENERAL_ERROR, '無法取得商品列表!!', '', __LINE__, __FILE__, $sql);
}
if ( $row = $db->sql_fetchrow($result) )
{
	$i = 1;
	$itemlist = '<form action="rpg.php" method="post" name="Go2Buy">
		<input type="hidden" name="action" value="items">
		<table class="forumline" width="100%" cellspacing="1" cellpadding="3" border="0">
		<tr><td class="catLeft" align="center" width="5%"><b><span class="gen">#</span></b></td>
		<td class="catLeft" align="center" width="15%"><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
	{
		$itemlist .= '<tr><td class="row3" align="center"><span class="gensmall">' . $i .
				'</span></td><td><span class="gensmall">' . $row['item_name'] .
				'</span></td><td><span class="gensmall">' . $row['item_description'] .
				'</span></td><td align="right" width="20%"><span class="gensmall">';
		if ($row['item_spec_price'] > 0)
		{
			$itemlist .= 'max(' . number_format($row['item_price']) . ', ' . $row['item_spec_price'] . '% 財產)';
		}
		else
		{
			$itemlist .= number_format($row['item_price']);
		}
		$itemlist .= '</span></td><td align="center"><input type="text" name="buy_count[' . $row['item_id'] . ']" value="0" size="4" align="right"></td></tr>';
		$i++;
	}
	while ( $row = $db->sql_fetchrow($result) );
	$itemlist .= '<tr><td colspan="5" align="right" class="catBottom"><input type="submit" name="buy" value="購買" class="liteoption" style="width: 43px;"></td></tr></table>';
}

// 取得攜帶列表
$ownlist = '無';
$sql = 'SELECT o.own_id, i.* FROM xoops_pbb_rpg_items i, xoops_pbb_rpg_owns o WHERE i.in_which_store = 0 AND i.item_id = o.item_id AND o.uid = ' .
	intval($userdata['uid']) . ' ORDER BY i.item_id LIMIT 0, 50';
if ( !($result = $db->sql_query($sql)) )
{
	message_die(GENERAL_ERROR, '無法取得攜帶列表!!', '', __LINE__, __FILE__, $sql);
}
if ( $row = $db->sql_fetchrow($result) )
{
	$i = 1;
	$ownlist = '<table class="forumline" width="100%" cellspacing="1" cellpadding="3" border="0">
		<tr><td class="catLeft" align="center" width="5%"><b><span class="gen">#</span></b></td>
		<td class="catLeft" align="center" width="15%"><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 class="catLeft" align="center" width="13%"><b><span class="gen">動作</span></b></td></tr>';
	do
	{
		$ownlist .= '<tr><td class="row3" align="center"><span class="gensmall">' . $i .
				'</span></td><td><span class="gensmall">' . $row['item_name'] .
				'</span></td><td><span class="gensmall">' . $row['item_description'] .
				'</span></td><td align="right"><span class="gensmall">' . floor($row['item_price'] * 0.6) .
				'</span></td><td align="center"><span class="gensmall"><a href=rpg.php?action=items&sell=' .
				$row['own_id'] . '>賣出</a>/';
		if ($row['need_to_use'])
		{
			if (($row['item_addition1'] != '') || ($row['item_addition2'] != ''))
			{
				$ownlist .= '<a href="rpg.php?action=items&itemid=' . $row['item_id'] . '&ownid=' . $row['own_id'] .
					'" onclick="_itemaddition=window.open(\'rpg.php?action=items&itemid=' . $row['item_id'] . '&ownid=' . $row['own_id'] .
					'\', \'_itemaddition\', \'height=400,resizable=yes,scrollbars=yes,width=320,height=320\'); _itemaddition.setfocus();return false;" target="_itemaddition">使用</a></span></td></tr>';
			}
			else
			{
				$ownlist .= '<a href=rpg.php?action=items&use=' . $row['own_id'] . '>使用';
				if ($row['item_id'] == 1)
				{
					$ownlist .= '</a>/<a href=rpg.php?action=items&use=' . $row['own_id'] . '&all=1>全用</a></span></td></tr>';
				}
				else
				{
					$ownlist .= '</a></span></td></tr>';
				}
			}
		}
		else
		{
			$ownlist .= '使用</span></td></tr>';
		}
		$i++;
	}
	while ( $row = $db->sql_fetchrow($result) );
	$ownlist .= '</table>';
}

// 產生資料頁面
$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_JOBS_LIST' => $jobslist,
	'RPG_ITEM_LIST' => $itemlist,
	'RPG_OWN_LIST' => $ownlist,
	'RPG_ITEM_ERROR' => $itemerror,
	'L_NOWMENU' => '水怪的道具店') );

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

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

?>