Initial commit
This commit is contained in:
99
server.py
Normal file
99
server.py
Normal file
@@ -0,0 +1,99 @@
|
||||
from collections import defaultdict
|
||||
from flask import Flask, render_template, request
|
||||
|
||||
import sys
|
||||
sys.path.append('/home/nate/projects/aoe/fighting')
|
||||
sys.path.append('/home/nate/projects/aoe/flashcards')
|
||||
|
||||
from fighting.fighting import Fighting
|
||||
from flashcards.maps import (create_maps,
|
||||
read_data_to_dict)
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
|
||||
|
||||
style = '''
|
||||
<style type="text/css">
|
||||
td {
|
||||
padding: 0 100px;
|
||||
}
|
||||
</style>
|
||||
'''
|
||||
|
||||
namelist = 'units-all.txt'
|
||||
# key into json file specifying which data you want
|
||||
name_json_keys = 'units_buildings'
|
||||
data_json_keys = ('data', 'Unit')
|
||||
|
||||
_, nums = create_maps(namelist, name_json_keys)
|
||||
data = read_data_to_dict(data_json_keys)
|
||||
|
||||
fight = Fighting(data, nums)
|
||||
|
||||
|
||||
@app.route("/", methods=["GET", "POST"])
|
||||
def serve_things():
|
||||
if request.method == "POST":
|
||||
options = defaultdict(int)
|
||||
options.update(request.form)
|
||||
return show_fight((request.form['attacker'],
|
||||
request.form['defender']),
|
||||
options)
|
||||
|
||||
if request.method == "GET":
|
||||
return render_template('main.html', units=sorted(fight.list_units()))
|
||||
|
||||
|
||||
def show_fight(names, options):
|
||||
tab1 = generate_table(names, options)
|
||||
tab2 = generate_table(names[::-1], options)
|
||||
return f'{style}{tab1}\n\n\n{tab2}'
|
||||
|
||||
|
||||
def generate_table(names, options):
|
||||
hp_bonus_names = []
|
||||
if options['bloodlines']:
|
||||
hp_bonus_names.append('Bloodlines')
|
||||
|
||||
res = fight.generate_fight_results_from_names(names[0],
|
||||
names[1],
|
||||
hp_bonus_names=hp_bonus_names,
|
||||
time_to_kill=False)
|
||||
if options['time']:
|
||||
res2 = fight.generate_fight_results_from_names(names[0],
|
||||
names[1],
|
||||
hp_bonus_names=hp_bonus_names,
|
||||
time_to_kill=True)
|
||||
else:
|
||||
res2 = None
|
||||
|
||||
tab = convert_to_table(names, res, res2=res2)
|
||||
return tab
|
||||
|
||||
|
||||
def convert_to_table(names, res, res2=None):
|
||||
headings = ['Hits to Kill']
|
||||
all_res = [res]
|
||||
|
||||
if res2:
|
||||
headings.append('Time to Kill (seconds)')
|
||||
all_res.append(res2)
|
||||
|
||||
colspan = len(all_res) + 1
|
||||
|
||||
table = '<html><table>'
|
||||
table += f'<tr><th colspan={colspan}>{names[0]} -> {names[1]}</th></tr>'
|
||||
|
||||
table += f'<tr><th>(Attack Upgrade - Armor Upgrade)</th>'
|
||||
table += ''.join([f'<th>{heading}</th>' for heading in headings])
|
||||
table += '</tr>'
|
||||
|
||||
for k in all_res[0]['hits'].keys():
|
||||
table += f'<tr><td>{k}</td>'
|
||||
table += ''.join([f'<td>{res['hits'][k]}</td>' for res in all_res])
|
||||
table += '</tr>'
|
||||
|
||||
table += f'<tr><td>notes:</td><td>{all_res[0]["notes"]}</td></tr>'
|
||||
table += '</table></html>'
|
||||
return table
|
||||
Reference in New Issue
Block a user