/
dnd_spells.py
114 lines (102 loc) · 3.34 KB
/
dnd_spells.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import requests,json,random
#get class information
rc = open("random_raceclass.json")
raceclass = json.load(rc)
char_class = raceclass['class_name']
#check for spellcasting ability
spell_check = requests.get('https://www.dnd5eapi.co/api/spellcasting')
sc_json = json.loads(spell_check.text)
for item in sc_json['results']:
if char_class in item['class']:
spell_url = item['url']
if "spell_url" in locals():
casting = requests.get('https://www.dnd5eapi.co'+spell_url)
casting_json = json.loads(casting.text)
if casting_json['level'] == 1:
spellcasting = True
else:
spellcasting = False
else:
spellcasting = False
#get spellcasting modifiers
if spellcasting == True:
casting = requests.get('https://www.dnd5eapi.co'+spell_url)
casting_json = json.loads(casting.text)
ability = casting_json['spellcasting_ability']['name'].lower()
mods = open("ability_mods.json")
a_mods = json.load(mods)
p = open("other_scores.json")
prof = json.load(p)
spell_attack = a_mods[ability] + prof['Proficiency Bonus']
spell_dc = 8 + a_mods[ability] + prof['Proficiency Bonus']
#number of spells: this is NOT included as a unique value in the API!
#... so i manually transcribed it from the "description" parts of the API ^^"
if char_class == "Bard":
cantrips = 2
spells = 4
spell_slots = 2
spell_refresh = "level up"
if char_class == "Cleric":
cantrips = 3
spells = a_mods['wis'] + 1
if spells < 1:
spells = 1
spell_slots = 2
spell_refresh = "long rest"
if char_class == "Druid":
cantrips = 2
spells = a_mods['wis'] + 1
if spells < 1:
spells = 1
spell_slots = 2
spell_refresh = "long rest"
if char_class == "Sorcerer":
cantrips = 4
spells = 2
spell_slots = 2
spell_refresh = "level up"
if char_class == "Warlock":
cantrips = 2
spells = 2
spell_slots = 1
spell_refresh = "level up"
if char_class == "Wizard":
cantrips = 3
spells = a_mods['int'] + 1
if spells < 1:
spells = 1
spell_slots = 2
spell_refresh = "long rest"
#get full spell lists!
if spellcasting == True:
cantrip_list = []
spell_list = []
all_spells = requests.get('https://www.dnd5eapi.co/api/spells')
full_spell_list = json.loads(all_spells.text)
for item in full_spell_list['results']:
this_spell = requests.get('https://www.dnd5eapi.co'+item['url'])
spell_info = json.loads(this_spell.text)
lvl = spell_info['level']
name = spell_info['name']
for x in spell_info['classes']:
if x['name'] == char_class:
if lvl == 0:
cantrip_list.append(name)
if lvl == 1:
spell_list.append(name)
#final spell lists
cantrips_known = random.sample(cantrip_list,cantrips)
spells_known = random.sample(spell_list,spells)
spell_info = {
"Cantrips":cantrips_known,
"Spells":spells_known,
"Spell Slots":spell_slots,
"New Spells":"every "+spell_refresh,
"Spell Attack Bonus":spell_attack,
"Spell DC":spell_dc
}
else:
spell_info = {"Spells":"No Spells Known"}
#dump spellcasting info to file
with open("spellcasting_info.json","w") as spellfile:
json.dump(spell_info,spellfile)