浏览代码

improve command ordering

Eren Yilmaz 6 年之前
父节点
当前提交
e94adfd4d2
共有 3 个文件被更改,包括 53 次插入51 次删除
  1. 35 35
      client_controller.py
  2. 12 16
      model.py
  3. 6 0
      publish_news_item.py

+ 35 - 35
client_controller.py

@@ -39,7 +39,7 @@ def login(username=None, password=None):
             print('Login failed.')
 
 
-def register(username=None, game_key='', password=None, retype_password=None):
+def register(username=None, game_key='', password=None, retype_pw=None):
     if connection.session_id is not None:
         connection.session_id = None
         fake_loading_bar('Signing out', duration=0.7)
@@ -50,19 +50,19 @@ def register(username=None, game_key='', password=None, retype_password=None):
     if password is None:
         if sys.stdin.isatty():
             password = getpass('New password: ')
-            retype_password = getpass('Retype password: ')
+            retype_pw = getpass('Retype password: ')
         else:
             password = input('New password: ')
-            retype_password = input('Retype password: ')
-        if password != retype_password:
+            retype_pw = input('Retype password: ')
+        if password != retype_pw:
             print('Passwords do not match.')
             return
-    elif retype_password is None:
+    elif retype_pw is None:
         if sys.stdin.isatty():
-            retype_password = getpass('Retype password: ')
+            retype_pw = getpass('Retype password: ')
         else:
-            retype_password = input('Retype password: ')
-        if password != retype_password:
+            retype_pw = input('Retype password: ')
+        if password != retype_pw:
             print('Passwords do not match.')
             return
 
@@ -91,25 +91,25 @@ def cancel_order(order_no=None):
         print('Order cancelling failed with message:', response['error_message'])
 
 
-def change_password(password=None, retype_password=None):
-    if password != retype_password:
+def change_password(password=None, retype_pw=None):
+    if password != retype_pw:
         password = None
     if password is None:
         if sys.stdin.isatty():
             password = getpass('New password: ')
-            retype_password = getpass('Retype password: ')
+            retype_pw = getpass('Retype password: ')
         else:
             password = input('New password: ')
-            retype_password = input('Retype password: ')
-        if password != retype_password:
+            retype_pw = input('Retype password: ')
+        if password != retype_pw:
             print('Passwords do not match.')
             return
-    elif retype_password is None:
+    elif retype_pw is None:
         if sys.stdin.isatty():
-            retype_password = getpass('Retype password: ')
+            retype_pw = getpass('Retype password: ')
         else:
-            retype_password = input('Retype password: ')
-        if password != retype_password:
+            retype_pw = input('Retype password: ')
+        if password != retype_pw:
             print('Passwords do not match.')
             return
 
@@ -136,11 +136,11 @@ def help():
         command_table.append([cmd] + [p for p in params])
 
     print(_my_tabulate(command_table,tablefmt='pipe',headers=['command',
-                                                              'parameter 1',
-                                                              'parameter 2',
-                                                              'parameter 3',
-                                                              'parameter 4',
-                                                              'parameter 5',
+                                                              'param 1',
+                                                              'param 2',
+                                                              'param 3',
+                                                              'param 4',
+                                                              'param 5',
                                                               ]))
     print('NOTE:')
     print('  All parameters for all commands are optional!')
@@ -210,7 +210,7 @@ def yn_dialog(msg):
             return False
 
 
-def buy(amount=None, object_name=None, limit='', stop_loss='', time_until_expiration=None):
+def buy(object_name=None, amount=None, limit='', stop_loss='', expiry=None):
     if object_name is None:  # TODO list some available objects
         object_name = input('Name of object to buy: ')
     if amount is None:
@@ -226,10 +226,10 @@ def buy(amount=None, object_name=None, limit='', stop_loss='', time_until_expira
     if limit is not None and stop_loss == '':
         stop_loss = yn_dialog('Is this a stop-loss limit?')
 
-    if time_until_expiration is None:
-        time_until_expiration = input('Time until order expires (minutes, default 43200):')
-        if time_until_expiration == '':
-            time_until_expiration = 43200
+    if expiry is None:
+        expiry = input('Time until order expires (minutes, default 43200):')
+        if expiry == '':
+            expiry = 43200
     fake_loading_bar('Loading Data', duration=1.3)
     response = client_request('order', {"buy": True,
                                         "session_id": connection.session_id,
@@ -237,7 +237,7 @@ def buy(amount=None, object_name=None, limit='', stop_loss='', time_until_expira
                                         "ownable": object_name,
                                         "limit": limit,
                                         "stop_loss": stop_loss,
-                                        "time_until_expiration": time_until_expiration})
+                                        "time_until_expiration": expiry})
     if 'error_message' in response:
         print('Order placement failed with message:', response['error_message'])
     else:
