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.

117 lines
4.0 KiB

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/worlds/<world>")
  52. def marchWorld(world):
  53. worldDir = "../Worlds/"+world + "/"
  54. #copy <worldname>.png from world.png into static
  55. copyfile(worldDir+"world.png","static/"+world+".png")
  56. #player_list = ["INVALID1","INVALID2","INVALID3"]
  57. empty = re.compile('^\s*$')
  58. pfile = open(worldDir + "players.txt",'r')
  59. pData = pfile.read().split('\n')
  60. player_list = [ x for x in pData if not empty.match(x)]
  61. pfile.close()
  62. #image_map = [(10,20,35),(50,40,20)] #("place name",x,y,r)
  63. mfile = open(worldDir + "map.json",'r')
  64. mData = mfile.read()
  65. image_map = [ (x['name'],x['x'],x['y'],x['r']) for x in json.loads(mData)['places']]
  66. log_list = [("5/3/19",[("Josh Gordon","bottom text"),("Anthony Rinaldo","lorem ipsum")])] # :: [(Date,[(CharacterName,String)])]
  67. return render_template("marchWorld.html",worldname=world,players=player_list,imap=image_map,logs=log_list)
  68. def bucket_route(uri, dir, sing=None):
  69. if sing is None:
  70. sing = dir[:-1] # minus "s"
  71. path = '../' + dir + '/*'
  72. root = '/' + uri
  73. def render_bucket():
  74. return render_template('listing.html', title=dir, uri=uri, items=get_items(path))
  75. app.add_url_rule(root, 'render_' + uri, render_bucket)
  76. def random_bucket():
  77. nm = choice(get_items(path))[0]
  78. return redirect(root + '#' + nm)
  79. app.add_url_rule(root + '/random', 'random_' + uri, random_bucket)
  80. def bucket_table(amt):
  81. try:
  82. return render_template('table.html', title=sing + ' Table', items=sample(get_items(path), amt), roll=amt)
  83. except ValueError:
  84. return render_template('table.html', title='you dun goofed', error="Can't construct that table (not enough items?)")
  85. app.add_url_rule(root + '/table/<int:amt>', dir + '_table', bucket_table)
  86. bucket_route('items', 'Items')
  87. bucket_route('places', 'Places')
  88. bucket_route('characters', 'Characters')
  89. bucket_route('spells', 'Spells')
  90. bucket_route('feats', 'Feats')
  91. if __name__ == "__main__":
  92. app.run()