<?php

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

function GetAvatar($profiledata)
{
	global $board_config;

	$avatar_img = '';
	if ( $profiledata['user_avatar_type'] && $profiledata['user_allowavatar'] )
	{
		switch( $profiledata['user_avatar_type'] )
		{
			case USER_AVATAR_UPLOAD:
				$avatar_img = ( $board_config['allow_avatar_upload'] ) ? '<img src="' . $board_config['avatar_path'] . '/' . $profiledata['user_avatar'] . '" border="0" />' : '';
				break;
			case USER_AVATAR_REMOTE:
				$avatar_img = ( $board_config['allow_avatar_remote'] ) ? '<img src="' . $profiledata['user_avatar'] . '" border="0" />' : '';
				break;
			case USER_AVATAR_GALLERY:
				$avatar_img = ( $board_config['allow_avatar_local'] ) ? '<img src="' . $board_config['avatar_gallery_path'] . '/' . $profiledata['user_avatar'] . '" border="0" />' : '';
				break;
		}
	}
	if ($profiledata['user_robfail'] > 0)
	{
		$avatar_img .= '<br />';
		$robid = floor(($profiledata['user_robfail'] - 1) / 4);
		$robcnt = (($profiledata['user_robfail'] - 1) % 4) + 1;
		if ($robid <= 7)
		{
			for ($i=0; $i < $robcnt; $i++)
			{
				$avatar_img .= '<img src="images/rob' . $robid . '.gif" />';
			}
		}
		else
		{
				$avatar_img .= '<img src="images/rob7.gif" />× ' . ($profiledata['user_robfail'] - 28);
		}
		$avatar_img .= '<br /><span class="gensmall">笨！失敗的搶匪</span>';
	}
	return $avatar_img;
}

function PM_User($toid, $fromid, $msubject, $mcontent)
{
	global $db;
	$sql = "INSERT INTO " . PRIVMSGS_TABLE . " (privmsgs_type, privmsgs_subject, privmsgs_from_userid, privmsgs_to_userid, privmsgs_date)
		VALUES (0, '" . $msubject . "', " . $fromid . ", " . $toid . ", " . time() . ")";
	if( !$db->sql_query($sql) )
	{
		message_die(GENERAL_ERROR, "無法傳遞個人訊息(PRIVMSGS)!!", "", __LINE__, __FILE__, $sql);
	}
	$privmsg_sent_id = $db->sql_nextid();
	$sql = "INSERT INTO " . PRIVMSGS_TEXT_TABLE . " (privmsgs_text_id, privmsgs_text) VALUES (" . $privmsg_sent_id .
		", '" . $mcontent . "')";
	if( !$db->sql_query($sql) )
	{
		message_die(GENERAL_ERROR, "無法傳遞個人訊息(PRIVMSGS_TEXT)!!", "", __LINE__, __FILE__, $sql);
	}
	$sql = "UPDATE xoops_users SET user_new_privmsg = user_new_privmsg + 1 WHERE uid = " . $toid;
	if( !$db->sql_query($sql) )
	{
		message_die(GENERAL_ERROR, "無法傳遞個人訊息(xoops_users)!!", "", __LINE__, __FILE__, $sql);
	}
	return true;
}

function CheckRefresh($typeid, $userid, $timeout)
{
	global $db;

	$too_fast = true;
	$sql = "select count(*) as data_exists, max(last_time) as last_time from xoops_pbb_rpg_interval
		where interval_type = $typeid and uid = $userid";
	if ($result = $db->sql_query($sql))
	{
		if ( $row = $db->sql_fetchrow($result) )
		{
			if ($row['data_exists'] == 0)
			{
				$sql = "insert into xoops_pbb_rpg_interval (interval_type, uid, last_time)
					values ($typeid, $userid, " . time() . ")";
				if ( $db->sql_query($sql) ) $too_fast = false;
			}
			else if ( (time() - $row['last_time']) >= $timeout )
			{
				$sql = "update xoops_pbb_rpg_interval set last_time = " . time() .
					" where interval_type = $typeid and uid = $userid";
				if ( $db->sql_query($sql) ) $too_fast = false;
			}
		}
	}
	return $too_fast;
}

