diff options
author | Carlo Zancanaro <carlo@zancanaro.id.au> | 2022-09-25 00:10:13 +1000 |
---|---|---|
committer | Carlo Zancanaro <carlo@zancanaro.id.au> | 2022-09-25 00:38:04 +1000 |
commit | 7daa5284f9eddf6d4b4e7838919e80ce25324bb0 (patch) | |
tree | ac92d9aa97621b4c314441a4cb7cdcab60b4f85e /bot_server.py | |
parent | e031af6e5e8324fe4cda66d9597904040b17ca80 (diff) |
Diffstat (limited to 'bot_server.py')
-rw-r--r-- | bot_server.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/bot_server.py b/bot_server.py new file mode 100644 index 0000000..6d8c72c --- /dev/null +++ b/bot_server.py @@ -0,0 +1,33 @@ +import importlib +import os +import robots +import sys + +if __name__ == '__main__': + if len(sys.argv) < 2: + print(f'Usage: python3 {sys.argv[0]} {{your-name}}') + print(f' your-name: your name, used to identify your server on the network') + print() + print(f'Run this command to host a server advertising all of the bots in the bots/ directory.') + sys.exit(1); + + name, = sys.argv[1:] + server = robots.Server() + server.SERVER_NAME = name + + num_bots = 0 + for filename in os.listdir('bots'): + if filename.endswith('.py'): + bot_name = filename.removesuffix('.py') + module = importlib.import_module(f'bots.{bot_name}') + try: + server.add_simple_bot(module.calculate_orders, bot_name) + num_bots += 1 + except AttributeError: + print(f'Warning: bots/{filename} was missing `calculate_orders` function.', file=sys.stderr) + + if num_bots == 0: + print(f'No bots found: not starting a bot server.', file=sys.stderr) + sys.exit(2) + else: + server.run() |