From 7daa5284f9eddf6d4b4e7838919e80ce25324bb0 Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Sun, 25 Sep 2022 00:10:13 +1000 Subject: Lots of changes! --- bot_server.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 bot_server.py (limited to 'bot_server.py') 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() -- cgit v1.2.3