def func(self, args, request): if not args: raise MudderyError(ERR.missing_args, 'Missing arguments.') if 'action' not in args: raise MudderyError(ERR.missing_args, 'Missing the argument: "action".') if 'event' not in args: raise MudderyError(ERR.missing_args, 'Missing the argument: "event".') if 'values' not in args: raise MudderyError(ERR.missing_args, 'Missing the argument: "values".') action_type = args["action"] event_key = args["event"] values = args["values"] # Get action's data. action = EVENT_ACTION_SET.get(action_type) if not action: raise MudderyError(ERR.no_table, "Can not find action: %s" % action_type) table_name = action.model_name # Remove old records. data_edit.delete_records(table_name, event_key=event_key) # Add new data. for value in values: data_edit.save_form(value, table_name) return success_response("success")
def query_event_action_forms(action_type, event_key): """ Query forms of the event action. Args: action_type: (string) action's type event_key: (string) event's key """ # Get action's data. action = EVENT_ACTION_SET.get(action_type) if not action: raise MudderyError(ERR.no_table, "Can not find action: %s" % action_type) # Get all forms. forms = [] table_name = action.model_name records = general_query_mapper.filter_records(table_name, event_key=event_key) if records: for record in records: forms.append(query_form(table_name, id=record.id)) else: forms.append(query_form(table_name)) return {"forms": forms, "repeatedly": action.repeatedly}
def at_start(self): """ Called every time the script is started. """ # The script will be unpaused when the server restarts. So pause it if the character is no online now. if self.db.begin_message: if self.obj: self.obj.msg(self.db.begin_message) # Offline intervals. if self.db.offline: last_time = self.db.last_trigger_time if last_time: current_time = time.time() times = int((current_time - last_time) / self.interval) if times > 0: self.db.last_trigger_time = current_time action = EVENT_ACTION_SET.get(self.db.action) if action and hasattr(action, "offline_func"): action.offline_func(self.db.event_key, self.obj, self.db.room, times)
def query_event_action_data(action_type, event_key): """ Query an event action's data. Args: action_type: (string) action's type event_key: (string) event's key """ # Get action's data. action = EVENT_ACTION_SET.get(action_type) if not action: raise MudderyError(ERR.no_table, "Can not find action: %s" % action_type) record = None try: record = action.get_event_data_table(event_key) except ObjectDoesNotExist: pass return record
class Meta: model = EVENT_ACTION_SET.get("ACTION_GET_OBJECTS").model() fields = '__all__'
class Meta: model = EVENT_ACTION_SET.get("ACTION_ROOM_INTERVAL").model() fields = '__all__'
class Meta: model = EVENT_ACTION_SET.get("ACTION_MESSAGE").model() fields = '__all__'
def load_cache(self, dialogue): """ To reduce database accesses, add a cache. Args: dialogue (string): dialogue's key """ if not dialogue: return if dialogue in self.dialogue_storage: # already cached return # Add cache of the whole dialogue. self.dialogue_storage[dialogue] = {} # Get db model try: dialogue_record = Dialogues.get(dialogue) dialogue_record = dialogue_record[0] except Exception as e: return nexts = DialogueRelations.get(dialogue) dependencies = DialogueQuests.get(dialogue) # Add db fields to data object. data = { "content": dialogue_record.content, "condition": dialogue_record.condition, } data["dependencies"] = [] for dependency in dependencies: data["dependencies"].append({"quest": dependency.dependency, "type": dependency.type}) # get events and quests event_trigger = EventTrigger(None, dialogue) events = event_trigger.get_events() if not events: data["event"] = None data["provide_quest"] = [] data["finish_quest"] = [] else: data["event"] = event_trigger provide_quest = [] finish_quest = [] if defines.EVENT_TRIGGER_DIALOGUE in events: for event_info in events[defines.EVENT_TRIGGER_DIALOGUE]: if event_info["action"] == "ACTION_ACCEPT_QUEST": action = EVENT_ACTION_SET.get(event_info["action"]) provide_quest.extend(action.get_quests(event_info["key"])) elif event_info["action"] == "ACTION_TURN_IN_QUEST": action = EVENT_ACTION_SET.get(event_info["action"]) finish_quest.extend(action.get_quests(event_info["key"])) data["provide_quest"] = provide_quest data["finish_quest"] = finish_quest data["nexts"] = [next_one.next_dlg for next_one in nexts] # Add to cache. self.dialogue_storage[dialogue] = data