Example #1
0
 def __init__(self, ship_id=0, position=cCommonGame.Position(0, 0), heading=0, size=0, is_sunken=False, parent=None):
     cCommonGame.ShipInfo.__init__(self, ship_id, position, heading, size, is_sunken)
     QObject.__init__(self, parent)
     self.type = ShipInfo.Type.FRIENDLY
     self.y_center = size / 2
     if size % 2 == 0:
         self.y_center -= 1
Example #2
0
    def submit_button_pressed(self):
        # Collate move action(s)
        move_list = list()
        for widget in self.move_action_widgets:
            ship_id, action = widget.get_move_info()
            move_info = cCommonGame.ShipMovementInfo(ship_id, action)
            move_list.append(move_info)
            self.wos_interface.log(move_info.to_string())

        # Collate fire action(s)
        fire_list = list()
        for widget in self.fire_action_widgets:
            x, y = widget.get_fire_info()
            fire_info = cCommonGame.FireInfo(cCommonGame.Position(x, y))
            fire_list.append(fire_info)
            self.wos_interface.log(fire_info.to_string())

        self.wos_interface.log("Sending commands to server..")
        # Consider doing validations
        WosClientInterfaceManager().send_action_move(move_list)
        WosClientInterfaceManager().send_action_attack(fire_list)
        self.wos_interface.log("Sent")

        self.end_turn()

        self.update_timer.start()
Example #3
0
 def collect_orders(self):
     orders = list()
     for i in range(0, self.layout.waypoint_layout.count()):
         waypoint_widget = self.layout.waypoint_layout.itemAt(i).widget()
         waypoint_info = waypoint_widget.get_waypoint_info()
         orders.append(
             cCommonGame.UwActionMoveScan(
                 cCommonGame.Position(waypoint_info[0], waypoint_info[1]),
                 waypoint_info[2]))
     self.orders_issued.emit(orders)
Example #4
0
    def __init__(self,
                 field_info,
                 drag_boundary,
                 ship_id=0,
                 name='Underwater vehicle',
                 x=0,
                 y=0):
        WosBattlefieldItem.__init__(self, field_info)
        self.drag_boundary = drag_boundary

        self.ship_info = UwShipInfo(ship_id, cCommonGame.Position(0, 0))

        self.set_type(ItemType.UW_SHIP)
        self.ship_info.moved.connect(self.ship_moved)

        self.field_info = field_info
        self.name = name

        self.body = QRectF(1, 1,
                           self.field_info.size.x() - 2,
                           self.field_info.size.y() - 2)
        self.body_line = QRectF(6, 6,
                                self.field_info.size.x() - 12,
                                self.field_info.size.y() - 12)
        self.setRotation(30)

        self.brushes = dict()
        self.brushes[ShipInfo.Type.FRIENDLY] = QBrush(QColor(0, 200, 0, 168))
        self.brushes[ShipInfo.Type.HOSTILE] = QBrush(QColor(200, 0, 0, 168))
        self.brushes[ShipInfo.Type.CIVILIAN] = QBrush(
            QColor(200, 200, 200, 168))
        self.brushes[ShipInfo.Type.UNKNOWN] = QBrush(QColor(200, 200, 0, 168))
        self.brushes[ShipInfo.Type.SHADOW] = QBrush(QColor(0, 0, 0, 0))
        self.brushes_hover = dict()
        self.brushes_hover[ShipInfo.Type.FRIENDLY] = QBrush(
            QColor(0, 200, 0, 255))
        self.brushes_hover[ShipInfo.Type.HOSTILE] = QBrush(
            QColor(200, 0, 0, 255))
        self.brushes_hover[ShipInfo.Type.CIVILIAN] = QBrush(
            QColor(200, 200, 200, 255))
        self.brushes_hover[ShipInfo.Type.UNKNOWN] = QBrush(
            QColor(200, 200, 0, 255))
        self.brushes_hover[ShipInfo.Type.SHADOW] = QBrush(QColor(0, 0, 0, 0))
        self.brush = self.brushes[self.ship_info.type]
        self.pens = dict()
        self.pens[ShipInfo.Type.FRIENDLY] = QPen(QColor(0, 0, 0, 168))
        self.pens[ShipInfo.Type.HOSTILE] = QPen(QColor(0, 0, 0, 255))
        self.pens[ShipInfo.Type.CIVILIAN] = QPen(QColor(0, 0, 0, 255))
        self.pens[ShipInfo.Type.UNKNOWN] = QPen(QColor(0, 0, 0, 255))
        self.pens[ShipInfo.Type.SHADOW] = QPen(QColor(0, 255, 0, 255), 2,
                                               Qt.DashLine)
        self.pen = self.pens[self.ship_info.type]
