Ejemplo n.º 1
0
 async def get_game_data(self) -> GameData:
     result = await self._execute(data=sc_pb.RequestData(ability_id=True,
                                                         unit_type_id=True,
                                                         upgrade_id=True,
                                                         buff_id=True,
                                                         effect_id=True))
     return GameData(result.data)
Ejemplo n.º 2
0
    async def on_start(self) -> None:
        """
        Set up data that require information from the game.

        Note: This function is called automatically at iteration = 0.

        Args:
            None

        Returns:
            None
        """
        raw_game_data = await self._client._execute(data=sc_pb.RequestData(
            ability_id=True,
            unit_type_id=True,
            upgrade_id=True,
            buff_id=True,
            effect_id=True,
        ))
        raw_game_info = await self._client._execute(
            game_info=sc_pb.RequestGameInfo())
        raw_observation = self.state.response_observation
        self.pathing = PathManager(raw_game_data, raw_game_info,
                                   raw_observation)
        self.creeper = Creeper(raw_game_data, raw_game_info, raw_observation)
        # build_selector = BuildOrderManager(self.enemy_race)
        # self.build_order = build_selector.select_build_order()
        with open("builds/1312.pickle", "rb") as f:
            self.build_order = pickle.load(f)  # nosec
        # all possible arguments are handled by BuildOrderManager class
        self.tag_dicts.append(self.pathing.pathing_dict)
        self.target = self.enemy_start_locations[0].position
        await self.chat_send("gl hf")
Ejemplo n.º 3
0
    async def play(self, ws, observation):
        success = True
        request_data = api.Request(
            data=api.RequestData(ability_id=True, unit_type_id=True, upgrade_id=True)
        )
        await asyncio.wait_for(ws.send(request_data.SerializeToString()), 5)
        try:
            result = await asyncio.wait_for(ws.recv(), 5)
            data_response = api.Response.FromString(result)
            game_data = data_response.data

            request_game_info = api.Request(game_info=api.RequestGameInfo())
            await asyncio.wait_for(ws.send(request_game_info.SerializeToString()), 5)
            result = await asyncio.wait_for(ws.recv(), 5)
            game_info_response = api.Response.FromString(result)

            # If game is still on
            if game_data.units:
                obj = decode_observation(observation.observation.observation, game_data, game_info_response)
                obs = MessageToDict(observation)
                obs = str(obs)
                obs = obs.replace("\'", "\"")
                obs = obs.replace("False", "false")
                obs = obs.replace("True", "true")
                obs = json.loads(obs,encoding="UTF-8")
                game_meta = api.Request(
                    game_info=api.RequestGameInfo()
                )
                await ws.send(game_meta.SerializeToString())
                result = await ws.recv()
                game_meta = api.Response.FromString(result)
                game_meta = MessageToDict(game_meta)
                game_meta = str(game_meta)
                game_meta = game_meta.replace("\'", "\"")
                game_meta = game_meta.replace("False", "false")
                game_meta = game_meta.replace("True", "true")
                game_meta = json.loads(game_meta,encoding="UTF-8")
                game_meta = game_meta.get("gameInfo", None)
                game_meta.pop("modNames")
                game_meta.pop("options")
                game_meta.pop("mapName")
                if("localMapPath" in game_meta.keys()):
                    game_meta.pop("localMapPath")
                game_meta.pop("playerInfo")
                game_meta.update(game_meta["startRaw"])
                game_meta.pop("startRaw")
                game_meta.pop("mapSize")
                await self.process_step(ws, obj, raw=(obs, game_meta, game_data))
                # function = self.decision_function
                # alvailable_actions = self.query_alvailable_actions()
                # to_do_action = function(observation, alvailable_actions)
                # while(to_do_action and alvailable_actions):
                #    self.send_order(self, to_do_action)
                #    to_do_action = self.query_alvailable_actions()
        except asyncio.TimeoutError:
            return False
        return True
Ejemplo n.º 4
0
 def data_raw(self,
              ability_id=True,
              unit_type_id=True,
              upgrade_id=True,
              buff_id=True,
              effect_id=True):
     """Get the raw static data for the current game. Prefer `data` instead."""
     return self._client.send(
         data=sc_pb.RequestData(ability_id=ability_id,
                                unit_type_id=unit_type_id,
                                upgrade_id=upgrade_id,
                                buff_id=buff_id,
                                effect_id=effect_id))
    async def store_data_to_file(self, file_path: str):
        # Grab all raw data from observation
        raw_game_data = await self._client._execute(
            data=sc_pb.RequestData(ability_id=True, unit_type_id=True, upgrade_id=True, buff_id=True, effect_id=True)
        )

        raw_game_info = await self._client._execute(game_info=sc_pb.RequestGameInfo())

        raw_observation = self.state.response_observation

        # To test if this data is convertable in the first place
        game_data = GameData(raw_game_data.data)
        game_info = GameInfo(raw_game_info.game_info)
        game_state = GameState(raw_observation)

        os.makedirs(os.path.dirname(file_path), exist_ok=True)
        with lzma.open(file_path, "wb") as f:
            pickle.dump([raw_game_data, raw_game_info, raw_observation], f)
