summaryrefslogtreecommitdiff
path: root/bot_server.py
blob: 6d8c72cf7982176bbb6c96400787d84a1a37fcfb (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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()