Beispiel #1
0
def start(args):
    """
    Starts the reporter

    :address:  Aggregator to connect to
    :hostname: Hostname
    """

    # Modules will be completely overwritten but that's as intended
    defaults = {
        'hostname': socket.gethostname(),
        'host'    : '127.0.0.1',
        'port'    : 13337,
        'modules' : {
            'Network': [
                {}
            ]
        }
    }

    # If no config file use dummy data
    if args.configFile:
        config = parseConfig(args.configFile, defaults = defaults)
    else:
        config = defaults

    if args.port:
        config["port"] = args.port
    if args.host:
        config["host"] = args.host
    if args.hostname:
        config['hostname'] = args.hostname

    # Initialize networking client
    client = Client((config["host"], config["port"]))

    # Create target function
    target = functools.partial(send_publish_socket, additional = {
        "host": config['hostname']
    }, client = client)

    # Initialize the publisher
    publisher = Publisher(target)

    # Load modules
    loadModulesFromConfig(config, publisher, moduleFinder)

    # Start client and publisher
    client.connect()
    publisher.start()

    # Wait until client finishes
    try:
        client.finished.wait()
    except KeyboardInterrupt:
        client.disconnect()
        client.finished.wait()
Beispiel #2
0
class BaseController(QObject):
    _instance = None

    def __init__(self, service_name):
        QObject.__init__(self)
        self.is_connecting = False
        self.is_connected = False
        self.service_name = service_name
        self.connector = SafeConnector()
        self.c = Client()

    def connect_client(self, adr, port):
        if not (self.is_connected or self.is_connecting):
            self.is_connecting = True
            self.c.connect(adr, port)  # will not return any error code
            # if ret == -1:
            #    self.is_connecting = False
            #    print_trace_exception()
            #    raise os.ConnectionError()
            self.is_connected = True
            self.is_connecting = False

    def get_client(self):
        return self.c
Beispiel #3
0
def setup():
    global screen, grid_size
    global client, doGameLoop
    global bothAI

    code = input("Enter game code (or blank to create a new game): ")

    # Connect to the server
    server = "127.0.0.1"
    if len(sys.argv) == 2:
        server = sys.argv[1]
    print(f"Connecting server at to {server}...")

    client = Client(server)
    try:
        joined = client.connect(code)
        if code == "":
            print(f"Created game {joined}")
    except Exception as ex:
        exit(f"Unable to connect to server: {ex}")

    pygame.init()
    pygame.display.set_caption(f"Place your fruit (game {joined})")

    #in game options will have a choice to change the grid size
    grid_size = (c.Drawing.SIZE +
                 c.Drawing.MARGIN) * c.Drawing.SQUARES + c.Drawing.MARGIN

    # Since the unplaced ships are on the right of the board, we need to add room for two columns of ships
    width = grid_size + c.Drawing.SIZE * 2 + 35
    screen = pygame.display.set_mode([width, grid_size])

    # Setup all unplaced ships (carrier, battleship, cruiser 1, cruiser 2, and patrol boat)
    # The first column only has room for two ships - the carrier (5) and the battleship (4)
    margin = c.Drawing.MARGIN + 15
    initial_x = grid_size + margin
    unplaced["carrier"] = [
        initial_x, c.Drawing.MARGIN, c.Drawing.SIZE, c.Drawing.SIZE * 5
    ]
    unplaced["battleship"] = [
        initial_x, unplaced["carrier"][3] + (c.Drawing.MARGIN * 3),
        c.Drawing.SIZE, c.Drawing.SIZE * 4
    ]

    # The second column can fit all of the other ships - the two cruisers (3) and the patrol boat (2)
    margin -= 5
    initial_x += c.Drawing.SIZE + margin
    unplaced["cruiser1"] = [
        initial_x, c.Drawing.MARGIN, c.Drawing.SIZE, c.Drawing.SIZE * 3
    ]
    unplaced["cruiser2"] = [
        initial_x, unplaced["cruiser1"][3] + (c.Drawing.MARGIN * 3),
        c.Drawing.SIZE, c.Drawing.SIZE * 3
    ]
    unplaced["patrol"] = [
        initial_x, unplaced["cruiser2"][3] + unplaced["cruiser2"][1] +
        (c.Drawing.MARGIN * 3), c.Drawing.SIZE, c.Drawing.SIZE * 2
    ]

    displayThread = threading.Thread(target=display)
    displayThread.start()

    # Check for updates from the server
    count = 2
    while doGameLoop:
        client.updateStats(opponentDecreased, weDecreased)

        if client.ready:
            # Count is here to avoid requesting updates before the AI is ready
            if count < 0:
                refreshGrid(True)
            else:
                count -= 1

        time.sleep(0.5)
Beispiel #4
0
              # 'buoy': upd_buoy
              }
    while run:
        update = connection.listen()    # update has form [property, value]
        if update is not None:
            switch[update[0]](update[1])
            print(update)
            draw_board()
    pass
    # listen for updates


########################################################
# make connection to server
n = Client()
clientID = n.connect('viewer')
# get initial game values
mapPath, boatList, buoyList, wind = n.send('get', 'initial')
# boats are received as [boatID, pos, heading, spinnaker, colour]
# buoys are received as [buoyID, pos, isFinish]
# create board
boardSize, terrain = create_map(mapPath)
# create flotilla
flotilla = {boat[0]: Boat(boat[1:5]) for boat in boatList}
buoys = {buoy[0]: Buoy(buoy[1:3]) for buoy in buoyList}

# initialise pygame window
pygame.init()
window = pygame.display.set_mode(tuple(i * (blockSize + borderSize) + borderSize for i in boardSize))
pygame.display.set_caption("boardViewer")
window.fill(colours['viewerBackground'])