Skip to content

Commit 763a52a

Browse files
authored
Merge pull request open5e#648 from open5e/eepMoody/issue644
EepMoody/issue644
2 parents b4160b5 + 4f56842 commit 763a52a

8 files changed

+54
-2
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local.env
22
selfsigned.*
33

44
**/.DS_Store
5+
__pycache__
56
staticfiles/
67
db\.sqlite3
78

@@ -15,6 +16,7 @@ server/whoosh_index/
1516

1617
# pycharm config
1718
.idea/
19+
1820
# pyenv env file
1921
.python-version
2022

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 5.1.2 on 2025-03-08 01:20
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('api_v2', '0024_alter_classfeatureitem_detail'),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name='creature',
15+
name='initiative_bonus',
16+
field=models.SmallIntegerField(blank=True, help_text='Initiative bonus. If not set, defaults to dexterity modifier.', null=True),
17+
),
18+
]

api_v2/models/creature.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,21 @@ class Creature(Object, HasAbilities, HasSenses, HasLanguage, HasSpeed, FromDocum
8585
environments = models.ManyToManyField(Environment,
8686
related_name="creature_environments")
8787

88+
initiative_bonus = models.SmallIntegerField(
89+
null=True,
90+
blank=True,
91+
help_text="Initiative bonus. If not set, defaults to dexterity modifier."
92+
)
93+
94+
def get_initiative_bonus(self):
95+
"""
96+
Returns the creature's initiative bonus. If no explicit bonus is set,
97+
returns the dexterity modifier.
98+
"""
99+
if self.initiative_bonus is not None:
100+
return self.initiative_bonus
101+
return self.modifier_dexterity
102+
88103
def as_text(self):
89104
text = self.name + '\n'
90105
for action in self.creatureaction_set.all():
@@ -156,7 +171,6 @@ def experience_points(self):
156171
except:
157172
return None
158173

159-
160174
@property
161175
def actions(self):
162176
"""Returns the set of actions that are related to this creature."""

api_v2/serializers/creature.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class CreatureSerializer(GameContentSerializer):
8181
size = SizeSerializer()
8282
languages = LanguageSerializer(many=True)
8383
environments = EnvironmentSerializer(many=True)
84+
initiative_bonus = serializers.SerializerMethodField()
8485

8586
class Meta:
8687
'''Serializer meta options.'''
@@ -106,6 +107,7 @@ class Meta:
106107
'experience_points',
107108
'ability_scores',
108109
'modifiers',
110+
'initiative_bonus',
109111
'saving_throws',
110112
'saving_throws_all',
111113
'skill_bonuses',
@@ -325,10 +327,14 @@ def get_challenge_rating_text(self, creature):
325327
@extend_schema_field(OpenApiTypes.INT)
326328
def get_experience_points(self, creature):
327329
return creature.experience_points
328-
330+
331+
@extend_schema_field(OpenApiTypes.INT)
332+
def get_initiative_bonus(self, creature):
333+
return creature.get_initiative_bonus()
329334

330335
class CreatureSetSerializer(GameContentSerializer):
331336
'''Serializer for the Creature Set object'''
337+
332338
key = serializers.ReadOnlyField()
333339
creatures = CreatureSerializer(many=True, read_only=True, context={'request':{}})
334340

api_v2/tests/responses/TestObjects.test_creature_ancient_example.approved.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@
238238
"experience_points": 62000,
239239
"hit_dice": "28d20+252",
240240
"hit_points": 546,
241+
"initiative_bonus": 0,
241242
"key": "srd_ancient-red-dragon",
242243
"languages": [
243244
{

api_v2/tests/responses/TestObjects.test_creature_goblin_example.approved.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@
235235
"experience_points": 50,
236236
"hit_dice": "2d6",
237237
"hit_points": 7,
238+
"initiative_bonus": 2,
238239
"key": "srd_goblin",
239240
"languages": [
240241
{

api_v2/tests/responses/TestObjects.test_creature_guard_example.approved.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@
162162
"experience_points": 25,
163163
"hit_dice": "2d8+2",
164164
"hit_points": 11,
165+
"initiative_bonus": 1,
165166
"key": "srd_guard",
166167
"languages": [
167168
{

api_v2/tests/responses/TestObjects.test_creatureset_example.approved.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
"experience_points": 25,
9797
"hit_dice": "2d10+4",
9898
"hit_points": 15,
99+
"initiative_bonus": -1,
99100
"key": "srd_camel",
100101
"languages": [],
101102
"modifiers": {
@@ -230,6 +231,7 @@
230231
"experience_points": 0,
231232
"hit_dice": null,
232233
"hit_points": 11,
234+
"initiative_bonus": 0,
233235
"key": "srd_donkey",
234236
"languages": [],
235237
"modifiers": {
@@ -409,6 +411,7 @@
409411
"experience_points": 50,
410412
"hit_dice": "3d10+3",
411413
"hit_points": 19,
414+
"initiative_bonus": 0,
412415
"key": "srd_draft-horse",
413416
"languages": [],
414417
"modifiers": {
@@ -641,6 +644,7 @@
641644
"experience_points": 1100,
642645
"hit_dice": "8d12+24",
643646
"hit_points": 76,
647+
"initiative_bonus": -1,
644648
"key": "srd_elephant",
645649
"languages": [],
646650
"modifiers": {
@@ -849,6 +853,7 @@
849853
"experience_points": 25,
850854
"hit_dice": "1d8+1",
851855
"hit_points": 5,
856+
"initiative_bonus": 2,
852857
"key": "srd_mastiff",
853858
"languages": [],
854859
"modifiers": {
@@ -1057,6 +1062,7 @@
10571062
"experience_points": 25,
10581063
"hit_dice": "2d8+2",
10591064
"hit_points": 11,
1065+
"initiative_bonus": 0,
10601066
"key": "srd_mule",
10611067
"languages": [],
10621068
"modifiers": {
@@ -1263,6 +1269,7 @@
12631269
"experience_points": 25,
12641270
"hit_dice": "2d8+2",
12651271
"hit_points": 11,
1272+
"initiative_bonus": 0,
12661273
"key": "srd_pony",
12671274
"languages": [],
12681275
"modifiers": {
@@ -1452,6 +1459,7 @@
14521459
"experience_points": 50,
14531460
"hit_dice": "2d10+2",
14541461
"hit_points": 13,
1462+
"initiative_bonus": 0,
14551463
"key": "srd_riding-horse",
14561464
"languages": [],
14571465
"modifiers": {
@@ -1631,6 +1639,7 @@
16311639
"experience_points": 100,
16321640
"hit_dice": "3d10+3",
16331641
"hit_points": 19,
1642+
"initiative_bonus": 1,
16341643
"key": "srd_warhorse",
16351644
"languages": [],
16361645
"modifiers": {

0 commit comments

Comments
 (0)