Browse Source

Prevent gifting into a negative balance

Eren Yilmaz 5 years ago
parent
commit
5e451a622d
1 changed files with 6 additions and 3 deletions
  1. 6 3
      server_controller.py

+ 6 - 3
server_controller.py

@@ -154,9 +154,12 @@ def gift(json_request):
 
     if model.user_available_ownable(sender_id, ownable_id) == 0:
         return BadRequest('You do not own any of these.')
-    if not model.user_has_at_least_available(amount, sender_id, ownable_id):
-        # for example if you have a 1.23532143213 Kollar and want to give them all away
-        amount = model.user_available_ownable(sender_id, ownable_id)
+
+    wealth = model.user_wealth(user_id=sender_id)
+    if wealth < amount:
+        raise PreconditionFailed(f'Your current wealth, computed from your owned equities minus debt, is {wealth}.\n'
+                                 f'To protect you from giving away more than you can afford, you not gift any amounts larger than this to other players.'
+                                 f'You can still try to emulate gifting by selling something at a low price and buying it back afterwards at a higher price.')
 
     recipient_id = model.get_user_id_by_name(json_request['username'])