我正在为我的网站编写一个任务系统,它的工作原理很像你在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 onequestuser表-保存用户已接受的所有任务,无论是否完成
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;
}谢谢你的帮助。
发布于 2011-08-25 06:11:14
子查询的补救措施
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所以你让数据库为你排序,这应该是超级快的。
该查询缺少用户已经执行的任务的排除,我将此作为练习,因为我正在智能手机上写作;)
https://stackoverflow.com/questions/7182635
复制相似问题