Initial commit

This commit is contained in:
Nate Bowman
2026-10-04 01:39:29 -04:00
commit 62907fd6ee
17 changed files with 81318 additions and 0 deletions

54
README.md Normal file
View File

@@ -0,0 +1,54 @@
Project structure:
```
create-maps-from-namelist.py -- script for creating maps
data/ -- input files from AoE itself (taken from another project)
decks/ -- python scripts to create individual decks
* actual decks are not stored here
* it should be called deck-creators, but I don't want to type that
flashcards/ -- all of the useful library code is in here
output/ -- files generated by the project
decks-output/ -- generated decks that can be read into Anki
maps/ -- mappings between names and numbers
namelists/ -- lists containing names to include for particular decks
settings.yaml -- controls where files are read from and written to
```
Once you have data in the correct format, creating the cards is trivial. The
data is provided by another project (I'll link it when I think about it; the
name of the larger group is SiegeEngineers) and pulled directly from AoE
itself. However, the names and identifiers used in the data can be tricky. Most
of the work here is to create mappings between names and id numbers for that
data.
Big picture process:
* Create a list of names that you want included in the deck
* These are not just any names -- they need to match what is used in the
data file. Use the process below to help with this.
* Generate mappings for the data file from names to numbers and numbers to
names based on your namelist
* Create a deck using these mappings
Process:
* Run flashcards/create_full_namelists.py
* `python -m flashcards.create_full_namelists`
* This generates two files in output/namelists/
* Each file corresponds to one key in the data file:
"units_buildings" and "techs"
* Make new namelists by deleting unused entries from the generated namelists
* This is the worst part
* You want to remove only the entirely useless things at first
* When finished, keep around these pruned namelists and start your deck
namelists as copies of them so you don't need to redo this step
* You can also just use the one I added, but there's no guarantee it is
up-to-date or mistake-free
* Copy one of the pruned namelists and delete more entries until you have just
the names you need for a given deck
* Run create-maps-from-namelist.py using your new deck-specific namelist
* This creates two files in output/generated-maps/
* Using one of the scripts in decks/ as a template, write a Python
script that reads your generated maps and the data file and outputs an Anki
deck
* Unless you want to add the flashcards package to your path,
you must run with `python -m decks.{file name without .py extension}`
* The deck will be stored in output/decks-output/
* Open Anki and import the deck

View File

@@ -0,0 +1,21 @@
from flashcards.maps import create_maps
if 0:
# namelist you created before
namelist_fname = 'base-units.txt'
# dictionary key into config.data_file
key = 'units_buildings'
# how you want to name the output maps
out_base_fname = 'base-units'
elif 0:
namelist_fname = 'research.txt'
key = 'techs'
out_base_fname = 'research'
else:
namelist_fname = 'buildings.txt'
key = 'units_buildings'
out_base_fname = 'buildings'
create_maps(namelist_fname, key, out_base_fname)

File diff suppressed because it is too large Load Diff

34
decks/basic-unit-costs.py Normal file
View File

@@ -0,0 +1,34 @@
from flashcards.decks import create_deck
from flashcards.maps import read_data_to_dicts
# your choice of deck name and filename
deck_name = 'Unit Costs'
deck_fname = 'unit-costs'
# specify the namelist used to create the deck
namelist = 'base-units'
# key into json file specifying which data you want
# uncomment either 'units_buildings' or 'techs'
data_json_key = 'units_buildings'
#data_json_key = 'techs'
# --- create list of notes ---
# read in data and maps between names and numbers
data, nums, names = read_data_to_dicts(namelist, data_json_key)
# create cards based on data and maps
notes = []
for num, name in nums.items():
cost = data[num]['cost']
back = '\n'.join([f'{res}: {val}' for res, val in cost.items()
if val != 0])
notes.append((name, back))
#print(f'front: {name}\tback: {back}')
# write deck to .apkg file
deck = create_deck(notes, deck_name, deck_fname)

34
decks/research-costs.py Normal file
View File

