client_controller.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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, game_key='', password=None, retype_password=None):
  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('New password: ')
  41. retype_password = getpass('Retype password: ')
  42. else:
  43. password = input('New password: ')
  44. retype_password = input('Retype password: ')
  45. if password != retype_password:
  46. print('Passwords do not match.')
  47. return
  48. elif retype_password is None:
  49. if sys.stdin.isatty():
  50. retype_password = getpass('Retype password: ')
  51. else:
  52. retype_password = input('Retype password: ')
  53. if password != retype_password:
  54. print('Passwords do not match.')
  55. return
  56. if not debug:
  57. if game_key == '':
  58. print('Entering a game key will provide you with some starting money and other useful stuff.')
  59. game_key = input('Game key (leave empty if you don\'t have one): ')
  60. fake_loading_bar('Validating Registration', duration=5.2)
  61. if game_key != '':
  62. fake_loading_bar('Validating Game Key', duration=0.4)
  63. response = client_request('register', {"username": username, "password": password, "game_key": game_key})
  64. if 'error_message' in response:
  65. print('Registration failed with message:', response['error_message'])
  66. def cancel_order(order_no=None):
  67. if order_no is None:
  68. order_no = input('Order No.: ')
  69. fake_loading_bar('Validating Request', duration=0.6)
  70. response = client_request('cancel_order', {"session_id": connection.session_id, "order_id": order_no})
  71. if 'error_message' in response:
  72. print('Order cancelling failed with message:', response['error_message'])
  73. def change_password(password=None, retype_password=None):
  74. if password != retype_password:
  75. password = None
  76. if password is None:
  77. if sys.stdin.isatty():
  78. password = getpass('New password: ')
  79. retype_password = getpass('Retype password: ')
  80. else:
  81. password = input('New password: ')
  82. retype_password = input('Retype password: ')
  83. if password != retype_password:
  84. print('Passwords do not match.')
  85. return
  86. elif retype_password is None:
  87. if sys.stdin.isatty():
  88. retype_password = getpass('Retype password: ')
  89. else:
  90. retype_password = input('Retype password: ')
  91. if password != retype_password:
  92. print('Passwords do not match.')
  93. return
  94. fake_loading_bar('Validating password', duration=1.2)
  95. fake_loading_bar('Changing password', duration=5.2)
  96. response = client_request('change_password', {"session_id": connection.session_id, "password": password})
  97. if 'error_message' in response:
  98. print('Changing password failed with message:', response['error_message'])
  99. fake_loading_bar('Signing out', duration=0.7)
  100. connection.session_id = None
  101. # noinspection PyShadowingBuiltins
  102. def help():
  103. print('Allowed commands:')
  104. for cmd in allowed_commands:
  105. this_module = sys.modules[__name__]
  106. method = getattr(this_module, cmd)
  107. params = signature(method).parameters
  108. num_args = len(params)
  109. if num_args > 0:
  110. print('`' + cmd + '`', 'takes the following', num_args, 'arguments:')
  111. for p in params:
  112. print(' -', p)
  113. else:
  114. print('`' + cmd + '`', 'takes no arguments')
  115. print()
  116. print('NOTE:')
  117. print(' Commands can be combined in one line with ; between them.')
  118. print(' All arguments for all commands are optional!')
  119. def _my_tabulate(data, **params):
  120. if data == [] and 'headers' in params:
  121. data = [(None for _ in params['headers'])]
  122. tabulate.MIN_PADDING = 0
  123. return tabulate.tabulate(data, **params)
  124. def depot():
  125. fake_loading_bar('Loading data', duration=1.3)
  126. response = client_request('depot', {"session_id": connection.session_id})
  127. success = 'data' in response and 'own_wealth' in response
  128. if success:
  129. data = response['data']
  130. for row in data:
  131. row.append(row[1] * row[2])
  132. print(_my_tabulate(data,
  133. headers=['Object', 'Amount', 'Course', 'Bid', 'Ask', 'Est. Value'],
  134. tablefmt="pipe"))
  135. print('This corresponds to a wealth of roughly', response['own_wealth'])
  136. else:
  137. if 'error_message' in response:
  138. print('Depot access failed with message:', response['error_message'])
  139. else:
  140. print('Depot access failed.')
  141. def leaderboard():
  142. fake_loading_bar('Loading data', duration=1.3)
  143. response = client_request('leaderboard', {"session_id": connection.session_id})
  144. success = 'data' in response
  145. if success:
  146. print(_my_tabulate(response['data'], headers=['User', 'Wealth'], tablefmt="pipe"))
  147. else:
  148. if 'error_message' in response:
  149. print('Leaderboard access failed with message:', response['error_message'])
  150. else:
  151. print('Leaderboard access failed.')
  152. def activate_key(key=''):
  153. if key == '':
  154. print('Entering a game key may get you some money or other useful stuff.')
  155. key = input('Key: ')
  156. if key == '':
  157. print('Invalid key.')
  158. fake_loading_bar('Validating Key', duration=0.4)
  159. response = client_request('activate_key', {"session_id": connection.session_id, 'key': key})
  160. if 'error_message' in response:
  161. print('Key activation failed with message:', response['error_message'])
  162. def yn_dialog(msg):
  163. while True:
  164. result = input(msg + ' [y/n]: ')
  165. if result == 'y':
  166. return True
  167. if result == 'n':
  168. return False
  169. def buy(amount=None, object_name=None, limit='', stop_loss='', time_until_expiration=None):
  170. if object_name is None: # TODO list some available objects
  171. object_name = input('Name of object to buy: ')
  172. if amount is None:
  173. amount = input('Amount: ')
  174. if limit == '':
  175. set_limit = yn_dialog('Do you want to place a limit?')
  176. if set_limit:
  177. limit = input('Limit: ')
  178. stop_loss = yn_dialog('Is this a stop-loss limit?')
  179. else:
  180. limit = None
  181. stop_loss = None
  182. if limit is not None and stop_loss == '':
  183. stop_loss = yn_dialog('Is this a stop-loss limit?')
  184. if time_until_expiration is None:
  185. time_until_expiration = input('Time until order expires (minutes, default 43200):')
  186. if time_until_expiration == '':
  187. time_until_expiration = 43200
  188. fake_loading_bar('Loading Data', duration=1.3)
  189. response = client_request('order', {"buy": True,
  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 sell(amount=None, object_name=None, limit='', stop_loss='', time_until_expiration=None):
  202. if object_name is None: # TODO list some available objects
  203. object_name = input('Name of object to sell: ')
  204. if amount is None:
  205. amount = input('Amount: ')
  206. if limit == '':
  207. set_limit = yn_dialog('Do you want to place a limit?')
  208. if set_limit:
  209. limit = input('Limit: ')
  210. stop_loss = yn_dialog('Is this a stop-loss limit?')
  211. else:
  212. limit = None
  213. stop_loss = None
  214. if limit != '' and stop_loss == '':
  215. stop_loss = yn_dialog('Is this a stop-loss limit?')
  216. if time_until_expiration is None:
  217. time_until_expiration = input('Time until order expires (minutes, default 43200):')
  218. if time_until_expiration == '':
  219. time_until_expiration = 43200
  220. fake_loading_bar('Loading Data', duration=1.3)
  221. response = client_request('order', {"buy": False,
  222. "session_id": connection.session_id,
  223. "amount": amount,
  224. "ownable": object_name,
  225. "limit": limit,
  226. "stop_loss": stop_loss,
  227. "time_until_expiration": time_until_expiration})
  228. if 'error_message' in response:
  229. print('Order placement failed with message:', response['error_message'])
  230. else:
  231. print('You might want to use the `transactions` or `depot` commands',
  232. 'to see if the order has been executed already.')
  233. def orders():
  234. fake_loading_bar('Loading Data', duration=0.9)
  235. response = client_request('orders', {"session_id": connection.session_id})
  236. success = 'data' in response
  237. if success:
  238. print(_my_tabulate(response['data'],
  239. headers=['Buy?', 'Name', 'Amount', 'Limit', 'stop-loss', 'Expires', 'No.'],
  240. tablefmt="pipe"))
  241. else:
  242. if 'error_message' in response:
  243. print('Order access failed with message:', response['error_message'])
  244. else:
  245. print('Order access failed.')
  246. def orders_on(object_name=None):
  247. if object_name is None: # TODO list some available objects
  248. object_name = input('Name of object to check: ')
  249. fake_loading_bar('Loading Data', duration=2.3)
  250. response = client_request('orders_on', {"session_id": connection.session_id, "ownable": object_name})
  251. success = 'data' in response
  252. if success:
  253. print(_my_tabulate(response['data'],
  254. headers=['Buy?', 'Name', 'Amount', 'Limit', 'stop-loss', 'Expires', 'No.'],
  255. tablefmt="pipe"))
  256. else:
  257. if 'error_message' in response:
  258. print('Order access failed with message:', response['error_message'])
  259. else:
  260. print('Order access failed.')
  261. def gift(username=None, amount=None, object_name=None):
  262. if username is None:
  263. username = input('Username of recipient: ')
  264. if object_name is None:
  265. object_name = input('Name of object to give: ')
  266. if amount is None:
  267. amount = input('How many?: ')
  268. fake_loading_bar('Sending Gift', duration=4.2)
  269. response = client_request('gift',
  270. {"session_id": connection.session_id,
  271. "username": username,
  272. "object_name": object_name,
  273. "amount": amount})
  274. if 'error_message' in response:
  275. print('Order access failed with message:', response['error_message'])
  276. elif 'message' in response:
  277. print(response['message'])
  278. def news():
  279. fake_loading_bar('Loading Data', duration=0.76)
  280. response = client_request('news', {})
  281. success = 'data' in response
  282. if success:
  283. print(_my_tabulate(response['data'],
  284. headers=['Date', 'Title'],
  285. tablefmt="pipe"))
  286. else:
  287. if 'error_message' in response:
  288. print('Order access failed with message:', response['error_message'])
  289. else:
  290. print('Order access failed.')
  291. def transactions(object_name=None):
  292. if object_name is None: # TODO list some available objects
  293. object_name = input('Name of object to check: ')
  294. fake_loading_bar('Loading Data', duration=1.3)
  295. response = client_request('transactions', {"session_id": connection.session_id, "ownable": object_name})
  296. success = 'data' in response
  297. if success:
  298. print(_my_tabulate(response['data'],
  299. headers=['Time', 'Volume', 'Price'],
  300. tablefmt="pipe"))
  301. else:
  302. if 'error_message' in response:
  303. print('Transactions access failed with message:', response['error_message'])
  304. else:
  305. print('Transactions access failed.')
  306. # noinspection PyShadowingBuiltins
  307. def exit():
  308. global exiting
  309. exiting = True