commit f549c32fcac2b9676c7fb64298d78a10f75e1167 Author: Nate Bowman Date: Wed Oct 28 19:30:54 2026 -0400 Initial commit diff --git a/server.py b/server.py new file mode 100644 index 0000000..4dad8e9 --- /dev/null +++ b/server.py @@ -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 = ''' + +''' + +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 = '' + table += f'' + + table += f'' + table += ''.join([f'' for heading in headings]) + table += '' + + for k in all_res[0]['hits'].keys(): + table += f'' + table += ''.join([f'' for res in all_res]) + table += '' + + table += f'' + table += '
{names[0]} -> {names[1]}
(Attack Upgrade - Armor Upgrade){heading}
{k}{res['hits'][k]}
notes:{all_res[0]["notes"]}
' + return table diff --git a/templates/main.html b/templates/main.html new file mode 100644 index 0000000..a406cbe --- /dev/null +++ b/templates/main.html @@ -0,0 +1,30 @@ + + + Fight!!! + + +
+ + + + +
+ + +
+ + +
+ +
+ + +