client_controller.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. import sys
  2. from getpass import getpass
  3. from inspect import signature
  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', duration=0.7)
  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('Password: ')
  18. else:
  19. password = input('Password: ')
  20. fake_loading_bar('Signing in', duration=2.3)
  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. connection.session_id = None
  34. fake_loading_bar('Signing out', duration=0.7)
  35. if username is None:
  36. username = input('Username: ')
  37. if password is None:
  38. if sys.stdin.isatty():
  39. password = getpass('Password: ')
  40. else:
  41. password = input('Password: ')
  42. if not debug:
  43. if game_key == '':
  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. fake_loading_bar('Validating Registration', duration=5.2)
  47. if game_key != '':
  48. fake_loading_bar('Validating Game Key', duration=0.4)
  49. response = client_request('register', {"username": username, "password": password, "game_key": game_key})
  50. if 'error_message' in response:
  51. print('Registration failed with message:', response['error_message'])
  52. def cancel_order(order_no=None):
  53. if order_no is None:
  54. order_no = input('Order No.: ')
  55. fake_loading_bar('Validating Request', duration=0.6)
  56. response = client_request('cancel_order', {"session_id": connection.session_id, "order_id": order_no})
  57. if 'error_message' in response:
  58. print('Order cancelling failed with message:', response['error_message'])
  59. # noinspection PyShadowingBuiltins
  60. def help():
  61. print('Allowed commands:')
  62. for cmd in allowed_commands:
  63. this_module = sys.modules[__name__]
  64. method = getattr(this_module, cmd)
  65. params = signature(method).parameters
  66. num_args = len(params)
  67. if num_args > 0:
  68. print('`' + cmd + '`', 'takes the following', num_args, 'arguments:')
  69. for p in params:
  70. print(' -', p)
  71. else:
  72. print('`' + cmd + '`', 'takes no arguments')
  73. print()
  74. print('NOTE:')
  75. print(' Commands can be combined in one line with ; between them.')
  76. print(' All arguments for all commands are optional!')
  77. def depot():
  78. fake_loading_bar('Loading data', duration=1.3)
  79. response = client_request('depot', {"session_id": connection.session_id})
  80. success = 'data' in response and 'own_wealth' in response
  81. if success:
  82. print(tabulate(response['data'], headers=['Object', 'Amount', 'Est. Value'], tablefmt="pipe"))
  83. print('This corresponds to a wealth of roughly', response['own_wealth'])
  84. else:
  85. if 'error_message' in response:
  86. print('Depot access failed with message:', response['error_message'])
  87. else:
  88. print('Depot access failed.')
  89. def leaderboard():
  90. fake_loading_bar('Loading data', duration=1.3)
  91. response = client_request('leaderboard', {"session_id": connection.session_id})
  92. success = 'data' in response
  93. if success:
  94. print(tabulate(response['data'], headers=['User', 'Wealth'], tablefmt="pipe"))
  95. else:
  96. if 'error_message' in response:
  97. print('Leaderboard access failed with message:', response['error_message'])
  98. else:
  99. print('Leaderboard access failed.')
  100. def activate_key(key=''):
  101. if key == '':
  102. print('Entering a game key may get you some money or other useful stuff.')
  103. key = input('Key: ')
  104. if key == '':
  105. print('Invalid key.')
  106. fake_loading_bar('Validating Key', duration=0.4)
  107. response = client_request('activate_key', {"session_id": connection.session_id, 'key': key})
  108. if 'error_message' in response:
  109. print('Key activation failed with message:', response['error_message'])
  110. def yn_dialog(msg):
  111. while True:
  112. result = input(msg + ' [y/n]: ')
  113. if result == 'y':
  114. return True
  115. if result == 'n':
  116. return False
  117. def buy(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 buy: ')
  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": True,
  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 sell(amount=None, object_name=None, limit=None, stop_loss=None, time_until_expiration=None):
  147. if object_name is None: # TODO list some available objects
  148. object_name = input('Name of object to sell: ')
  149. if amount is None:
  150. amount = input('Amount: ')
  151. if limit is None:
  152. set_limit = yn_dialog('Do you want to place a limit?')
  153. if set_limit:
  154. limit = input('Limit: ')
  155. else:
  156. limit = None
  157. stop_loss = None
  158. if limit is not None:
  159. stop_loss = yn_dialog('Is this a stop-loss limit?')
  160. if time_until_expiration is None:
  161. time_until_expiration = input('Time until order expires (minutes, default 60):')
  162. if time_until_expiration == '':
  163. time_until_expiration = 60
  164. response = client_request('order', {"buy": False,
  165. "session_id": connection.session_id,
  166. "amount": amount,
  167. "ownable": object_name,
  168. "limit": limit,
  169. "stop_loss": stop_loss,
  170. "time_until_expiration": time_until_expiration})
  171. if 'error_message' in response:
  172. print('Order placement failed with message:', response['error_message'])
  173. else:
  174. print('You might want to use the `transactions` or `depot` commands',
  175. 'to see if the order has been executed already.')
  176. def orders():
  177. fake_loading_bar('Validating Key', duration=0.9)
  178. response = client_request('orders', {"session_id": connection.session_id})
  179. success = 'data' in response
  180. if success:
  181. print(tabulate(response['data'],
  182. headers=['Buy?', 'Name', 'Amount', 'Limit', 'Stop Loss?', 'Orig. Size', 'Expires', 'No.'],
  183. tablefmt="pipe"))
  184. else:
  185. if 'error_message' in response:
  186. print('Order access failed with message:', response['error_message'])
  187. else:
  188. print('Order access failed.')
  189. def orders_on(object_name=None):
  190. if object_name is None: # TODO list some available objects
  191. object_name = input('Name of object to check: ')
  192. fake_loading_bar('Validating Key', duration=2.3)
  193. response = client_request('orders_on', {"session_id": connection.session_id, "ownable": object_name})
  194. success = 'data' in response
  195. if success:
  196. print(tabulate(response['data'],
  197. headers=['Buy?', 'Name', 'Amount', 'Limit', 'Stop Loss?', 'Expires', 'No.'],
  198. tablefmt="pipe"))
  199. else:
  200. if 'error_message' in response:
  201. print('Order access failed with message:', response['error_message'])
  202. else:
  203. print('Order access failed.')
  204. def news():
  205. fake_loading_bar('Loading Data', duration=0.76)
  206. response = client_request('news', {"session_id": connection.session_id})
  207. success = 'data' in response
  208. if success:
  209. print(tabulate(response['data'],
  210. headers=['Date', 'Title'],
  211. tablefmt="pipe"))
  212. else:
  213. if 'error_message' in response:
  214. print('Order access failed with message:', response['error_message'])
  215. else:
  216. print('Order access failed.')
  217. def transactions(object_name=None):
  218. if object_name is None: # TODO list some available objects
  219. object_name = input('Name of object to check: ')
  220. fake_loading_bar('Loading Data', duration=1.3)
  221. response = client_request('transactions', {"session_id": connection.session_id, "ownable": object_name})
  222. success = 'data' in response
  223. if success:
  224. print(tabulate(response['data'],
  225. headers=['Time', 'Volume', 'Price'],
  226. tablefmt="pipe"))
  227. else:
  228. if 'error_message' in response:
  229. print('Transactions access failed with message:', response['error_message'])
  230. else:
  231. print('Transactions access failed.')