client_controller.py 8.6 KB

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