Database Reference
In-Depth Information
'''
result
=
db
.
item
.
update
(
{
'_id'
:
container_id
,
'inventory.id'
:
item_id
},
{
'$pull'
: {
'inventory'
: {
'id'
:
item_id
} } },
safe
=
True
)
iif
not
not
result
[
'updatedExisting'
]:
raise
raise
Conflict
()
item
=
item_index
[
item_id
]
container
=
item_index
[
item_id
]
character
[
'inventory'
]
.
append
(
item
)
container
[
'inventory'
]
=
[
item
for
for
item
iin
container
[
'inventory'
]
iif
item
[
'_id'
]
!=
item_id
]
db
.
character
.
update
(
{
'_id'
:
character
[
'_id'
] },
{
'$push'
: {
'inventory'
:
item
} } )
db
.
character
.
update
(
{
'_id'
:
character
[
'_id'
],
'inventory.id'
:
container_id
},
{
'$pull'
: {
'inventory.$.inventory'
: {
'id'
:
item_id
} } } )
Note in this code that we:
Ensure that the item's state makes this update reasonable (the item is actually con-
tained within the container). Abort with an error if this is not true.
Update the in-memory
character
document's inventory, adding the item.
In
the case that the character is moving an item from a container
in his own invent-
ory
, update the character's inventory representation of the container.
Move the Character to a Different Room
Inourgame,supposetheplayerdecidestomovenorth.Inthiscase,weneedtoupdatethe
character state to match the new location: