Explorar o código

more fixing trades

Eren Yilmaz %!s(int64=6) %!d(string=hai) anos
pai
achega
f3eb6d15bd
Modificáronse 9 ficheiros con 35 adicións e 24 borrados
  1. 1 1
      admin_console.py
  2. 2 3
      client_controller.py
  3. 8 7
      cool_query.py
  4. 1 1
      db_setup.py
  5. 2 1
      game.py
  6. 14 9
      model.py
  7. 1 0
      run_client.py
  8. 4 0
      run_db_setup.py
  9. 2 2
      run_server.py

+ 1 - 1
admin_console.py

@@ -23,7 +23,7 @@ def cleanup():
 
 if __name__ == '__main__':
     generate_keys(count=2)
-    print(model.new_stocks(count=1))
+    print(model.new_stocks(timeout=30, count=1))
     # unused_keys()
     # model.drop_old_sessions()
 

+ 2 - 3
client_controller.py

@@ -1,9 +1,8 @@
 import getpass
 import inspect
 import sys
-import client
 import connection
-from client import allowed_commands
+from run_client import allowed_commands, fake_loading_bar
 from connection import client_request
 from tabulate import tabulate
 
@@ -12,7 +11,7 @@ from util import debug
 
 def login(username=None, password=None):
     if connection.session_id is not None:
-        client.fake_loading_bar('Signing out', delay=0.025)
+        fake_loading_bar('Signing out', delay=0.025)
         connection.session_id = None
 
     if username is None:

+ 8 - 7
cool_query.py

@@ -2,10 +2,11 @@ import model
 
 model.connect()
 model.cursor.execute('''
-            SELECT rowid
-            FROM ownership
-            WHERE user_id = ?
-            AND ownable_id = ?
-            AND amount - ? >= ?
-            ''', (3, 2, 0, '50'))
-print(model.cursor.fetchall())
+        DELETE FROM orders
+        WHERE rowid = ?
+        ''', (2,))
+model.cursor.execute('''
+        SELECT * FROM orders
+        WHERE rowid = ?
+        ''', (2,))
+print(model.cursor.fetchall())

+ 1 - 1
db_setup.py

@@ -1,7 +1,7 @@
 from game import CURRENCY_NAME
 from util import debug
 
-replace = False and debug
+replace = True and debug
 
 
 def setup(cursor):

+ 2 - 1
game.py

@@ -1 +1,2 @@
-CURRENCY_NAME = 'Kollar'
+# noinspection SpellCheckingInspection
+CURRENCY_NAME = '₭ollar'

+ 14 - 9
model.py

@@ -45,6 +45,7 @@ def connect(reconnect=False):
 
         try:
             connection = db.connect(db_name)
+            # connection.text_factory = lambda x: unicode(x, 'utf-8', 'ignore')
 
             cursor = connection.cursor()
 
@@ -430,7 +431,7 @@ def ownable_name_exists(name):
         return False
 
 
-def new_stock(name=None):
+def new_stock( timeout=60, name=None):
     connect()
 
     while name is None:
@@ -466,12 +467,12 @@ def new_stock(name=None):
                ownable_id,
                price,
                amount,
-               60)
+               timeout)
     return name
 
 
-def new_stocks(count=1):
-    return [new_stock() for _ in range(count)]
+def new_stocks(timeout=60,count=1):
+    return [new_stock(timeout=timeout) for _ in range(count)]
 
 
 def ownable_id_by_name(ownable_name):
@@ -547,7 +548,7 @@ def execute_orders(ownable_id):
             AND seller.ownable_id = ?
             AND (buy_order."limit" IS NULL
                 OR sell_order."limit" IS NULL
-                OR (sell_order."limit" < buy_order."limit"
+                OR (sell_order."limit" <= buy_order."limit"
                     AND NOT sell_order.stop_loss
                     AND NOT buy_order.stop_loss))
             ORDER BY CASE WHEN sell_order."limit" IS NULL THEN 0 ELSE 1 END ASC, 
@@ -584,7 +585,7 @@ def execute_orders(ownable_id):
             price = (float(sell_limit) + float(buy_limit)) / 2
 
         if price == 0:
-            raise AssertionError()  # TODO find a solution
+            raise AssertionError()
 
         buyer_money = user_money(buyer_id)
 
@@ -592,6 +593,10 @@ def execute_orders(ownable_id):
                      sell_order_amount - sell_executed_amount,
                      floor(buyer_money / price))
 
+        if amount == 0:  # probable because buyer has not enough money
+            delete_order(buy_order_id)
+            continue
+
         buy_order_finished = (buy_order_amount - buy_executed_amount - amount <= 0) or (
             buyer_money - amount * price < price)
         sell_order_finished = (sell_order_amount - sell_executed_amount - amount <= 0)
@@ -655,9 +660,9 @@ def execute_orders(ownable_id):
                     "limit" = NULL
                 WHERE stop_loss IS NOT NULL
                 AND stop_loss
-                AND (buy AND "limit" > ?)
-                    OR (NOT buy AND "limit" < ?)
-                ''', (price, price,))
+                AND ? IN (SELECT ownable_id FROM ownership WHERE rowid = ownership_id)
+                AND ((buy AND "limit" < ?) OR (NOT buy AND "limit" > ?))
+                ''', (ownable_id, price, price,))
 
 
 def ownable_id_by_ownership_id(ownership_id):

+ 1 - 0
client.py → run_client.py

@@ -69,6 +69,7 @@ def one_command():
     for cmd in cmds:
         cmd = cmd.split()
         # cmd = [cmd.strip() for cmd in cmd]
+        # noinspection PySimplifyBooleanCheck
         if cmd == []:
             continue
         if cmd[0] not in allowed_commands:

+ 4 - 0
run_db_setup.py

@@ -0,0 +1,4 @@
+from model import setup
+
+if __name__ == '__main__':
+    setup()

+ 2 - 2
server.py → run_server.py

@@ -11,8 +11,6 @@ from util import debug
 if __name__ == '__main__':
     print('sqlite3.version', model.db.version)
 
-    model.setup()
-
     valid_routes = ['login',
                     'register',
                     'depot',
@@ -25,6 +23,7 @@ if __name__ == '__main__':
 
     @route('/<path>', method='POST')
     def process(path):
+        path = path.strip().lower()
         if path not in valid_routes:
             return not_found()
         response.content_type = 'application/json'
@@ -42,5 +41,6 @@ if __name__ == '__main__':
             model.connection.rollback()
             return server_controller.bad_request('Action violates database constraints.')
 
+
     run(host='localhost', port=connection.port, debug=debug)
     model.db.connection.disconnect()