Difference between revisions of "Ability/Script"

From Wildermyth Wiki
m
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
This is a [https://www.python.org/downloads/ Python] script to generate the [[Ability]] tables. Copy the code in the box below into a text file named '''<nowiki>abilityTable.py</nowiki>''' (or whatever you want) inside the '''<nowiki>Wildermyth</nowiki>''' directory. Then run it by calling '''<nowiki>python abilityTable.py</nowiki>''' from the command line. It will create a text file named '''<nowiki>abilityTable.txt</nowiki>''' with the appropriate wiki-formatted content.
This is a [https://www.python.org/downloads/ Python] script to generate the [[Ability]] tables. Copy the code in the box below into a text file named '''<nowiki>wikiAbilityTable.py</nowiki>''' (or whatever you want) inside the '''<nowiki>Wildermyth</nowiki>''' directory. Then run it by calling '''<nowiki>python wikiAbilityTable.py</nowiki>''' from the command line.<ref>More precisely, you can put the script anywhere, but you need to call it from the '''<nowiki>Wildermyth</nowiki>''' directory.</ref> It will create a text file named '''<nowiki>wikiAbilityTable.txt</nowiki>''' with the appropriate wiki-formatted content.


{| role="presentation" class="wikitable mw-collapsible mw-collapsed"
{| role="presentation" class="wikitable mw-collapsible mw-collapsed"
| '''<nowiki>abilityTable.py</nowiki>'''
| '''<nowiki>wikiAbilityTable.py</nowiki>'''
|-
|-
|
|
Line 10: Line 10:


aspect_directory = 'assets/data/aspects'
aspect_directory = 'assets/data/aspects'
theme_directory = 'assets/data/themes'
text_file = 'assets/text/aspects/aspects.properties'
text_file = 'assets/text/aspects/aspects.properties'


Line 79: Line 78:
for deck in decks:
for deck in decks:
   decks[deck] = sorted(decks[deck], key=lambda d: aspectNames[d['id']])  
   decks[deck] = sorted(decks[deck], key=lambda d: aspectNames[d['id']])  
 
 
# make dictionary of theme links
'''
themes = {}
themefiles = glob(theme_directory + '/*')
for file in themefiles:
  with open(file) as f:
    data = json.load(f)
    theme = data['id']
    for piece in data['pieces']:
      'themePiece_' + theme
     
    for entry in data:
      for deck in decks:
        if entry.get('info', {}).get('abilityDeckUsage') == deck:
          decks[deck] += [entry]
'''
# wiki blurb for each deck
# wiki blurb for each deck


Line 125: Line 108:
         string = string[:start] + new + string[start+len(tag)+2:end-2-len(tag_)] + new + string[end:]
         string = string[:start] + new + string[start+len(tag)+2:end-2-len(tag_)] + new + string[end:]
         break
         break
  return string
# function to replace <a/b/.../y:aa/bz/.../yy/zz> with zz
def fail_condition(string):
  for opentag in ['<self.','<test:','<mf']:
    while True:
      start = string.find(opentag)
      if start < 0:
        break
      num = 1
      slashes = []
      passed_colon = False
      for i,c in enumerate(string[start+len(opentag):]):
        if c == '<':
          num += 1
        elif c == '>':
          num -= 1
          if num == 0:
            end = i+start+len(opentag)+1
            break
        elif c == '/' and num == 1 and passed_colon:
          slashes += [i+start+len(opentag)]
        elif c == ':':
          passed_colon = True
      if len(slashes) == 0:
        slashes += [end-1]
      string = string[:start] + string[slashes[-1]+1:end-1] + string[end:]
   return string
   return string


# list of words to replace with links in the wiki
# list of words to replace with links in the wiki


linkwords = [
linkwords = {
   'Bonus Damage',
   'Bonus Damage':None,
   'Potency',
  'Spell Damage':None,
   'Stunt Chance',
   'Potency':None,
   'Temp Health',
   'Stunt Chance':None,
   'Temporary Health',
   'Temp Health':None,
   'Armor',
   'Temporary Health':None,
   'Warding',
   'Armor':'Armor (Stat)',
   'Dodge',
   'Warding':None,
   'Block',
   'Dodge':None,
   'Speed',
   'Block':None,
   'Recovery Rate',
   'Speed':None,
   'Retirement Age',
   'Recovery Rate':None,
   'Grayplane',
   'Retirement Age':None,
   'bonus damage',
   'Grayplane':None,
   'spell damage',
   'bonus damage':None,
   'armor',
   'spell damage':None,
   'warding',
   'armor':'Armor (Stat)',
   'dodge',
   'warding':None,
   'block',
   'dodge':None,
   'augment',
   'block':None,
   'grayplane',
   'augment':None,
   ]
   'grayplane':None,
  'hidden':None,
   }


# function to turn game-formatted ability blurb into wiki-formatted ability blurb
# function to turn game-formatted ability blurb into wiki-formatted ability blurb
Line 164: Line 177:
   out = out.replace('<self>','Hero')
   out = out.replace('<self>','Hero')
   out = out.replace('<name>','Hero')
   out = out.replace('<name>','Hero')
 
  # assume any requirements are not met
  out = fail_condition(out)
    
    
   ### TODO: rewrite these to appropriately respect nested brackets and braces
   ### TODO: rewrite these to appropriately respect nested brackets and braces
Line 169: Line 185:
   out = re.sub(r'\<int:(.*?)\>', 'x', out) # formula -> x
   out = re.sub(r'\<int:(.*?)\>', 'x', out) # formula -> x
   out = re.sub(r'\<float:(.*?)\>', 'x', out) # formula -> x
   out = re.sub(r'\<float:(.*?)\>', 'x', out) # formula -> x
 
  out = re.sub(r'\<mf:.*?/.*?/(.*?)\>', '\g<1>', out) # use they/them
    
    
   # status effect -> link
   # status effect -> link
   out = re.sub(r'\[:statusEffect\.(.*?)\](.*?)\[\]', '[[\g<1>|\g<2>]]', out)
   out = re.sub(r'\[:statusEffect\.(.*?)\](.*?)\[\]', '[[\g<1>|\g<2>]]', out)
   out = re.sub(r'\[statusEffect:(.*?)\](.*?)\[\]', '[[\g<1>|\g<2>]]', out)
   out = re.sub(r'\[statusEffect:(.*?)\](.*?)\[\]', '[[\g<1>|\g<2>]]', out)
 
  # assume any requirements are not met
  out = re.sub(r'\<self\..*?:.*?[^<]/(.*?)\>', '\g<1>', out)
  out = re.sub(r'\<self\..*?:.*?\>', '', out)
  out = re.sub(r'\<test:.*?:(.*?[^<])/.*?\>', '\g<1>', out)
  out = re.sub(r'\<test:.*?:.*?\>', '', out)
    
    
   # leave out "(Currently ...)" note which is based on hero's current stats
   # leave out "(Currently ...)" note which is based on hero's current stats
   out = re.sub(r'\(Current.*?\)', '', out)
   out = re.sub(r'\(Current.*?\)', '', out)
    
    
   ###
   ### ---
    
    
   out = out.replace('\\n','<br />') # newline -> newline
   out = out.replace('\\n','<br />') # newline -> newline
    
    
   for word in linkwords: # replace words with links
   for word in linkwords: # replace words with links
     out = out.replace(word,'[[%s]]'%word)
     if linkwords[word] is not None:
      out = re.sub(r'(^|[^\[])' + word + r'($|[^\]])','\g<1>[[%s|%s]]\g<2>'%(linkwords[word],word),out)
    else:
      out = re.sub(r'(^|[^\[])' + word + r'($|[^\]])','\g<1>[[%s]]\g<2>'%(word),out)
   return out
   return out


Line 257: Line 268:
          
          
         # if requirement = theme, don't repeat it
         # if requirement = theme, don't repeat it
         if onlyTheme is not None and aspectNames[req] == aspectNames[onlyTheme]:
         if onlyTheme is not None and req == onlyTheme:
           continue
           continue
          
          
Line 263: Line 274:
         if (req.endswith('leftArm') or req.endswith('rightArm')) and arms[themes[i]] != 1:
         if (req.endswith('leftArm') or req.endswith('rightArm')) and arms[themes[i]] != 1:
           if arms.get(themes[i]) >= 2:
           if arms.get(themes[i]) >= 2:
             reqname = re.sub(r'\(.*?\)', '(either)', aspectNames[req])
             reqname = re.sub(r' \([LR]\)', '', aspectNames[req])
             arms[themes[i]] = -1
             arms[themes[i]] = -1
           elif arms.get(themes[i]) < 0:
           elif arms.get(themes[i]) < 0:
Line 271: Line 282:
         elif (req.endswith('leftLeg') or req.endswith('rightLeg')) and legs[themes[i]] != 1:
         elif (req.endswith('leftLeg') or req.endswith('rightLeg')) and legs[themes[i]] != 1:
           if legs.get(themes[i]) >= 2:
           if legs.get(themes[i]) >= 2:
             reqname = re.sub(r'\(.*?\)', '(either)', aspectNames[req])
             reqname = re.sub(r' \([LR]\)', '', aspectNames[req])
             legs[themes[i]] = -1
             legs[themes[i]] = -1
           elif legs.get(themes[i]) < 0:
           elif legs.get(themes[i]) < 0:
Line 301: Line 312:
# write the final string
# write the final string


with open('abilityTable.txt', 'w') as f:
with open('wikiAbilityTable.txt', 'w') as f:
   f.write(string)</nowiki>
   f.write(string)</nowiki>
|}
|}

Latest revision as of 07:31, 27 May 2022

This is a Python script to generate the Ability tables. Copy the code in the box below into a text file named wikiAbilityTable.py (or whatever you want) inside the Wildermyth directory. Then run it by calling python wikiAbilityTable.py from the command line.[1] It will create a text file named wikiAbilityTable.txt with the appropriate wiki-formatted content.

  1. More precisely, you can put the script anywhere, but you need to call it from the Wildermyth directory.