client_controller.py 13 KB

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