123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369 |
- import sys
- from getpass import getpass
- from inspect import signature
- import connection
- from run_client import allowed_commands, fake_loading_bar
- from connection import client_request
- import tabulate
- from debug import debug
- exiting = False
- def login(username=None, password=None):
- if connection.session_id is not None:
- fake_loading_bar('Signing out', duration=0.7)
- connection.session_id = None
- if username is None:
- username = input('Username: ')
- if password is None:
- if sys.stdin.isatty():
- password = getpass('Password: ')
- 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:
- connection.session_id = response['session_id']
- print('Login successful.')
- else:
- if 'error_message' in response:
- print('Login failed with message:', response['error_message'])
- else:
- print('Login failed.')
- def register(username=None, game_key='', password=None, retype_password=None):
- if connection.session_id is not None:
- connection.session_id = None
- fake_loading_bar('Signing out', duration=0.7)
- if username is None:
- username = input('Username: ')
- if password is None:
- if sys.stdin.isatty():
- password = getpass('New password: ')
- retype_password = getpass('Retype password: ')
- else:
- password = input('New password: ')
- retype_password = input('Retype password: ')
- if password != retype_password:
- print('Passwords do not match.')
- return
- elif retype_password is None:
- if sys.stdin.isatty():
- retype_password = getpass('Retype password: ')
- else:
- retype_password = input('Retype password: ')
- if password != retype_password:
- print('Passwords do not match.')
- return
- if not debug:
- 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:
- print('Registration failed with message:', response['error_message'])
- 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:
- print('Order cancelling failed with message:', response['error_message'])
- def change_password(password=None, retype_password=None):
- if password != retype_password:
- password = None
- if password is None:
- if sys.stdin.isatty():
- password = getpass('New password: ')
- retype_password = getpass('Retype password: ')
- else:
- password = input('New password: ')
- retype_password = input('Retype password: ')
- if password != retype_password:
- print('Passwords do not match.')
- return
- elif retype_password is None:
- if sys.stdin.isatty():
- retype_password = getpass('Retype password: ')
- else:
- retype_password = input('Retype password: ')
- if password != retype_password:
- print('Passwords do not match.')
- return
- fake_loading_bar('Validating password', duration=1.2)
- fake_loading_bar('Changing password', duration=5.2)
- response = client_request('change_password', {"session_id": connection.session_id, "password": password})
- if 'error_message' in response:
- print('Changing password failed with message:', response['error_message'])
- fake_loading_bar('Signing out', duration=0.7)
- connection.session_id = None
- # noinspection PyShadowingBuiltins
- def help():
- print('Allowed commands:')
- command_table = []
- for cmd in allowed_commands:
- this_module = sys.modules[__name__]
- method = getattr(this_module, cmd)
- params = signature(method).parameters
- num_args = len(params)
- command_table.append([cmd] + [p for p in params])
- print(_my_tabulate(command_table,tablefmt='pipe',headers=['command',
- 'parameter 1',
- 'parameter 2',
- 'parameter 3',
- 'parameter 4',
- 'parameter 5',
- ]))
- print('NOTE:')
- print(' All parameters for all commands are optional!')
- print(' Commands can be combined in one line with ; between them.')
- print(' Parameters are separated with any whitespace.')
- def _my_tabulate(data, **params):
- if data == [] and 'headers' in params:
- data = [(None for _ in params['headers'])]
- tabulate.MIN_PADDING = 0
- return tabulate.tabulate(data, **params)
- 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:
- data = response['data']
- for row in data:
- row.append(row[1] * row[2])
- print(_my_tabulate(data,
- headers=['Object', 'Amount', 'Course', 'Bid', 'Ask', 'Est. Value'],
- tablefmt="pipe"))
- print('This corresponds to a wealth of roughly', response['own_wealth'])
- else:
- if 'error_message' in response:
- print('Depot access failed with message:', response['error_message'])
- else:
- print('Depot access failed.')
- 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:
- print(_my_tabulate(response['data'], headers=['User', 'Wealth'], tablefmt="pipe"))
- else:
- if 'error_message' in response:
- print('Leaderboard access failed with message:', response['error_message'])
- else:
- print('Leaderboard access failed.')
- def activate_key(key=''):
- if key == '':
- print('Entering a game key may get you some money or other useful stuff.')
- key = input('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'])
- def yn_dialog(msg):
- while True:
- result = input(msg + ' [y/n]: ')
- if result == 'y':
- return True
- if result == 'n':
- return False
- def buy(amount=None, object_name=None, limit='', stop_loss='', time_until_expiration=None):
- if object_name is None: # TODO list some available objects
- object_name = input('Name of object to buy: ')
- if amount is None:
- amount = input('Amount: ')
- if limit == '':
- set_limit = yn_dialog('Do you want to place a limit?')
- if set_limit:
- limit = input('Limit: ')
- stop_loss = yn_dialog('Is this a stop-loss limit?')
- else:
- limit = None
- stop_loss = None
- if limit is not None and stop_loss == '':
- stop_loss = yn_dialog('Is this a stop-loss limit?')
- if time_until_expiration is None:
- time_until_expiration = input('Time until order expires (minutes, default 43200):')
- if time_until_expiration == '':
- time_until_expiration = 43200
- fake_loading_bar('Loading Data', duration=1.3)
- response = client_request('order', {"buy": True,
- "session_id": connection.session_id,
- "amount": amount,
- "ownable": object_name,
- "limit": limit,
- "stop_loss": stop_loss,
- "time_until_expiration": time_until_expiration})
- if 'error_message' in response:
- print('Order placement failed with message:', response['error_message'])
- else:
- print('You might want to use the `transactions` or `depot` commands',
- 'to see if the order has been executed already.')
- def sell(amount=None, object_name=None, limit='', stop_loss='', time_until_expiration=None):
- if object_name is None: # TODO list some available objects
- object_name = input('Name of object to sell: ')
- if amount is None:
- amount = input('Amount: ')
- if limit == '':
- set_limit = yn_dialog('Do you want to place a limit?')
- if set_limit:
- limit = input('Limit: ')
- stop_loss = yn_dialog('Is this a stop-loss limit?')
- else:
- limit = None
- stop_loss = None
- if limit != '' and stop_loss == '':
- stop_loss = yn_dialog('Is this a stop-loss limit?')
- if time_until_expiration is None:
- time_until_expiration = input('Time until order expires (minutes, default 43200):')
- if time_until_expiration == '':
- time_until_expiration = 43200
- fake_loading_bar('Loading Data', duration=1.3)
- response = client_request('order', {"buy": False,
- "session_id": connection.session_id,
- "amount": amount,
- "ownable": object_name,
- "limit": limit,
- "stop_loss": stop_loss,
- "time_until_expiration": time_until_expiration})
- if 'error_message' in response:
- print('Order placement failed with message:', response['error_message'])
- else:
- print('You might want to use the `transactions` or `depot` commands',
- 'to see if the order has been executed already.')
- def orders():
- fake_loading_bar('Loading Data', duration=0.9)
- response = client_request('orders', {"session_id": connection.session_id})
- success = 'data' in response
- if success:
- print(_my_tabulate(response['data'],
- headers=['Buy?', 'Name', 'Amount', 'Limit', 'stop-loss', 'Expires', 'No.'],
- tablefmt="pipe"))
- else:
- if 'error_message' in response:
- print('Order access failed with message:', response['error_message'])
- else:
- print('Order access failed.')
- 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('Loading Data', duration=2.3)
- response = client_request('orders_on', {"session_id": connection.session_id, "ownable": object_name})
- success = 'data' in response
- if success:
- print(_my_tabulate(response['data'],
- headers=['Buy?', 'Name', 'Amount', 'Limit', 'stop-loss', 'Expires', 'No.'],
- tablefmt="pipe"))
- else:
- if 'error_message' in response:
- print('Order access failed with message:', response['error_message'])
- else:
- print('Order access failed.')
- def gift(username=None, amount=None, object_name=None):
- if username is None:
- username = input('Username of recipient: ')
- if object_name is None:
- object_name = input('Name of object to give: ')
- if amount is None:
- amount = input('How many?: ')
- fake_loading_bar('Sending Gift', duration=4.2)
- response = client_request('gift',
- {"session_id": connection.session_id,
- "username": username,
- "object_name": object_name,
- "amount": amount})
- if 'error_message' in response:
- print('Order access failed with message:', response['error_message'])
- elif 'message' in response:
- print(response['message'])
- def news():
- fake_loading_bar('Loading Data', duration=0.76)
- response = client_request('news', {})
- success = 'data' in response
- if success:
- print(_my_tabulate(response['data'],
- headers=['Date', 'Title'],
- tablefmt="pipe"))
- else:
- if 'error_message' in response:
- print('Order access failed with message:', response['error_message'])
- else:
- print('Order access failed.')
- 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:
- print(_my_tabulate(response['data'],
- headers=['Time', 'Volume', 'Price'],
- tablefmt="pipe"))
- else:
- if 'error_message' in response:
- print('Transactions access failed with message:', response['error_message'])
- else:
- print('Transactions access failed.')
- # noinspection PyShadowingBuiltins
- def exit():
- global exiting
- exiting = True
|