Example #1
0
    def get_next_dialogues(self, dlg_key, caller, npc):
        """
        Get dialogues next to this dialogue.

        Args:
            dlg_key: (string) the key of the currrent dialogue.
            caller: (object) the character who want to start a talk.
            npc: (object) the NPC that the character want to talk to.

        Returns:
            sentences: (list) a list of available sentences.
        """
        # Get current dialogue.
        dlg = self.get_dialogue(dlg_key)
        if not dlg:
            return

        target = {}
        if npc:
            target = {
                "dbref": npc.dbref,
                "name": npc.get_name(),
                "icon": getattr(npc, "icon", None),
            }

        dialogues = []
        for next_dlg_key in dlg["nexts"]:
            # Get next dialogue.
            next_dlg = self.get_dialogue(next_dlg_key)
            if not next_dlg:
                continue

            # Match conditions.
            if not STATEMENT_HANDLER.match_condition(next_dlg["condition"], caller, npc):
                continue

            # Match dependencies.
            match = True
            for dep in next_dlg["dependencies"]:
                status = QUEST_STATUS_SET.get(dep["type"])
                if not status.match(caller, dep["quest"]):
                    match = False
                    break

            if not match:
                continue

            dialogues.append({
                "key": next_dlg_key,
                "content": next_dlg["content"],
            })

        return {
            "target": target,
            "dialogues": dialogues,
        }
Example #2
0
    def __init__(self, *args, **kwargs):
        super(QuestDependenciesForm, self).__init__(*args, **kwargs)

        objects = CM.QUESTS.all_with_base()
        choices = [(obj["key"], obj["name"] + " (" + obj["key"] + ")") for obj in objects]
        self.fields['quest'] = forms.ChoiceField(choices=choices)
        self.fields['dependency'] = forms.ChoiceField(choices=choices)
        
        choices = QUEST_STATUS_SET.choice_all()
        self.fields['type'] = forms.ChoiceField(choices=choices)

        localize_form_fields(self)
Example #3
0
    def dialogue_have_quest(self, caller, npc, dialogue):
        """
        Find quests by recursion.
        """
        provide_quest = False
        finish_quest = False

        # check if the dialogue is available
        npc_dlg = self.get_dialogue(dialogue)
        if not npc_dlg:
            return provide_quest, finish_quest

        if not STATEMENT_HANDLER.match_condition(npc_dlg["condition"], caller, npc):
            return provide_quest, finish_quest

        match = True
        for dep in npc_dlg["dependencies"]:
            status = QUEST_STATUS_SET.get(dep["type"])
            if not status.match(caller, dep["quest"]):
                match = False
                break
        if not match:
            return provide_quest, finish_quest

        # find quests in its sentences
        for quest_key in npc_dlg["finish_quest"]:
            if caller.quest_handler.is_accomplished(quest_key):
                finish_quest = True
                return provide_quest, finish_quest

        if not provide_quest and npc_dlg["provide_quest"]:
            for quest_key in npc_dlg["provide_quest"]:
                if caller.quest_handler.can_provide(quest_key):
                    provide_quest = True
                    return provide_quest, finish_quest

        for dlg_key in npc_dlg["nexts"]:
            # get next dialogue
            provide, finish = self.dialogue_have_quest(caller, npc, dlg_key)
                
            provide_quest = (provide_quest or provide)
            finish_quest = (finish_quest or finish)

            if finish_quest:
                break

            if not caller.quest_handler.get_accomplished_quests():
                if provide_quest:
                    break

        return provide_quest, finish_quest
Example #4
0
    def match_dependencies(self, quest_key):
        """
        Check quest's dependencies

        Args:
            quest_key: (string) quest's key

        Returns:
            (boolean) result
        """
        for dep in QuestDependencies.get(quest_key):
            status = QUEST_STATUS_SET.get(dep.type)
            if not status.match(self.owner, dep.dependency):
                return False
        return True
Example #5
0
    def get_npc_dialogues(self, caller, npc):
        """
        Get NPC's dialogues that can show to the caller.

        Args:
            caller: (object) the character who want to start a talk.
            npc: (object) the NPC that the character want to talk to.

        Returns:
            dialogues: (list) a list of available dialogues.
        """
        if not caller:
            return

        if not npc:
            return

        dialogues = []

        # Get npc's dialogues.
        for dlg_key in npc.dialogues:
            # Get all dialogues.
            npc_dlg = self.get_dialogue(dlg_key)
            if not npc_dlg:
                continue

            # Match conditions.
            if not STATEMENT_HANDLER.match_condition(npc_dlg["condition"], caller, npc):
                continue

            # Match dependencies.
            match = True
            for dep in npc_dlg["dependencies"]:
                status = QUEST_STATUS_SET.get(dep["type"])
                if not status.match(caller, dep["quest"]):
                    match = False
                    break

            if not match:
                continue

            dialogues.append({
                "key": dlg_key,
                "content": npc_dlg["content"],
            })

        if not dialogues:
            # Use default sentences.
            # Default sentences should not have condition and dependencies.
            for dlg_key in npc.default_dialogues:
                npc_dlg = self.get_dialogue(dlg_key)
                if npc_dlg:
                    dialogues.append({
                        "key": dlg_key,
                        "content": npc_dlg["content"],
                    })
            
        return {
            "target": {
                "dbref": npc.dbref,
                "name": npc.get_name(),
                "icon": getattr(npc, "icon", None),
            },
            "dialogues": dialogues,
        }