client_controller.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import getpass
  2. import inspect
  3. import sys
  4. import client
  5. import connection
  6. from client import allowed_commands
  7. from connection import client_request
  8. from tabulate import tabulate
  9. from util import debug
  10. def login(username=None, password=None):
  11. if connection.session_id is not None:
  12. client.fake_loading_bar('Signing out', delay=0.025)
  13. connection.session_id = None
  14. if username is None:
  15. username = input('Username: ')
  16. if password is None:
  17. if sys.stdin.isatty():
  18. password = getpass.getpass('Password: ')
  19. else:
  20. password = input('Password: ')
  21. response = client_request('login', {"username": username, "password": password})
  22. success = 'session_id' in response
  23. if success:
  24. connection.session_id = response['session_id']
  25. print('Login successful.')
  26. else:
  27. if 'error_message' in response:
  28. print('Login failed with message:', response['error_message'])
  29. else:
  30. print('Login failed.')
  31. def register(username=None, password=None, game_key=''):
  32. if connection.session_id is not None:
  33. client.fake_loading_bar('Signing out', delay=0.025)
  34. connection.session_id = None
  35. if username is None:
  36. username = input('Username: ')
  37. if password is None:
  38. if sys.stdin.isatty():
  39. password = getpass.getpass('Password: ')
  40. else:
  41. password = input('Password: ')
  42. if not debug:
  43. if game_key is '':
  44. print('Entering a game key will provide you with some starting money and other useful stuff.')
  45. game_key = input('Game key (leave empty if you don\'t have one): ')
  46. response = client_request('register', {"username": username, "password": password, "game_key": game_key})
  47. if 'error_message' in response:
  48. print('Registration failed with message:', response['error_message'])
  49. # noinspection PyShadowingBuiltins
  50. def help():
  51. print('Allowed commands:')
  52. for cmd in allowed_commands:
  53. this_module = sys.modules[__name__]
  54. method = getattr(this_module, cmd)
  55. params = inspect.signature(method).parameters
  56. num_args = len(params)
  57. if num_args > 0:
  58. print('`' + cmd + '`', 'takes the following', num_args, 'arguments:')
  59. for p in params:
  60. print(' -', p)
  61. else:
  62. print('`' + cmd + '`', 'takes no arguments')
  63. print('NOTE: All arguments are optional!')
  64. def depot():
  65. response = client_request('depot', {"session_id": connection.session_id})
  66. success = 'data' in response
  67. if success:
  68. print(tabulate(response['data'], headers=['Object', 'Amount'], tablefmt="pipe"))
  69. else:
  70. if 'error_message' in response:
  71. print('Depot access failed with message:', response['error_message'])
  72. else:
  73. print('Depot access failed.')
  74. def activate_key(key=''):
  75. if key == '':
  76. print('Entering a game key may get you some money or other useful stuff.')
  77. key = input('Key: ')
  78. if key == '':
  79. print('Invalid key.')
  80. response = client_request('activate_key', {"session_id": connection.session_id, 'key': key})
  81. if 'error_message' in response:
  82. print('Key activation failed with message:', response['error_message'])
  83. def yn_dialog(msg):
  84. while True:
  85. result = input(msg + ' [y/n]: ')
  86. if result == 'y':
  87. return True
  88. if result == 'n':
  89. return False
  90. def buy(amount=None, object_name=None, limit='', stop_loss='', time_until_expiration=None):
  91. if object_name is None: # TODO list some available objects
  92. object_name = input('Name of object to buy: ')
  93. if amount is None:
  94. amount = input('Amount: ')
  95. if limit != '':
  96. set_limit = yn_dialog('Do you want to place a limit?')
  97. if set_limit:
  98. limit = input('Limit: ')
  99. stop_loss = yn_dialog('Is this a stop-loss limit?')
  100. if time_until_expiration is None:
  101. time_until_expiration = input('Time until order expires (minutes, default 60):')
  102. if time_until_expiration == '':
  103. time_until_expiration = 60
  104. response = client_request('order', {"buy": True,
  105. "session_id": connection.session_id,
  106. "amount": amount,
  107. "ownable": object_name,
  108. "limit": limit,
  109. "stop_loss": stop_loss,
  110. "time_until_expiration": time_until_expiration})
  111. if 'error_message' in response:
  112. print('Order placement failed with message:', response['error_message'])
  113. else:
  114. print('You might want to use the `transactions` or `depot` commands',
  115. 'to see if the order has been executed already.')
  116. def sell(amount=None, object_name=None, limit='', stop_loss='', time_until_expiration=None):
  117. if object_name is None: # TODO list some available objects
  118. object_name = input('Name of object to sell: ')
  119. if amount is None:
  120. amount = input('Amount: ')
  121. if limit != '':
  122. set_limit = yn_dialog('Do you want to place a limit?')
  123. if set_limit:
  124. limit = input('Limit: ')
  125. stop_loss = yn_dialog('Is this a stop-loss limit?')
  126. if time_until_expiration is None:
  127. time_until_expiration = input('Time until order expires (minutes, default 60):')
  128. if time_until_expiration == '':
  129. time_until_expiration = 60
  130. response = client_request('order', {"buy": False,
  131. "session_id": connection.session_id,
  132. "amount": amount,
  133. "ownable": object_name,
  134. "limit": limit,
  135. "stop_loss": stop_loss,
  136. "time_until_expiration": time_until_expiration})
  137. if 'error_message' in response:
  138. print('Order placement failed with message:', response['error_message'])
  139. else:
  140. print('You might want to use the `transactions` or `depot` commands',
  141. 'to see if the order has been executed already.')
  142. def orders():
  143. response = client_request('orders', {"session_id": connection.session_id})
  144. success = 'data' in response
  145. if success:
  146. print(tabulate(response['data'],
  147. headers=['Buy?', 'Name', 'Amount', 'Limit', 'Stop Loss?', 'Orig. Order Size', 'Expires'],
  148. tablefmt="pipe"))
  149. else:
  150. if 'error_message' in response:
  151. print('Order access failed with message:', response['error_message'])
  152. else:
  153. print('Order access failed.')
  154. def news():
  155. response = client_request('news', {"session_id": connection.session_id})
  156. success = 'data' in response
  157. if success:
  158. print(tabulate(response['data'],
  159. headers=['Date', 'Title'],
  160. tablefmt="pipe"))
  161. else:
  162. if 'error_message' in response:
  163. print('Order access failed with message:', response['error_message'])
  164. else:
  165. print('Order access failed.')
  166. def transactions(object_name=None):
  167. if object_name is None: # TODO list some available objects
  168. object_name = input('Name of object to check: ')
  169. response = client_request('transactions', {"session_id": connection.session_id, "ownable": object_name})
  170. success = 'data' in response
  171. if success:
  172. print(tabulate(response['data'],
  173. # TODO headers=['Date', 'Title'],
  174. tablefmt="pipe"))
  175. else:
  176. if 'error_message' in response:
  177. print('Transactions access failed with message:', response['error_message'])
  178. else:
  179. print('Transactions access failed.')