@@ -0,0 +1,34 @@
from flashcards.decks import create_deck
from flashcards.maps import read_data_to_dicts
# your choice of deck name and filename
deck_name = 'Research Costs'
deck_fname = 'research-costs'
# specify the namelist used to create the deck
namelist = 'research'
# key into json file specifying which data you want
# uncomment either 'units_buildings' or 'techs'
#data_json_key = 'units_buildings'
data_json_key = 'techs'
# --- create list of notes ---
# read in data and maps between names and numbers
data, nums, names = read_data_to_dicts(namelist, data_json_key)
# create cards based on data and maps
notes = []
for num, name in nums.items():
cost = data[num]['cost']
back = '\n'.join([f'{res}: {val}' for res, val in cost.items()
if val != 0])
notes.append((name, back))
#print(f'front: {name}\tback: {back}')
# write deck to .apkg file
deck = create_deck(notes, deck_name, deck_fname)

17
flashcards/config.py Normal file
View File

@@ -0,0 +1,17 @@
from pathlib import Path
from types import SimpleNamespace
import yaml
config_file = '/home/nate/aoe/flashcards/settings.yaml'
with open(config_file, 'r') as f:
c = yaml.safe_load(f)
root_path = Path(c['storage']['root'])
config = SimpleNamespace()
config.data_file = root_path / c['storage']['data'] / c['data_file']
config.namelists_dir = root_path / c['storage']['namelists']
config.maps_dir = root_path / c['storage']['maps']
config.decks_dir = root_path / c['storage']['decks']

View File

@@ -0,0 +1,45 @@
# run with `python -m flashcards.create_full_namelists`
# Create two lists -- once for each high-level key in the data file
# * units_buildings
# * techs
# The resulting lists will likely still have a lot of junk.
# You will want to manually remove what you don't need before creating maps
# from the lists.
from flashcards.config import config
import json
from pathlib import Path
def generate_namelist(big, key, namelist_fname):
added = set()
with open(namelist_fname, 'w') as f:
for k, v in big[key].items():
lname = v['localised_name'].strip()
costs = sum([v for k, v in v['cost'].items()])
# for some reason, "cost" of a civ "research" is 1
#if lname.strip() != '' and costs != 0 and lname not in added:
if lname.strip() != '' and costs > 1 and lname not in added:
added.add(lname)
f.write(lname + '\n')
if __name__ == '__main__':
namelist_info = {
'units_buildings': 'full-units-namelist.txt',
'techs': 'full-research-namelist.txt',
}
# Read in the full data dictionary
with open(config.data_file, 'r') as f:
big = json.load(f)
for key, fname in namelist_info.items():
namelist_path = config.namelists_dir / fname
if not namelist_path.exists():
# if the global namelist does not exist, create it and exit
generate_namelist(big, key, namelist_path)
else:
print(f'File {namelist_path} already exists. Skipping.')

24
flashcards/decks.py Normal file
View File

@@ -0,0 +1,24 @@
from flashcards.models import simple
from flashcards.random import random_anki_identifier
from flashcards.config import config
import genanki
def create_deck(notes, deck_name, deck_fname):
# Create anki deck
deck = genanki.Deck(
random_anki_identifier(),
deck_name)
# Add notes to deck
for front, back in notes:
note = genanki.Note(
model=simple,
fields=[front, back])
deck.add_note(note)
# Write deck to file
out_path = config.decks_dir / f'{deck_name}.apkg'
print(f'Writing to {out_path}')
genanki.Package(deck).write_to_file(out_path)

57
flashcards/maps.py Normal file
View File

@@ -0,0 +1,57 @@
# Converts a list of lnames to two maps:
# lname -> num and num -> lname
from flashcards.config import config
import json
from pathlib import Path
def _fill_costs(big, key, whitelist):
names = {} # num -> localized name
nums = {} # localized name -> num
for k, v in big[key].items():
lname = v['localised_name'].strip()
costs = sum([v for k, v in v['cost'].items()])
if lname in whitelist and lname not in nums.keys():
names[k] = lname
nums[lname] = k
return names, nums
def _write_map(fname, mapping):
out_path = config.maps_dir / fname
with open(out_path, 'w', encoding='utf-8') as f:
json.dump(mapping, f, ensure_ascii=False, indent=4)
def create_maps(namelist_fname, key, out_base_fname):
# Read in the full data dictionary
with open(config.data_file, 'r') as f:
big = json.load(f)
# Use whitelist to filter data and map only as needed
namelist_path = config.namelists_dir / namelist_fname
with open(namelist_path, 'r') as f:
whitelist = [s.strip() for s in f.readlines()]
names, nums = _fill_costs(big, key, whitelist)
# Write names -> nums map
_write_map(f'{out_base_fname}-lname-to-num.json', nums)
# Write nums -> names map
_write_map(f'{out_base_fname}-num-to-lname.json', names)
def read_data_to_dicts(basename, key):
with open(config.data_file, 'r') as f:
all_data = json.load(f)
data = all_data[key]
with open(config.maps_dir / f'{basename}-lname-to-num.json', 'r') as f:
nums = json.load(f)
with open(config.maps_dir / f'{basename}-num-to-lname.json', 'r') as f:
names = json.load(f)
return data, nums, names

