def test_speed(): try: process_client = RemoteProcessClient('wgforge-srv.wargaming.net', 443) player = process_client.login("Test12345") map_graph = process_client.read_map() map_graph.pos = dict( [(cord['idx'], (cord['x'] / 200, cord['y'] / 200)) for cord in process_client.read_position()["coordinate"]]) objects = process_client.read_objects() process_client.update_objects(objects, map_graph, player) process_client.move(Move(1, 1, 1)) process_client.turn() process_client.turn() process_client.update_objects(objects, map_graph, player) train = objects.trains[1] print(" pos - ", train.position, " speed - ", train.speed) process_client.move(Move(1, 0, 1)) process_client.move(Move(1, -1, 1)) process_client.turn() process_client.update_objects(objects, map_graph, player) print(" pos - ", train.position, " speed - ", train.speed) finally: process_client.logout() process_client.close()
def testGameStatus(self): self.assertEqual(self.controller.gameStatus(), 0) self.repository.addMove(Move("X", 1, 1)) self.repository.addMove(Move("X", 2, 2)) self.repository.addMove(Move("X", 3, 3)) self.repository.addMove(Move("X", 4, 4)) self.assertEqual(self.controller.gameStatus(), 1)
def run(self): try: self.remote_process_client.write_token_message(self.token) team_size = self.remote_process_client.read_team_size_message() self.remote_process_client.write_protocol_version_message() game = self.remote_process_client.read_game_context_message() strategies = [] for _ in xrange(team_size): strategies.append(MyStrategy()) while True: player_context = self.remote_process_client.read_player_context_message( ) if player_context is None: break player_cars = player_context.cars if player_cars is None or player_cars.__len__() != team_size: break moves = [] for car_index in xrange(team_size): player_car = player_cars[car_index] move = Move() moves.append(move) strategies[player_car.teammate_index].move( player_car, player_context.world, game, move) self.remote_process_client.write_moves_message(moves) finally: self.remote_process_client.close()
def testAddMove(self): move = Move("X", 0, 0) self.moveRepository.addMove(move) self.assertEqual(self.moveRepository.getMove(0, 0), move) self.assertEqual(self.moveRepository.getMove(1, 1), None) with self.assertRaises(DuplicateMoveError): self.moveRepository.addMove(move)
def run(self): try: self.remote_process_client.write_token_message(self.token) self.remote_process_client.write_protocol_version_message() self.remote_process_client.read_team_size_message() game = self.remote_process_client.read_game_context_message() strategy = MyStrategy() while True: player_context = self.remote_process_client.read_player_context_message( ) if player_context is None: break player = player_context.player if player is None: break move = Move() strategy.move(player, player_context.world, game, move) self.remote_process_client.write_move_message(move) finally: self.remote_process_client.close()
def run(self, pid=None): try: self.remote_process_client.write_token_message(self.token) self.remote_process_client.write_protocol_version_message() team_size = self.remote_process_client.read_team_size_message() game = self.remote_process_client.read_game_context_message() strategies = [] for _ in range(team_size): strategies.append(MyStrategy()) while True: player_context = self.remote_process_client.read_player_context_message() if player_context is None: break player_wizards = player_context.wizards if player_wizards is None or player_wizards.__len__() != team_size: break moves = [] for wizard_index in range(team_size): player_wizard = player_wizards[wizard_index] move = Move() moves.append(move) strategies[wizard_index].move(player_wizard, player_context.world, game, move) self.remote_process_client.write_moves_message(moves) finally: self.remote_process_client.close() if pid: os.kill(pid, signal.SIGTERM)
def get_moves(self): self.update_targets() moves = [] trains_targets = dict() for train_id in self.trains_points: train = self.objects.trains[train_id] if train.point: trains_targets[train.idx] = (train.point, self.trains_points[train.idx][0]) print(trains_targets) self.solver = TrafficController() paths = self.solver.find_paths(self.map, self.objects.trains, trains_targets) for train_id in paths: path = paths[train_id] if path: next_step = path[1] print("move {}, {}".format(self.objects.trains[train_id].point, next_step)) move_obj = self._move_to_point(self.objects.trains[train_id], next_step) moves.append(move_obj) else: train = self.objects.trains[train_id] moves.append(Move(train.line_idx, 0, train.idx)) # moves = self.step() # print(move_obj) if moves: return moves
def new_move(self, action=None, group=0, x_range=(0.0, WORLD_WIDTH), y_range=(0.0, WORLD_HEIGHT), vector=(0.0, 0.0), angle=0.0, factor=0.0, max_speed=0.0, max_angular_speed=0.0, vehicle_type=None, facility_id=-1, vehicle_id=-1): m = Move() m.action = action m.group = group m.left, m.right = x_range m.top, m.bottom = y_range m.x, m.y = vector m.angle = angle m.factor = factor m.max_speed = max_speed m.max_angular_speed = max_angular_speed m.vehicle_type = vehicle_type m.facility_id = facility_id m.vehicle_id = vehicle_id return m
def run(self): try: self.remote_process_client.write_token(self.token) team_size = self.remote_process_client.read_team_size() strategies = [] tank_types = [] for strategy_index in range(team_size): strategy = MyStrategy() strategies.append(strategy) tank_types.append(strategy.select_tank(strategy_index, team_size)) self.remote_process_client.write_selected_tanks(tank_types) while True: player_context = self.remote_process_client.read_player_context() if player_context is None: break player_tanks = player_context.tanks if player_tanks.__len__() != team_size: break moves = [] for strategy_index in range(team_size): move = Move() moves.append(move) strategies[strategy_index].move(player_tanks[strategy_index], player_context.world, move) self.remote_process_client.write_moves(moves) finally: self.remote_process_client.close()
def get_move(self, train: Train, map_graph: Map): if train.speed == 0: train.arrival() if train.position is None: # Если поезд ещё не начинал путь train.departure(self.home, map_graph.get_first_neighbor(self.home) ) # Отправляем его к первому соседнему городу line = map_graph.get_line(train.departure_point, train.arrival_point) return Move(line, 1, train.idx) if train.current_point != self.home: # Если поезд прибыл не домой train.departure(train.current_point, self.home) # Отправляем поезд домой line = map_graph.get_line(train.departure_point, train.arrival_point) return Move(line, 1, train.idx) self.in_progress = False
def test_wrong_move_from_node(self): self.train.position = 10 self.train.line_idx = 1 self.process_client.turn_trains() move = Move(3, 1, self.train.idx) self.process_client.move(move) assert self.train.speed == 0 assert self.train.line_idx == 1
def test_right_move_from_line(self): self.train.position = 5 self.train.line_idx = 1 self.process_client.turn_trains() move = Move(1, 1, self.train.idx) self.process_client.move(move) assert self.train.speed == 1 assert self.train.line_idx == 1
def test_defult_example(): remote_process_client = RemoteProcessClient('wgforge-srv.wargaming.net', 443) assert remote_process_client.login("Test12345")[0] == 0 assert remote_process_client.move(Move(1, 1, 0))[0] == 0 assert remote_process_client.map(1)[0] == 0 assert remote_process_client.turn()[0] == 0 remote_process_client.logout() remote_process_client.close()
def test_defult(): strategy = Strategy(start_data) next_move = strategy.move(world, game) print(next_move) test_move = Move(12, 1, 0) assert next_move.line_idx == test_move.line_idx assert next_move.speed == test_move.speed assert next_move.train_idx == test_move.train_idx
def test_run_example(): runner = Runner() runner.remote_process_client.login("Test_Conway") runner.remote_process_client.map(0) runner.remote_process_client.turn() # reset timer on server runner.remote_process_client.move(Move(1, 1, 0)) for i in range(11): response = runner.remote_process_client.map(1) print("Position - ", response[1]["train"][0]["position"]) runner.remote_process_client.turn() assert response[1]["train"][0]["position"] == 10 runner.remote_process_client.move(Move(1, 1, 0)) for i in range(11): response = runner.remote_process_client.map(1) print("Position - ", response[1]["train"][0]["position"]) runner.remote_process_client.turn() assert response[1]["train"][0]["position"] == 10 runner.remote_process_client.logout() runner.remote_process_client.close()
def run(self): try: self.remote_process_client.write_token_message(self.token) self.remote_process_client.write_protocol_version_message() team_size = self.remote_process_client.read_team_size_message() game = self.remote_process_client.read_game_context_message() self.strategies = [] self.start_input() world_time = 0 while True: while self.freeze: time.sleep(0.5) if self.reload: print(f'actually reloading at {world_time}') for module in [ x for x in sys.modules if x.startswith('aicup2016') ]: del sys.modules[module] from aicup2016.strategy import Strategy self.strategies = [] for _ in range(team_size): self.strategies.append( Strategy(debug_client=self.debug_client, input_event=self.input_event)) self.reload = False player_context = self.remote_process_client.read_player_context_message( ) if player_context is None: break world_time = player_context.world.tick_index player_wizards = player_context.wizards if player_wizards is None or player_wizards.__len__( ) != team_size: break moves = [] for wizard_index in range(team_size): player_wizard = player_wizards[wizard_index] move = Move() moves.append(move) self.strategies[wizard_index].move(player_wizard, player_context.world, game, move) self.remote_process_client.write_moves_message(moves) finally: self.remote_process_client.close()
def step(self): moves = [] trains = dict() trains_order = [] self.update_targets() for train in self.objects.get_my_trains(self.player.idx): pos = Position(train.point, train.line_idx, train.position) trains.update({train.idx: pos}) trains_order.append(train.idx) shuffle(trains_order) while len(trains_order): train_id = trains_order.pop(0) train = self.objects.trains[train_id] maybe_next = self.next_step(train_id) pos = trains[train_id] if not maybe_next: print("No Path") for t in trains_order: self.unreserve(t) self.paths[t] = [] if not self.valid(train, pos, maybe_next): print("Path Invalid") self.paths[train_id] = [] maybe_next = self.next_step(train_id) if not maybe_next: maybe_next = pos line_speed = self.pos_to_line_speed(pos, maybe_next) if line_speed[0] == train.line_idx and line_speed[1] == train.speed: continue if train.speed == -line_speed[1]: moves.append(Move(line_speed[0], 0, train_id)) moves.append(Move(line_speed[0], line_speed[1], train_id)) logger.info("reservation on tick - %d \n %s", self.objects.tick, pformat(self.trains_reservations, indent=4)) pprint(self.trains_roles) return moves
def makeMove(self, xCoordinate, yCoordinate): """ Makes a move for the player :param xCoordinate: The x coordinate of the move :param yCoordinate: The y coordinate of the move """ move = Move(self.sign, xCoordinate, yCoordinate) Validator.validateMove( move, self.__moveRepository.lastMove.sign if self.__moveRepository.lastMove is not None else "") self.__moveRepository.addMove(move)
def enterMove(self, ctx: dgdlParser.MoveContext): move = Move() data = str(ctx.getText()) if str(ctx.IDENT()) in data: move.name = StringParser.before(data, OPEN_BRACE) if ctx.content(): move.content = self.visit(ctx.content()) if ctx.conditions(): move.conditions = self.visit(ctx.conditions()) if ctx.effects(): move.effects = self.visit(ctx.effects()) self.game.moves.append(move)
def read_move(self): if not self.read_boolean(): return None move = Move() move.engine_power = self.read_double() move.brake = self.read_boolean() move.wheel_turn = self.read_double() move.throw_projectile = self.read_boolean() move.use_nitro = self.read_boolean() move.spill_oil = self.read_boolean() return move
def _move_to_point(self, train, arrival_point): if train.point == arrival_point: return None if train.point is None: line = self.map.lines[train.line_idx] else: line = self.map.Graph.get_edge_data(train.point, arrival_point)['line'] if line is None: return None speed = -1 if line.start_point == arrival_point else 1 if line == train.line_idx and speed == train.speed: return None return Move(line.idx, speed, train.idx)
def read_move(self): if not self.read_boolean(): return None move = Move() move.speed_up = self.read_double() move.turn = self.read_double() move.action = self.read_enum(ActionType) if move.action == ActionType.PASS: move.pass_power = self.read_double() move.pass_angle = self.read_double() elif move.action == ActionType.SUBSTITUTE: move.teammate_index = self.read_int() return move
def get_moves(self): moves = [] self.update_targets() trains_targets = {} for train_id, points in self.trains_points.items(): train = self.objects.trains[train_id] if train.point is None: print("train %d at line, no find_path" % train_id) line = self.map.lines[train.line_idx] # if train.speed == -1: # moving_trains.append((train.position, line.start_point, line.end_point)) # else: # moving_trains.append((line.length - train.position, line.end_point, line.start_point)) # continue next_target = self.trains_points[train_id][0] if next_target == self.objects.trains[train_id].point: self.trains_points[train_id].pop(0) if not self.trains_points[train_id]: self._get_target_points(self.objects.trains[train_id]) next_target = self.trains_points[train_id][0] trains_targets[train_id] = (self.objects.trains[train_id].point, next_target) self.solver = TrafficController() paths = self.solver.find_paths(self.map, self.objects.trains, trains_targets) for train_id in paths: path = paths[train_id] if path: next_step = path[1] print("move {}, {}".format(self.objects.trains[train_id].point, next_step)) move_obj = self._move_to_point(self.objects.trains[train_id], next_step) moves.append(move_obj) else: train = self.objects.trains[train_id] moves.append(Move(train.line_idx, 0, train.idx)) return moves
def searchBestMove(self, possibleMoves): """ Searches for a move that wins or blocks the winning of the opponent :param possibleMoves: The moves that are possible :return: The winning/blocking move, or None if it does not exists """ for move in possibleMoves: lastMove = self.gameController.getLastMove() self.player.makeMove(move[0], move[1]) gameStatus = self.gameController.gameStatus() self.gameController.moveRepository.undoMove( move[0], move[1], lastMove) if gameStatus == 1: return move for move in possibleMoves: lastMove = self.gameController.getLastMove() self.gameController.moveRepository.addMove( Move(self.player.otherSign(), move[0], move[1])) gameStatus = self.gameController.gameStatus() self.gameController.moveRepository.undoMove( move[0], move[1], lastMove) if gameStatus == 1: return move
def get_move(self, train): if train.position == 0 or train.position == None: return Move(1, 1, 0) # Just test elif train.position == self.map.lines[train.line_idx].length: return Move(1, -1, 0) # Just test
def setUp(self): self.move1 = Move("X", 0, 0) self.move2 = Move("O", -1, 0)
def get_move(self): return Move(1, 1, 0) # Just test
def setUp(self): self.repository = MoveRepository() self.move = Move("X", 0, 0) self.repository.addMove(self.move) self.controller = GameController(self.repository)
def test_move(self): me, world, game = load_objects() strategy = MyStrategy() move = Move() strategy.move(me, world, game, move)
def testMoveNumber(self): move = Move("X", 0, 0) self.moveRepository.addMove(move) self.assertEqual(self.moveRepository.moveNumber, 1)