beverage_convert.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/env python3
  2. import os
  3. from glob import glob
  4. import fileinput
  5. import re
  6. import sys
  7. def file_convert(filename):
  8. global tw_sources_path, tw_destination_path
  9. source_filepath= os.path.join(tw_sources_path,filename)+'.tw'
  10. destination_filepath= os.path.join(tw_destination_path,filename)+'compiled.js'
  11. with open(source_filepath, 'r') as file:
  12. lines = [line.rstrip() for line in file]
  13. npccounter = 0
  14. with open(destination_filepath, 'w') as file:
  15. file.write("if(!setup.beverages)\n\tsetup.beverages = {};\n")
  16. identifier = ''
  17. counter = 0
  18. for line in lines:
  19. if match := re.match(r"""\s*<<(?:else)?if\s+\$location_var\[\$here\]\[0\]\s*==\s*'(.*?)'\s*>>\s*""",line):
  20. if identifier != '':
  21. if hydra_inc > 0:
  22. file.write(f"\thydra_inc: {hydra_inc},\n")
  23. elif hydra_dec > 0:
  24. file.write(f"\thydra_dec: {hydra_dec},\n")
  25. file.write("}\n")
  26. identifier = match.group(1)
  27. file.write("setup.beverages['"+identifier+"'] = {\n")
  28. hydra_inc = 0
  29. hydra_dec = 0
  30. continue
  31. elif identifier != '':
  32. if match := re.search(r"""<<set(?:init)?\s+(.*?)\s*([+-]?)=\s*(.*?)>>""",line):
  33. var = match.group(1)
  34. val = match.group(3)
  35. suffix = ''
  36. if(match.group(2) == '+'):
  37. suffix = '_inc'
  38. if(match.group(2) == '-'):
  39. suffix = '_dec'
  40. if(var.startswith('$pc.')):
  41. var = var[4:]
  42. if(var.startswith('pcs_')):
  43. var = var[4:]
  44. if(var.startswith('$time.')):
  45. var = var[6:]
  46. if(var == '$finances.cash'):
  47. var = 'cash'
  48. val = '0'
  49. if(var == '$cumspclnt'):
  50. var = 'cumclean'
  51. if(var == '$boozeVar'):
  52. var = 'booze'
  53. if(var == "teeth['caffe_or_tea']"):
  54. var = 'teeth_caffe_or_tea'
  55. var += suffix
  56. if(var == 'hydra_inc'):
  57. hydra_inc = max(int(val),hydra_inc)
  58. elif(var == 'hydra_dec'):
  59. hydra_dec = max(int(val),hydra_dec)
  60. else:
  61. file.write(f"\t{var}: {val},\n")
  62. elif match := re.search(r"""<<gs\s+'drugs'\s+'alcohol'\s*('\w+')\s*>>""",line):
  63. alco = match.group(1)
  64. file.write(f"\talcohol: {alco},\n")
  65. elif match := re.search(r"""<<image\s+"(.*?)"\s*>>""",line):
  66. image =match.group(1)
  67. file.write(f'\timage: "{image}",\n')
  68. elif match := re.search(r"""<<image\s+`'(?:images/)?(.*?)'\s*\+\s*rand\((\d+)\s*,\s*(\d+)\s*\)\s*\+\s*'(.*?)'`\s*>>""",line):
  69. image = match.group(1) + '#' + match.group(4)
  70. rand0 = match.group(2)
  71. rand1 = match.group(3)
  72. file.write('\timageRand: {template:"'+image+'",min:'+rand0+',max:'+rand1+'},\n')
  73. file.write("}\n")
  74. tw_sources_path = os.path.join("sugarcube","src","autogenerated","beverage")
  75. tw_destination_path = os.path.join(tw_sources_path,"compiled")
  76. os.makedirs(tw_destination_path,exist_ok=True)
  77. filesNames = [y for x in os.walk(tw_sources_path) for y in glob(os.path.join(x[0], '*.tw'))]
  78. #print(filesNames)
  79. for file in filesNames:
  80. #print(file)
  81. file_convert(os.path.basename(file).split('.')[0])