31
flashcards/models.py Normal file
View File

@@ -0,0 +1,31 @@
import genanki
general_css = '''
.card {
font-family: arial;
font-size: 32px;
text-align: center;
color: black;
background-color: white;
white-space: pre-line;
}
'''
simple = genanki.Model(
1653277989,
'Simple Model',
fields=[
{'name': 'Question'},
{'name': 'Answer'},
],
templates=[
{
'name': 'Card 1',
'qfmt': '{{Question}}',
'afmt': '{{FrontSide}}<hr id="answer">{{Answer}}',
},
],
css=general_css
)

8
flashcards/random.py Normal file
View File

@@ -0,0 +1,8 @@
# If doing this via the command line, could run:
#python3 -c "import random; print(random.randrange(1 << 30, 1 << 31))"
import random
def random_anki_identifier():
return random.randrange(1 << 30, 1 << 31)

1
output/decks-output/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
*

1
output/maps/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
*

1
output/namelists/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
*

View File

@@ -0,0 +1,307 @@
Elite Tarkan
Yeomen
Holcans
Furor Celtica
Drill
Citadels
Town Watch
Zealotry
Artillery
Crenellations
Crop Rotation
Heavy Plow
Horse Collar
Guilds
Anarchy
Banking
Atheism
Loom
Coinage
Garland Wars
Elite Plumed Archer
Bimaristan
Medium Warships
Heavy Warships
Husbandry
Faith
Devotion
Chemistry
Caravan
Bogsveigar
Masonry
Architecture
Rocketry
Treadmill Crane
Gold Mining
Kataparuto
Elite Conquistador
Logistica
Keep
Bombard Tower
Gillnets
Forging
Iron Casting
Scale Mail Armor
Blast Furnace
Chain Mail Armor
Plate Mail Armor
Plate Barding Armor
Scale Barding Armor
Chain Barding Armor
Bearded Axe
Ballistics
Capped Ram
Elite Skirmisher
Crossbowman
Feudal Age
Castle Age
Imperial Age
Guard Tower
Gold Shaft Mining
Fortified Wall
Pikeman
Fletching
Bodkin Arrow
Bracer
Double-Bit Axe
Bow Saw
Long Swordsman
Cavalier
Padded Archer Armor
Leather Archer Armor
Wheelbarrow
Squires
Two-Handed Swordsman
Heavy Cavalry Archer
Ring Archer Armor
Two-Man Saw
Man-at-Arms
Block Printing
Sanctity
Illumination
Heavy Camel Rider
Arbalester
Heavy Scorpion
Heavy Demolition Ship
Hand Cart
Fervor
Light Cavalry
Siege Ram
Onager
Champion
Paladin
Stone Mining
Stone Shaft Mining
Town Patrol
Conscription
Redemption
Atonement
Siege Onager
Sappers
Murder Holes
Elite Longbowman
Elite Cataphract
Elite Chu Ko Nu
Elite Throwing Axeman
Elite Teutonic Knight
Elite Huskarl
Elite Samurai
Elite War Elephant
Elite Mameluke
Elite Janissary
Elite Woad Raider
Elite Mangudai
Elite Longboat
Shipwright
Careening
Dry Dock
Elite Cannon Galleon
Siege Engineers
Hoardings
Heated Shot
Eagle Warrior
Elite Berserk
Spies/Treason
Hussar
Halberdier
Elite Jaguar Warrior
Elite Eagle Warrior
Bloodlines
Parthian Tactics
Thumb Ring
Theocracy
Heresy
Supremacy
Herbal Medicine
Shinkichon
Elite Turtle Ship
Elite War Wagon
Counterweights
Detinets
Perfusion
Atlatl
Warwolf
Great Wall
Chieftains
Greek Fire
Elite Genoese Crossbowman
Elite Magyar Huszar
Elite Elephant Archer
Stronghold
Marauders
Yasama
Hul'che Javelineers
Eupseong
Nomads
Kamandaran
Ironclad
Madrasah
Sipahi
Inquisition
Chivalry
Pavise
Silk Road
Elite Boyar
Grand Trunk Road
Shatagni
Elite Kamayuk
Orthodoxy
Druzhina
Corvinian Army
Recurve Bow
Andean Sling
Fabric Shields
Imperial Camel Rider
Revetments
Savar
Enable Sheep
Enable Llamas
Enable Cows
Enable Turkeys
Elite Organ Gun
Elite Camel Archer
Elite Gbeto
Elite Shotel Warrior
Carrack
Arquebus
Royal Heirs
Torsion Engines
Tigui
Farimba
Kasbah
Maghrebi Camels
Elite Caravel
Elite Genitour
Arson
Arrowslits
Elite Ballista Elephant
Elite Karambit Warrior
Elite Arambai
Elite Rattan Archer
Tusk Swords
Double Crossbow
Thalassocracy
Forced Levy
Howdah
Manipur Cavalry
Chatras
Paper Money
Elite Battle Elephant
Imperial Skirmisher
Elite Konnik
Elite Keshik
Elite Kipchak
Elite Leitis
Stirrups
Bagains
Silk Armor
Timurid Siegecraft
Steppe Husbandry
Cuman Mercenaries
Hill Forts
Tower Shields
Elite Steppe Lancer
Supplies
Elite Coustillier
Elite Serjeant
Burgundian Vineyards
Flemish Revolution
First Crusade
Hauberk
Elite Obuch
Elite Hussite Wagon
Szlachta Privileges
Lechitic Legacy
Wagenburg Tactics
Hussite Reforms
Winged Hussar
Houfnice
Elite Urumi Swordsman
Elite Ratha
Elite Chakram Thrower
Medical Corps
Wootz Steel
Paiks
Mahayana
Kshatriyas
Frontier Guards
Siege Elephant
Elite Ghulam
Elite Shrivamsha Rider
Gambesons
Elite Centurion
Ballistas
Comitatenses
Legionary
Pirotechnia
Demolition Ship
Fishing Lines
Carvel Hull
Clinker Construction
Siphons
Incendiaries
Elite Composite Bowman
Elite Monaspa
Fereters
Cilician Fleet
Svan Towers
Aznauri Cavalry
Elite Qizilbash Warrior
Heavy Rocket Cart
Elite Fire Lancer
Elite Iron Pagoda
Fortified Bastions
Thunderclap Bombs
Elite Liao Dao
Lamellar Armor
Ordo Cavalry
Transhumance
Pastoralism
Domestication
Heavy Hei Guang Cavalry
Elite Tiger Cavalry
Tuntian
Ming Guang Armor
Elite White Feather Guard
Bolt Magazine
Coiled Serpent Array
Elite Fire Archer
Red Cliffs Tactics
Sitting Tiger
Champi Warrior
Elite Champi Warrior
Elite Guecha Warrior
Herbalism
Huaracas
Elite Kona
Elite Bolas Rider
Malon
Butalmapu
Elite War Dog
Elite Blackwood Archer
Elite Ibirapema Warrior
Caciques
Curare
Elite Temple Guard
Champi Runner
Circumnavigation