@@ -245,7 +245,7 @@ def buy(amount=None, object_name=None, limit='', stop_loss='', time_until_expira
               'to see if the order has been executed already.')
 
 
-def sell(amount=None, object_name=None, limit='', stop_loss='', time_until_expiration=None):
+def sell(object_name=None, amount=None, limit='', stop_loss='', expiry=None):
     if object_name is None:  # TODO list some available objects
         object_name = input('Name of object to sell: ')
     if amount is None:
@@ -261,10 +261,10 @@ def sell(amount=None, object_name=None, limit='', stop_loss='', time_until_expir
     if limit != '' and stop_loss == '':
         stop_loss = yn_dialog('Is this a stop-loss limit?')
 
-    if time_until_expiration is None:
-        time_until_expiration = input('Time until order expires (minutes, default 43200):')
-        if time_until_expiration == '':
-            time_until_expiration = 43200
+    if expiry is None:
+        expiry = input('Time until order expires (minutes, default 43200):')
+        if expiry == '':
+            expiry = 43200
     fake_loading_bar('Loading Data', duration=1.3)
     response = client_request('order', {"buy": False,
                                         "session_id": connection.session_id,
@@ -272,7 +272,7 @@ def sell(amount=None, object_name=None, limit='', stop_loss='', time_until_expir
                                         "ownable": object_name,
                                         "limit": limit,
                                         "stop_loss": stop_loss,
-                                        "time_until_expiration": time_until_expiration})
+                                        "time_until_expiration": expiry})
     if 'error_message' in response:
         print('Order placement failed with message:', response['error_message'])
     else:
@@ -312,7 +312,7 @@ def orders_on(object_name=None):
             print('Order access failed.')
 
 
-def gift(username=None, amount=None, object_name=None):
+def gift(username=None, object_name=None, amount=None):
     if username is None:
         username = input('Username of recipient: ')
     if object_name is None:

+ 12 - 16
model.py

@@ -492,20 +492,11 @@ def new_stock(timeout=60, name=None):
         VALUES (?)
         ''', (name,))
 
-    cursor.execute('''
-        INSERT INTO news(title)
-        VALUES (?)
-        ''', ('A new stock can now be bought: ' + name,))
+    new_news('A new stock can now be bought: ' + name)
     if random.getrandbits(1):
-        cursor.execute('''
-            INSERT INTO news(title)
-            VALUES (?)
-            ''', ('Experts expect the price of ' + name + ' to fall',))
+        new_news('Experts expect the price of ' + name + ' to fall')
     else:
-        cursor.execute('''
-            INSERT INTO news(title)
-            VALUES (?)
-            ''', ('Experts expect the price of ' + name + ' to rise',))
+        new_news('Experts expect the price of ' + name + ' to rise')
 
     amount = random.randrange(100, 10000)
     price = random.randrange(10000, 20000) / amount
@@ -757,10 +748,7 @@ def bank_order(buy, ownable_id, limit, amount, time_until_expiration):
                 amount,
                 time_until_expiration)
     ownable_name = ownable_name_by_id(ownable_id)
-    cursor.execute('''
-        INSERT INTO news(title)
-        VALUES (?)
-        ''', ('External investors are selling ' + ownable_name + ' atm',))
+    new_news('External investors are selling ' + ownable_name + ' atm')
 
 
 def current_db_time():  # might differ from datetime.datetime.now() for time zone reasons
@@ -1000,3 +988,11 @@ def hash_all_users_passwords():
             SET password = ?
             WHERE rowid = ?
             ''', (pw, user_id,))
+
+
+def new_news(message):
+    connect()
+    cursor.execute('''
+        INSERT INTO news(title)
+        VALUES (?)
+        ''', (message,))

+ 6 - 0
publish_news_item.py

@@ -0,0 +1,6 @@
+import model
+from admin_console import cleanup
+
+if __name__ == '__main__':
+    print(model.new_news(input('Message of the news:')))
+    cleanup()