client_controller.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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, str2bool
  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. stop_loss = str2bool(stop_loss)
  101. if time_until_expiration is None:
  102. time_until_expiration = input('Time until order expires (minutes, default 60):')
  103. if time_until_expiration == '':
  104. time_until_expiration = 60
  105. response = client_request('order', {"buy": True,
  106. "session_id": connection.session_id,
  107. "amount": amount,
  108. "ownable": object_name,
  109. "limit": limit,
  110. "stop_loss": stop_loss,
  111. "time_until_expiration": time_until_expiration})
  112. if 'error_message' in response:
  113. print('Order placement failed with message:', response['error_message'])
  114. else:
  115. print('You might want to use the `transactions` or `depot` commands',
  116. 'to see if the order has been executed already.')
  117. def sell(amount=None, object_name=None, limit='', stop_loss='', time_until_expiration=None):
  118. if object_name is None: # TODO list some available objects
  119. object_name = input('Name of object to sell: ')
  120. if amount is None:
  121. amount = input('Amount: ')
  122. if limit == '':
  123. set_limit = yn_dialog('Do you want to place a limit?')
  124. if set_limit:
  125. limit = input('Limit: ')
  126. stop_loss = yn_dialog('Is this a stop-loss limit?')
  127. else:
  128. limit = None
  129. stop_loss = None
  130. if time_until_expiration is None:
  131. time_until_expiration = input('Time until order expires (minutes, default 60):')
  132. if time_until_expiration == '':
  133. time_until_expiration = 60
  134. response = client_request('order', {"buy": False,
  135. "session_id": connection.session_id,
  136. "amount": amount,
  137. "ownable": object_name,
  138. "limit": limit,
  139. "stop_loss": stop_loss,
  140. "time_until_expiration": time_until_expiration})
  141. if 'error_message' in response:
  142. print('Order placement failed with message:', response['error_message'])
  143. else:
  144. print('You might want to use the `transactions` or `depot` commands',
  145. 'to see if the order has been executed already.')
  146. def orders():
  147. response = client_request('orders', {"session_id": connection.session_id})
  148. success = 'data' in response
  149. if success:
  150. print(tabulate(response['data'],
  151. headers=['Buy?', 'Name', 'Amount', 'Limit', 'Stop Loss?', 'Orig. Size', 'Expires', 'No.'],
  152. tablefmt="pipe"))
  153. else:
  154. if 'error_message' in response:
  155. print('Order access failed with message:', response['error_message'])
  156. else:
  157. print('Order access failed.')
  158. def orders_on(object_name=None):
  159. if object_name is None: # TODO list some available objects
  160. object_name = input('Name of object to check: ')
  161. response = client_request('orders_on', {"session_id": connection.session_id, "ownable": object_name})
  162. success = 'data' in response
  163. if success:
  164. print(tabulate(response['data'],
  165. headers=['Buy?', 'Name', 'Amount', 'Limit', 'Stop Loss?', 'Expires', 'No.'],
  166. tablefmt="pipe"))
  167. else:
  168. if 'error_message' in response:
  169. print('Order access failed with message:', response['error_message'])
  170. else:
  171. print('Order access failed.')
  172. def news():
  173. response = client_request('news', {"session_id": connection.session_id})
  174. success = 'data' in response
  175. if success:
  176. print(tabulate(response['data'],
  177. headers=['Date', 'Title'],
  178. tablefmt="pipe"))
  179. else:
  180. if 'error_message' in response:
  181. print('Order access failed with message:', response['error_message'])
  182. else:
  183. print('Order access failed.')
  184. def transactions(object_name=None):
  185. if object_name is None: # TODO list some available objects
  186. object_name = input('Name of object to check: ')
  187. response = client_request('transactions', {"session_id": connection.session_id, "ownable": object_name})
  188. success = 'data' in response
  189. if success:
  190. print(tabulate(response['data'],
  191. # TODO headers=['Date', 'Title'],
  192. tablefmt="pipe"))
  193. else:
  194. if 'error_message' in response:
  195. print('Transactions access failed with message:', response['error_message'])
  196. else:
  197. print('Transactions access failed.')