client_controller.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. def change_password(password=None, retype_password=None):
  61. if password is None:
  62. while True:
  63. if sys.stdin.isatty():
  64. password = getpass('New password: ')
  65. retype_password = getpass('Retype password: ')
  66. else:
  67. password = input('New password: ')
  68. retype_password = getpass('Retype password: ')
  69. if password == retype_password:
  70. break
  71. print('Passwords do not match.')
  72. fake_loading_bar('Validating password', duration=1.2)
  73. fake_loading_bar('Changing password', duration=5.2)
  74. response = client_request('change_password', {"session_id": connection.session_id, "password": password})
  75. if 'error_message' in response:
  76. print('Changing password failed with message:', response['error_message'])
  77. fake_loading_bar('Signing out', duration=0.7)
  78. connection.session_id = None
  79. # noinspection PyShadowingBuiltins
  80. def help():
  81. print('Allowed commands:')
  82. for cmd in allowed_commands:
  83. this_module = sys.modules[__name__]
  84. method = getattr(this_module, cmd)
  85. params = signature(method).parameters
  86. num_args = len(params)
  87. if num_args > 0:
  88. print('`' + cmd + '`', 'takes the following', num_args, 'arguments:')
  89. for p in params:
  90. print(' -', p)
  91. else:
  92. print('`' + cmd + '`', 'takes no arguments')
  93. print()
  94. print('NOTE:')
  95. print(' Commands can be combined in one line with ; between them.')
  96. print(' All arguments for all commands are optional!')
  97. def depot():
  98. fake_loading_bar('Loading data', duration=1.3)
  99. response = client_request('depot', {"session_id": connection.session_id})
  100. success = 'data' in response and 'own_wealth' in response
  101. if success:
  102. print(tabulate(response['data'], headers=['Object', 'Amount', 'Est. Value'], tablefmt="pipe"))
  103. print('This corresponds to a wealth of roughly', response['own_wealth'])
  104. else:
  105. if 'error_message' in response:
  106. print('Depot access failed with message:', response['error_message'])
  107. else:
  108. print('Depot access failed.')
  109. def leaderboard():
  110. fake_loading_bar('Loading data', duration=1.3)
  111. response = client_request('leaderboard', {"session_id": connection.session_id})
  112. success = 'data' in response
  113. if success:
  114. print(tabulate(response['data'], headers=['User', 'Wealth'], tablefmt="pipe"))
  115. else:
  116. if 'error_message' in response:
  117. print('Leaderboard access failed with message:', response['error_message'])
  118. else:
  119. print('Leaderboard access failed.')
  120. def activate_key(key=''):
  121. if key == '':
  122. print('Entering a game key may get you some money or other useful stuff.')
  123. key = input('Key: ')
  124. if key == '':
  125. print('Invalid key.')
  126. fake_loading_bar('Validating Key', duration=0.4)
  127. response = client_request('activate_key', {"session_id": connection.session_id, 'key': key})
  128. if 'error_message' in response:
  129. print('Key activation failed with message:', response['error_message'])
  130. def yn_dialog(msg):
  131. while True:
  132. result = input(msg + ' [y/n]: ')
  133. if result == 'y':
  134. return True
  135. if result == 'n':
  136. return False
  137. def buy(amount=None, object_name=None, limit='', stop_loss='', time_until_expiration=None):
  138. if object_name is None: # TODO list some available objects
  139. object_name = input('Name of object to buy: ')
  140. if amount is None:
  141. amount = input('Amount: ')
  142. if limit == '':
  143. set_limit = yn_dialog('Do you want to place a limit?')
  144. if set_limit:
  145. limit = input('Limit: ')
  146. stop_loss = yn_dialog('Is this a stop-loss limit?')
  147. else:
  148. limit = None
  149. stop_loss = None
  150. if time_until_expiration is None:
  151. time_until_expiration = input('Time until order expires (minutes, default 60):')
  152. if time_until_expiration == '':
  153. time_until_expiration = 60
  154. response = client_request('order', {"buy": True,
  155. "session_id": connection.session_id,
  156. "amount": amount,
  157. "ownable": object_name,
  158. "limit": limit,
  159. "stop_loss": stop_loss,
  160. "time_until_expiration": time_until_expiration})
  161. if 'error_message' in response:
  162. print('Order placement failed with message:', response['error_message'])
  163. else:
  164. print('You might want to use the `transactions` or `depot` commands',
  165. 'to see if the order has been executed already.')
  166. def sell(amount=None, object_name=None, limit=None, stop_loss=None, time_until_expiration=None):
  167. if object_name is None: # TODO list some available objects
  168. object_name = input('Name of object to sell: ')
  169. if amount is None:
  170. amount = input('Amount: ')
  171. if limit is None:
  172. set_limit = yn_dialog('Do you want to place a limit?')
  173. if set_limit:
  174. limit = input('Limit: ')
  175. else:
  176. limit = None
  177. stop_loss = None
  178. if limit is not None:
  179. stop_loss = yn_dialog('Is this a stop-loss limit?')
  180. if time_until_expiration is None:
  181. time_until_expiration = input('Time until order expires (minutes, default 60):')
  182. if time_until_expiration == '':
  183. time_until_expiration = 60
  184. response = client_request('order', {"buy": False,
  185. "session_id": connection.session_id,
  186. "amount": amount,
  187. "ownable": object_name,
  188. "limit": limit,
  189. "stop_loss": stop_loss,
  190. "time_until_expiration": time_until_expiration})
  191. if 'error_message' in response:
  192. print('Order placement failed with message:', response['error_message'])
  193. else:
  194. print('You might want to use the `transactions` or `depot` commands',
  195. 'to see if the order has been executed already.')
  196. def orders():
  197. fake_loading_bar('Loading Data', duration=0.9)
  198. response = client_request('orders', {"session_id": connection.session_id})
  199. success = 'data' in response
  200. if success:
  201. print(tabulate(response['data'],
  202. headers=['Buy?', 'Name', 'Amount', 'Limit', 'Stop Loss?', 'Orig. Size', 'Expires', 'No.'],
  203. tablefmt="pipe"))
  204. else:
  205. if 'error_message' in response:
  206. print('Order access failed with message:', response['error_message'])
  207. else:
  208. print('Order access failed.')
  209. def orders_on(object_name=None):
  210. if object_name is None: # TODO list some available objects
  211. object_name = input('Name of object to check: ')
  212. fake_loading_bar('Loading Data', duration=2.3)
  213. response = client_request('orders_on', {"session_id": connection.session_id, "ownable": object_name})
  214. success = 'data' in response
  215. if success:
  216. print(tabulate(response['data'],
  217. headers=['Buy?', 'Name', 'Amount', 'Limit', 'Stop Loss?', 'Expires', 'No.'],
  218. tablefmt="pipe"))
  219. else:
  220. if 'error_message' in response:
  221. print('Order access failed with message:', response['error_message'])
  222. else:
  223. print('Order access failed.')
  224. def news():
  225. fake_loading_bar('Loading Data', duration=0.76)
  226. response = client_request('news', {"session_id": connection.session_id})
  227. success = 'data' in response
  228. if success:
  229. print(tabulate(response['data'],
  230. headers=['Date', 'Title'],
  231. tablefmt="pipe"))
  232. else:
  233. if 'error_message' in response:
  234. print('Order access failed with message:', response['error_message'])
  235. else:
  236. print('Order access failed.')
  237. def transactions(object_name=None):
  238. if object_name is None: # TODO list some available objects
  239. object_name = input('Name of object to check: ')
  240. fake_loading_bar('Loading Data', duration=1.3)
  241. response = client_request('transactions', {"session_id": connection.session_id, "ownable": object_name})
  242. success = 'data' in response
  243. if success:
  244. print(tabulate(response['data'],
  245. headers=['Time', 'Volume', 'Price'],
  246. tablefmt="pipe"))
  247. else:
  248. if 'error_message' in response:
  249. print('Transactions access failed with message:', response['error_message'])
  250. else:
  251. print('Transactions access failed.')
  252. # noinspection PyShadowingBuiltins
  253. def exit():
  254. global exiting
  255. exiting = True