|
@@ -4,6 +4,7 @@ import re
|
|
|
import sqlite3 as db
|
|
|
import sys
|
|
|
import uuid
|
|
|
+from math import floor
|
|
|
|
|
|
import db_setup
|
|
|
from game import CURRENCY_NAME
|
|
@@ -103,6 +104,9 @@ def register(username, password, game_key):
|
|
|
|
|
|
|
|
|
def own(user_id, ownable_name):
|
|
|
+ if not isinstance(ownable_name, str):
|
|
|
+ return AssertionError('A name must be a string.')
|
|
|
+
|
|
|
cursor.execute('''
|
|
|
WITH one_ownable_id AS (SELECT rowid FROM ownables WHERE name = ?),
|
|
|
one_user_id AS (SELECT ?)
|
|
@@ -130,8 +134,6 @@ def send_ownable(from_user_id, to_user_id, ownable_name, amount):
|
|
|
WHERE user_id = ?
|
|
|
AND ownable_id = (SELECT rowid FROM ownables WHERE name = ?)
|
|
|
''', (amount, from_user_id, ownable_name,))
|
|
|
- if not cursor.fetchone():
|
|
|
- return False
|
|
|
|
|
|
cursor.execute('''
|
|
|
UPDATE ownership
|
|
@@ -139,8 +141,6 @@ def send_ownable(from_user_id, to_user_id, ownable_name, amount):
|
|
|
WHERE user_id = ?
|
|
|
AND ownable_id = (SELECT rowid FROM ownables WHERE name = ?)
|
|
|
''', (amount, to_user_id, ownable_name))
|
|
|
- if cursor.rowcount == 0:
|
|
|
- return False
|
|
|
return True
|
|
|
|
|
|
|
|
@@ -235,7 +235,7 @@ def get_user_id_by_session_id(session_id):
|
|
|
''', (session_id,))
|
|
|
|
|
|
ids = cursor.fetchone()
|
|
|
- if ids is None:
|
|
|
+ if not ids:
|
|
|
return False
|
|
|
return ids[0]
|
|
|
|
|
@@ -274,8 +274,6 @@ def activate_key(key, user_id):
|
|
|
AND key = ?
|
|
|
''', (user_id, key,))
|
|
|
|
|
|
- if cursor.rowcount == 0:
|
|
|
- raise AssertionError
|
|
|
send_ownable(bank_id(), user_id, CURRENCY_NAME, 1000)
|
|
|
|
|
|
|
|
@@ -323,18 +321,48 @@ def get_user_orders(user_id):
|
|
|
WHEN orders.stop_loss THEN 'Yes'
|
|
|
ELSE 'No'
|
|
|
END,
|
|
|
- orders.ordered_amount
|
|
|
- orders.expiry_dt
|
|
|
+ orders.ordered_amount,
|
|
|
+ datetime(orders.expiry_dt),
|
|
|
+ orders.rowid
|
|
|
FROM orders, ownables, ownership
|
|
|
WHERE ownership.user_id = ?
|
|
|
AND ownership.ownable_id = ownables.rowid
|
|
|
AND orders.ownership_id = ownership.rowid
|
|
|
- ORDER BY orders.buy DESC, ownables.name ASC
|
|
|
+ ORDER BY ownables.name ASC, orders.stop_loss ASC, orders.buy DESC, orders."limit" ASC
|
|
|
''', (user_id,))
|
|
|
|
|
|
return cursor.fetchall()
|
|
|
|
|
|
|
|
|
+def get_ownable_orders(ownable_id):
|
|
|
+ connect()
|
|
|
+
|
|
|
+ cursor.execute('''
|
|
|
+ SELECT
|
|
|
+ CASE
|
|
|
+ WHEN orders.buy THEN 'Buy'
|
|
|
+ ELSE 'Sell'
|
|
|
+ END,
|
|
|
+ ownables.name,
|
|
|
+ orders.ordered_amount - orders.executed_amount,
|
|
|
+ orders."limit",
|
|
|
+ CASE
|
|
|
+ WHEN orders."limit" IS NULL THEN NULL
|
|
|
+ WHEN orders.stop_loss THEN 'Yes'
|
|
|
+ ELSE 'No'
|
|
|
+ END,
|
|
|
+ datetime(orders.expiry_dt),
|
|
|
+ orders.rowid
|
|
|
+ FROM orders, ownables, ownership
|
|
|
+ WHERE ownership.ownable_id = ?
|
|
|
+ AND ownership.ownable_id = ownables.rowid
|
|
|
+ AND orders.ownership_id = ownership.rowid
|
|
|
+ ORDER BY ownables.name ASC, orders.stop_loss ASC, orders.buy DESC, orders."limit" ASC
|
|
|
+ ''', (ownable_id,))
|
|
|
+
|
|
|
+ return cursor.fetchall()
|
|
|
+
|
|
|
+
|
|
|
def sell_ordered_amount(user_id, ownable_id):
|
|
|
connect()
|
|
|
|
|
@@ -428,8 +456,10 @@ def new_stock(name=None):
|
|
|
|
|
|
amount = random.randrange(100, 10000)
|
|
|
price = random.randrange(10000, 20000) / amount
|
|
|
- bank_order(True,
|
|
|
- ownable_id_by_name(name),
|
|
|
+ ownable_id = ownable_id_by_name(name)
|
|
|
+ own(bank_id(), name)
|
|
|
+ bank_order(False,
|
|
|
+ ownable_id,
|
|
|
price,
|
|
|
amount,
|
|
|
60)
|
|
@@ -477,14 +507,34 @@ def currency_id():
|
|
|
return cursor.fetchone()[0]
|
|
|
|
|
|
|
|
|
+def user_money(user_id):
|
|
|
+ connect()
|
|
|
+
|
|
|
+ cursor.execute('''
|
|
|
+ SELECT amount
|
|
|
+ FROM ownership
|
|
|
+ WHERE user_id = ?
|
|
|
+ AND ownable_id = ?
|
|
|
+ ''', (user_id, currency_id()))
|
|
|
+
|
|
|
+ return cursor.fetchone()[0]
|
|
|
+
|
|
|
+
|
|
|
+def delete_order(order_id):
|
|
|
+ connect()
|
|
|
+
|
|
|
+ cursor.execute('''
|
|
|
+ DELETE FROM orders
|
|
|
+ WHERE rowid = ?
|
|
|
+ ''', (order_id,))
|
|
|
+
|
|
|
+
|
|
|
def execute_orders(ownable_id):
|
|
|
connect()
|
|
|
- executed_any = True
|
|
|
- while executed_any:
|
|
|
- executed_any = False
|
|
|
+ while True:
|
|
|
|
|
|
cursor.execute('''
|
|
|
- SELECT buy_order.*, sell_order.*, buyer.*, seller.*
|
|
|
+ SELECT buy_order.*, sell_order.*, buyer.user_id, seller.user_id, buy_order.rowid, sell_order.rowid
|
|
|
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
|
|
@@ -494,67 +544,92 @@ def execute_orders(ownable_id):
|
|
|
AND (buy_order."limit" IS NULL
|
|
|
OR sell_order."limit" IS NULL
|
|
|
OR (sell_order."limit" < buy_order."limit"
|
|
|
- AND NOT sell_order.stop_loss
|
|
|
- AND NOT buy_order.stop_loss))
|
|
|
+ AND sell_order.stop_loss IS NULL
|
|
|
+ AND buy_order.stop_loss IS NULL))
|
|
|
ORDER BY COALESCE(sell_order."limit", 0) ASC,
|
|
|
- COALESCE(buy_order."limit", 0) DESC
|
|
|
+ -COALESCE(buy_order."limit", 0) ASC
|
|
|
LIMIT 1
|
|
|
''', (ownable_id, ownable_id,))
|
|
|
|
|
|
matching_orders = cursor.fetchone()
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
if not matching_orders:
|
|
|
- continue
|
|
|
+ break
|
|
|
+
|
|
|
+
|
|
|
|
|
|
-
|
|
|
- price = -1
|
|
|
+ buy_ownership_id, _, buy_limit, _, buy_order_amount, buy_executed_amount, buy_expiry_dt, sell_ownership_id, _, sell_limit, _, sell_order_amount, sell_executed_amount, sell_expiry_dt, buyer_id, seller_id, buy_order_id, sell_order_id = matching_orders
|
|
|
|
|
|
- if price < 0 or amount < 0:
|
|
|
+ if not isinstance(buy_limit, str) and not isinstance(buy_limit, float):
|
|
|
+ return AssertionError()
|
|
|
+ if not isinstance(sell_limit, str) and not isinstance(sell_limit, float):
|
|
|
return AssertionError()
|
|
|
|
|
|
-
|
|
|
- cursor.execute('''
|
|
|
- UPDATE ownership
|
|
|
- SET amount = amount - ?
|
|
|
- WHERE user_id = ?
|
|
|
- ''', (price, buyer_id))
|
|
|
- if not cursor.fetchone():
|
|
|
+ if buy_limit == '' and sell_limit == '':
|
|
|
raise AssertionError()
|
|
|
+ elif buy_limit == '':
|
|
|
+ price = sell_limit
|
|
|
+ elif sell_limit == '':
|
|
|
+ price = buy_limit
|
|
|
+ else:
|
|
|
+ price = (float(sell_limit) + float(buy_limit)) / 2
|
|
|
|
|
|
- cursor.execute('''
|
|
|
- UPDATE ownership
|
|
|
- SET amount = amount - ?
|
|
|
- WHERE user_id = ?
|
|
|
- ''', (amount, seller_id))
|
|
|
+ buyer_money = user_money(buyer_id)
|
|
|
|
|
|
- if not cursor.fetchone():
|
|
|
- raise AssertionError()
|
|
|
- cursor.execute('''
|
|
|
- UPDATE ownership
|
|
|
- SET amount = amount + ?
|
|
|
- WHERE user_id = ?
|
|
|
- ''', (amount, buyer_id))
|
|
|
+ amount = min(buy_order_amount - buy_executed_amount,
|
|
|
+ sell_order_amount - sell_executed_amount,
|
|
|
+ floor(buyer_money / price))
|
|
|
|
|
|
- if not cursor.fetchone():
|
|
|
- raise AssertionError()
|
|
|
- cursor.execute('''
|
|
|
- UPDATE ownership
|
|
|
- SET amount = amount + ?
|
|
|
- WHERE user_id = ?
|
|
|
- ''', (price, seller_id))
|
|
|
- if not cursor.fetchone():
|
|
|
- raise AssertionError()
|
|
|
+ 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)
|
|
|
+
|
|
|
+ if price < 0 or amount <= 0:
|
|
|
+ return AssertionError()
|
|
|
+
|
|
|
+
|
|
|
+ if buyer_id != bank_id():
|
|
|
+ cursor.execute('''
|
|
|
+ UPDATE ownership
|
|
|
+ SET amount = amount - ?
|
|
|
+ WHERE user_id = ?
|
|
|
+ AND ownable_id = ?
|
|
|
+ ''', (price * amount, buyer_id, currency_id()))
|
|
|
+ if seller_id != bank_id():
|
|
|
+ cursor.execute('''
|
|
|
+ UPDATE ownership
|
|
|
+ SET amount = amount - ?
|
|
|
+ WHERE rowid = ?
|
|
|
+ ''', (amount, sell_ownership_id))
|
|
|
+ if buyer_id != bank_id():
|
|
|
+ cursor.execute('''
|
|
|
+ UPDATE ownership
|
|
|
+ SET amount = amount + ?
|
|
|
+ WHERE rowid = ?
|
|
|
+ ''', (amount, buy_ownership_id))
|
|
|
+ if seller_id != bank_id():
|
|
|
+ cursor.execute('''
|
|
|
+ UPDATE ownership
|
|
|
+ SET amount = amount + ?
|
|
|
+ WHERE user_id = ?
|
|
|
+ AND ownable_id = ?
|
|
|
+ ''', (amount, seller_id, currency_id()))
|
|
|
cursor.execute('''
|
|
|
UPDATE orders
|
|
|
SET executed_amount = executed_amount + ?
|
|
|
WHERE rowid = ?
|
|
|
OR rowid = ?
|
|
|
''', (amount, buy_order_id, sell_order_id))
|
|
|
- if not cursor.fetchone():
|
|
|
- raise AssertionError()
|
|
|
|
|
|
- executed_any = True
|
|
|
+ if buy_order_finished:
|
|
|
+ delete_order(buy_order_id)
|
|
|
+ if sell_order_finished:
|
|
|
+ delete_order(sell_order_id)
|
|
|
|
|
|
- if seller_id != buyer_id:
|
|
|
+ if seller_id != buyer_id:
|
|
|
cursor.execute('''
|
|
|
INSERT INTO transactions
|
|
|
(price, ownable_id, amount)
|
|
@@ -562,16 +637,15 @@ def execute_orders(ownable_id):
|
|
|
''', (price, ownable_id, amount,))
|
|
|
|
|
|
|
|
|
- cursor.execute('''
|
|
|
- UPDATE orders
|
|
|
- SET stop_loss = FALSE,
|
|
|
- "limit" = NULL
|
|
|
- WHERE stop_loss
|
|
|
- AND (buy AND "limit" > ?)
|
|
|
- OR (NOT buy AND "limit" < ?)
|
|
|
- (price, ownable_id, amount)
|
|
|
- VALUES(?, ?, ?)
|
|
|
- ''', (price, price,))
|
|
|
+ if buyer_id != seller_id:
|
|
|
+ cursor.execute('''
|
|
|
+ UPDATE orders
|
|
|
+ SET stop_loss = NULL
|
|
|
+ "limit" = NULL
|
|
|
+ WHERE stop_loss
|
|
|
+ AND (buy AND "limit" > ?)
|
|
|
+ OR (NOT buy AND "limit" < ?)
|
|
|
+ ''', (price, price,))
|
|
|
|
|
|
|
|
|
def ownable_id_by_ownership_id(ownership_id):
|
|
@@ -586,9 +660,21 @@ def ownable_id_by_ownership_id(ownership_id):
|
|
|
return cursor.fetchone()[0]
|
|
|
|
|
|
|
|
|
+def ownable_name_by_id(ownable_id):
|
|
|
+ connect()
|
|
|
+
|
|
|
+ cursor.execute('''
|
|
|
+ SELECT name
|
|
|
+ FROM ownables
|
|
|
+ WHERE rowid = ?
|
|
|
+ ''', (ownable_id,))
|
|
|
+
|
|
|
+ return cursor.fetchone()[0]
|
|
|
+
|
|
|
+
|
|
|
def bank_order(buy, ownable_id, limit, amount, time_until_expiration):
|
|
|
- if limit is None:
|
|
|
- raise AssertionError()
|
|
|
+ if not limit:
|
|
|
+ raise AssertionError('The bank does not give away anything.')
|
|
|
place_order(buy,
|
|
|
get_ownership_id(ownable_id, bank_id()),
|
|
|
limit,
|
|
@@ -599,7 +685,7 @@ def bank_order(buy, ownable_id, limit, amount, time_until_expiration):
|
|
|
cursor.execute('''
|
|
|
INSERT INTO news(title)
|
|
|
VALUES (?)
|
|
|
- ''', ('External investors are selling ' + ownable_name))
|
|
|
+ ''', ('External investors are selling ' + ownable_name,))
|
|
|
|
|
|
|
|
|
def place_order(buy, ownership_id, limit, stop_loss, amount, time_until_expiration):
|
|
@@ -628,3 +714,14 @@ def transactions(ownable_id):
|
|
|
''', (ownable_id,))
|
|
|
|
|
|
return cursor.fetchall()
|
|
|
+
|
|
|
+
|
|
|
+def drop_expired_orders():
|
|
|
+ connect()
|
|
|
+
|
|
|
+ cursor.execute('''
|
|
|
+ DELETE FROM orders
|
|
|
+ WHERE expiry_dt < DATETIME('now')
|
|
|
+ ''')
|
|
|
+
|
|
|
+ return cursor.fetchall()
|