run_client.py 3.7 KB

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