run_client.py 3.8 KB

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