client_controller.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 _my_tabulate(data, **params):
  100. if data == [] and 'headers' in params:
  101. data = [(None for _ in params['headers'])]
  102. return tabulate(data, **params)
  103. def depot():
  104. fake_loading_bar('Loading data', duration=1.3)
  105. response = client_request('depot', {"session_id": connection.session_id})
  106. success = 'data' in response and 'own_wealth' in response
  107. if success:
  108. print(_my_tabulate(response['data'], headers=['Object', 'Amount', 'Est. Value'], tablefmt="pipe"))
  109. print('This corresponds to a wealth of roughly', response['own_wealth'])
  110. else:
  111. if 'error_message' in response:
  112. print('Depot access failed with message:', response['error_message'])
  113. else:
  114. print('Depot access failed.')
  115. def leaderboard():
  116. fake_loading_bar('Loading data', duration=1.3)
  117. response = client_request('leaderboard', {"session_id": connection.session_id})
  118. success = 'data' in response
  119. if success:
  120. print(_my_tabulate(response['data'], headers=['User', 'Wealth'], tablefmt="pipe"))
  121. else:
  122. if 'error_message' in response:
  123. print('Leaderboard access failed with message:', response['error_message'])
  124. else:
  125. print('Leaderboard access failed.')
  126. def activate_key(key=''):
  127. if key == '':
  128. print('Entering a game key may get you some money or other useful stuff.')
  129. key = input('Key: ')
  130. if key == '':
  131. print('Invalid key.')
  132. fake_loading_bar('Validating Key', duration=0.4)
  133. response = client_request('activate_key', {"session_id": connection.session_id, 'key': key})
  134. if 'error_message' in response:
  135. print('Key activation failed with message:', response['error_message'])
  136. def yn_dialog(msg):
  137. while True:
  138. result = input(msg + ' [y/n]: ')
  139. if result == 'y':
  140. return True
  141. if result == 'n':
  142. return False
  143. def buy(amount=None, object_name=None, limit='', stop_loss='', time_until_expiration=None):
  144. if object_name is None: # TODO list some available objects
  145. object_name = input('Name of object to buy: ')
  146. if amount is None:
  147. amount = input('Amount: ')
  148. if limit == '':
  149. set_limit = yn_dialog('Do you want to place a limit?')
  150. if set_limit:
  151. limit = input('Limit: ')
  152. stop_loss = yn_dialog('Is this a stop-loss limit?')
  153. else:
  154. limit = None
  155. stop_loss = None
  156. if limit is not None and stop_loss == '':
  157. stop_loss = yn_dialog('Is this a stop-loss limit?')
  158. if time_until_expiration is None:
  159. time_until_expiration = input('Time until order expires (minutes, default 1440):')
  160. if time_until_expiration == '':
  161. time_until_expiration = 1440
  162. response = client_request('order', {"buy": True,
  163. "session_id": connection.session_id,
  164. "amount": amount,
  165. "ownable": object_name,
  166. "limit": limit,
  167. "stop_loss": stop_loss,
  168. "time_until_expiration": time_until_expiration})
  169. if 'error_message' in response:
  170. print('Order placement failed with message:', response['error_message'])
  171. else:
  172. print('You might want to use the `transactions` or `depot` commands',
  173. 'to see if the order has been executed already.')
  174. def sell(amount=None, object_name=None, limit='', stop_loss='', time_until_expiration=None):
  175. if object_name is None: # TODO list some available objects
  176. object_name = input('Name of object to sell: ')
  177. if amount is None:
  178. amount = input('Amount: ')
  179. if limit == '':
  180. set_limit = yn_dialog('Do you want to place a limit?')
  181. if set_limit:
  182. limit = input('Limit: ')
  183. stop_loss = yn_dialog('Is this a stop-loss limit?')
  184. else:
  185. limit = None
  186. stop_loss = None
  187. if limit != '' and stop_loss == '':
  188. stop_loss = yn_dialog('Is this a stop-loss limit?')
  189. if time_until_expiration is None:
  190. time_until_expiration = input('Time until order expires (minutes, default 1440):')
  191. if time_until_expiration == '':
  192. time_until_expiration = 1440
  193. response = client_request('order', {"buy": False,
  194. "session_id": connection.session_id,
  195. "amount": amount,
  196. "ownable": object_name,
  197. "limit": limit,
  198. "stop_loss": stop_loss,
  199. "time_until_expiration": time_until_expiration})
  200. if 'error_message' in response:
  201. print('Order placement failed with message:', response['error_message'])
  202. else:
  203. print('You might want to use the `transactions` or `depot` commands',
  204. 'to see if the order has been executed already.')
  205. def orders():
  206. fake_loading_bar('Loading Data', duration=0.9)
  207. response = client_request('orders', {"session_id": connection.session_id})
  208. success = 'data' in response
  209. if success:
  210. print(_my_tabulate(response['data'],
  211. headers=['Buy?', 'Name', 'Amount', 'Limit', 'Stop Loss?', 'Orig. Size', 'Expires', 'No.'],
  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 orders_on(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=2.3)
  222. response = client_request('orders_on', {"session_id": connection.session_id, "ownable": object_name})
  223. success = 'data' in response
  224. if success:
  225. print(_my_tabulate(response['data'],
  226. headers=['Buy?', 'Name', 'Amount', 'Limit', 'Stop Loss?', 'Expires', 'No.'],
  227. tablefmt="pipe"))
  228. else:
  229. if 'error_message' in response:
  230. print('Order access failed with message:', response['error_message'])
  231. else:
  232. print('Order access failed.')
  233. def gift(username=None, amount=None, object_name=None):
  234. if username is None:
  235. username = input('Username of recipient: ')
  236. if object_name is None:
  237. object_name = input('Name of object to give: ')
  238. if amount is None:
  239. amount = input('How many?: ')
  240. fake_loading_bar('Sending Gift', duration=4.2)
  241. response = client_request('gift',
  242. {"session_id": connection.session_id,
  243. "username": username,
  244. "object_name": object_name,
  245. "amount": amount})
  246. if 'error_message' in response:
  247. print('Order access failed with message:', response['error_message'])
  248. elif 'message' in response:
  249. print(response['message'])
  250. def news():
  251. fake_loading_bar('Loading Data', duration=0.76)
  252. response = client_request('news', {"session_id": connection.session_id})
  253. success = 'data' in response
  254. if success:
  255. print(_my_tabulate(response['data'],
  256. headers=['Date', 'Title'],
  257. tablefmt="pipe"))
  258. else:
  259. if 'error_message' in response:
  260. print('Order access failed with message:', response['error_message'])
  261. else:
  262. print('Order access failed.')
  263. def transactions(object_name=None):
  264. if object_name is None: # TODO list some available objects
  265. object_name = input('Name of object to check: ')
  266. fake_loading_bar('Loading Data', duration=1.3)
  267. response = client_request('transactions', {"session_id": connection.session_id, "ownable": object_name})
  268. success = 'data' in response
  269. if success:
  270. print(_my_tabulate(response['data'],
  271. headers=['Time', 'Volume', 'Price'],
  272. tablefmt="pipe"))
  273. else:
  274. if 'error_message' in response:
  275. print('Transactions access failed with message:', response['error_message'])
  276. else:
  277. print('Transactions access failed.')
  278. # noinspection PyShadowingBuiltins
  279. def exit():
  280. global exiting
  281. exiting = True