Contact me to be added to this repository. Push the creations you make for D&D, and they will be displayed in a nice website. See the website for how to contribute.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
5.5 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. import sys
  2. from flask import Flask, render_template, request, redirect
  3. from items import get_items, get_worlds
  4. from random import choice, sample
  5. import re
  6. import json
  7. from shutil import copyfile
  8. if sys.version_info.major < 3:
  9. # XXX OOF
  10. reload(sys)
  11. sys.setdefaultencoding('utf8')
  12. app = Flask(__name__)
  13. @app.route('/style.css')
  14. def style():
  15. return render_template('style.css')
  16. @app.route('/')
  17. def index():
  18. featureType = choice(["Item","Place","Spell"])
  19. item = choice(get_items("../"+featureType+"s/*"))
  20. return render_template('index.html',featureType=featureType,featureText=item)
  21. @app.route('/search', methods=['POST'])
  22. def search():
  23. typ = request.form['type']
  24. query = request.form['inputText']
  25. print(typ + ": " + query)
  26. results = []
  27. for direct in ["../Items/*","../Places/*","../Spells/*"]:
  28. objects = get_items(direct)
  29. for (name,heads,body) in objects:
  30. if typ == "name":
  31. if query.lower() in name.lower():
  32. results.append((name,heads,body))
  33. elif typ == "rarity":
  34. if "rarity" in [h[0].lower() for h in heads]:
  35. if query.lower() in [h[1].lower() for h in heads if h[0].lower() == "rarity"]:
  36. results.append((name,heads,body))
  37. elif typ == "aura":
  38. if "aura" in [h[0].lower() for h in heads]:
  39. if query.lower() in [h[1].lower() for h in heads if "aura" in h[0].lower()][0]:
  40. results.append((name,heads,body))
  41. elif typ == "text":
  42. if query.lower() in body.lower():
  43. results.append((name,heads,body))
  44. print("Number of results: " + str(len(results)))
  45. featureType = choice(["Item","Place","Spell"])
  46. item = choice(get_items("../"+featureType+"s/*"))
  47. return render_template('index.html',featureType=featureType,featureText=item, searchResults=results)
  48. @app.route("/marches")
  49. def marches():
  50. return render_template('marches.html', worlds=get_worlds())
  51. @app.route("/marches/maps/<world>")
  52. def marchMap(world):
  53. #image_map = [("Temple of Constant Explosions",10,20,35),("Deep Gnome Workshop",50,40,20)] #(place,x,y,r)
  54. mfile = open("../Worlds/"+ world + "/map.json",'r')
  55. mData = mfile.read()
  56. image_map = [ (x['name'],x['x'],x['y'],x['r']) for x in json.loads(mData)['places']]
  57. return render_template("marchMap.html",worldname=world,imap=image_map)
  58. @app.route("/marches/worlds/<world>")
  59. def marchWorld(world):
  60. worldDir = "../Worlds/"+world + "/"
  61. #copy <worldname>.png from world.png into static
  62. copyfile(worldDir+"world.png","static/"+world+".png")
  63. #player_list = ["INVALID1","INVALID2","INVALID3"]
  64. empty = re.compile('^\s*$')
  65. pfile = open(worldDir + "players.txt",'r')
  66. pData = pfile.read().split('\n')
  67. player_list = [ x for x in pData if not empty.match(x)]
  68. pfile.close()
  69. #image_map = [(10,20,35),(50,40,20)] #("place name",x,y,r)
  70. #image_map = [("Temple of Constant Explosions",10,20,35),("Deep Gnome Workshop",50,40,20)] #(place,x,y,r)
  71. log_list = ["Raiding Cosi"] # :: [AdventureName]
  72. return render_template("marchWorld.html",worldname=world,players=player_list,logs=log_list)
  73. @app.route("/marches/worlds/<world>/<logname>")
  74. def marchLog(world,logname):
  75. print(logname)
  76. #... code
  77. #logname is title of adventure
  78. #log type is (DateString,X,Y,[(Name,Text)])
  79. worldDir = "../Worlds/"+world + "/"
  80. lookup = json.load(open(worldDir + "log.json", 'r'))
  81. logFile = open(worldDir+ "logs/" + lookup[logname],'r')
  82. logData = logFile.read().split('\n')
  83. name = logData[0].split(":")[1]
  84. date = logData[1].split(":")[1]
  85. X = logData[2].split(":")[1]
  86. Y = logData[3].split(":")[1]
  87. logs = []
  88. breakLine = re.compile("^---*$")
  89. for i in range(4,len(logData)):
  90. if len(logData[i]) == 0:
  91. continue
  92. if breakLine.match(logData[i]):
  93. char = logData[i+1]
  94. text = ""
  95. i += 1 #adjust offset in file. We are now at the player's name
  96. s = logData[i] #s will be the next line of the player's entry
  97. while len(s) > 0: #while there is more written text
  98. s = logData[i+1] #grab line of text
  99. text += "\n"+s
  100. i += 1
  101. logs.append((char,text))
  102. return render_template("marchLog.html",worldname=world,log=(name,date,X,Y,logs))
  103. def bucket_route(uri, dir, sing=None):
  104. if sing is None:
  105. sing = dir[:-1] # minus "s"
  106. path = '../' + dir + '/*'
  107. root = '/' + uri
  108. def render_bucket():
  109. return render_template('listing.html', title=dir, uri=uri, items=get_items(path))
  110. app.add_url_rule(root, 'render_' + uri, render_bucket)
  111. def random_bucket():
  112. nm = choice(get_items(path))[0]
  113. return redirect(root + '#' + nm)
  114. app.add_url_rule(root + '/random', 'random_' + uri, random_bucket)
  115. def bucket_table(amt):
  116. try:
  117. return render_template('table.html', title=sing + ' Table', items=sample(get_items(path), amt), roll=amt)
  118. except ValueError:
  119. return render_template('table.html', title='you dun goofed', error="Can't construct that table (not enough items?)")
  120. app.add_url_rule(root + '/table/<int:amt>', dir + '_table', bucket_table)
  121. bucket_route('items', 'Items')
  122. bucket_route('places', 'Places')
  123. bucket_route('characters', 'Characters')
  124. bucket_route('spells', 'Spells')
  125. bucket_route('feats', 'Feats')
  126. if __name__ == "__main__":
  127. app.run()