Eren Yilmaz преди 6 години
родител
ревизия
88ff7e002e
променени са 5 файла, в които са добавени 109 реда и са изтрити 7 реда
  1. 1 1
      client.py
  2. 49 5
      client_controller.py
  3. 21 0
      model.py
  4. 1 1
      server.py
  5. 37 0
      server_controller.py

+ 1 - 1
client.py

@@ -50,7 +50,7 @@ To display an overview of available commands type \'help\'.
 ''')
 
 
-allowed_commands = ['login', 'register', 'help', 'depot', 'activate_key']
+allowed_commands = ['login', 'register', 'help', 'depot', 'activate_key', 'buy', 'orders']
 
 
 def one_command():

+ 49 - 5
client_controller.py

@@ -86,19 +86,63 @@ def depot():
         print(tabulate(response['data'], headers=['Object', 'Amount'], tablefmt="pipe"))
     else:
         if 'error_message' in response:
-            print('Login failed with message:', response['error_message'])
+            print('Depot access failed with message:', response['error_message'])
         else:
-            print('Login failed.')
+            print('Depot access failed.')
 
 
 def activate_key(key=''):
-    if key is '':
+    if key == '':
         print('Entering a game key may get you some money or other useful stuff.')
         key = input('Key: ')
 
-    if key is '':
+    if key == '':
         print('Invalid key.')
 
     response = client_request('activate_key', {"session_id": connection.session_id, 'key':key })
     if 'error_message' in response:
-        print('Key activation failed with message:', response['error_message'])
+        print('Key activation failed with message:', response['error_message'])
+
+
+def yn_dialog(msg):
+    while True:
+        result = input(msg + ' [y/n]: ')
+        if result == 'y':
+            return True
+        if result == 'n':
+            return False
+
+
+def buy(amount=None, object_name=None, limit='', stop_loss=''):
+    if object_name is None:  # TODO list some available objects
+        object_name = input('Name of object to buy: ')
+    if amount is None:
+        print('Entering a game key may get you some money or other useful stuff.')
+        amount = input('Amount: ')
+    if limit != '':
+        set_limit = yn_dialog('Do you want to place a limit?')
+        if set_limit:
+            limit = input('Limit: ')
+            stop_loss = yn_dialog('Is this a stop-loss limit?')
+    response = client_request('order', {"buy": True,
+                                        "session_id": connection.session_id,
+                                        "amount":amount,
+                                        "ownable": object_name,
+                                        "limit": limit,
+                                        "stop_loss": stop_loss})
+    if 'error_message' in response:
+        print('Order placement failed with message:', response['error_message'])
+
+
+def orders():
+    response = client_request('orders', {"session_id": connection.session_id})
+    success = 'data' in response
+    if success:
+        print(tabulate(response['data'],
+                       headers=['Buy?', 'Name', 'Amount', 'Limit', 'Stop Loss?', 'Orig. Order Size'],
+                       tablefmt="pipe"))
+    else:
+        if 'error_message' in response:
+            print('Order access failed with message:', response['error_message'])
+        else:
+            print('Order access failed.')

+ 21 - 0
model.py

@@ -284,3 +284,24 @@ def valid_session_id(session_id):
         return True
     else:
         return False
+
+
+def get_user_orders(user_id):
+    connect()
+
+    cursor.execute('''
+        SELECT 
+            orders.buy, 
+            ownables.name, 
+            orders.ordered_amount - order.executed_amount, 
+            orders.limit, 
+            orders.stop_loss, 
+            orders.ordered_amount
+        FROM orders, ownables, ownership
+        WHERE ownership.user_id = ?
+        AND ownership.ownable_id = ownables.rowid
+        AND orders.ownable_id = ownership.rowid
+        ORDER BY orders.buy, ownables.name
+        ''', (user_id,))
+
+    return cursor.fetchall()

+ 1 - 1
server.py

@@ -13,7 +13,7 @@ if __name__ == '__main__':
 
     model.setup()
 
-    valid_routes = ['login', 'register', 'depot', 'activate_key']
+    valid_routes = ['login', 'register', 'depot', 'activate_key', 'order']
 
 
     @route('/<path>', method='POST')

+ 37 - 0
server_controller.py

@@ -70,6 +70,43 @@ def activate_key():
         return bad_request('Invalid key.')
 
 
+def order():
+    missing = missing_attributes(['buy', 'session_id', 'amount', 'ownable'])
+    if missing:
+        return bad_request(missing)
+    buy = request.json['buy']
+    sell = not buy
+    session_id = request.json['session_id']
+    amount = request.json['amount']
+    ownable_name = request.json['ownable']
+    ownable_id = model.ownable_id_by_name(ownable_name)
+    user_id = model.get_user_id_by_session_id(session_id)
+    ownership_id = model.ownership_id(ownable_id, user_id)
+
+    limit = None
+    if 'limit' in request.json:
+        limit = request.json['limit']
+    stop_loss = 'stop_loss' in request.json and bool(request.json['stop_loss'])  # TODO is this a string or a bool?
+
+    if sell:
+        if not model.user_owns_at_least(user_id, amount, ownership_id):
+            return bad_request('You can not sell more than you own.')
+    if buy:
+        if not model.user_owns_at_least(user_id, amount, model.kollar_id()):
+            return bad_request('You do not have the money to buy that much.')
+
+    model.place_order(buy, user_id, ownable_id, limit, stop_loss, amount)
+    return {'message': "Order placed."}
+
+
+def orders():
+    missing = missing_attributes(['session_id'])
+    if missing:
+        return bad_request(missing)
+    data = model.get_user_orders(model.get_user_id_by_session_id(request.json['session_id']))
+    return {'data': data}
+
+
 def not_found(msg=''):
     response.status = 404
     if debug: