run_client.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. from __future__ import print_function
  2. import sys
  3. import time
  4. import client_controller
  5. from debug import debug
  6. def fake_loading_bar(msg, duration=5.):
  7. if len(msg) >= 60:
  8. raise AssertionError('Loading bar label too large')
  9. msg += ': '
  10. print(msg, end='')
  11. sys.stdout.flush()
  12. bar_length = 79 - len(msg)
  13. for _ in range(bar_length):
  14. if not debug:
  15. time.sleep(duration / bar_length)
  16. print('#', end='')
  17. sys.stdout.flush()
  18. print('\n', end='')
  19. sys.stdout.flush()
  20. def load():
  21. print('Loading...')
  22. fake_loading_bar('Initializing fake loading bars', duration=5)
  23. fake_loading_bar('Loading data from disk', duration=1)
  24. fake_loading_bar('Loading available commands', duration=3.5)
  25. fake_loading_bar('Updating indices', duration=2)
  26. fake_loading_bar('Waiting', duration=5)
  27. print('Done.\n\n')
  28. def welcome():
  29. print('''
  30. $$$$$$\ $$\
  31. $$ __$$\ $$ |
  32. $$ / $$ | $$$$$$\ $$$$$$$ | $$$$$$\ $$$$$$\ $$$$$$\ $$$$$$\
  33. $$ | $$ |$$ __$$\ $$ __$$ |$$ __$$\ $$ __$$\ $$ __$$\ $$ __$$\
  34. $$ | $$ |$$ | \__|$$ / $$ |$$$$$$$$ |$$ | \__|$$$$$$$$ |$$ | \__|
  35. $$ | $$ |$$ | $$ | $$ |$$ ____|$$ | $$ ____|$$ |
  36. $$$$$$ |$$ | \$$$$$$$ |\$$$$$$$\ $$ | \$$$$$$$\ $$ |
  37. \______/ \__| \_______| \_______|\__| \_______|\__|
  38. To display an overview of available commands type \'help\'.
  39. ''')
  40. allowed_commands = ['help',
  41. 'login',
  42. 'register',
  43. 'change_pw',
  44. 'news',
  45. 'tradables',
  46. 'depot',
  47. 'orders',
  48. 'orders_on',
  49. 'old_orders',
  50. 'trades',
  51. 'trades_on',
  52. 'buy',
  53. 'sell',
  54. 'cancel_order',
  55. 'gift',
  56. 'leaderboard',
  57. 'activate_key',
  58. 'exit']
  59. def one_command():
  60. cmd = input('*> ').strip()
  61. cmds = cmd.split(';')
  62. for cmd in cmds:
  63. cmd = cmd.split()
  64. # cmd = [cmd.strip() for cmd in cmd]
  65. # noinspection PySimplifyBooleanCheck
  66. if cmd == []:
  67. continue
  68. cmd[0] = cmd[0].lower()
  69. if cmd[0] not in allowed_commands:
  70. print('Invalid command:', cmd[0])
  71. else:
  72. method_to_call = getattr(client_controller, cmd[0])
  73. # noinspection PyBroadException
  74. try:
  75. method_to_call(*cmd[1:])
  76. except TypeError:
  77. print('Invalid command syntax.')
  78. except ConnectionError:
  79. print('There has been a problem connecting when to the server.')
  80. except Exception as _:
  81. print('An unknown error occurred while executing a command.')
  82. def exit_client():
  83. global exiting
  84. exiting = True
  85. if __name__ == '__main__':
  86. load()
  87. welcome()
  88. while not client_controller.exiting:
  89. one_command()