Pārlūkot izejas kodu

tradables command and public leaderboard access

Eren Yilmaz 6 gadi atpakaļ
vecāks
revīzija
a2573f8448
6 mainītis faili ar 94 papildinājumiem un 11 dzēšanām
  1. 18 3
      client_controller.py
  2. 29 4
      cool_query.py
  3. 40 0
      model.py
  4. 2 1
      run_client.py
  5. 1 0
      run_server.py
  6. 4 3
      server_controller.py

+ 18 - 3
client_controller.py

@@ -177,7 +177,7 @@ def depot():
 
 def leaderboard():
     fake_loading_bar('Loading data', duration=1.3)
-    response = client_request('leaderboard', {"session_id": connection.session_id})
+    response = client_request('leaderboard', {})
     success = 'data' in response
     if success:
         print(_my_tabulate(response['data'], headers=['Trader', 'Wealth'], tablefmt="pipe"))
@@ -368,9 +368,24 @@ def news():
                            tablefmt="pipe"))
     else:
         if 'error_message' in response:
-            print('Order access failed with message:', response['error_message'])
+            print('News access failed with message:', response['error_message'])
         else:
-            print('Order access failed.')
+            print('News access failed.')
+
+
+def tradables():
+    fake_loading_bar('Loading Data', duration=12.4)
+    response = client_request('tradables', {})
+    success = 'data' in response
+    if success:
+        print(_my_tabulate(response['data'],
+                           headers=['Name', 'Course', 'Market Cap.'],
+                           tablefmt="pipe"))
+    else:
+        if 'error_message' in response:
+            print('Data access failed with message:', response['error_message'])
+        else:
+            print('Data access failed.')
 
 
 def transactions(obj_name=None, limit=5):

+ 29 - 4
cool_query.py

@@ -4,9 +4,21 @@ from client_controller import _my_tabulate
 model.connect()
 model.cursor.execute('''
         EXPLAIN QUERY PLAN
-        DELETE FROM orders 
-        WHERE expiry_dt < DATETIME('now')
-        '''.replace('?','1'))
+        SELECT name, course,
+            (SELECT SUM(amount)
+            FROM ownership
+            WHERE ownership.ownable_id = ownables_with_course.rowid) market_size
+        FROM (SELECT
+                name, ownables.rowid,
+                CASE WHEN ownables.rowid = ? 
+                THEN 1
+                ELSE (SELECT price
+                      FROM transactions
+                      WHERE ownable_id = ownables.rowid
+                      ORDER BY rowid DESC -- equivalent to ordering by dt
+                      LIMIT 1) END course
+             FROM ownables) ownables_with_course
+        '''.replace('?', '1'))
 
 # model.cursor.execute('''
 #             SELECT 2
@@ -17,4 +29,17 @@ model.cursor.execute('''
 #             LIMIT 1
 #          '''.replace('?','1'))
 
-print(_my_tabulate(model.cursor.fetchall(),tablefmt='pipe'))
+# model.cursor.execute('''SELECT CASE CAST(1+(0.5 - RANDOM() / CAST(-9223372036854775808 AS REAL) / 2)*3 AS INTEGER)
+#   WHEN 1 THEN 'one'
+#   WHEN 2 THEN 'two'
+#   WHEN 3 THEN 'three'
+# END
+#   FROM (VALUES (1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) v
+#          '''.replace('?','1'))
+
+# model.cursor.execute('''
+# SELECT *
+#   FROM (SELECT RANDOM() / CAST(-9223372036854775808 AS REAL) / 2) r, (VALUES (1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) v
+#          '''.replace('?','1'))
+
+print(_my_tabulate(model.cursor.fetchall(), tablefmt='pipe'))

+ 40 - 0
model.py

@@ -580,6 +580,10 @@ def delete_order(order_id):
 
 def current_value(ownable_id):
     connect()
+
+    if ownable_id == currency_id():
+        return 1
+
     cursor.execute('''SELECT price 
                       FROM transactions
                       WHERE ownable_id = ?
@@ -1056,3 +1060,39 @@ def new_news(message):
         INSERT INTO news(title)
         VALUES (?)
         ''', (message,))
+
+
+def ownables():
+    connect()
+
+    cursor.execute('''
+        SELECT name, course,
+            (SELECT SUM(amount)
+            FROM ownership
+            WHERE ownership.ownable_id = ownables_with_course.rowid) market_size
+        FROM (SELECT
+                name, ownables.rowid,
+                CASE WHEN ownables.rowid = ? 
+                THEN 1
+                ELSE (SELECT price
+                      FROM transactions
+                      WHERE ownable_id = ownables.rowid
+                      ORDER BY rowid DESC -- equivalent to ordering by dt
+                      LIMIT 1) END course
+             FROM ownables) ownables_with_course
+        ''', (currency_id(),))
+
+    data = cursor.fetchall()
+
+    for idx in range(len(data)):
+        # compute market cap
+        row = data[idx]
+        if row[1] is None:
+            market_cap = None
+        elif row[2] is None:
+            market_cap = None
+        else:
+            market_cap = row[1] * row[2]
+        data[idx] = (row[0], row[1], market_cap)
+
+    return data

+ 2 - 1
run_client.py

@@ -56,9 +56,10 @@ allowed_commands = ['help',
                     'login',
                     'register',
                     'change_pw',
+                    'news',
+                    'tradables',
                     'depot',
                     'orders',
-                    'news',
                     'activate_key',
                     'buy',
                     'sell',

+ 1 - 0
run_server.py

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

+ 4 - 3
server_controller.py

@@ -201,6 +201,10 @@ def news():
     return {'data': model.news()}
 
 
+def tradables():
+    return {'data': model.ownables()}
+
+
 def transactions():
     missing = missing_attributes(['session_id', 'ownable', 'limit'])
     if missing:
@@ -211,9 +215,6 @@ def transactions():
 
 
 def leaderboard():
-    missing = missing_attributes(['session_id'])
-    if missing:
-        return bad_request(missing)
     return {'data': model.leaderboard()}