1
1

client_controller.py 8.6 KB

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