View File

@@ -0,0 +1,274 @@
Archer
Hand Cannoneer
Elite Skirmisher
Skirmisher
Longbowman
Archery Range
Mangudai
Barracks
Fishing Ship
Junk
Trade Cog
Blacksmith
War Galley
Crossbowman
Teutonic Knight
Monastery
Fortress
Battering Ram
Bombard Cannon
Knight
Cavalry Archer
Cataphract
Huskarl
Trebuchet
Dock
Janissary
Siege Workshop
Farm
Fortified Gate
Mill
House
Town Center
Palisade Wall
Chu Ko Nu
Militia
Man-at-Arms
Heavy Swordsman
Long Swordsman
Gate
Watch Tower
Castle
Market
Stable
Spearman
Trade Workshop
Stone Wall
Monk
Fortified Wall
Condottiero
Slinger
Fish Trap
Imperial Camel Rider
University
Woad Raider
Guard Tower
Keep
Bombard Tower
War Elephant
Longboat
Wonder
Scorpion
Mangonel
Throwing Axeman
Mameluke
Cavalier
Samurai
Camel Rider
Heavy Camel Rider
Trebuchet (Packed)
Pikeman
Halberdier
Cannon Galleon
Capped Ram
King
Petard
Hussar
Galleon
Scout Cavalry
Two-Handed Swordsman
Heavy Cavalry Archer
Arbalester
Heavy Crossbowman
Demolition Ship
Heavy Demolition Ship
Fire Ship
Elite Longbowman
Elite Throwing Axeman
Fast Fire Ship
Elite Longboat
Elite Woad Raider
Galley
Heavy Scorpion
Transport Ship
Light Cavalry
Siege Ram
Onager
Elite Cataphract
Elite Teutonic Knight
Elite Huskarl
Elite Mameluke
Elite Janissary
Elite War Elephant
Elite Chu Ko Nu
Elite Samurai
Elite Mangudai
Lumber Camp
Champion
Paladin
Gold Miner
Genitour
Mining Camp
Siege Onager
Elite Genitour
Outpost
Elite Cannon Galleon
Berserk
Elite Berserk
Jaguar Warrior
Elite Jaguar Warrior
Eagle Scout
Elite Eagle Warrior
Eagle Warrior
Tarkan
Elite Tarkan
Plumed Archer
Elite Plumed Archer
Conquistador
Elite Conquistador
Missionary
Palisade Gate
War Wagon
Elite War Wagon
Turtle Ship
Elite Turtle Ship
Genoese Crossbowman
Elite Genoese Crossbowman
Magyar Huszar
Elite Magyar Huszar
Elephant Archer
Elite Elephant Archer
Boyar
Elite Boyar
Kamayuk
Elite Kamayuk
Siege Tower
Heavy Pikeman
Jarl
Dragon Ship
Organ Gun
Elite Organ Gun
Caravel
Elite Caravel
Camel Archer
Elite Camel Archer
Gbeto
Elite Gbeto
Shotel Warrior
Elite Shotel Warrior
Feitoria
Fortified Tower
Fire Galley
Demolition Raft
Ballista Elephant
Elite Ballista Elephant
Karambit Warrior
Elite Karambit Warrior
Arambai
Elite Arambai
Rattan Archer
Elite Rattan Archer
Battle Elephant
Elite Battle Elephant
Imperial Skirmisher
Harbor
Konnik
Elite Konnik
Keshik
Elite Keshik
Kipchak
Elite Kipchak
Leitis
Elite Leitis
Krepost
Flaming Camel
Steppe Lancer
Elite Steppe Lancer
Xolotl Warrior
Coustillier
Elite Coustillier
Serjeant
Elite Serjeant
Donjon
Flemish Militia
Obuch
Elite Obuch
Hussite Wagon
Elite Hussite Wagon
Winged Hussar
Houfnice
Folwark
Urumi Swordsman
Elite Urumi Swordsman
Ratha (Melee)
Elite Ratha (Melee)
Chakram Thrower
Elite Chakram Thrower
Armored Elephant
Siege Elephant
Ghulam
Elite Ghulam
Thirisadai
Shrivamsha Rider
Elite Shrivamsha Rider
Caravanserai
Camel Scout
Ratha (Ranged)
Elite Ratha (Ranged)
Centurion
Elite Centurion
Legionary
Dromon
Composite Bowman
Elite Composite Bowman
Monaspa
Elite Monaspa
Fortified Church
Warrior Priest
Savar
Fire Lancer
Elite Fire Lancer
Rocket Cart
Heavy Rocket Cart
Iron Pagoda
Elite Iron Pagoda
Grenadier
Liao Dao
Elite Liao Dao
Mounted Trebuchet
Traction Trebuchet
Hei Guang Cavalry
Heavy Hei Guang Cavalry
Lou Chuan
Tiger Cavalry
Elite Tiger Cavalry
Xianbei Raider
Cao Cao
White Feather Guard
Elite White Feather Guard
Liu Bei
Fire Archer
Elite Fire Archer
Jian Swordsman
Sun Jian
Champi Scout
Champi Warrior
Elite Champi Warrior
Settlement
Guecha Warrior
Elite Guecha Warrior
Kona
Elite Kona
Bolas Rider
Elite Bolas Rider
Blackwood Archer
Elite Blackwood Archer
Ibirapema Warrior
Elite Ibirapema Warrior
Temple Guard
Elite Temple Guard
Champi Runner
Hulk
War Hulk
Carrack
Catapult Galleon

7
settings.yaml Normal file
View File

@@ -0,0 +1,7 @@
storage:
root: '/home/nate/aoe/flashcards/'
data: 'data'
namelists: 'output/namelists'
maps: 'output/maps'
decks: 'output/decks-output'
data_file: 'units_buildings_techs.de.json'