Quellcode durchsuchen

add some more fake loading bars

Eren Yilmaz vor 6 Jahren
Ursprung
Commit
edf9155b79
6 geänderte Dateien mit 48 neuen und 20 gelöschten Zeilen
  1. 16 4
      client_controller.py
  2. 4 1
      connection.py
  3. 1 1
      create_new_key.py
  4. 14 2
      model.py
  5. 10 9
      run_client.py
  6. 3 3
      server_controller.py

+ 16 - 4
client_controller.py

@@ -12,7 +12,7 @@ from util import debug
 
 def login(username=None, password=None):
     if connection.session_id is not None:
-        fake_loading_bar('Signing out', delay=0.025)
+        fake_loading_bar('Signing out', duration=0.7)
         connection.session_id = None
 
     if username is None:
@@ -24,6 +24,7 @@ def login(username=None, password=None):
         else:
             password = input('Password: ')
 
+    fake_loading_bar('Signing in', duration=2.3)
     response = client_request('login', {"username": username, "password": password})
     success = 'session_id' in response
     if success:
@@ -38,8 +39,8 @@ def login(username=None, password=None):
 
 def register(username=None, password=None, game_key=''):
     if connection.session_id is not None:
-        fake_loading_bar('Signing out', delay=0.025)
         connection.session_id = None
+        fake_loading_bar('Signing out', duration=0.7)
 
     if username is None:
         username = input('Username: ')
@@ -51,10 +52,13 @@ def register(username=None, password=None, game_key=''):
             password = input('Password: ')
 
     if not debug:
-        if game_key is '':
+        if game_key == '':
             print('Entering a game key will provide you with some starting money and other useful stuff.')
             game_key = input('Game key (leave empty if you don\'t have one): ')
 
+    fake_loading_bar('Validating Registration', duration=5.2)
+    if game_key != '':
+        fake_loading_bar('Validating Game Key', duration=0.4)
     response = client_request('register', {"username": username, "password": password, "game_key": game_key})
 
     if 'error_message' in response:
@@ -65,6 +69,7 @@ def cancel_order(order_no=None):
     if order_no is None:
         order_no = input('Order No.: ')
 
+    fake_loading_bar('Validating Request', duration=0.6)
     response = client_request('cancel_order', {"session_id": connection.session_id, "order_id": order_no})
 
     if 'error_message' in response:
@@ -93,10 +98,11 @@ def help():
 
 
 def depot():
+    fake_loading_bar('Loading data', duration=1.3)
     response = client_request('depot', {"session_id": connection.session_id})
     success = 'data' in response and 'own_wealth' in response
     if success:
-        print(tabulate(response['data'], headers=['Object', 'Amount'], tablefmt="pipe"))
+        print(tabulate(response['data'], headers=['Object', 'Amount', 'Est. Value'], tablefmt="pipe"))
         print('This corresponds to a wealth of roughly', response['own_wealth'])
     else:
         if 'error_message' in response:
@@ -106,6 +112,7 @@ def depot():
 
 
 def leaderboard():
+    fake_loading_bar('Loading data', duration=1.3)
     response = client_request('leaderboard', {"session_id": connection.session_id})
     success = 'data' in response
     if success:
@@ -125,6 +132,7 @@ def activate_key(key=''):
     if key == '':
         print('Invalid key.')
 
+    fake_loading_bar('Validating Key', duration=0.4)
     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'])
