1
1

run_client.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. 'activate_key',
  49. 'buy',
  50. 'sell',
  51. 'transactions',
  52. 'orders_on',
  53. 'cancel_order',
  54. 'gift',
  55. 'leaderboard',
  56. 'exit']
  57. def one_command():
  58. cmd = input('*> ').strip()
  59. cmds = cmd.split(';')
  60. for cmd in cmds:
  61. cmd = cmd.split()
  62. # cmd = [cmd.strip() for cmd in cmd]
  63. # noinspection PySimplifyBooleanCheck
  64. if cmd == []:
  65. continue
  66. cmd[0] = cmd[0].lower()
  67. if cmd[0] not in allowed_commands:
  68. print('Invalid command:', cmd[0])
  69. else:
  70. method_to_call = getattr(client_controller, cmd[0])
  71. # noinspection PyBroadException
  72. try:
  73. method_to_call(*cmd[1:])
  74. except TypeError:
  75. print('Invalid command syntax.')
  76. except Exception:
  77. print('An error occurred while executing a command.')
  78. def exit_client():
  79. global exiting
  80. exiting = True
  81. if __name__ == '__main__':
  82. load()
  83. welcome()
  84. while not client_controller.exiting:
  85. one_command()