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.

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