From 8bf19e61536937e89aa5e8cae487794ba6c3dd30 Mon Sep 17 00:00:00 2001 From: Nicholas Hope Date: Sun, 4 Sep 2022 09:26:57 -0400 Subject: [PATCH] All verification done, impulse.toTF2() done --- src/tfscript/tftypes.py | 102 +++++++++++++++++++++++++++++++++++----- 1 file changed, 90 insertions(+), 12 deletions(-) diff --git a/src/tfscript/tftypes.py b/src/tfscript/tftypes.py index 19f51e4..2404817 100644 --- a/src/tfscript/tftypes.py +++ b/src/tfscript/tftypes.py @@ -33,6 +33,7 @@ class bind: ''' def __init__(self, key, fields): + self.alias = False self.key = key self.fields = fields self.errors = [] @@ -67,14 +68,14 @@ class bind: return try: - isalias = self.fields.pop('alias') - if not isinstance(isalias, bool): - self.errors.append(f'alias should be yes or no, not "{isalias}"') - isalias = False + self.alias = self.fields.pop('alias') + if not isinstance(self.alias, bool): + self.errors.append(f'alias should be yes or no, not "{self.alias}"') + self.alias = False except (KeyError, AttributeError, TypeError): - isalias = False + self.alias = False - if (not isalias) and (self.key not in validKeyList): + if (not self.alias) and (self.key not in validKeyList): self.errors.append(f'invalid key name: "{self.key}"') types = { @@ -112,15 +113,92 @@ class impulse(bind): try: self.command = self.fields.pop('command') - if not isinstance(self.command, (str, list)): - self.err('command must be string or list') - self.command = None except (KeyError, AttributeError, TypeError): self.fields = {'command': self.fields} self.command = self.fields.pop('command') - if not isinstance(self.command, (str, list)): - self.err('must be command or argument of string or list') - self.command = None + + if isinstance(self.command, str): + self.command = self.command.split(';') + + elif not isinstance(self.command, list): + self.err('must be command or argument of string or list') + self.command = None + + def toTF2(self) -> str: + if self.alias: + bindOrAlias = 'alias' + else: + bindOrAlias = 'bind' + + allInstructions = self.shortcut(self.command) + instruction = ';'.join(allInstructions) + + return f'{bindOrAlias} {self.key} "{instruction}"\n' + + def shortcut(self, instList): + for i, instruction in enumerate(instList): + try: + cmd, restOfCmd = instruction.split(' ', 1) + except ValueError: + # no spaces in cmd + cmd, restOfCmd = instruction, '' + + simpleSCs = { + "primary": "slot1", + "secondary": "slot2", + "melee": "slot3" + } + try: + cmd = simpleSCs[cmd] + except KeyError: + # not a shortcut + pass + + if cmd == "voice": + cmd = "voicemenu" + restOfCmd = self.expandVoice(restOfCmd) + + elif cmd == "build" or cmd == "destroy": + restOfCmd = self.expandBuildings(restOfCmd) + + elif cmd == "load_itempreset" and restOfCmd.isalpha(): + try: + restOfCmd = restOfCmd.lower() + restOfCmd = ['a','b','c','d'].index(restOfCmd) + except ValueError: + # not a load_itempreset shortcut + pass + + if restOfCmd != "": + cmd += ' ' + restOfCmd + instList[i] = cmd + + return instList + + def expandVoice(self, keyword): + keyword = keyword.lower() + + allLists = ( + ("medic", "thanks", "go", "move up", "go left", "go right", "yes", "no", "pass to me"), + ("incoming", "spy", "sentry ahead", "teleporter here", "dispenser here", "sentry here", "activate uber", "uber ready"), + ("help", "battle cry", "cheers", "jeers", "positive", "negative", "nice shot", "good job"), + ) + + for menu, voiceList in enumerate(allLists): + for selection, shortcut in enumerate(voiceList): + if keyword == shortcut: + return f'{menu} {selection}' + + def expandBuildings(self, building): + buildingNums = { + "dispenser": "0 0", + "entrance": "1 0", + "exit": "1 1", + "sentry": "2 0" + } + for shortBuild, num in buildingNums.items(): + if building == shortBuild: + return num class hold(bind):