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()