<?php

define('UPGRADE_LEVEL', 4);

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

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

$returnurl = '<br /><br /><a href="rpg.php?action=position">返回上一頁</a>';
$template->set_filenames(array(
	'menu' => 'rpg_header.tpl',
	'body' => 'rpg_position.tpl'));

// 放棄
if (	( isset($HTTP_GET_VARS['cc']) || isset($HTTP_POST_VARS['cc']) ) &&
	( isset($HTTP_GET_VARS['co']) || isset($HTTP_POST_VARS['co']) ) 	)
{
	$cid = intval(( isset($HTTP_GET_VARS['cc']) ) ? $HTTP_GET_VARS['cc'] : $HTTP_POST_VARS['cc']);
	$oid = intval(( isset($HTTP_GET_VARS['co']) ) ? $HTTP_GET_VARS['co'] : $HTTP_POST_VARS['co']);
	$sql = "select i.item_name from xoops_pbb_rpg_items i, xoops_pbb_rpg_owns o
		where i.in_which_store = 3 and o.uid = " . intval($userdata['uid']) .
		" and i.need_class = $cid and i.need_level > $oid and o.item_id = i.item_id";
	$result = $db->sql_query($sql);
	if ( $row = $db->sql_fetchrow($result) )
	{
		message_die(GENERAL_MESSAGE, '在放棄這張職照之前，請先遺忘您所學得的技能: ' . $row['item_name'] . $returnurl);
	}

	$sql = "delete from xoops_pbb_rpg_medals where uid = " . intval($userdata['uid']) .
		" and position_class = $cid and position_order = $oid";
	$db->sql_query($sql);
	$sql = "update xoops_pbb_rpg_medals set last_class = 1 where position_class = $cid and position_order = $oid - 1 and uid = " . intval($userdata['uid']);
	$db->sql_query($sql);
	UpdateRPGProperties(intval($userdata['uid']));
	unset($cid);
	unset($oid);
}

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

$sql = 'select * from xoops_pbb_rpg_medals where uid = ' . $profiledata['uid'] . ' order by position_class, position_order';
$result = $db->sql_query($sql);
$got_medals = 0;
$find_bug = false;
if ( $row = $db->sql_fetchrow($result) )
{
	if ( $row['position_class'] != 0 )
	{
		$find_bug = true;
	}
	else
	{
		$check_class = 0;
		$check_order = 0;
		do
		{
			if ( $row['position_class'] == $check_class )
			{
				if ( $row['position_order'] == $check_order + 1 )
				{
					$check_order++;
				}
				else
				{
					$find_bug = true;
				}
			}
			else
			{
				if ( $row['position_order'] == 0 )
				{
					$check_class = $row['position_class'];
					$check_order = 0;
				}
				else
				{
					$find_bug = true;
				}
			}
			$got_medals++;
		}
		while ( $row = $db->sql_fetchrow($result) );
	}
}

// 上次等級
$lastlevel = $got_medals * UPGRADE_LEVEL;
if ( ($profiledata['rpg_level'] < $lastlevel) || ($find_bug) )
{
	$sql = 'delete from xoops_pbb_rpg_medals where uid = ' . $profiledata['uid'];
	$db->sql_query($sql);
	$sql = "update xoops_users set rpg_cur_hp = 1, rpg_max_hp = 1, rpg_cur_mp = 1, rpg_max_mp = 1, rpg_attack = 1,
		rpg_defense = 1, rpg_magic = 1, rpg_speed = 1, rpg_medals = '路人' where uid = " . $profiledata['uid'];
	$db->sql_query($sql);
	message_die(GENERAL_MESSAGE, '職照違規，請重新考照!!' . $returnurl);
}

$err_posmsg = '';

// 基礎職照一共有幾個
$sql = 'SELECT MAX(position_order) as base_top FROM xoops_pbb_rpg_position WHERE position_class = 0';
$result = $db->sql_query($sql);
if ( $row = $db->sql_fetchrow($result) )
{
	$basetoplevel = $row['base_top'];
}
else
{
	message_die(GENERAL_ERROR, '無法取得基礎職照資料!!', '', __LINE__, __FILE__, $sql);
}