@@ -204,6 +212,7 @@ def sell(amount=None, object_name=None, limit=None, stop_loss=None, time_until_e
 
 
 def orders():
+    fake_loading_bar('Validating Key', duration=0.9)
     response = client_request('orders', {"session_id": connection.session_id})
     success = 'data' in response
     if success:
@@ -220,6 +229,7 @@ def orders():
 def orders_on(object_name=None):
     if object_name is None:  # TODO list some available objects
         object_name = input('Name of object to check: ')
+    fake_loading_bar('Validating Key', duration=2.3)
     response = client_request('orders_on', {"session_id": connection.session_id, "ownable": object_name})
     success = 'data' in response
     if success:
@@ -234,6 +244,7 @@ def orders_on(object_name=None):
 
 
 def news():
+    fake_loading_bar('Loading Data', duration=0.76)
     response = client_request('news', {"session_id": connection.session_id})
     success = 'data' in response
     if success:
@@ -250,6 +261,7 @@ def news():
 def transactions(object_name=None):
     if object_name is None:  # TODO list some available objects
         object_name = input('Name of object to check: ')
+    fake_loading_bar('Loading Data', duration=1.3)
     response = client_request('transactions', {"session_id": connection.session_id, "ownable": object_name})
     success = 'data' in response
     if success:

+ 4 - 1
connection.py

@@ -5,7 +5,10 @@ import requests
 from util import debug
 
 port = 8451
-host = 'http://localhost:' + str(port)
+if debug:
+    host = 'http://localhost:' + str(port)
+else:
+    host = 'http://koljastrohm-games.com:' + str(port)
 json_headers = {'Content-type': 'application/json'}
 
 

+ 1 - 1
create_new_key.py

@@ -4,4 +4,4 @@ from model import generate_keys, unused_keys
 if __name__ == '__main__':
     generate_keys(count=1)
     print(unused_keys())
-    cleanup()
+    cleanup()

+ 14 - 2
model.py

@@ -272,11 +272,23 @@ def get_user_ownership(user_id):
     connect()
 
     cursor.execute('''
-        SELECT ownables.name, ownership.amount
+        SELECT 
+            ownables.name, 
+            ownership.amount, 
+            COALESCE (
+            CASE -- sum score for each of the users ownables
+            WHEN ownership.ownable_id = ? THEN 1
+            ELSE (SELECT price 
+                  FROM transactions
+                  WHERE ownable_id = ownership.ownable_id 
+                  ORDER BY dt DESC 
+                  LIMIT 1)
+            END, 0) AS value
         FROM ownership, ownables
         WHERE user_id = ?
+        AND ownership.amount > 0
         AND ownership.ownable_id = ownables.rowid
-        ''', (user_id,))
+        ''', (currency_id(), user_id,))
 
     return cursor.fetchall()
 

+ 10 - 9
run_client.py

@@ -7,15 +7,16 @@ import client_controller
 from util import debug
 
 
-def fake_loading_bar(msg, delay=0.100):
-    if len(msg) >= 32:
+def fake_loading_bar(msg, duration=5.):
+    if len(msg) >= 50:
         raise AssertionError('Loading bar label too large')
     msg += ': '
     print(msg, end='')
     sys.stdout.flush()
-    for _ in range(78 - len(msg)):
+    bar_length = 78 - len(msg)
+    for _ in range(bar_length):
         if not debug:
-            time.sleep(delay)
+            time.sleep(duration / bar_length)
         print('#', end='')
         sys.stdout.flush()
     print('\n', end='')
@@ -25,11 +26,11 @@ def fake_loading_bar(msg, delay=0.100):
 def load():
     print('Loading...')
 
-    fake_loading_bar('Initializing fake loading bars')
-    fake_loading_bar('Loading data from disk')
-    fake_loading_bar('Loading available commands')
-    fake_loading_bar('Updating indices')
-    fake_loading_bar('Waiting')
+    fake_loading_bar('Initializing fake loading bars', duration=5)
+    fake_loading_bar('Loading data from disk', duration=1)
+    fake_loading_bar('Loading available commands', duration=3.5)
+    fake_loading_bar('Updating indices', duration=2)
+    fake_loading_bar('Waiting', duration=5)
     print('Done.\n\n')
 
 

+ 3 - 3
server_controller.py

@@ -35,9 +35,9 @@ def depot():
     missing = missing_attributes(['session_id'])
     if missing:
         return bad_request(missing)
-    data = model.get_user_ownership(model.get_user_id_by_session_id(request.json['session_id']))
-    return {'data': data,
-            'own_wealth': model.user_wealth(model.get_user_id_by_session_id(request.json['session_id']))}
+    user_id = model.get_user_id_by_session_id(request.json['session_id'])
+    return {'data': model.get_user_ownership(user_id),
+            'own_wealth': model.user_wealth(user_id)}
 
 
 def register():