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.

133 lines
4.7 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
  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. return render_template("marchLog.html",worldname=world,log=("current time",40,50,[("Lorem","ipsum. this is some data. data data data."),("Anthony","Someone's gonna see this code and theyre gonna shoot me")]))
  80. def bucket_route(uri, dir, sing=None):
  81. if sing is None:
  82. sing = dir[:-1] # minus "s"
  83. path = '../' + dir + '/*'
  84. root = '/' + uri
  85. def render_bucket():
  86. return render_template('listing.html', title=dir, uri=uri, items=get_items(path))
  87. app.add_url_rule(root, 'render_' + uri, render_bucket)
  88. def random_bucket():
  89. nm = choice(get_items(path))[0]
  90. return redirect(root + '#' + nm)
  91. app.add_url_rule(root + '/random', 'random_' + uri, random_bucket)
  92. def bucket_table(amt):
  93. try:
  94. return render_template('table.html', title=sing + ' Table', items=sample(get_items(path), amt), roll=amt)
  95. except ValueError:
  96. return render_template('table.html', title='you dun goofed', error="Can't construct that table (not enough items?)")
  97. app.add_url_rule(root + '/table/<int:amt>', dir + '_table', bucket_table)
  98. bucket_route('items', 'Items')
  99. bucket_route('places', 'Places')
  100. bucket_route('characters', 'Characters')
  101. bucket_route('spells', 'Spells')
  102. bucket_route('feats', 'Feats')
  103. if __name__ == "__main__":
  104. app.run()