// 報考
if (	( isset($HTTP_GET_VARS['cid']) || isset($HTTP_POST_VARS['cid']) ) &&
	( isset($HTTP_GET_VARS['oid']) || isset($HTTP_POST_VARS['oid']) ) 	)
{
	if ($profiledata['rpg_level'] < $lastlevel + UPGRADE_LEVEL)
	{
		$err_posmsg = '等級不足，無法報考!!';
	}
	else
	{
		$cid = intval(( isset($HTTP_GET_VARS['cid']) ) ? $HTTP_GET_VARS['cid'] : $HTTP_POST_VARS['cid']);
		$oid = intval(( isset($HTTP_GET_VARS['oid']) ) ? $HTTP_GET_VARS['oid'] : $HTTP_POST_VARS['oid']);
		$sql = "select count(*) as medals_count from xoops_pbb_rpg_medals where uid = " . $profiledata['uid'] .
			" and position_class = $cid and position_order = $oid";
		$result = $db->sql_query($sql);
		if ( $row = $db->sql_fetchrow($result) )
		{
			if ( $row['medals_count'] > 0 )
				message_die(GENERAL_MESSAGE, '您已經持有這張職照了!!' . $returnurl);
		}

		if ((($cid == 0) && ($oid > 1)) || (($cid != 0) && ($oid > 0)) )
		{
			$sql = "select count(*) as medals_count from xoops_pbb_rpg_medals where uid = " . $profiledata['uid'] .
				" and position_class = $cid and position_order = $oid - 1";
			 $result = $db->sql_query($sql);
			if ( $row = $db->sql_fetchrow($result) )
			{
				if ( $row['medals_count'] == 0 )
					message_die(GENERAL_MESSAGE, '不得越級考試!!' . $returnurl);
			}
		}
		else if ($oid == 0)
		{
			$sql = 'select ifnull(max(position_order), 0) as top_order from xoops_pbb_rpg_medals where uid = ' .
				$profiledata['uid'] . ' and position_class = 0';
			$result = $db->sql_query($sql);
			if ( $row = $db->sql_fetchrow($result) )
			{
				if ( $row['top_order'] != $basetoplevel )
					message_die(GENERAL_MESSAGE, '不得越級考試!!' . $returnurl);
			}
		}

		// 檢查報名費
		$sql = "select position_fee from xoops_pbb_rpg_position where position_class = $cid and position_order = $oid";
		$result = $db->sql_query($sql);
		if ( $row = $db->sql_fetchrow($result) )
		{
			if ($row['position_fee'] > $profiledata['user_money'])
				$err_posmsg = '現金不足，無法繳交報名費!!';
		}
		else
		{
			message_die(GENERAL_ERROR, '無法取得考照報名費!!', '', __LINE__, __FILE__, $sql);
		}
		if ($err_posmsg == '')
		{

			$sql = "update " . CONFIG_TABLE . " set config_value = config_value + " . $row['position_fee'] . " where config_name = 'rpg_jackpot'";
			$db->sql_query($sql);
			$sql = "update " . USERS_TABLE . " set user_money = user_money - " . $row['position_fee'] . " where uid = " . $profiledata['uid'];
			$db->sql_query($sql);
			$sql = "insert into xoops_pbb_rpg_medals values (" . $profiledata['uid'] . ", $cid, $oid, " . time() . ", 1)";
			$db->sql_query($sql);
			$sql = "update xoops_pbb_rpg_medals set last_class = 0 where position_class = $cid and position_order < $oid and uid = " . $profiledata['uid'];
			$db->sql_query($sql);
			UpdateRPGProperties($profiledata['uid']);
			$profiledata = get_userdata(intval($userdata['uid']));
			$lastlevel += UPGRADE_LEVEL;
		}
		unset($oid);
		unset($cid);
	}
}

// 取得已持有職照
$got_list = '';
$want_got_sql = '';

// 已持有基礎職業
$sql = 'SELECT m.position_order, m.got_date, p.* FROM xoops_pbb_rpg_medals m, xoops_pbb_rpg_position p
	WHERE m.position_class = 0 AND m.uid = ' . $profiledata['uid'] . ' AND p.position_class = 0
	AND m.position_order = p.position_order ORDER BY m.position_order DESC';
