tabletop-frog/ttfrog/db/bootstrap.py

97 lines
3.1 KiB
Python
Raw Normal View History

2024-01-28 00:46:19 -08:00
import logging
2024-01-30 01:25:02 -08:00
from ttfrog.db.manager import db
from ttfrog.db import schema
2024-01-28 00:46:19 -08:00
2024-01-30 01:25:02 -08:00
from sqlalchemy.exc import IntegrityError
2024-01-28 00:46:19 -08:00
# move this to json or whatever
data = {
2024-02-08 01:14:35 -08:00
'CharacterClass': [
{
'name': 'fighter',
'hit_dice': '1d10',
'hit_dice_stat': 'CON',
'proficiencies': 'all armor, all shields, simple weapons, martial weapons',
'saving_throws': ['STR, CON'],
'skills': ['Acrobatics', 'Animal Handling', 'Athletics', 'History', 'Insight', 'Intimidation', 'Perception', 'Survival'],
},
{
'name': 'rogue',
'hit_dice': '1d8',
'hit_dice_stat': 'DEX',
'proficiencies': 'simple weapons, hand crossbows, longswords, rapiers, shortswords',
'saving_throws': ['DEX', 'INT'],
'skills': ['Acrobatics', 'Athletics', 'Deception', 'Insight', 'Intimidation', 'Investigation', 'Perception', 'Performance', 'Persuasion', 'Sleight of Hand', 'Stealth'],
},
],
'Skill': [
{'name': 'Acrobatics'},
{'name': 'Animal Handling'},
{'name': 'Athletics'},
{'name': 'Deception'},
{'name': 'History'},
{'name': 'Insight'},
{'name': 'Intimidation'},
{'name': 'Investigation'},
{'name': 'Perception'},
{'name': 'Performance'},
{'name': 'Persuasion'},
{'name': 'Sleight of Hand'},
{'name': 'Stealth'},
{'name': 'Survival'},
],
2024-01-30 01:25:02 -08:00
'Ancestry': [
2024-02-08 01:14:35 -08:00
{'name': 'human', 'creature_type': 'humanoid'},
{'name': 'dragonborn', 'creature_type': 'humanoid'},
{'name': 'tiefling', 'creature_type': 'humanoid'},
2024-01-28 00:46:19 -08:00
],
2024-01-30 01:25:02 -08:00
'Character': [
2024-02-08 01:14:35 -08:00
{
'id': 1,
'name': 'Sabetha',
'ancestry': 'tiefling',
'character_class': ['fighter', 'rogue'],
'level': 1,
'armor_class': 10,
'max_hit_points': 14,
'hit_points': 14,
'temp_hit_points': 0,
'passive_perception': 10,
'passive_insight': 10,
'passive_investigation': 10,
'speed': '30 ft.',
'str': 16,
'dex': 12,
'con': 18,
'int': 11,
'wis': 12,
'cha': 8,
'proficiencies': 'all armor, all shields, simple weapons, martial weapons',
'saving_throws': ['STR', 'CON'],
'skills': ['Acrobatics', 'Animal Handling'],
},
2024-01-28 14:31:50 -08:00
]
2024-01-28 00:46:19 -08:00
}
def bootstrap():
"""
Initialize the database with source data. Idempotent; will skip anything that already exists.
"""
2024-01-30 01:25:02 -08:00
db.init()
for table, records in data.items():
model = getattr(schema, table)
2024-01-28 22:14:50 -08:00
2024-01-30 01:25:02 -08:00
for rec in records:
2024-01-31 22:39:54 -08:00
obj = model(**rec)
try:
with db.transaction():
db.session.add(obj)
2024-02-04 11:40:30 -08:00
logging.info(f"Created {table} {obj}")
2024-01-31 22:39:54 -08:00
except IntegrityError as e:
if 'UNIQUE constraint failed' in str(e):
logging.info(f"Skipping existing {table} {obj}")
continue
raise