function UseAdditionItem($itemid, $ownid, $add1, $add2)
{
	global $db, $userdata, $returnurl, $board_config;

	$sql = 'SELECT COUNT(*) as use_count FROM xoops_pbb_rpg_owns WHERE item_id = ' . $itemid . ' and 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) )
	{
		if ($row['use_count'] == 0)
		{
			message_die(GENERAL_ERROR, '找不到欲使用的道具!!', '', __LINE__, __FILE__, $sql);
		}
	}

	$haveused = false;
	switch ($itemid)
	{
		// 自訂頭銜
		case 2:
			$add1 = ereg_replace("&amp;", "&", htmlspecialchars(trim($add1)));
			$userdata['user_rank'] = intval($userdata['user_rank']);
			if ($userdata['user_rank'] > 0)
			{
				$sql = 'SELECT COUNT(*) as rank_count FROM xoops_users WHERE user_rank = ' . $userdata['user_rank'] .
					' AND uid <> ' . intval($userdata['uid']);
				if ( !($result = $db->sql_query($sql)) )
				{
					message_die(GENERAL_ERROR, '無法取得頭銜狀態!!', '', __LINE__, __FILE__, $sql);
				}
				if ( $row = $db->sql_fetchrow($result) )
				{
					if ($row['rank_count'] == 0)
					{
						$sql = 'DELETE FROM xoops_pbb_ranks WHERE rank_id = ' . $userdata['user_rank'];
						if ( !$db->sql_query($sql) )
						{
							message_die(GENERAL_ERROR, '無法刪除先前的頭銜!!', '', __LINE__, __FILE__, $sql);
						}
					}
				}
			}
			$sql = 'SELECT COUNT(*) as rank_count FROM xoops_pbb_ranks WHERE rank_title = "' . $add1 . '" AND rank_special = 1';
			if ( !($result = $db->sql_query($sql)) )
			{
				message_die(GENERAL_ERROR, '無法取得現有頭銜!!', '', __LINE__, __FILE__, $sql);
			}
			if ( $row = $db->sql_fetchrow($result) )
			{
				if ($row['rank_count'] == 0)
				{
					$sql = 'INSERT INTO xoops_pbb_ranks (rank_title, rank_min, rank_special, rank_image) VALUES ("' .
						$add1 . '", -1, 1, "' . $add2 . '")';
					if ( !$db->sql_query($sql) )
					{
						message_die(GENERAL_ERROR, '無法新增頭銜!!', '', __LINE__, __FILE__, $sql);
					}
				}
			}
			$sql = 'SELECT rank_id FROM xoops_pbb_ranks WHERE rank_title = "' . $add1 . '" AND rank_special = 1';
			if ( !($result = $db->sql_query($sql)) )
			{
				message_die(GENERAL_ERROR, '無法取得頭銜列表!!', '', __LINE__, __FILE__, $sql);
			}
			if ( $row = $db->sql_fetchrow($result) )
			{
				$sql = 'UPDATE xoops_users SET user_rank = ' . $row['rank_id'] .
					' WHERE uid = ' . intval($userdata['uid']);
				if ( !$db->sql_query($sql) )
				{
					message_die(GENERAL_ERROR, '嗚!!缺紙，無法列印名片!!', '', __LINE__, __FILE__, $sql);
				}
			}
			$haveused = true;
			break;
		// 圖書禮券
		case 3:
			PM_User(1, intval($userdata['uid']), "領取圖書通知", $add1);
			$haveused = true;
			break;
		// 神偷手套
		case 4:
			$sql = 'SELECT uid, user_money, user_bank, user_robfail FROM xoops_users WHERE uname = "' . $add1 . '"';
			if ( !($result = $db->sql_query($sql)) )
			{
				message_die(GENERAL_ERROR, '無法取得受害者資料!!', '', __LINE__, __FILE__, $sql);
			}
			if ( !($row = $db->sql_fetchrow($result)) )
			{
				message_die(GENERAL_ERROR, '找不到受害者!!', '', __LINE__, __FILE__, $sql);
			}
			$sql = 'SELECT own_id FROM xoops_pbb_rpg_owns WHERE item_id = 100 AND uid = ' . $row['uid'];
			if ( $result = $db->sql_query($sql) )
			{
				if ($row2 = $db->sql_fetchrow($result))
				{
					$sql = 'DELETE FROM xoops_pbb_rpg_owns WHERE own_id = ' . $row2['own_id'];
					$db->sql_query($sql);
					$sql = 'UPDATE xoops_users SET user_robfail = user_robfail + 10 WHERE uid = ' . intval($userdata['uid']);
					$db->sql_query($sql);
					$sql = 'DELETE FROM xoops_pbb_rpg_owns WHERE own_id = ' . $ownid . ' AND uid = ' . intval($userdata['uid']);
					$db->sql_query($sql);
					message_die(GENERAL_MESSAGE, "您偷到的是....假鈔!!". $returnurl);
				}
			}


			$sql = 'UPDATE xoops_users SET user_bank = FLOOR(user_bank * 0.8)';
			$stealamount = floor($row['user_bank'] * 0.2);
			if ($row['user_robfail'] > 0)
			{
				if ($row['user_robfail'] > 50)
				{
					$row['user_robfail'] = 50;
				}
				$stealamount += floor($row['user_money'] * $row['user_robfail'] * 2 / 100);
				$sql .= ', user_money = FLOOR(user_money * ' . (100 - $row['user_robfail'] * 2) . ' / 100)';
			}
			$sql .= ' WHERE uid = ' . $row['uid'];
			if( !$db->sql_query($sql) )
			{
				message_die(GENERAL_ERROR, "老天爺有問題，偷不到....", "", __LINE__, __FILE__, $sql);
			}
			$sql = 'UPDATE xoops_users SET user_money = user_money + ' . $stealamount .
				', user_robfail = user_robfail + 10 WHERE uid = ' . intval($userdata['uid']);
			if( !$db->sql_query($sql) )
			{
				message_die(GENERAL_ERROR, "老天爺不賞臉，偷不到....", "", __LINE__, __FILE__, $sql);
			}
			PM_User($row['uid'], $row['uid'], "被偷通知",
				"被偷存款: " . floor($row['user_bank'] * 0.2) .
				" \n被偷現金: " . floor($row['user_money'] * $row['user_robfail'] * 2 / 100) .
				" \n總　　計: " . $stealamount);
			$haveused = true;
			break;
		// 草人禮盒
		case 5:
			srand((double)microtime()*1000000);
			$randid = rand(1, 100);
			if ($randid > 80)
			{
				$sql = 'UPDATE xoops_users SET user_robfail = user_robfail + 1 WHERE uid = ' . intval($userdata['uid']);
				if( $db->sql_query($sql) )
				{
					message_die(GENERAL_MESSAGE, "送禮不成反害己，結果值: " . $randid . $returnurl);
				}
			}
			else
			{
				$sql = 'SELECT uid, user_session_time FROM xoops_users WHERE uname = "' . $add1 . '"';
				if ( !($result = $db->sql_query($sql)) )
				{
					message_die(GENERAL_ERROR, '無法取得受害者資料!!', '', __LINE__, __FILE__, $sql);
				}
				if ( !($row = $db->sql_fetchrow($result)) )
				{
					message_die(GENERAL_ERROR, '找不到受害者!!', '', __LINE__, __FILE__, $sql);
				}
				if ($row['user_session_time'] >= ( time() - 300 ))
				{
					$targetid = $row['uid'];
					// 檢查受害者是否有大人不在家拒收草人 (item_id = 83)
					$sql = 'SELECT own_id FROM xoops_pbb_rpg_owns WHERE item_id = 83 AND uid = ' . $targetid;
					if ( $result = $db->sql_query($sql) )
					{
						if ( $row = $db->sql_fetchrow($result) )
						{
							$sql = 'DELETE FROM xoops_pbb_rpg_owns WHERE own_id = ' . $row['own_id'];
							if( !$db->sql_query($sql) )
							{
								message_die(GENERAL_ERROR, "大人不在家....是錯的!!", "", __LINE__, __FILE__, $sql);
							}
							$sql = 'UPDATE xoops_users SET user_robfail = user_robfail + 1 WHERE uid = ' . intval($userdata['uid']);
							if( $db->sql_query($sql) )
							{
								message_die(GENERAL_MESSAGE, '受害者有道具: "大人不在家" ....請自食惡禮!!' . $returnurl);
							}
						}
						else
						{
							$sql = 'UPDATE xoops_users SET user_robfail = user_robfail + 1 WHERE uid = ' . $targetid;
							if( !$db->sql_query($sql) )
							{
								message_die(GENERAL_ERROR, "可能禮物太貴重，送不到....", "", __LINE__, __FILE__, $sql);
							}
						}
					}
				}
				else
				{
					message_die(GENERAL_MESSAGE, '受害者(?)目前不在線上喲!!' . $returnurl);
				}
			}
			$haveused = true;
			break;
		// BgBone的搶劫計劃書
		case 102:
			if ( CheckRefresh(102, intval($userdata['uid']), 3600) )
			{
				message_die(GENERAL_MESSAGE, '本計劃書一小時才能計劃一次喲!!' . $returnurl);
			}
			if ($add1 == $add2) message_die(GENERAL_MESSAGE, '共犯與被害者不可以是同一人!!' . $returnurl);
			// 取得共犯資料
			$sql = "select uid, user_posts, user_robfail, user_money, rpg_rob_cnt from xoops_users where uname = '$add1'";
			$result = $db->sql_query($sql);
			if ( !($row = $db->sql_fetchrow($result)) )
			{
				message_die(GENERAL_ERROR, '找不到所輸入的共犯!!', '', __LINE__, __FILE__, $sql);
			}
			if ( $row['user_posts'] <= 100 )
			{
				$i = 5;
			}
			else
			{
				$i = floor(($row['user_posts'] - 100) / 50) + 6;
			}
			$i += floor( $row['user_money'] / 10000 );
			if ($i < $row['user_robfail'])
			{
				message_die(GENERAL_MESSAGE, '共犯草人太多(' . $row['user_robfail'] . '/' . $i . ')，無法控制!!' . $returnurl);
			}
			// 取得被害者資料
			$sql = "select uid from xoops_users where uname = '$add2'";
			$result = $db->sql_query($sql);
			if ( !($row2 = $db->sql_fetchrow($result)) )
			{
				message_die(GENERAL_ERROR, '找不到所輸入的被害者!!', '', __LINE__, __FILE__, $sql);
			}
			// 主僕關係
			$sql = "select count(*) as bosscount from xoops_pbb_rpg_bodyguard where yes_or_no = 1 and from_id = " .
				$row['uid'] . " and invite_time >= " . (time() - 86400 * 3) . " and to_id = " . $row2['uid'];
			$result = $db->sql_query($sql);
			if ( !($row3 = $db->sql_fetchrow($result)) )
			{
				message_die(GENERAL_ERROR, '無法判斷共犯與被害者的主僕關係!!', '', __LINE__, __FILE__, $sql);
			}
			if ($row3['bosscount'] > 0)
			{
				$sql = "delete from xoops_pbb_rpg_owns where own_id = $ownid and uid = " . intval($userdata['uid']);
				$db->sql_query($sql);
				message_die(GENERAL_MESSAGE, '共犯與被害者有主僕關係，計劃無效!!' . $returnurl);
			}
			// 機率判斷
			$rob_ok = floor($userdata['user_posts'] * 100 / $row['user_posts']);
			if ($rob_ok > 80)
			{
				$rob_ok = 80;
			}
			else if ($rob_ok < 3)
			{
				$rob_ok = 3;
			}
			$rob_ok = $rob_ok - $userdata['user_robfail'] + $row['user_robfail'];
			if ($row['rpg_rob_cnt'] == 0) $rob_ok = 7;
			srand((double)microtime()*1000000);
			$randid = rand(1, 100);
			if ($randid > $rob_ok)
			{
				$sql = "delete from xoops_pbb_rpg_owns where own_id = $ownid and uid = " . intval($userdata['uid']);
				$db->sql_query($sql);
				$sql = "update xoops_users set user_robfail = user_robfail + 2 where uid = " . intval($userdata['uid']);
				$db->sql_query($sql);
				message_die(GENERAL_MESSAGE, "念力超出控制($randid)，計劃無效，有效念力範圍($rob_ok)。" . $returnurl);
			}
			// 時機判斷
			$sql = 'select max(join_time) as max_time from xoops_pbb_rpg_rob where attack_target <> -1 and attack_id = ' . $row['uid'];
			$result = $db->sql_query($sql);
			if ( !($row3 = $db->sql_fetchrow($result)) )
			{
				message_die(GENERAL_ERROR, '無法判斷搶劫有效時限!!', '', __LINE__, __FILE__, $sql);
			}
			if ($row3['max_time'] >= (time() - 86400 / 12))
			{
				$sql = "delete from xoops_pbb_rpg_owns where own_id = $ownid and uid = " . intval($userdata['uid']);
				$db->sql_query($sql);
				message_die(GENERAL_MESSAGE, '您必須等到 ' . (create_date($userdata['user_dateformat'], ($row3['max_time'] + 86400 / 12), $board_config['board_timezone'])) . ' 才可以邀請這位共犯。' . $returnurl);
			}
			$sql = 'select max(join_time) as max_time from xoops_pbb_rpg_rob where attack_target = ' . $row2['uid'] . ' and attack_id = ' . $row['uid'];
			$result = $db->sql_query($sql);
			if ( !($row3 = $db->sql_fetchrow($result)) )
			{
				message_die(GENERAL_ERROR, '無法判斷搶劫有效時限!!', '', __LINE__, __FILE__, $sql);
			}
			if ($row3['max_time'] >= (time() - 86400 / 2))
			{
				$sql = "delete from xoops_pbb_rpg_owns where own_id = $ownid and uid = " . intval($userdata['uid']);
				$db->sql_query($sql);
				message_die(GENERAL_MESSAGE, '您必須等到 ' . (create_date($userdata['user_dateformat'], ($row3['max_time'] + 86400 / 2), $board_config['board_timezone'])) . ' 才可以邀請這位共犯搶劫所選擇的被害者。' . $returnurl);
			}
			// 檢查是否有 Join牌反心靈控制器
			$sql = 'select own_id from xoops_pbb_rpg_owns where item_id = 103 and uid = ' . $row['uid'];
			if ( $result = $db->sql_query($sql) )
			{
				if ( $row3 = $db->sql_fetchrow($result) )
				{
					$sql = 'delete from xoops_pbb_rpg_owns where own_id = ' . $row3['own_id'];
					$db->sql_query($sql);
					$sql = "update xoops_users set user_robfail = user_robfail + 1 where uid = " . intval($userdata['uid']);
					$db->sql_query($sql);
				}
				else
				{
					// EnobGb報告書
					$sql = 'select own_id from xoops_pbb_rpg_owns where item_id = 106 and uid = ' . $row['uid'];
					if ( $result = $db->sql_query($sql) )
					{
						if ( $row3 = $db->sql_fetchrow($result) )
						{
							$sql = 'delete from xoops_pbb_rpg_owns where own_id = ' . $row3['own_id'];
							$db->sql_query($sql);
							PM_User($row['uid'], intval($userdata['uid']), "嘿嘿嘿，我們來搶 $add2 吧!!", "As Title....(爆)");
						}
					}
					// 加入行搶
					$sql = 'insert into xoops_pbb_rpg_rob values (' . $row2['uid'] . ', "' . str_replace("\'", "''", $add2) .
						'", ' . $row['uid'] . ', "' . $add1 . '", ' . time() . ', 0)';
					if( !$db->sql_query($sql) )
					{
						message_die(GENERAL_ERROR, "無法更新搶劫列表!!", "", __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);
		}
	}
	return true;
}

function GetProperties($row)
{
	$properities = '';
	if ($row['attack_property'] != 0)
	{
		if ($row['attack_property'] > 0)
		{
			$properities .= ' 攻擊+' . $row['attack_property'];
		}
		else
		{
			$properities .= ' 攻擊' . $row['attack_property'];
		}
	}
	if ($row['defense_property'] != 0)
	{
		if ($row['defense_property'] > 0)
		{
			$properities .= ' 防禦+' . $row['defense_property'];
		}
		else
		{
			$properities .= ' 防禦' . $row['defense_property'];
		}
	}
	if ($row['magic_property'] != 0)
	{
		if ($row['magic_property'] > 0)
		{
			$properities .= ' 魔力+' . $row['magic_property'];
		}
		else
		{
			$properities .= ' 魔力' . $row['magic_property'];
		}
	}
	if ($row['speed_property'] != 0)
	{
		if ($row['speed_property'] > 0)
		{
			$properities .= ' 速度+' . $row['speed_property'];
		}
		else
		{
			$properities .= ' 速度' . $row['speed_property'];
		}
	}
	if ($row['hp_property'] != 0)
	{
		if ($row['hp_property'] > 0)
		{
			$properities .= ' HP+' . $row['hp_property'];
		}
		else
		{
			$properities .= ' HP' . $row['hp_property'];
		}
	}
	if ($row['mp_property'] != 0)
	{
		if ($row['mp_property'] > 0)
		{
			$properities .= ' MP+' . $row['mp_property'];
		}
		else
		{
			$properities .= ' MP' . $row['mp_property'];
		}
	}
	return $properities;
}

function GetTypies($classlist)
{
	global $db;

	$typies = '';
	if (strstr($classlist, '0'))
	{
		$typies = ' 不限';
	}
	else
	{
		$sql = 'SELECT * FROM xoops_pbb_rpg_setting WHERE setting_id = 1';
		if ( ($result = $db->sql_query($sql)) )
		{
			if ( $row = $db->sql_fetchrow($result) )
			{
				$classid = array();
				$classname = array();
				do
				{
					$classid[] = $row['setting_value1'];
					$classname[$row['setting_value1']] = $row['setting_value2'];
				}
				while ( $row = $db->sql_fetchrow($result) );
				for ($i = 0; $i < count($classid); $i++)
				{
					if (strstr($classlist, strval($classid[$i])))
					{
						$typies .= ' ' . $classname[$classid[$i]];
					}
				}
			}
		}
	}

	return $typies;
}

function UpdateRPGProperties($uid)
{
	global $db;

	$attplus = 1.0;
	$defplus = 1.0;
	$magplus = 1.0;
	$spdplus = 1.0;
	$hp_plus = 0.0;
	$mp_plus = 0.0;
	$sql = 'SELECT MAX(position_order) as basetop FROM xoops_pbb_rpg_position WHERE position_class = 0';
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, '無法取得基礎職業屬性!!', '', __LINE__, __FILE__, $sql);
	}
	if ( $row = $db->sql_fetchrow($result) )
	{
		$sql = 'SELECT * FROM xoops_pbb_rpg_position WHERE position_class = 0 AND position_order = ' . $row['basetop'];
		if ( !($result = $db->sql_query($sql)) )
		{
			message_die(GENERAL_ERROR, '無法取得基礎職業屬性!!', '', __LINE__, __FILE__, $sql);
		}
		if ( $row = $db->sql_fetchrow($result) )
		{
			// 取得基礎職業的標準值
			$base_att = $row['attack_property'];
			$base_def = $row['defense_property'];
			$base_mag = $row['magic_property'];
			$base_spd = $row['speed_property'];
			$base_hpp = $row['hp_property'];
			$base_mpp = $row['magic_property'];
		}
		$sql = 'SELECT p.* FROM xoops_pbb_rpg_position p, xoops_pbb_rpg_medals m WHERE p.position_class = m.position_class
			AND p.position_order = m.position_order AND m.last_class = 1 AND m.position_class <> 0 AND m.uid = ' . $uid;
		if ( !($result = $db->sql_query($sql)) )
		{
			message_die(GENERAL_ERROR, '無法取得專業職業屬性!!', '', __LINE__, __FILE__, $sql);
		}
		if ( $row = $db->sql_fetchrow($result) )
		{
			$i = 1;
			$newposition = '';
			do
			{
				if ($i == 1)
				{
					$attplus = $row['attack_property'];
					$defplus = $row['defense_property'];
					$magplus = $row['magic_property'];
					$spdplus = $row['speed_property'];
					$hp_plus = $row['hp_property'];
					$mp_plus = $row['magic_property'];
					$newposition = $row['position_name'];
				}
				else
				{
					$attplus += $row['attack_property'] - $base_att;
					$defplus += $row['defense_property'] - $base_def;
					$magplus += $row['magic_property'] - $base_mag;
					$spdplus += $row['speed_property'] - $base_spd;
					$hp_plus += $row['hp_property'] - $base_hpp;
					$mp_plus += $row['magic_property'] - $base_mpp;
					$newposition .= '<br />' . $row['position_name'];
				}
				$i++;
			}
			while ( $row = $db->sql_fetchrow($result) );

			if(strlen($newposition) > 200)
			{
				$newposition = substr($newposition, 0, 200);
			}
		}
		else
		{
			// 只有基礎職業
			$sql = 'SELECT p.* FROM xoops_pbb_rpg_position p, xoops_pbb_rpg_medals m WHERE p.position_class = 0 AND m.last_class = 1
				AND m.position_class = 0 AND p.position_order = m.position_order AND m.uid = ' . $uid;
			if ( !($result = $db->sql_query($sql)) )
			{
				message_die(GENERAL_ERROR, '無法取得基礎職業屬性!!', '', __LINE__, __FILE__, $sql);
			}
			if ( $row = $db->sql_fetchrow($result) )
			{
				$attplus = $row['attack_property'];
				$defplus = $row['defense_property'];
				$magplus = $row['magic_property'];
				$spdplus = $row['speed_property'];
				$hp_plus = $row['hp_property'];
				$mp_plus = $row['magic_property'];
				$newposition = $row['position_name'];
			}
		}
	}

	$sql = 'SELECT IFNULL(SUM(i.attack_property), 0) as attack_x, IFNULL(SUM(i.defense_property), 0) as defense_x,
		IFNULL(SUM(i.magic_property), 0) as magic_x, IFNULL(SUM(i.speed_property), 0) as speed_x,
		IFNULL(SUM(i.hp_property), 0) as hp_x, IFNULL(SUM(i.mp_property), 0) as mp_x FROM xoops_pbb_rpg_items i,
		Xoops_pbb_rpg_owns o WHERE i.item_id = o.item_id AND o.uid = ' . $uid;
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, '無法取得攜帶物品屬性!!', '', __LINE__, __FILE__, $sql);
	}
	if ( $row = $db->sql_fetchrow($result) )
	{
		$attplus += $row['attack_x'];
		$defplus += $row['defense_x'];
		$magplus += $row['magic_x'];
		$spdplus += $row['speed_x'];
		$hp_plus += $row['hp_x'];
		$mp_plus += $row['mp_x'];
	}

	$sql = "update xoops_users set rpg_max_hp = floor(rpg_level * (100 + $hp_plus + rpg_level)),
		rpg_max_mp = floor(rpg_level * (80 + rpg_level) + rpg_level * $mp_plus),
		rpg_attack = round(rpg_level * $attplus), rpg_defense = round(rpg_level * $defplus),
		rpg_magic = round(rpg_level * $magplus), rpg_speed = round(rpg_level * $spdplus),
		rpg_medals = '$newposition' where uid = $uid";
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, '無法更新 RPG 屬性!!', '', __LINE__, __FILE__, $sql);
	}
}

