Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from evennia.utils import logger
- from evennia.utils.dbserialize import deserialize
- from copy import copy
- from evennia.objects.objects import DefaultObject
- from evennia.utils import string_partial_matching, string_suggestions
- class HandlerBase:
- def __init__(self, obj, db_attr, db_cat='quests', default_data={}): # obj = original quest object, default_data is all the data filled in on that quest
- self.obj = obj # QuestHandler doesn't have it's own db so data wouldn't be persistent, thus would need to be loaded on initialization.
- self._db_attr = db_attr # the character's self.db.quest attribute, which is an array of dicts
- self._db_cat = db_cat # might not be needed for children, but useful for a global handler
- self._data = copy(default_data) # making a copy of the data as is currently - copy vs ref so changes on this copy won't impact the real world ref and vice versa
- self._load()
- def _load(self):
- if data := self.obj.attributes.get(self._db_attr, category=self._db_cat): # create local 'data' var, search character for specified attribute ('quests' in this case)
- self._data = deserialize(data) # basically loading the data from the database into a live instance
- def _save(self):
- try:
- self.obj.attributes.add(self._db_attr, self._data, category=self._db_cat) #taking the updated quest data in the instance we have and storing it in the player's db
- except Exception as e:
- logger.log_err(f"Could not save handler data for {type(self)} on {self.obj} (#{self.obj.pk})! Cached data may be corrupt; reloading from database.")
- logger.log_err(f"Cached data was: {self._data}")
- self._load()
- class QuestHandler(HandlerBase):
- def __init__(self, obj):
- super().__init__(obj, "quests")
- def _load(self):
- super()._load()
- loaded_data = {}
- for key, data in self._data.items(): #creating an array of quest data loaded in from character's db.quest attribute
- loaded_data[key] = QuestMaster(handler=self, **data) #using a Key:Value pair system, aka dict. ie: 'QuestID':'QuestData'
- self.data = loaded_data # quest data is now initialized on the QuestHandler
- def __getattr__(self, attr):
- if quest := self.get(attr): # temp variable basically just getting/seeing if a certain attribute on this handler exists,
- return quest # probably because you can't search it like object attributes can be searched?
- raise AttributeError(f"'{self.__name__}' object has no attribute '{attr}'")
- def _save(self, key=None):
- if key and key in self.data.keys():
- data = { k:v for k, v in vars(self.data[key]).items() if k != 'handler' }
- self._data[key] = data
- else:
- flattened = {}
- for key, clsobj in self.data.items():
- data = vars(clsobj)
- del data['handler']
- flattened[key] = data
- self._data = flattened
- super()._save()
- class QuestMaster:
- parent = None # Unsure what this is for? Maybe attaching to an object?
- child = None # Unsure what this is for?
- def __init__(self, handler, name, key, **kwargs):
- # Handler ref here is used at line 36 in the handler when loading data from the quest to the handler.
- # The data itself is being passed from the handler to create a populated instance of this quest.
- self.handler = handler
- self.name = name
- self.key = key
- self.QuestID = 0
- self.QuestName = "" # Public facing name. If quest is TalkToTom3 it might be "Talk to Tom Again"
- self.QuestDesc = "" # Shows up in the quest log when selecting this specific quest.
- self.Requirements = [] # Other quests needed to be completed (by ID) to do this quest.
- #################################
- # Text Msgs
- #################################
- self.QuestOffer = ""
- self.QuestRejected = ""
- self.QuestAccepted = ""
- self.QuestActive = ""
- self.QuestTurnIn = ""
- self.QuestCompleted = ""
- #################################
- # IDs
- #################################
- self.QuestTypeID = 0 # 0='collect', 1='kill', 2='interact' 3='location'
- self.QuestGiverID = 0 # ID of the object or NPC giving this quest
- self.QuestTurnInID = 0 # either the ID of the location on auto-turn in, or the ID of the NPC the quest is turned in with
- #################################
- # Text Msgs
- #################################
- self.Rewards = []
- for key, val in kwargs.items(): # loops through every attribute passed to this quest instance to populate it with the passed through data. Very versatile.
- if callable(getattr(self, key, None)):
- continue
- setattr(self, key, val)
- def __str__(self):
- return self.name
Advertisement
Advertisement