def trigger(self, event_type, character, obj): """ Trigger an event. Args: event_type: (string) event's type. character: (object) the character who trigger this event. obj: (object) the event object. Return: triggered: (boolean) if an event is triggered. """ if not character: return False if self.can_bypass(character): return False if event_type not in self.events: return False # Get all event's of this type. event_list = self.events[event_type] candidates = [e for e in event_list if not character.is_event_closed(e["key"]) and STATEMENT_HANDLER.match_condition(e["condition"], character, obj)] triggered = False rand = random.random() for event in candidates: if event["multiple"]: if rand < event["odds"]: func = EVENT_ACTION_SET.func(event["action"]) if func: func(event["key"], character, obj) triggered = True rand = random.random() else: if rand < event["odds"]: func = EVENT_ACTION_SET.func(event["action"]) if func: func(event["key"], character, obj) triggered = True break rand -= event["odds"] return triggered
def __init__(self, *args, **kwargs): super(ActionRoomIntervalForm, self).__init__(*args, **kwargs) choices = EVENT_ACTION_SET.choice_repeatedly() self.fields['action'] = forms.ChoiceField(choices=choices) localize_form_fields(self)
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 __init__(self, *args, **kwargs): super(EventDataForm, self).__init__(*args, **kwargs) choices = EVENT_ACTION_SET.choice_all() self.fields['action'] = forms.ChoiceField(choices=choices) choices = EVENT_TRIGGER_SET.choice_all() self.fields['trigger_type'] = forms.ChoiceField(choices=choices) localize_form_fields(self)
def at_repeat(self): """ Trigger events. """ if not self.obj.location: # The character's location is empty (maybe just login). return if self.obj.location != self.db.room: # The character has left the room. self.obj.scripts.delete(self) return # Do actions. if self.db.offline: self.db.last_trigger_time = time.time() func = EVENT_ACTION_SET.func(self.db.action) if func: func(self.db.event_key, self.obj, self.db.room)
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