Example #5
0
    def __init__(self,
                 field_info,
                 drag_boundary,
                 ship_id=0,
                 length=5,
                 is_sunken=False):
        WosBattlefieldItem.__init__(self, field_info)
        self.drag_boundary = drag_boundary

        self.ship_info = ShipInfo(ship_id, cCommonGame.Position(0, 0))
        self.ship_info.size = length
        self.ship_info.is_sunken = is_sunken
        self.ship_info.rotated.connect(self.ship_rotated)
        self.ship_info.moved.connect(self.ship_moved)

        self.set_type(ItemType.SHIP)
        WosItemDepthManager().set_depth(self)

        self.start_pos = QPointF()
        self.end_pos = QPointF()
        self.head = QPolygonF()
        self.tail = QPolygonF()
        self.body = QPolygonF()
        self.update_body()

        self.brushes = dict()
        self.brushes[ShipInfo.Type.FRIENDLY] = QBrush(QColor(0, 200, 0, 255))
        self.brushes[ShipInfo.Type.HOSTILE] = QBrush(QColor(200, 0, 0, 255))
        self.brushes[ShipInfo.Type.CIVILIAN] = QBrush(
            QColor(200, 200, 200, 255))
        self.brushes[ShipInfo.Type.UNKNOWN] = QBrush(QColor(200, 200, 0, 255))
        self.brushes[ShipInfo.Type.BLACK] = QBrush(QColor(255, 255, 255, 255))
        self.brushes[ShipInfo.Type.SHADOW] = QBrush(QColor(0, 0, 0, 0))
        self.pens = dict()
        self.pens[ShipInfo.Type.FRIENDLY] = QPen(QColor(0, 0, 0, 255))
        self.pens[ShipInfo.Type.HOSTILE] = QPen(QColor(0, 0, 0, 255))
        self.pens[ShipInfo.Type.CIVILIAN] = QPen(QColor(0, 0, 0, 255))
        self.pens[ShipInfo.Type.UNKNOWN] = QPen(QColor(0, 0, 0, 255))
        self.pens[ShipInfo.Type.BLACK] = QPen(QColor(0, 0, 0, 255))
        self.pens[ShipInfo.Type.SHADOW] = QPen(QColor(0, 255, 0, 255), 2,
                                               Qt.DashLine)
        self.pen = QPen(QColor(0, 0, 0, 255))
