client_controller.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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
  75. if success:
  76. print(tabulate(response['data'], headers=['Object', 'Amount'], tablefmt="pipe"))
  77. else:
  78. if 'error_message' in response:
  79. print('Depot access failed with message:', response['error_message'])
  80. else:
  81. print('Depot access failed.')
  82. def leaderboard():
  83. response = client_request('leaderboard', {"session_id": connection.session_id})
  84. success = 'data' in response
  85. if success:
  86. print(tabulate(response['data'], headers=['User', 'Score'], tablefmt="pipe"))
  87. else:
  88. if 'error_message' in response:
  89. print('Leaderboard access failed with message:', response['error_message'])
  90. else:
  91. print('Leaderboard access failed.')
  92. def activate_key(key=''):
  93. if key == '':
  94. print('Entering a game key may get you some money or other useful stuff.')
  95. key = input('Key: ')
  96. if key == '':
  97. print('Invalid key.')
  98. response = client_request('activate_key', {"session_id": connection.session_id, 'key': key})
  99. if 'error_message' in response:
  100. print('Key activation failed with message:', response['error_message'])
  101. def yn_dialog(msg):
  102. while True:
  103. result = input(msg + ' [y/n]: ')
  104. if result == 'y':
  105. return True
  106. if result == 'n':
  107. return False
  108. def buy(amount=None, object_name=None, limit='', stop_loss='', time_until_expiration=None):
  109. if object_name is None: # TODO list some available objects
  110. object_name = input('Name of object to buy: ')
  111. if amount is None:
  112. amount = input('Amount: ')
  113. if limit == '':
  114. set_limit = yn_dialog('Do you want to place a limit?')
  115. if set_limit:
  116. limit = input('Limit: ')
  117. stop_loss = yn_dialog('Is this a stop-loss limit?')
  118. else:
  119. limit = None
  120. stop_loss = None
  121. if time_until_expiration is None:
  122. time_until_expiration = input('Time until order expires (minutes, default 60):')
  123. if time_until_expiration == '':
  124. time_until_expiration = 60
  125. response = client_request('order', {"buy": True,
  126. "session_id": connection.session_id,
  127. "amount": amount,
  128. "ownable": object_name,
  129. "limit": limit,
  130. "stop_loss": stop_loss,
  131. "time_until_expiration": time_until_expiration})
  132. if 'error_message' in response:
  133. print('Order placement failed with message:', response['error_message'])
  134. else:
  135. print('You might want to use the `transactions` or `depot` commands',
  136. 'to see if the order has been executed already.')
  137. def sell(amount=None, object_name=None, limit=None, stop_loss=None, time_until_expiration=None):
  138. if object_name is None: # TODO list some available objects
  139. object_name = input('Name of object to sell: ')
  140. if amount is None:
  141. amount = input('Amount: ')
  142. if limit is None:
  143. set_limit = yn_dialog('Do you want to place a limit?')
  144. if set_limit:
  145. limit = input('Limit: ')
  146. else:
  147. limit = None
  148. stop_loss = None
  149. if limit is not None:
  150. stop_loss = yn_dialog('Is this a stop-loss limit?')
  151. if time_until_expiration is None:
  152. time_until_expiration = input('Time until order expires (minutes, default 60):')
  153. if time_until_expiration == '':
  154. time_until_expiration = 60
  155. response = client_request('order', {"buy": False,
  156. "session_id": connection.session_id,
  157. "amount": amount,
  158. "ownable": object_name,
  159. "limit": limit,
  160. "stop_loss": stop_loss,
  161. "time_until_expiration": time_until_expiration})
  162. if 'error_message' in response:
  163. print('Order placement failed with message:', response['error_message'])
  164. else:
  165. print('You might want to use the `transactions` or `depot` commands',
  166. 'to see if the order has been executed already.')
  167. def orders():
  168. response = client_request('orders', {"session_id": connection.session_id})
  169. success = 'data' in response
  170. if success:
  171. print(tabulate(response['data'],
  172. headers=['Buy?', 'Name', 'Amount', 'Limit', 'Stop Loss?', 'Orig. Size', 'Expires', 'No.'],
  173. tablefmt="pipe"))
  174. else:
  175. if 'error_message' in response:
  176. print('Order access failed with message:', response['error_message'])
  177. else:
  178. print('Order access failed.')
  179. def orders_on(object_name=None):
  180. if object_name is None: # TODO list some available objects
  181. object_name = input('Name of object to check: ')
  182. response = client_request('orders_on', {"session_id": connection.session_id, "ownable": object_name})
  183. success = 'data' in response
  184. if success:
  185. print(tabulate(response['data'],
  186. headers=['Buy?', 'Name', 'Amount', 'Limit', 'Stop Loss?', 'Expires', 'No.'],
  187. tablefmt="pipe"))
  188. else:
  189. if 'error_message' in response:
  190. print('Order access failed with message:', response['error_message'])
  191. else:
  192. print('Order access failed.')
  193. def news():
  194. response = client_request('news', {"session_id": connection.session_id})
  195. success = 'data' in response
  196. if success:
  197. print(tabulate(response['data'],
  198. headers=['Date', 'Title'],
  199. tablefmt="pipe"))
  200. else:
  201. if 'error_message' in response:
  202. print('Order access failed with message:', response['error_message'])
  203. else:
  204. print('Order access failed.')
  205. def transactions(object_name=None):
  206. if object_name is None: # TODO list some available objects
  207. object_name = input('Name of object to check: ')
  208. response = client_request('transactions', {"session_id": connection.session_id, "ownable": object_name})
  209. success = 'data' in response
  210. if success:
  211. print(tabulate(response['data'],
  212. headers=['Time', 'Volume', 'Price'],
  213. tablefmt="pipe"))
  214. else:
  215. if 'error_message' in response:
  216. print('Transactions access failed with message:', response['error_message'])
  217. else:
  218. print('Transactions access failed.')