Browse Source

fix leaderboard

Eren Yilmaz 6 years ago
parent
commit
71fbb8cde4
2 changed files with 19 additions and 17 deletions
  1. 18 17
      cool_query.py
  2. 1 0
      model.py

+ 18 - 17
cool_query.py

@@ -2,20 +2,21 @@ import model
 
 model.connect()
 model.cursor.execute('''
-            SELECT sell_order.stop_loss, buy_order.stop_loss
-            FROM orders buy_order, orders sell_order, ownership buyer, ownership seller
-            WHERE buy_order.buy AND NOT sell_order.buy
-            AND buyer.rowid = buy_order.ownership_id
-            AND seller.rowid = sell_order.ownership_id
-            AND buyer.ownable_id = ?
-            AND seller.ownable_id = ?
-            AND sell_order."limit" <= buy_order."limit"
-            ORDER BY CASE WHEN sell_order."limit" IS NULL THEN 0 ELSE 1 END ASC, 
-                     CASE WHEN buy_order."limit" IS NULL THEN 0 ELSE 1 END ASC, 
-                     buy_order."limit" DESC,
-                     sell_order."limit" ASC,
-                     buy_order.ordered_amount - buy_order.executed_amount DESC,
-                     sell_order.ordered_amount - sell_order.executed_amount DESC
-            LIMIT 1
-            ''', (3, 3,))
-print(model.cursor.fetchone())
+        SELECT 
+            username, 
+            SUM(CASE -- sum score for each of the users ownables
+                WHEN ownership.ownable_id = ? THEN ownership.amount
+                ELSE ownership.amount * (SELECT price 
+                                         FROM transactions
+                                         WHERE ownable_id = ownership.ownable_id 
+                                         ORDER BY dt DESC 
+                                         LIMIT 1)
+                END
+            ) score
+        FROM users, ownership
+        WHERE ownership.user_id = users.rowid
+        AND users.username != 'bank'
+        GROUP BY users.rowid
+        ''', (model.currency_id(),))
+
+print(model.cursor.fetchall())

+ 1 - 0
model.py

@@ -815,6 +815,7 @@ def leaderboard():
             FROM users, ownership
             WHERE ownership.user_id = users.rowid
             AND users.username != 'bank'
+            GROUP BY users.rowid
         ) AS scores
         ORDER BY score DESC
         LIMIT 50