def load_dictionary(self, filename): try: self.dictionary = json.loads(open(filename, 'r').read()) dprint(f"[+]Dictionary {filename} loaded.") except FileNotFoundError: dprint(f"[-]Word dictionary {filename} not found") return {}
def registerAction(self, action, content, once=False): dprint(f"[+]Registring: {action}") action_index = len(self.game_actions) + 1 self.game_actions[action_index] = { 'name': [i.strip() for i in action.split('|')], 'instructions': content, 'once': once }
def process(self, sentence, actions=[]): # dprint(f"[+]Scanning action {sentence}.") if (self.dictionary is None): dprint("[-]Word dictionary not initialized") return False for action in actions: if (self.compare(action, sentence)): return action
def openAdventure(self, adventure_name, adventure_dir=''): """Load adventure and initialize game vars (for GUI)""" if self.in_game: return False self.resetActions() self.reset_vars() adventure_content = self.loadStage(adventure_name, adv_dir=adventure_dir) if adventure_content: dprint("[+]Adventure initialized!") self.in_game = True return True else: return False
def open_stage_file(self, filename): """Open and read adventure file""" try: full_name = filename stage_content = open(full_name, "r", encoding="utf-8").read() except FileNotFoundError as error: dprint(f"[!]Stage file not found:", filename) print(filename, " is not found") return False code_block = re.compile( r'([#\!\¡])([áéíóúa-zA-Z0-9_-\|-\s-]*)\{(.*?)\}', re.MULTILINE | re.DOTALL) blocks = code_block.findall(stage_content) if blocks: return blocks return False
def loadStage(self, stage_name, adv_dir): """Open and initialize stage variables""" dprint(f"[+]Loading {stage_name}...") parser_content = self.load_stage_file(stage_name, adv_dir) if (parser_content is False): return self.resetActions() for block_type, block_name, block_content, in parser_content: # #ROOM{} block_name = block_name.strip().lower() if block_type == '#' and block_name == 'room': """Room specifications""" self.interpret_block_code(block_content) if (self.game_vars.get('stage_name')): self.current_stage = self.game_vars['stage_name'] if (self.game_vars.get('adventure_name')): self.adventure_name = self.game_vars['adventure_name'] # #LOAD_AGAIN{} if self.current_stage in self.stage_history: """If loaded again""" if block_type == '#' and block_name == 'load_again': # Initialize scene variables self.interpret_block_code(block_content) # #LOAD{} elif block_type == '#' and block_name == 'load': # Initialize scene variables self.interpret_block_code(block_content) print(self.game_vars) # !ACTION{} if block_type == '!': # Register actions self.registerAction(block_name, block_content) # !!ACTION run only once if block_type == '¡': self.registerAction(block_name, block_content, once=True) dprint(f"\n[+]Stage '{stage_name}' loaded.") self.stage_history.append(self.current_stage) return True
def load_stage_file(self, stage_file, adv_dir=''): """Open stage file and initialize vars""" if adv_dir: dprint(f"[+]Setting current directory in {adv_dir}") self.current_directory = adv_dir if not self.current_directory: self.current_directory = '' full_name = f"{self.current_directory}\\{stage_file}.adventure" dprint(f"[+]Loading {full_name[-10:]}...") parser_content = self.open_stage_file(full_name) if not parser_content: dprint("[!]Error to load stage, invalid content or empty file") return False return parser_content
def executeAction(self, sentence): dprint(f"\n[+]Executing action: {sentence}") self.clear_output_buffer() sentence = sentence.lower() dprint("vars:", self.game_vars) for index in self.game_actions: game_action = self.game_actions[index] if (sentence in game_action['name'] or self.sentence_processor.process(sentence, game_action['name'])): if game_action[ 'once'] is True and index in self.executed_actions: return False dprint(f"[+]Action {sentence} is found") self.interpret_block_code(game_action['instructions']) self.executed_actions.append(index) return True return None
def resetActions(self): """Reset game actions""" dprint("[+]Game actions reseted") self.game_actions = {}