我正在为我的网站编写一个任务系统,它几乎与你在MMORPG中找到的一样。我已经完成了所有工作,但我真的需要加快速度,因为我编写的效率低下。不是因为我不能编码,而是因为我不确定如何去做。
基本上我想显示用户可用的所有任务。有任务没有要求,有些有。要查看任务是否已完成,并且任务可用,您将检查questuser表以获取questuserCompleted的值。如果它是1,那么它就完成了。
以下是我的表格。我把无关紧要的事情留下了。
任务表 - 保存所有任务数据
questID
questNPC
Where they get the Quest from
questPreReq
refers to a quest they would have needed to comple to get this one
questuser table - 保存用户已接受的所有任务,是否完成
questuserID
questuserUser
User's ID
questuserQuest
refers to the ID of the quest from the quest table
questuserCompleted
0 is in progress, 1 is complete
肯定有比现在更好的方法。我通常在这样的事情上更有效率,但是因为我之前从未编写过类似的东西,所以我不确定如何去做。
基本上它只是遍历每一个任务,并使用if语句检查questuserCompleted。一旦开始执行大量任务,对于页面的每次加载都会变得非常缓慢。
function displayAvailable($npc = 0){
$completed[] = 0;
$notCompleted = array();
$query=mysql_query("
SELECT a.questID, a.questPreReq, a.questTitle, b.questuserCompleted, a.questText , a.questNPC
FROM quest a
LEFT JOIN questuser b ON a.questID = b.questuserQuest AND b.questuserUser = '".$this->userID."'
ORDER BY a.questID");
$comments = $this->ProcessRowSet($query);
$num = mysql_num_rows($query);
if($num){
foreach ($comments as $c){
if($c['questuserCompleted']){
$completed[] = $c['questID'];
}else{
$notCompleted[] = $c['questID'];
}
if(in_array($c['questPreReq'], $completed) && !in_array($c['questID'], $completed) && $c['questuserCompleted'] != '0'){
if($npc == 0 || $c['questNPC'] == $npc){
$count++;
$return .= "<p><a href=\"?action=new&id=".$c['questID']."\">".$c['questTitle']."</a></p>";
}
}
}
}
if(!$count){
$return = "You have no available quests";
}
return $return;
}
感谢您的帮助。
答案 0 :(得分:0)
救援的子查询
SELECT a.questID, a.questPreReq, a.questTitle, a.questText , a.questNPC
FROM quest a
WHERE a.questPreReq IS NULL
OR a.questPreReq IN (SELECT questuserQuest FROM questuser WHERE questuserUser = 'UserID' AND questuseCompleted = 1)
ORDER BY a.questID
所以你让数据库为你排序,这应该是超高速的。
查询缺少用户已经完成的任务排除,我将此作为练习,因为我在智能手机上写作; )