function LevelUp($uid, $full_mh = false)
{
	global $db;

	$sql = "select rpg_level, rpg_cur_exp, rpg_max_exp from xoops_users where uid = $uid";
	$result = $db->sql_query($sql);
	if ( $row2 = $db->sql_fetchrow($result) )
	{
		if ( $row2['rpg_cur_exp'] >= $row2['rpg_max_exp'] )
		{
			if ( $row2['rpg_level'] < 250 )
			{
				$sql = "update xoops_users set rpg_level = rpg_level + 1, rpg_cur_exp = rpg_cur_exp - rpg_max_exp, rpg_cur_hp = rpg_max_hp, rpg_cur_mp = rpg_max_mp,
					rpg_max_exp = FLOOR( POW( 10, POW( rpg_level, (1/3) ) ) ) - FLOOR( POW( 10, POW( rpg_level - 1, (1/3) ) ) )
					where uid = $uid";
				$db->sql_query($sql);
				UpdateRPGProperties($uid);
			}
			if ( $full_mh )
			{
				$sql = "update xoops_users set rpg_cur_hp = rpg_max_hp, rpg_cur_mp = rpg_max_mp where uid = $uid";
				$db->sql_query($sql);
			}
		}
	}
	unset($sql);
	unset($row2);
	unset($result);
}
?>