Example #6
0
def wos_test_client(num_of_rounds, player_id, boundary):

    # network setting
    addr_svr = "127.0.0.1"
    port_req = 5556
    port_sub = 5557
    req_rep_if = cCommonCommEngine.ConnInfo(addr_svr, port_req)
    pub_sub_if = cCommonCommEngine.ConnInfo(addr_svr, port_sub)

    print("Client %s: Creating CommEngine" % player_id)
    client_comm_engine = ClientCommEngine(player_id, req_rep_if, pub_sub_if,
                                          1000)

    print("Client %s: Starting CommEngine" % player_id)
    client_comm_engine.start()

    while not client_comm_engine.is_ready:
        print("Client %s: Waiting for CommEngine to be ready" % player_id)
        time.sleep(1)

    print("Client %s: READY..." % player_id)

    # map_x_len = 0
    # map_y_len = 0

    # Test 1: Register four client to get the data map
    test_reply = client_comm_engine.req_register()
    if isinstance(test_reply, cMessages.MsgRepAckMap):
        print(test_reply)
        assert (test_reply.ack is True)
        # map_x_len, map_y_len = np.shape(test_reply.map_data)
        print("Client %s: Recv Map Size: %s" %
              (player_id, np.shape(test_reply.map_data)))
    else:
        print("Incorrect Msg type : %s" % test_reply)
    assert (isinstance(test_reply, cMessages.MsgRepAckMap))

    # Test 2: Register the ships for the four client
    # Register 3x ship
    ship_list = []
    for ship_id in range(3):
        ship_info = cCommonGame.ShipInfo(
            ship_id=ship_id,
            ship_type=cCommonGame.ShipType.MIL,
            position=cCommonGame.Position(
                int(
                    np.random.random_integers(boundary[0][0] + 3,
                                              boundary[0][1] - 3)),
                int(
                    np.random.random_integers(boundary[1][0] + 3,
                                              boundary[1][1] - 3))),
            heading=0,
            size=3,
            is_sunken=False)
        if ship_id == 1:
            ship_info.position.x = boundary[0][0] + 0
            ship_info.position.y = boundary[1][0] + ((ship_info.size - 1) // 2)
            ship_info.heading = 0
        ship_list.append(ship_info)

    # Register 1x uw_ship
    uw_ship_list = []
    uw_ship_list.append(
        cCommon.UwShipInfo(ship_id=1,
                           position=cCommonGame.Position(
                               boundary[0][0], boundary[0][1])))

    test_reply = client_comm_engine.req_register_ships(
        ship_list=ship_list, uw_ship_list=uw_ship_list)
    if isinstance(test_reply, cMessages.MsgRepAck):
        print(test_reply)
        assert (test_reply.ack == True)
    else:
        print("Incorrect Msg type")
    assert (isinstance(test_reply, cMessages.MsgRepAck))

    # Test 3: Get the Game Configuration from the server
    game_config_list = dict()
    if isinstance(client_comm_engine, ClientCommEngine):
        test_reply = client_comm_engine.req_config()
        if isinstance(test_reply, cMessages.MsgRepGameConfig):
            print(test_reply)
            assert (test_reply.ack == True)
            game_config_list[player_id] = test_reply
        else:
            print("Incorrect Msg type")
        assert (isinstance(test_reply, cMessages.MsgRepGameConfig))

    game_status = client_comm_engine.recv_from_publisher()
    while game_status is None:
        print("game status is None")
        time.sleep(1)
        game_status = client_comm_engine.recv_from_publisher()

    while game_status.get_enum_game_state() == cCommonGame.GameState.INIT:
        print("game status is INIT")
        time.sleep(1)
        game_status = client_comm_engine.recv_from_publisher()

    round_offset = 0
    # # Test timeout for round 1
    # game_status = client_comm_engine.recv_from_publisher()
    # while game_status.game_round < 2:
    #     print("*** *** Round %s - Turn %s - Time remain %s" % (game_status.game_round,
    #                                                            game_status.player_turn,
    #                                                            game_status.time_remain))
    #     time.sleep(1)
    #     game_status = client_comm_engine.recv_from_publisher()
    # round_offset = round_offset + 1

    # Test 4: Test Game Play
    for rounds_cnt in range(num_of_rounds):

        # Wait until new round has started
        game_status = client_comm_engine.recv_from_publisher()
        while game_status.game_round == (rounds_cnt + round_offset) and \
                game_status.get_enum_game_state() != cCommonGame.GameState.STOP:
            game_status = client_comm_engine.recv_from_publisher()

        print("*** *** Round %s - Turn %s - Time remain %s" %
              (game_status.game_round, game_status.player_turn,
               game_status.time_remain))

        if isinstance(client_comm_engine, ClientCommEngine):
            # Test 4 i  : Get the Game Turn from the server
            test_reply = client_comm_engine.req_turn_info()
            if (game_status.get_enum_game_state() != cCommonGame.GameState.STOP) and \
                    isinstance(test_reply, cMessages.MsgRepTurnInfo):
                print("%s/%s - %s" %
                      (game_status.player_turn, player_id, test_reply))
                assert (test_reply.ack is True)
                # if game_status.player_turn == player_id:
                #     assert (test_reply.ack is True)
                # else:
                #     assert (test_reply.ack is False)
            elif (game_status.get_enum_game_state() == cCommonGame.GameState.STOP) and \
                    isinstance(test_reply, cMessages.MsgRepAck):
                print("%s/%s - %s" %
                      (game_status.player_turn, player_id, test_reply))
                assert (test_reply.ack is False)
            else:
                print("Incorrect Msg type: %s/%s - %s %s" %
                      (game_status.player_turn, player_id, test_reply,
                       type(test_reply)))

            if game_status.get_enum_game_state() != cCommonGame.GameState.STOP:
                assert (isinstance(test_reply, cMessages.MsgRepTurnInfo))
            else:
                assert (isinstance(test_reply, cMessages.MsgRepAck))

            # Test 4 ii : Request satcom action
            satcom_act = cCommonGame.SatcomInfo(6378 + 2000, 0, 5, 0, 150, 0,
                                                True, False)
            test_reply = client_comm_engine.req_action_satcom(satcom_act)
            if (game_status.get_enum_game_state() != cCommonGame.GameState.STOP) and \
                    isinstance(test_reply, cMessages.MsgRepAckMap):
                print("%s/%s - %s" %
                      (game_status.player_turn, player_id, test_reply))
                if game_config_list[player_id].config.en_satellite:
                    assert (test_reply.ack is True)
                else:
                    assert (test_reply.ack is False)
                # if (game_status.player_turn == player_id) and \
                #         (game_config_list[player_id].config.en_satellite):
                #     assert (test_reply.ack is True)
                # else:
                #     assert (test_reply.ack is False)
            elif (game_status.get_enum_game_state() == cCommonGame.GameState.STOP) and \
                 isinstance(test_reply, cMessages.MsgRepAck):
                print("%s/%s - %s" %
                      (game_status.player_turn, player_id, test_reply))
                assert (test_reply.ack is False)
            else:
                print("Incorrect Msg type: %s/%s - %s %s" %
                      (game_status.player_turn, player_id, test_reply,
                       type(test_reply)))

            if game_status.get_enum_game_state() != cCommonGame.GameState.STOP:
                assert (isinstance(test_reply, cMessages.MsgRepAckMap))
            else:
                assert (isinstance(test_reply, cMessages.MsgRepAck))

            # Test 4 iii: Get the Game Turn from the server with satcom
            test_reply = client_comm_engine.req_turn_info()
            if (game_status.get_enum_game_state() != cCommonGame.GameState.STOP) and \
                    isinstance(test_reply, cMessages.MsgRepTurnInfo):
                print("%s/%s - %s" %
                      (game_status.player_turn, player_id, test_reply))
                assert (test_reply.ack is True)
                # if game_status.player_turn == player_id:
                #     assert (test_reply.ack is True)
                # else:
                #     assert (test_reply.ack is False)
            elif (game_status.get_enum_game_state() == cCommonGame.GameState.STOP) and \
                    isinstance(test_reply, cMessages.MsgRepAck):
                print("%s/%s - %s" %
                      (game_status.player_turn, player_id, test_reply))
                assert (test_reply.ack is False)
            else:
                print("Incorrect Msg type: %s/%s - %s %s" %
                      (game_status.player_turn, player_id, test_reply,
                       type(test_reply)))

            if game_status.get_enum_game_state() != cCommonGame.GameState.STOP:
                assert (isinstance(test_reply, cMessages.MsgRepTurnInfo))
            else:
                assert (isinstance(test_reply, cMessages.MsgRepAck))

            # Test 4 iv : Request uw report / action
            test_reply = client_comm_engine.req_uw_report(ship_id=1)
            if isinstance(test_reply, cMessages.MsgRepUwReport):
                print("uw_report: %s/%s - %s" %
                      (game_status.player_turn, player_id, test_reply))
            elif (game_status.get_enum_game_state() == cCommonGame.GameState.STOP) and \
                    isinstance(test_reply, cMessages.MsgRepAck):
                print("uw_report: %s/%s - %s" %
                      (game_status.player_turn, player_id, test_reply))
            else:
                print("uw_report: Incorrect Msg type: %s/%s - %s %s" %
                      (game_status.player_turn, player_id, test_reply,
                       type(test_reply)))
            if game_status.get_enum_game_state() != cCommonGame.GameState.STOP:
                assert (isinstance(test_reply, cMessages.MsgRepUwReport))
            else:
                assert (isinstance(test_reply, cMessages.MsgRepAck))

            if isinstance(
                    test_reply,
                    cMessages.MsgRepUwReport) and (test_reply.ack is True):
                # send new order to the server
                ship_1_uw_action_list = list()
                ship_1_uw_action_list.append(
                    cCommonGame.UwActionMoveScan(goto_pos=cCommonGame.Position(
                        int((boundary[0][0] + boundary[1][0]) // 2),
                        int((boundary[1][0] + boundary[1][1]) // 2)),
                                                 scan_dur=10))
                # ship_2_uw_action_list = list()
                # ship_2_uw_action_list.append(cCommonGame.UwActionMoveScan(
                #     goto_pos=cCommonGame.Position(int((boundary[0][0] + boundary[1][0]) // 2),
                #                                   int((boundary[1][0] + boundary[1][1]) // 2)),
                #     scan_dur=10))
                uw_move_info_list = list()
                uw_move_info_list.append(
                    cCommonGame.UwShipMovementInfo(
                        ship_id=1, actions=ship_1_uw_action_list))
                # uw_move_info_list.append(cCommonGame.UwShipMovementInfo(ship_id=2,
                #                                                         actions=ship_2_uw_action_list))
                test_reply = client_comm_engine.req_action_uw_ops(
                    uw_move_info_list)
                if isinstance(test_reply, cMessages.MsgRepAck):
                    print("uw_ops_action : player_id: %s - %s" %
                          (player_id, test_reply.ack))
                else:
                    print("uw_ops_action : %s" % test_reply)
                assert (isinstance(test_reply, cMessages.MsgRepAck))
            # else do nothing

            # Test 4  v : Request move action
            move_list = list()
            move_list.append(
                cCommonGame.ShipMovementInfo(1, cCommonGame.Action.FWD))
            move_list.append(
                cCommonGame.ShipMovementInfo(1, cCommonGame.Action.CW))
            test_reply = client_comm_engine.req_action_move(move_list)
            if isinstance(test_reply, cMessages.MsgRepAck):
                print("%s/%s - %s" %
                      (game_status.player_turn, player_id, test_reply))
                if game_status.get_enum_game_state(
                ) != cCommonGame.GameState.STOP:
                    assert (test_reply.ack is True)
                else:
                    assert (test_reply.ack is False)
                # if game_status.get_enum_game_state() != cCommonGame.GameState.STOP and \
                #         game_status.player_turn == player_id:
                #     assert (test_reply.ack is True)
                # else:
                #     assert (test_reply.ack is False)
            else:
                print("Incorrect Msg type: %s/%s - %s %s" %
                      (game_status.player_turn, player_id, test_reply,
                       type(test_reply)))

            assert (isinstance(test_reply, cMessages.MsgRepAck))

            # Test 4 vi : Request fire action
            fire_list = list()
            fire_list.append(cCommonGame.FireInfo(cCommonGame.Position(0, 0)))
            test_reply = client_comm_engine.req_action_fire(fire_list)
            if isinstance(test_reply, cMessages.MsgRepAck):
                print("%s/%s - %s" %
                      (game_status.player_turn, player_id, test_reply))
                if game_status.get_enum_game_state(
                ) != cCommonGame.GameState.STOP:
                    assert (test_reply.ack is True)
                else:
                    assert (test_reply.ack is False)
                # if game_status.get_enum_game_state() != cCommonGame.GameState.STOP and \
                #         game_status.player_turn == player_id:
                #     assert (test_reply.ack is True)
                # else:
                #     assert (test_reply.ack is False)
            else:
                print("%s/%s - %s" %
                      (game_status.player_turn, player_id, test_reply))
                print("Incorrect Msg type")

            assert (isinstance(test_reply, cMessages.MsgRepAck))

    # Teardown
    client_comm_engine.stop()

    pass
Example #7
0
 def __init__(self, ship_id=0, position=cCommonGame.Position(0, 0), size=0, is_sunken=False, parent=None):
     cCommonGame.UwShipInfo.__init__(self, ship_id, position, size, is_sunken)
     QObject.__init__(self, parent)
     self.type = ShipInfo.Type.FRIENDLY
Example #8
0
def test_scenerio_1():
    ###
    """
    Scenario:
    ----------
        Ship position:
        --------------
        (1,1) size:3, heading=0
        (5,2) size:3, heading=0

        Movement:
        ---------
        1) Start at position (0, 0) :               : Turn 0
        2) Move to  position (2, 2) : Take 3 turn   : Turn 0
        3) Scan for 2 turn          : Take 2 turn   : Turn 3
        4) Move to  position (2, 4) : Take 2 turn   : Turn 5
        5) Scan for 2 turn          : Take 2 turn   : Turn 7
        6) Move to  position (0, 4) : Take 2 turn   : Turn 9
        7) Move to  position (0, 0) : Take 4 turn   : Turn 11
        8) Scan for 5 turn          : Take 5 turn   : Turn 15
        9) End                      :               : Turn 20
    """

    # set the position of the ship
    uw_ship = wosBattleshipServer.cCommon.UwShipInfo(
        ship_id=0,
        position=cCommonGame.Position(0, 0),
        size=1,
        is_sunken=False,
        ship_type=cCommonGame.ShipType.MIL,
        mov_speed=1,
        scan_size=3)

    print("Is ship idle: %s" % uw_ship.is_idle())

    # set the order for the ship
    orders = list()
    orders.append(cCommonGame.UwActionMoveScan(cCommonGame.Position(2, 2), 2))
    orders.append(cCommonGame.UwActionMoveScan(cCommonGame.Position(2, 4), 2))
    orders.append(cCommonGame.UwActionMoveScan(cCommonGame.Position(0, 4), 0))
    orders.append(cCommonGame.UwActionMoveScan(cCommonGame.Position(0, 0), 0))
    orders.append(cCommonGame.UwActionMoveScan(None, 5))
    orders.append(cCommonGame.UwActionMoveScan())

    # plant the ship for testing
    ships_list = list()
    ships_list.append(
        cCommonGame.ShipInfo(ship_id=1,
                             position=cCommonGame.Position(1, 1),
                             heading=0,
                             size=3,
                             ship_type=cCommonGame.ShipType.CIV,
                             is_sunken=False))
    ships_list.append(
        cCommonGame.ShipInfo(ship_id=2,
                             position=cCommonGame.Position(5, 2),
                             heading=0,
                             size=3,
                             ship_type=cCommonGame.ShipType.CIV,
                             is_sunken=False))

    if len(orders) > 0:
        print("Set orders")
        uw_ship.set_ops_order(orders)

    print("Is ship idle: %s" % uw_ship.is_idle())

    # execute the order
    print("Executing")
    counter = 0
    while not uw_ship.is_idle():
        uw_ship.execute(ships_list)
        print("--- %s ---" % counter)
        counter += 1
        print("pos: x=%s, y=%s" % (uw_ship.position.x, uw_ship.position.y))
        print("\tscan=%s" % ((uw_ship.remain_scan_ops > 0) and
                             (uw_ship.remain_move_ops == 0)))
        print("\tremaining: move=%s, scan=%s, order=%s" %
              (uw_ship.remain_move_ops, uw_ship.remain_scan_ops,
               len(uw_ship.orders)))
        print("\treport size=%s" % len(uw_ship.report))
        print("\tIs ship idle: %s" % uw_ship.is_idle())
        # uw_ship.execute()
    print("ship pos: x=%s, y=%s" % (uw_ship.position.x, uw_ship.position.y))

    print("*** Get the report ***")
    uw_ship_report = uw_ship.get_report()

    # print the report
    print("Report:")
    print("\tnum of row: %s " % len(uw_ship_report))
    for uw_ship_report_data in uw_ship_report:
        if isinstance(uw_ship_report, cCommonGame.UwDetectInfo):
            print("\tDist: %s   Ship: %s" %
                  (uw_ship_report_data.dist, uw_ship_report_data.ship_info))
    assert (len(uw_ship_report) is 20)

    # convert the ship report
    processed_report = uw_compute(uw_ship_report, False)

    # display the processed report
    plot_generated_uw_data(processed_report, "test_scenerio_1_1", True)
    plot_generated_uw_data_as_img(processed_report, "test_scenerio_1_2", True)
Example #9
0
def test_scenerio_5():
    ###
    """
    Scenario:
    ----------
        Ship position:
        --------------
        (5,5) size:3, heading=90
        (3,3) size:3, heading=0

        Movement:
        ---------
        1) Start at position (0, 0) :               : Turn 0
        2) Move to  position (60, 0): Take 6 turn   : Turn 0
        3) Scan for 1 turn          : Take 1 turn   : Turn 6
        4) Do nothing for 3 turns   : Take 3 turn   : Turn 7
        5) End                      :               : Turn 7
    """

    # set the position of the ship
    uw_ship = wosBattleshipServer.cCommon.UwShipInfo(
        ship_id=0,
        position=cCommonGame.Position(0, 0),
        size=1,
        is_sunken=False,
        ship_type=cCommonGame.ShipType.MIL,
        mov_speed=5,
        scan_size=3)

    print("Is ship idle: %s" % uw_ship.is_idle())

    # set the order for the ship
    orders = list()
    orders.append(cCommonGame.UwActionMoveScan(cCommonGame.Position(60, 0), 0))
    # orders.append(cCommonGame.UwActionMoveScan(None, 5))
    #orders.append(cCommonGame.UwActionMoveScan())

    # plant the ship for testing
    ships_list = list()
    # ships_list.append(cCommonGame.ShipInfo(ship_id=1,
    #                                        position=cCommonGame.Position(2, 0),
    #                                        heading=270,
    #                                        size=5,
    #                                        ship_type=cCommonGame.ShipType.MIL,
    #                                        is_sunken=False))
    # ships_list.append(cCommonGame.ShipInfo(ship_id=2,
    #                                        position=cCommonGame.Position(2, 3),
    #                                        heading=270,
    #                                        size=5,
    #                                        ship_type=cCommonGame.ShipType.MIL,
    #                                        is_sunken=False))
    # ships_list.append(cCommonGame.ShipInfo(ship_id=3,
    #                                        position=cCommonGame.Position(0, 1),
    #                                        heading=0,
    #                                        size=2,
    #                                        ship_type=cCommonGame.ShipType.MIL,
    #                                        is_sunken=False))
    # ships_list.append(cCommonGame.ShipInfo(ship_id=4,
    #                                        position=cCommonGame.Position(5, 1),
    #                                        heading=0,
    #                                        size=4,
    #                                        ship_type=cCommonGame.ShipType.MIL,
    #                                        is_sunken=False))

    if len(orders) > 0:
        print("Set orders")
        uw_ship.set_ops_order(orders)

    print("Is ship idle: %s" % uw_ship.is_idle())

    # execute the order
    print("Executing")

    for counter in range(65):
        uw_ship.execute(ships_list)
        print("--- %s ---" % counter)
        print("pos: x=%s, y=%s" % (uw_ship.position.x, uw_ship.position.y))
        # print("\tscan=%s" % ((uw_ship.remain_scan_ops > 0) and
        #                      (uw_ship.remain_move_ops == 0)))
        print("\tremaining: move=%s, scan=%s, order=%s" %
              (uw_ship.remain_move_ops, uw_ship.remain_scan_ops,
               len(uw_ship.orders)))
        print("\treport size=%s" % len(uw_ship.report))
        print("\tIs ship idle: %s" % uw_ship.is_idle())
        # uw_ship.execute()
    print("ship pos: x=%s, y=%s" % (uw_ship.position.x, uw_ship.position.y))

    print("*** Get the report ***")
    uw_ship_report = uw_ship.get_report()

    # print the report
    print("Report:")
    print("\tnum of row: %s " % len(uw_ship_report))
    for uw_ship_report_data in uw_ship_report:
        if isinstance(uw_ship_report, cCommonGame.UwDetectInfo):
            print("\tDist: %s   Ship: %s" %
                  (uw_ship_report_data.dist, uw_ship_report_data.ship_info))
    # assert (len(uw_ship_report) is 7)

    # convert the ship report
    processed_report = uw_compute(uw_ship_report, False)

    # display the processed report
    plot_generated_uw_data(processed_report, "test_scenerio_2_1", True)
    plot_generated_uw_data_as_img(processed_report, "test_scenerio_2_2", True)
Example #10
0
import json
import numpy as np

import cMessages
import cCommonGame

from cMessages import MsgJsonEncoder, MsgJsonDecoder
from cMessages import MsgJsonDecoder

print("Position:")
a = cCommonGame.Position(1, 2)
b = MsgJsonEncoder().encode(a)
c = json.dumps(a, cls=cMessages.MsgJsonEncoder)
print("Encoder: %s" % b)
print("JSON  : %s" % c)
print("Decoder: %s" % MsgJsonDecoder().decode(b).__dict__)

print("Size:")
a = cCommonGame.Size(1, 2)
b = MsgJsonEncoder().encode(a)
c = json.dumps(a, cls=cMessages.MsgJsonEncoder)
print("Encoder: %s" % b)
print("JSON  : %s" % c)
print("Decoder: %s" % MsgJsonDecoder().decode(b).__dict__)

print("Boundary:")
a = cCommonGame.Boundary(0, 12, 12, 24)
b = MsgJsonEncoder().encode(a)
c = json.dumps(a, cls=cMessages.MsgJsonEncoder)
print("Encoder: %s" % b)
print("JSON  : %s" % c)
Example #11
0
 def parse_position(obj):
     return cCommonGame.Position(obj['x'], obj['y'])
Example #12
0
    def test_scenario_01(self):
        ###
        """
        Scenario:
            1) Start at position (0, 0) :               : Turn 0
            2) Move to  position (2, 2) : Take 3 turn   : Turn 0
            3) Scan for 2 turn          : Take 2 turn   : Turn 3
            4) Move to  position (2, 4) : Take 2 turn   : Turn 5
            5) Scan for 2 turn          : Take 2 turn   : Turn 7
            6) Move to  position (0, 4) : Take 2 turn   : Turn 9
            7) Move to  position (0, 0) : Take 4 turn   : Turn 11
            8) Scan for 5 turn          : Take 5 turn   : Turn 15
            9) End                      :               : Turn 20
        """

        # set the position of the ship
        uw_ship = wosBattleshipServer.cCommon.UwShipInfo(ship_id=0,
                                                         position=cCommonGame.Position(0, 0),
                                                         size=1,
                                                         is_sunken=False,
                                                         ship_type=cCommonGame.ShipType.MIL,
                                                         mov_speed=1,
                                                         scan_size=3)

        print("Is ship idle: %s" % uw_ship.is_idle())

        # set the order for the ship
        orders = list()
        orders.append(cCommonGame.UwActionMoveScan(cCommonGame.Position(2, 2), 2))
        orders.append(cCommonGame.UwActionMoveScan(cCommonGame.Position(2, 4), 2))
        orders.append(cCommonGame.UwActionMoveScan(cCommonGame.Position(0, 4), 0))
        orders.append(cCommonGame.UwActionMoveScan(cCommonGame.Position(0, 0), 0))
        orders.append(cCommonGame.UwActionMoveScan(None, 5))
        orders.append(cCommonGame.UwActionMoveScan())

        # plant the ship for testing
        # todo:...

        if len(orders) > 0:
            print("Set orders")
            uw_ship.set_ops_order(orders)

        print("Is ship idle: %s" % uw_ship.is_idle())

        # execute the order
        print("Executing")
        # counter = 0
        while not uw_ship.is_idle():
            uw_ship.execute()
            # print("--- %s ---" % counter)
            # counter += 1
            # print("pos: x=%s, y=%s" % (uw_ship.position.x,
            #                            uw_ship.position.y))
            # print("\tscan=%s" % ((uw_ship.remain_scan_ops > 0) and
            #                      (uw_ship.remain_move_ops == 0)))
            # print("\tremaining: move=%s, scan=%s, order=%s" %
            #       (uw_ship.remain_move_ops,
            #        uw_ship.remain_scan_ops,
            #        len(uw_ship.orders)))
            # print("\treport size=%s" % len(uw_ship.report))
            # print("\tIs ship idle: %s" % uw_ship.is_idle())
            # # uw_ship.execute()
        print("ship pos: x=%s, y=%s" % (uw_ship.position.x, uw_ship.position.y))

        print("*** Get the report ***")
        uw_ship_report = uw_ship.get_report()

        # print the report
        print("Report:")
        print("\tnum of row: %s " % len(uw_ship_report))
        for uw_ship_report_data in uw_ship_report:
            if isinstance(uw_ship_report, cCommonGame.UwDetectInfo):
                print("\tDist: %s   Ship: %s" % (uw_ship_report_data.dist, uw_ship_report_data.ship_info))

        # convert the ship report
        processed_report = uw_compute(uw_ship_report, False)

        # check if the required ship is in the report
        self.assertEqual(len(uw_ship_report), 20)
    def start(self):
        rep = WosClientInterfaceManager().register_player()
        if not rep and not self.wos_interface.is_debug:
            return

        self.wos_interface.log("<b>Deployment phase</b>.")
        self.wos_interface.log(
            "Please assign the ships to a desired location in the map. Click and drag the ships to "
            "reposition with left mouse button, rotate the ships with right mouse button."
        )

        cfg = self.wos_interface.cfg
        self.wos_interface.battlefield.battle_scene.update_boundaries(
            cfg.boundary)
        player_id = str(self.wos_interface.player_info.player_id)
        player_boundary = self.wos_interface.cfg.boundary[player_id]

        if rep:
            self.wos_interface.battlefield.update_map(rep.map_data)

        field_info = self.wos_interface.battlefield.battle_scene.get_field_info(
        )

        # todo: Read config file for squad list, stub for now
        self.ships_items = [
            WosBattleShipItem(field_info, player_boundary, 0, 2),
            WosBattleShipItem(field_info, player_boundary, 1, 2),
            WosBattleShipItem(field_info, player_boundary, 2, 3),
            WosBattleShipItem(field_info, player_boundary, 3, 3),
            WosBattleShipItem(field_info, player_boundary, 4, 4),
            WosBattleShipItem(field_info, player_boundary, 5, 4),
            WosBattleShipItem(field_info, player_boundary, 6, 5),
            WosBattleShipItem(field_info, player_boundary, 7, 5)
        ]
        if self.wos_interface.cfg.en_submarine:
            self.ship_uw_items = [
                WosUnderwaterShipItem(field_info, player_boundary, 8)
            ]

        scene = self.wos_interface.battlefield.battle_scene.scene()
        start_position = cCommonGame.Position()
        start_position.x = player_boundary.min_x
        start_position.y = player_boundary.min_y
        cnt = 0
        for i in range(0, len(self.ships_items)):
            scene.addItem(self.ships_items[i])
            self.ships_items[i].set_ship_type(ShipInfo.Type.FRIENDLY)
            # self.ships_items[i].set_grid_position(start_position.x + i, start_position.y + 2)
            self.set_ship_position(self.ships_items[i])
            cnt += 1
        for i in range(0, len(self.ship_uw_items)):
            scene.addItem(self.ship_uw_items[i])
            self.ship_uw_items[i].set_ship_type(ShipInfo.Type.FRIENDLY)
            # self.ship_uw_items[i].set_grid_position(start_position.x + cnt, start_position.y + 2)
            self.set_ship_position(self.ship_uw_items[i])
            cnt += 1

        self.wos_interface.battlefield.battle_scene.centerOn(
            self.ships_items[-1])

        self.update_action_widget()

        self.deployment_ended.connect(self.send_deployment)
Example #14
0
def wos_test_client(
    client_comm_engine,
    game_state,
    round_count,
):

    if isinstance(client_comm_engine, ClientCommEngine) and \
            isinstance(game_state, cCommonGame.GameState):

        if game_state is cCommonGame.GameState.INIT:
            # Register the ships for the player
            ship_list = []

            # Ship id 0
            ship_info = copy.deepcopy(
                cCommonGame.ShipInfo(ship_id=0,
                                     ship_type=cCommonGame.ShipType.MIL,
                                     position=cCommonGame.Position(0, 0),
                                     heading=0,
                                     size=3,
                                     is_sunken=False))
            ship_list.append(ship_info)

            # Ship id 1
            ship_info = copy.deepcopy(
                cCommonGame.ShipInfo(ship_id=1,
                                     ship_type=cCommonGame.ShipType.MIL,
                                     position=cCommonGame.Position(0, 0),
                                     heading=0,
                                     size=3,
                                     is_sunken=False))
            ship_list.append(ship_info)

            # Ship id 2
            ship_info = copy.deepcopy(
                cCommonGame.ShipInfo(ship_id=2,
                                     ship_type=cCommonGame.ShipType.MIL,
                                     position=cCommonGame.Position(0, 0),
                                     heading=0,
                                     size=3,
                                     is_sunken=False))
            ship_list.append(ship_info)

            # Ship id 3
            ship_info = copy.deepcopy(
                cCommonGame.ShipInfo(ship_id=3,
                                     ship_type=cCommonGame.ShipType.MIL,
                                     position=cCommonGame.Position(0, 0),
                                     heading=0,
                                     size=3,
                                     is_sunken=False))
            ship_list.append(ship_info)

            # Ship id 4
            ship_info = copy.deepcopy(
                cCommonGame.ShipInfo(ship_id=4,
                                     ship_type=cCommonGame.ShipType.MIL,
                                     position=cCommonGame.Position(0, 0),
                                     heading=0,
                                     size=3,
                                     is_sunken=False))
            ship_list.append(ship_info)

            # Ship id 5
            ship_info = copy.deepcopy(
                cCommonGame.ShipInfo(ship_id=5,
                                     ship_type=cCommonGame.ShipType.MIL,
                                     position=cCommonGame.Position(0, 0),
                                     heading=0,
                                     size=3,
                                     is_sunken=False))
            ship_list.append(ship_info)

            # Ship id 6
            ship_info = copy.deepcopy(
                cCommonGame.ShipInfo(ship_id=6,
                                     ship_type=cCommonGame.ShipType.MIL,
                                     position=cCommonGame.Position(0, 0),
                                     heading=0,
                                     size=3,
                                     is_sunken=False))
            ship_list.append(ship_info)

            test_reply = client_comm_engine.req_register_ships(
                ship_list=ship_list)
            if isinstance(test_reply, cMessages.MsgRepAck):
                print("Is registration ok: %s" % test_reply.ack)
            else:
                print("Incorrect Msg type")

        elif game_state is cCommonGame.GameState.PLAY_INPUT:
            if round_count is 1:
                pass
                # Perform Satcom action

                # Perform Ship movement

                # Perform Fire action

            elif round_count is 2:
                pass
                # Perform Satcom action

                # Perform Ship movement

                # Perform Fire action

            elif round_count is 3:
                pass
                # Perform Satcom action

                # Perform Ship movement

                # Perform Fire action

            elif round_count is 4:
                pass
                # Perform Satcom action

                # Perform Ship movement

                # Perform Fire action

            elif round_count is 5:
                pass
                # Perform Satcom action

                # Perform Ship movement

                # Perform Fire action

            elif round_count is 6:
                pass
                # Perform Satcom action

                # Perform Ship movement

                # Perform Fire action

            elif round_count is 7:
                pass
                # Perform Satcom action

                # Perform Ship movement

                # Perform Fire action

            elif round_count is 8:
                pass
                # Perform Satcom action

                # Perform Ship movement

                # Perform Fire action

            elif round_count is 9:
                pass
                # Perform Satcom action

                # Perform Ship movement

                # Perform Fire action

            elif round_count is 10:
                pass
                # Perform Satcom action

                # Perform Ship movement

                # Perform Fire action

            elif round_count is 11:
                pass
                # Perform Satcom action

                # Perform Ship movement

                # Perform Fire action

            elif round_count is 12:
                pass
                # Perform Satcom action

                # Perform Ship movement

                # Perform Fire action

            elif round_count is 13:
                pass
                # Perform Satcom action

                # Perform Ship movement

                # Perform Fire action

            elif round_count is 14:
                pass
                # Perform Satcom action

                # Perform Ship movement

                # Perform Fire action

            elif round_count is 15:
                pass
                # Perform Satcom action

                # Perform Ship movement

                # Perform Fire action

            else:
                pass

        elif game_state is cCommonGame.GameState.PLAY_COMPUTE:
            # do nothing
            print("Server is processing...")
            time.sleep(2)

        elif game_state is cCommonGame.GameState.STOP:
            # do nothing
            print("Game has ended...")
            time.sleep(1)

    else:
        print("Unexpected input parameters")