Files
dungeon-sheets/SaveSpells.ipynb
T

308 lines
6.1 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"from tqdm import tqdm\n",
"import string\n",
"import os"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from DnD4py.lookup_5e import DnDSpell, Roll20Spell"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_csv('all_spells.csv')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"spells = {}\n",
"failed = {}"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"names = sorted([s.lower().replace('', '').replace('/', '').replace('\\'', '') for s in df['Name'].values])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 461/461 [06:02<00:00, 1.27it/s]\n"
]
}
],
"source": [
"for i, name in tqdm(enumerate(names), total=len(names)):\n",
" if name in spells:\n",
" continue\n",
" try:\n",
" spells[name] = DnDSpell(name).as_dungeonsheets_class()\n",
" except Exception as e1:\n",
" try:\n",
" spells[name] = Roll20Spell(name).as_dungeonsheets_class()\n",
" except Exception as e2:\n",
" failed[name] = (e1, e2)\n",
" print(f'Failed: {name}')"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"def save_spell(name, text):\n",
" filename = f'dungeonsheets/spells/spells_{name[0].lower()}.py'\n",
" if os.path.isfile(filename):\n",
" with open(filename, 'r') as f:\n",
" current_text = f.read().strip()\n",
" else:\n",
" with open(filename, 'a') as f:\n",
" f.write('from .spells import Spell\\n\\n\\n')\n",
" current_text = ''\n",
" if text.strip() in current_text:\n",
" raise ValueError('Text already exists in the file')\n",
" with open(filename, 'a') as f:\n",
" f.write(text + '\\n')"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"for c in string.ascii_lowercase:\n",
" filename = f'dungeonsheets/spells/spells_{c}.py'\n",
" if os.path.isfile(filename):\n",
" os.remove(filename)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 460/460 [00:00<00:00, 2537.91it/s]\n"
]
}
],
"source": [
"for name in tqdm(sorted(spells.keys())):\n",
" save_spell(name, spells[name])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import dungeonsheets"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"from dungeonsheets.spells import *"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"tests = {}\n",
"for k, v in dungeonsheets.spells.__dict__.items():\n",
" if isinstance(v, type) and issubclass(v, dungeonsheets.spells.Spell):\n",
" tests[k] = v()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"Unknown spell\""
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tests.pop('Spell')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"test_char = dungeonsheets.Character()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"test_char._spells = test_char._spells_prepared = list(tests.values())"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"create_spellbook_pdf(test_char, 'Entire_Spellbook')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"test_char.wield_weapon('Spear +1')"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[\"Spear+1\"]"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"test_char.weapons"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}