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 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)