client_controller.py 11 KB

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