1
1

client_controller.py 10 KB

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