if ( !($result = $db->sql_query($sql)) )
{
	message_die(GENERAL_ERROR, '無法取得基礎職業列表!!' . $sql , '', __LINE__, __FILE__, $sql);
}
if ( $row = $db->sql_fetchrow($result) )
{
	$got_list = '<table class="forumline" width="80%" 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>
			<tr><td class="row3" align="center"><span class="gensmall">1</span></td>
			<td><span class="gensmall">' . $row['position_name'] . '</span></td><td><span class="gensmall">' .
			GetProperties($row) . '</span></td><td align="center"><span class="gensmall">' .
			create_date($userdata['user_dateformat'], $row['got_date'], $board_config['board_timezone']) .
			'</span></td><td></td></tr>';
	if ($row['position_order'] < $basetoplevel)
	{
		$got_list .= '</table>';
		$want_got_sql = 'SELECT * FROM xoops_pbb_rpg_position WHERE position_class = 0 AND position_order = ' . ($row['position_order'] + 1);
	}
	else
	{
		// 專業職照類別
		$sql = 'SELECT position_class FROM xoops_pbb_rpg_position WHERE position_class <> 0 GROUP BY position_class';
		if ( !($result = $db->sql_query($sql)) )
		{
			message_die(GENERAL_ERROR, '無法取得專業職照資料!!', '', __LINE__, __FILE__, $sql);
		}
		if ( $row = $db->sql_fetchrow($result) )
		{
			$classlevel = array();
			$classlist = array();
			do
			{
				$classlevel[$row['position_class']] = -1;
				$classlist[] = $row['position_class'];
			}
			while ( $row = $db->sql_fetchrow($result) );
			$sql = 'SELECT p.*, m.got_date FROM xoops_pbb_rpg_medals m, xoops_pbb_rpg_position p
				WHERE m.uid = ' . $profiledata['uid'] . ' AND m.last_class = 1
				AND m.position_class <> 0 AND m.position_class = p.position_class
				AND m.position_order = p.position_order ORDER BY m.position_class';
			if ( !($result = $db->sql_query($sql)) )
			{
				message_die(GENERAL_ERROR, '無法取得已持有的專業職照!!', '', __LINE__, __FILE__, $sql);
			}
			if ( $row = $db->sql_fetchrow($result) )
			{
				$i = 2;
				do
				{
					$classlevel[$row['position_class']] = $row['position_order'];
					$got_list .= '<tr><td class="row3" align="center"><span class="gensmall">' . $i .
							'</span></td><td><span class="gensmall">' . $row['position_name'] .
							'</span></td><td><span class="gensmall">' . GetProperties($row) .
							'</span></td><td align="center"><span class="gensmall">' .
							create_date($userdata['user_dateformat'], $row['got_date'], $board_config['board_timezone']) .
							'</span></td><td align="center"><span class="gensmall"><a href="rpg.php?action=position&cc=' .
							$row['position_class'] . '&co=' . $row['position_order'] . '">放棄</a></span></td></tr>';
					$i++;
				}
				while ( $row = $db->sql_fetchrow($result) );

				$want_got_sql = 'SELECT * FROM xoops_pbb_rpg_position WHERE ';
				for ($i = 0; $i < count($classlist); $i++)
				{
					if ($i == 0)
					{
						$want_got_sql .= '(position_class = ' . $classlist[$i] .
								' AND position_order = ' . ($classlevel[$classlist[$i]] + 1) . ') ';
					}
					else
					{
						$want_got_sql .= 'OR (position_class = ' . $classlist[$i] .
								' AND position_order = ' . ($classlevel[$classlist[$i]] + 1) . ') ';
					}
				}
				$want_got_sql .= ' ORDER BY position_class';
			}
			else
			{
				$want_got_sql = 'SELECT * FROM xoops_pbb_rpg_position WHERE position_class <> 0 AND position_order = 0';
			}
			$got_list .= '</table>';
		}
		else
		{
			message_die(GENERAL_ERROR, '無法取得專業職照資料!!', '', __LINE__, __FILE__, $sql);
		}
	}
}
else
{
	$got_list = '無';
	$want_got_sql = 'SELECT * FROM xoops_pbb_rpg_position WHERE position_class = 0 AND position_order = 1';
}

// 舉行報考職照
$result = $db->sql_query($want_got_sql);
if ( $row = $db->sql_fetchrow($result) )
{
	$want_got = '<table class="forumline" width="80%" 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>';
	$i = 1;
	do
	{
		$want_got .= '<tr><td class="row3" align="center"><span class="gensmall">' . $i .
				'</span></td><td><span class="gensmall">' . $row['position_name'] .
				'</span></td><td><span class="gensmall">' . GetProperties($row) .
				'</span></td><td align="right"><span class="gensmall">' . number_format($row['position_fee']) .
				'</span></td><td align="center"><span class="gensmall"><a href="rpg.php?action=position&cid=' .
				$row['position_class'] . '&oid=' . $row['position_order'] . '">報考</a></span></td></tr>';
		$i++;
	}
	while ( $row = $db->sql_fetchrow($result) );
	$want_got .= '</table>';
}
else
{
	$want_got = '無';
}

// 產生資料頁面
$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_LEVEL' => $profiledata['rpg_level'],
	'RPG_PREVIOUS_LEVEL' => $lastlevel,
	'RPG_POS_LIST' => $want_got,
	'RPG_GOT_LIST' => $got_list,
	'RPG_POS_ERROR' => $err_posmsg,
	'L_NOWMENU' => '職業考照檢定') );

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

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

unset($returnurl);
unset($row);
unset($lastlevel);
unset($want_got);
unset($got_list);
unset($err_posmsg);
unset($want_got_sql);
unset($sql);
unset($result);
unset($got_medals);
unset($find_bug);
unset($check_class);
unset($check_order);
unset($basetoplevel);

?>