Database Reference
In-Depth Information
result = db . character . update (
{ '_id' : character [ '_id' ],
'gold' : { '$gte' : price } },
{ '$inc' : { 'gold' : - price } },
safe = True )
iif not
not result [ 'updatedExisting' ]:
raise
raise InsufficientFunds ()
try
try :
pick_up_item ( character , item_id )
except
except :
# Add the gold back to the character
result = db . character . update (
{ '_id' : character [ '_id' ] },
{ '$inc' : { 'gold' : price } } )
raise
raise
character [ 'gold' ] -= price
db . character . update (
{ '_id' : shopkeeper [ '_id' ] },
{ '$inc' : { 'gold' : price } } )
Note that the buy() function ensures that the character has sufficient gold to pay for the
item using the updatedExisting trick used for picking up items. The race condition for item
pickup is handled as well, “rolling back” the removal of gold from the character's wallet if the
item cannot be picked up.
Search WWH ::




Custom Search