소스 검색

sending gifts

Eren Yilmaz 6 년 전
부모
커밋
676151491a
4개의 변경된 파일45개의 추가작업 그리고 3개의 파일을 삭제
  1. 19 0
      client_controller.py
  2. 1 0
      run_client.py
  3. 1 0
      run_server.py
  4. 24 3
      server_controller.py

+ 19 - 0
client_controller.py

@@ -275,6 +275,25 @@ def orders_on(object_name=None):
             print('Order access failed.')
             print('Order access failed.')
 
 
 
 
+def gift(username=None, amount=None, object_name=None):
+    if username is None:
+        username = input('Username of recipient: ')
+    if object_name is None:
+        object_name = input('Name of object to give: ')
+    if amount is None:
+        amount = input('How many?: ')
+    fake_loading_bar('Sending Gift', duration=4.2)
+    response = client_request('gift',
+                              {"session_id": connection.session_id,
+                               "username": username,
+                               "object_name": object_name,
+                               "amount":amount})
+    if 'error_message' in response:
+        print('Order access failed with message:', response['error_message'])
+    elif 'message' in response:
+        print(response['message'])
+
+
 def news():
 def news():
     fake_loading_bar('Loading Data', duration=0.76)
     fake_loading_bar('Loading Data', duration=0.76)
     response = client_request('news', {"session_id": connection.session_id})
     response = client_request('news', {"session_id": connection.session_id})

+ 1 - 0
run_client.py

@@ -65,6 +65,7 @@ allowed_commands = ['help',
                     'transactions',
                     'transactions',
                     'orders_on',
                     'orders_on',
                     'cancel_order',
                     'cancel_order',
+                    'gift',
                     'leaderboard',
                     'leaderboard',
                     'exit']
                     'exit']
 
 

+ 1 - 0
run_server.py

@@ -21,6 +21,7 @@ if __name__ == '__main__':
                     'orders_on',
                     'orders_on',
                     'cancel_order',
                     'cancel_order',
                     'leaderboard',
                     'leaderboard',
+                    'gift',
                     'change_password']
                     'change_password']
 
 
 
 

+ 24 - 3
server_controller.py

@@ -8,7 +8,7 @@ from debug import debug
 
 
 def missing_attributes(attributes):
 def missing_attributes(attributes):
     for attr in attributes:
     for attr in attributes:
-        if attr not in request.json or request.json[attr] == '':
+        if attr not in request.json or request.json[attr] == '' or request.json[attr] is None:
             if str(attr) == 'session_id':
             if str(attr) == 'session_id':
                 return 'You are not signed in.'
                 return 'You are not signed in.'
             return 'Missing value for attribute ' + str(attr)
             return 'Missing value for attribute ' + str(attr)
@@ -116,8 +116,6 @@ def order():
     except KeyError:  # for example when stop_loss was not specified
     except KeyError:  # for example when stop_loss was not specified
         stop_loss = None
         stop_loss = None
 
 
-    # TODO test if stop loss works
-
     if sell:
     if sell:
         if not model.user_owns_at_least(amount, user_id, ownable_id):
         if not model.user_owns_at_least(amount, user_id, ownable_id):
             return bad_request('You can not sell more than you own.')
             return bad_request('You can not sell more than you own.')
@@ -126,6 +124,29 @@ def order():
     return {'message': "Order placed."}
     return {'message': "Order placed."}
 
 
 
 
+def gift():
+    missing = missing_attributes(['session_id', 'amount', 'object_name', 'username'])
+    if missing:
+        return bad_request(missing)
+    if not model.ownable_name_exists(request.json['object_name']):
+        return bad_request('This kind of object can not be given away.')
+    if request.json['username'] == 'bank' or not model.user_exists(request.json['username']):
+        return bad_request('There is no user with this name.')
+    try:
+        amount = float(request.json['amount'])
+    except ValueError:
+        return bad_request('Invalid amount.')
+    ownable_id = model.ownable_id_by_name(request.json['object_name'])
+    sender_id = model.get_user_id_by_session_id(request.json['session_id'])
+    recipient_id = model.get_user_id_by_name(request.json['username'])
+    if not model.user_owns_at_least(amount, sender_id, ownable_id):
+        return bad_request('You do not own enough.')
+
+    model.send_ownable(sender_id, recipient_id, request.json['object_name'], amount)
+
+    return {'message': "Gift sent."}
+
+
 def orders():
 def orders():
     missing = missing_attributes(['session_id'])
     missing = missing_attributes(['session_id'])
     if missing:
     if missing: