client_controller.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 util import debug
  9. def login(username=None, password=None):
  10. if connection.session_id is not None:
  11. fake_loading_bar('Signing out', delay=0.025)
  12. connection.session_id = None
  13. if username is None:
  14. username = input('Username: ')
  15. if password is None:
  16. if sys.stdin.isatty():
  17. password = getpass('Password: ')
  18. else:
  19. password = input('Password: ')
  20. response = client_request('login', {"username": username, "password": password})
  21. success = 'session_id' in response
  22. if success:
  23. connection.session_id = response['session_id']
  24. print('Login successful.')
  25. else:
  26. if 'error_message' in response:
  27. print('Login failed with message:', response['error_message'])
  28. else:
  29. print('Login failed.')
  30. def register(username=None, password=None, game_key=''):
  31. if connection.session_id is not None:
  32. fake_loading_bar('Signing out', delay=0.025)
  33. connection.session_id = None
  34. if username is None:
  35. username = input('Username: ')
  36. if password is None:
  37. if sys.stdin.isatty():
  38. password = getpass('Password: ')
  39. else:
  40. password = input('Password: ')
  41. if not debug:
  42. if game_key is '':
  43. print('Entering a game key will provide you with some starting money and other useful stuff.')
  44. game_key = input('Game key (leave empty if you don\'t have one): ')
  45. response = client_request('register', {"username": username, "password": password, "game_key": game_key})
  46. if 'error_message' in response:
  47. print('Registration failed with message:', response['error_message'])
  48. def cancel_order(order_no=None):
  49. if order_no is None:
  50. order_no = input('Order No.: ')
  51. response = client_request('cancel_order', {"session_id": connection.session_id, "order_id": order_no})
  52. if 'error_message' in response:
  53. print('Order cancelling failed with message:', response['error_message'])
  54. # noinspection PyShadowingBuiltins
  55. def help():
  56. print('Allowed commands:')
  57. for cmd in allowed_commands:
  58. this_module = sys.modules[__name__]
  59. method = getattr(this_module, cmd)
  60. params = signature(method).parameters
  61. num_args = len(params)
  62. if num_args > 0:
  63. print('`' + cmd + '`', 'takes the following', num_args, 'arguments:')
  64. for p in params:
  65. print(' -', p)
  66. else:
  67. print('`' + cmd + '`', 'takes no arguments')
  68. print()
  69. print('NOTE:')
  70. print(' Commands can be combined in one line with ; between them.')
  71. print(' All arguments for all commands are optional!')
  72. def depot():
  73. response = client_request('depot', {"session_id": connection.session_id})
  74. success = 'data' in response and 'own_wealth' in response
  75. if success:
  76. print(tabulate(response['data'], headers=['Object', 'Amount'], tablefmt="pipe"))
  77. print('This corresponds to a wealth of roughly', response['own_wealth'])
  78. else:
  79. if 'error_message' in response:
  80. print('Depot access failed with message:', response['error_message'])
  81. else:
  82. print('Depot access failed.')
  83. def leaderboard():
  84. response = client_request('leaderboard', {"session_id": connection.session_id})
  85. success = 'data' in response
  86. if success:
  87. print(tabulate(response['data'], headers=['User', 'Wealth'], tablefmt="pipe"))
  88. else:
  89. if 'error_message' in response:
  90. print('Leaderboard access failed with message:', response['error_message'])
  91. else:
  92. print('Leaderboard access failed.')
  93. def activate_key(key=''):
  94. if key == '':
  95. print('Entering a game key may get you some money or other useful stuff.')
  96. key = input('Key: ')
  97. if key == '':
  98. print('Invalid key.')
  99. response = client_request('activate_key', {"session_id": connection.session_id, 'key': key})
  100. if 'error_message' in response:
  101. print('Key activation failed with message:', response['error_message'])
  102. def yn_dialog(msg):
  103. while True:
  104. result = input(msg + ' [y/n]: ')
  105. if result == 'y':
  106. return True
  107. if result == 'n':
  108. return False
  109. def buy(amount=None, object_name=None, limit='', stop_loss='', time_until_expiration=None):
  110. if object_name is None: # TODO list some available objects
  111. object_name = input('Name of object to buy: ')
  112. if amount is None:
  113. amount = input('Amount: ')
  114. if limit == '':
  115. set_limit = yn_dialog('Do you want to place a limit?')
  116. if set_limit:
  117. limit = input('Limit: ')
  118. stop_loss = yn_dialog('Is this a stop-loss limit?')
  119. else:
  120. limit = None
  121. stop_loss = None
  122. if time_until_expiration is None:
  123. time_until_expiration = input('Time until order expires (minutes, default 60):')
  124. if time_until_expiration == '':
  125. time_until_expiration = 60
  126. response = client_request('order', {"buy": True,
  127. "session_id": connection.session_id,
  128. "amount": amount,
  129. "ownable": object_name,
  130. "limit": limit,
  131. "stop_loss": stop_loss,
  132. "time_until_expiration": time_until_expiration})
  133. if 'error_message' in response:
  134. print('Order placement failed with message:', response['error_message'])
  135. else:
  136. print('You might want to use the `transactions` or `depot` commands',
  137. 'to see if the order has been executed already.')
  138. def sell(amount=None, object_name=None, limit=None, stop_loss=None, time_until_expiration=None):
  139. if object_name is None: # TODO list some available objects
  140. object_name = input('Name of object to sell: ')
  141. if amount is None:
  142. amount = input('Amount: ')
  143. if limit is None:
  144. set_limit = yn_dialog('Do you want to place a limit?')
  145. if set_limit:
  146. limit = input('Limit: ')
  147. else:
  148. limit = None
  149. stop_loss = None
  150. if limit is not None:
  151. stop_loss = yn_dialog('Is this a stop-loss limit?')
  152. if time_until_expiration is None:
  153. time_until_expiration = input('Time until order expires (minutes, default 60):')
  154. if time_until_expiration == '':
  155. time_until_expiration = 60
  156. response = client_request('order', {"buy": False,
  157. "session_id": connection.session_id,
  158. "amount": amount,
  159. "ownable": object_name,
  160. "limit": limit,
  161. "stop_loss": stop_loss,
  162. "time_until_expiration": time_until_expiration})
  163. if 'error_message' in response:
  164. print('Order placement failed with message:', response['error_message'])
  165. else:
  166. print('You might want to use the `transactions` or `depot` commands',
  167. 'to see if the order has been executed already.')
  168. def orders():
  169. response = client_request('orders', {"session_id": connection.session_id})
  170. success = 'data' in response
  171. if success:
  172. print(tabulate(response['data'],
  173. headers=['Buy?', 'Name', 'Amount', 'Limit', 'Stop Loss?', 'Orig. Size', 'Expires', 'No.'],
  174. tablefmt="pipe"))
  175. else:
  176. if 'error_message' in response:
  177. print('Order access failed with message:', response['error_message'])
  178. else:
  179. print('Order access failed.')
  180. def orders_on(object_name=None):
  181. if object_name is None: # TODO list some available objects
  182. object_name = input('Name of object to check: ')
  183. response = client_request('orders_on', {"session_id": connection.session_id, "ownable": object_name})
  184. success = 'data' in response
  185. if success:
  186. print(tabulate(response['data'],
  187. headers=['Buy?', 'Name', 'Amount', 'Limit', 'Stop Loss?', 'Expires', 'No.'],
  188. tablefmt="pipe"))
  189. else:
  190. if 'error_message' in response:
  191. print('Order access failed with message:', response['error_message'])
  192. else:
  193. print('Order access failed.')
  194. def news():
  195. response = client_request('news', {"session_id": connection.session_id})
  196. success = 'data' in response
  197. if success:
  198. print(tabulate(response['data'],
  199. headers=['Date', 'Title'],
  200. tablefmt="pipe"))
  201. else:
  202. if 'error_message' in response:
  203. print('Order access failed with message:', response['error_message'])
  204. else:
  205. print('Order access failed.')
  206. def transactions(object_name=None):
  207. if object_name is None: # TODO list some available objects
  208. object_name = input('Name of object to check: ')
  209. response = client_request('transactions', {"session_id": connection.session_id, "ownable": object_name})
  210. success = 'data' in response
  211. if success:
  212. print(tabulate(response['data'],
  213. headers=['Time', 'Volume', 'Price'],
  214. tablefmt="pipe"))
  215. else:
  216. if 'error_message' in response:
  217. print('Transactions access failed with message:', response['error_message'])
  218. else:
  219. print('Transactions access failed.')