client_controller.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import getpass
  2. import inspect
  3. import sys
  4. import client
  5. import connection
  6. from client import allowed_commands
  7. from connection import client_request
  8. from tabulate import tabulate
  9. from util import debug
  10. def login(username=None, password=None):
  11. if connection.session_id is not None:
  12. client.fake_loading_bar('Signing out', delay=0.025)
  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.getpass('Password: ')
  19. else:
  20. password = input('Password: ')
  21. response = client_request('login', {"username": username, "password": password})
  22. success = 'session_id' in response
  23. if success:
  24. connection.session_id = response['session_id']
  25. print('Login successful.')
  26. else:
  27. if 'error_message' in response:
  28. print('Login failed with message:', response['error_message'])
  29. else:
  30. print('Login failed.')
  31. def register(username=None, password=None, game_key=''):
  32. if connection.session_id is not None:
  33. client.fake_loading_bar('Signing out', delay=0.025)
  34. connection.session_id = None
  35. if username is None:
  36. username = input('Username: ')
  37. if password is None:
  38. if sys.stdin.isatty():
  39. password = getpass.getpass('Password: ')
  40. else:
  41. password = input('Password: ')
  42. if not debug:
  43. if game_key is '':
  44. print('Entering a game key will provide you with some starting money and other useful stuff.')
  45. game_key = input('Game key (leave empty if you don\'t have one): ')
  46. response = client_request('register', {"username": username, "password": password, "game_key": game_key})
  47. if 'error_message' in response:
  48. print('Registration failed with message:', response['error_message'])
  49. # noinspection PyShadowingBuiltins
  50. def help():
  51. print('Allowed commands:')
  52. for cmd in allowed_commands:
  53. this_module = sys.modules[__name__]
  54. method = getattr(this_module, cmd)
  55. params = inspect.signature(method).parameters
  56. num_args = len(params)
  57. if num_args > 0:
  58. print('`' + cmd + '`', 'takes the following', num_args, 'arguments:')
  59. for p in params:
  60. print(' -', p)
  61. else:
  62. print('`' + cmd + '`', 'takes no arguments')
  63. print('NOTE: All arguments are optional!')
  64. def depot():
  65. response = client_request('depot', {"session_id": connection.session_id})
  66. success = 'data' in response
  67. if success:
  68. print(tabulate(response['data'], headers=['Object', 'Amount'], tablefmt="pipe"))
  69. else:
  70. if 'error_message' in response:
  71. print('Depot access failed with message:', response['error_message'])
  72. else:
  73. print('Depot access failed.')
  74. def activate_key(key=''):
  75. if key == '':
  76. print('Entering a game key may get you some money or other useful stuff.')
  77. key = input('Key: ')
  78. if key == '':
  79. print('Invalid key.')
  80. response = client_request('activate_key', {"session_id": connection.session_id, 'key': key})
  81. if 'error_message' in response:
  82. print('Key activation failed with message:', response['error_message'])
  83. def yn_dialog(msg):
  84. while True:
  85. result = input(msg + ' [y/n]: ')
  86. if result == 'y':
  87. return True
  88. if result == 'n':
  89. return False
  90. def buy(amount=None, object_name=None, limit='', stop_loss=''):
  91. if object_name is None: # TODO list some available objects
  92. object_name = input('Name of object to buy: ')
  93. if amount is None:
  94. amount = input('Amount: ')
  95. if limit != '':
  96. set_limit = yn_dialog('Do you want to place a limit?')
  97. if set_limit:
  98. limit = input('Limit: ')
  99. stop_loss = yn_dialog('Is this a stop-loss limit?')
  100. response = client_request('order', {"buy": True,
  101. "session_id": connection.session_id,
  102. "amount": amount,
  103. "ownable": object_name,
  104. "limit": limit,
  105. "stop_loss": stop_loss})
  106. if 'error_message' in response:
  107. print('Order placement failed with message:', response['error_message'])
  108. def orders():
  109. response = client_request('orders', {"session_id": connection.session_id})
  110. success = 'data' in response
  111. if success:
  112. print(tabulate(response['data'],
  113. headers=['Buy?', 'Name', 'Amount', 'Limit', 'Stop Loss?', 'Orig. Order Size'],
  114. tablefmt="pipe"))
  115. else:
  116. if 'error_message' in response:
  117. print('Order access failed with message:', response['error_message'])
  118. else:
  119. print('Order access failed.')
  120. def news():
  121. response = client_request('news', {"session_id": connection.session_id})
  122. success = 'data' in response
  123. if success:
  124. print(tabulate(response['data'],
  125. headers=['Date', 'Title'],
  126. tablefmt="pipe"))
  127. else:
  128. if 'error_message' in response:
  129. print('Order access failed with message:', response['error_message'])
  130. else:
  131. print('Order access failed.')