From b70430351bd1f3e4d3c74bf5ca0c1f4f655828b0 Mon Sep 17 00:00:00 2001 From: Paco Hope Date: Sat, 6 Aug 2022 22:12:31 -0400 Subject: [PATCH] Moved from tfscript.py --- src/tfscript/__init__.py | 206 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 206 insertions(+) diff --git a/src/tfscript/__init__.py b/src/tfscript/__init__.py index e69de29..5e9169c 100644 --- a/src/tfscript/__init__.py +++ b/src/tfscript/__init__.py @@ -0,0 +1,206 @@ +""" Makes the configs as a massive string """ + +# Used for the conditions in the type +condDict = {} + +def makeCFG(cfg): + condDict.clear() + ret = '' + for key, data in cfg.items(): + # I know all of these fields exist because it was verified in verify.py + bindType = firstTypeIn(data.keys()) + bindContent = data[bindType] + ret += branch(key, bindContent, bindType) + + # Doubles are weird. All of the toggles got put into a dictionary. + # This takes all of the nested dictionaries and turns them into the right string + for key, toggles in condDict.items(): + ret += f'alias +{key}_toggles "{toggles["plus_toggles"]}"\n' +\ + f'alias -{key}_toggles "{toggles["minus_toggles"]}"\n' +\ + f'bind {key} "+{key}_toggles"\n' + + return ret + +def firstTypeIn(inputList): + """ Find the first element common to both lists """ + types = [ + "impulse", + "hold", + "toggle", + "double", + "repeat" + ] + for t in types: + if t in inputList: + return t + +def branch(keyName, bindContent, bindType): + if bindType == "impulse": + return impulse(keyName, bindContent) + + elif bindType == "hold": + if isinstance(bindContent, str): + return simpleHold(keyName, bindContent) + else: + return listHold(keyName, bindContent) + + elif bindType == "toggle": + return toggle(keyName, bindContent) + + elif bindType == "double": + return double(keyName, bindContent) + + elif bindType == "repeat": + return repeat(keyName, bindContent) + +def impulse(key, instruction): + if isinstance(instruction, list): + instruction = ';'.join(instruction) + + allInstructions = [] + + for indivCmd in instruction.split(';'): + allInstructions.append(impulseShortcuts(indivCmd)) + + instruction = ';'.join(allInstructions) + + return f'bind {key} "{instruction}"\n' + +def impulseShortcuts(instruction): + splitCommand = instruction.split(' ') + cmd = splitCommand[0] + shortcuts = { + "primary": "slot1", + "secondary": "slot2", + "melee": "slot3" + } + if cmd in shortcuts: + for sc, expansion in shortcuts.items(): + if cmd == shortcut: + splitCommand[0] = expansion + break + instruction = ' '.join(splitCommand) + + restOfCmd = ' '.join(splitCommand[1:]) + if cmd == "voice": + instruction = voice(restOfCmd) + + elif cmd == "build" or cmd == "destroy": + instruction = f"{cmd} " + expandBuildings(restOfCmd) + + return instruction + +def voice(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'voicemenu {menu} {selection}' + +def expandBuildings(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 + +def simpleHold(key, instruction): + # This isn't quite right, fix later! + if instruction[0] != '+': + return f'bind {key} "+{instruction}"\n' + else: + return f'bind {key} "{instruction}"\n' + +def listHold(key, options): + press_str = options["press"] + if isinstance(press_str, list): + press_str = ';'.join(press_str) + + release_str = options["release"] + if isinstance(release_str, list): + release_str = ';'.join(release_str) + + ret = f'alias +{key}_bind "{press_str}"\n' +\ + f'alias -{key}_bind "{release_str}"\n' +\ + f'bind {key} "+{key}_bind"\n' + return ret + +def toggle(key, instruction): + onStr = f'turn_{key}_on' + offStr = f'turn_{key}_off' + togStr = f'toggle_{key}' + + ret = f'alias {onStr} "+{instruction}; alias {togStr} {offStr}"\n' +\ + f'alias {offStr} "-{instruction}; alias {togStr} {onStr}"\n' +\ + f'alias {togStr} "{onStr}"\n' +\ + f'bind {key} "{togStr}"\n' + return ret + +def double(key, options): + prim_action = options["primary"] + pBindType = firstTypeIn(prim_action.keys()) + pBindContent = prim_action[pBindType] + + sec_action = options["secondary"] + sBindType = firstTypeIn(sec_action.keys()) + sBindContent = sec_action[sBindType] + + main_str = f'{key}_main' + alt_str = f'{key}_alt' + tog_str = f'toggle_{key}' + + recursive_code = branch(main_str, pBindContent, pBindType) +\ + branch(alt_str, sBindContent, sBindType) + + newcode = [] + for line in recursive_code.split('\n'): + # For every line gotten by the recursive call, change all "bind"s to "alias", + # since main_str and alt_str aren't valid bind targes + llist = line.split(' ') + for i in range(len(llist)): + alphanum_chars = ''.join(c for c in llist[i] if c.isalnum()) + if alphanum_chars == 'bind': + if llist[i][0].isalnum(): + llist[i] = 'alias' + else: + # If the first character isn't a normal character. + # Almost always because it is a double quote + llist[i] = llist[i][0] + 'alias' + newcode.append(' '.join(llist)) + + ret = '\n'.join(newcode) +\ + f'alias +{tog_str} "bind {key} {alt_str}"\n' +\ + f'alias -{tog_str} "bind {key} {main_str}"\n'+\ + f'bind {key} "{main_str}"\n' + + cond_name = options["condition"] + + if cond_name in condDict: + # If the condition key (like "mouse4") already has toggles, + # just append another toggle string (it gets encased in quotes later) + condDict[cond_name]["plus_toggles"] += f"; +{tog_str}" + condDict[cond_name]["minus_toggles"] += f"; -{tog_str}" + else: + # If the condition key doesn't already exist, make it with the correct values + condDict.update( { + cond_name: { + "plus_toggles": f"+{tog_str}", + "minus_toggles": f"-{tog_str}" + } + } ) + + return ret + +def repeat(key, options): + return f'placeholder for {key} (repeat)\n'