Ejemplo n.º 6
0
 async def dump_data(self, ability_id=True, unit_type_id=True, upgrade_id=True, buff_id=True, effect_id=True):
     """
     Dump the game data files
     choose what data to dump in the keywords
     this function writes to a text file
     call it one time in on_step with:
     await self._client.dump_data()
     """
     result = await self._execute(
         data=sc_pb.RequestData(
             ability_id=ability_id,
             unit_type_id=unit_type_id,
             upgrade_id=upgrade_id,
             buff_id=buff_id,
             effect_id=effect_id,
         )
     )
     with open("data_dump.txt", "a") as file:
         file.write(str(result.data))
    async def on_start_async(self):
        raw_game_data = await self._client._execute(
            data=sc_pb.RequestData(ability_id=True, unit_type_id=True, upgrade_id=True, buff_id=True, effect_id=True)
        )

        raw_game_info = await self._client._execute(game_info=sc_pb.RequestGameInfo())

        raw_observation = self.state.response_observation

        # To test if this data is convertable in the first place
        game_data = GameData(raw_game_data.data)
        game_info = GameInfo(raw_game_info.game_info)
        game_state = GameState(raw_observation)

        print(f"Saving file to {self.map_name}.xz")
        file_path = self.get_pickle_file_path()
        os.makedirs(os.path.dirname(file_path), exist_ok=True)
        with lzma.open(file_path, "wb") as f:
            pickle.dump([raw_game_data, raw_game_info, raw_observation], f)

        await self._client.leave()
Ejemplo n.º 8
0
 def data_raw(self):
     """Get the raw static data for the current game. Prefer `data` instead."""
     return self._client.send(
         data=sc_pb.RequestData(ability_id=True, unit_type_id=True))
Ejemplo n.º 9
0
    def process_replay(self, controller, replay_data, map_data, player_id,
                       out):
        """Process a single replay, updating the stats."""
        self._update_stage("start_replay")
        controller.start_replay(
            sc_pb.RequestStartReplay(replay_data=replay_data,
                                     map_data=map_data,
                                     options=interface,
                                     observed_player_id=player_id))

        if 'data' not in out:
            data = controller._client.send(
                data=sc_pb.RequestData(ability_id=True,
                                       unit_type_id=True,
                                       upgrade_id=True,
                                       buff_id=True,
                                       effect_id=True))
            out['data'] = data.SerializeToString()

        game_info = controller.game_info()
        if 'game_info' not in out:
            out['game_info'] = game_info.SerializeToString()

        out['step_mul'] = FLAGS.step_mul

        observations = []
        out['player' + str(player_id)] = observations

        feat = features.features_from_game_info(game_info)

        self.stats.replay_stats.replays += 1
        self._update_stage("step")
        controller.step()
        while True:
            self.stats.replay_stats.steps += 1
            self._update_stage("observe")
            obs = controller.observe()
            observations.append(obs.SerializeToString())
            for action in obs.actions:
                act_fl = action.action_feature_layer
                if act_fl.HasField("unit_command"):
                    self.stats.replay_stats.made_abilities[
                        act_fl.unit_command.ability_id] += 1
                if act_fl.HasField("camera_move"):
                    self.stats.replay_stats.camera_move += 1
                if act_fl.HasField("unit_selection_point"):
                    self.stats.replay_stats.select_pt += 1
                if act_fl.HasField("unit_selection_rect"):
                    self.stats.replay_stats.select_rect += 1
                if action.action_ui.HasField("control_group"):
                    self.stats.replay_stats.control_group += 1

                try:
                    func = feat.reverse_action(action).function
                except ValueError:
                    func = -1
                self.stats.replay_stats.made_actions[func] += 1

            for valid in obs.observation.abilities:
                self.stats.replay_stats.valid_abilities[valid.ability_id] += 1

            for u in obs.observation.raw_data.units:
                self.stats.replay_stats.unit_ids[u.unit_type] += 1

            for ability_id in feat.available_actions(obs.observation):
                self.stats.replay_stats.valid_actions[ability_id] += 1

            if obs.player_result:
                break

            self._update_stage("step")
            controller.step(FLAGS.step_mul)