예제 #1
0
파일: main.py 프로젝트: earthshakira/aicup
 def __init__(self, host, port, token):
     self.socket = socket.socket()
     self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, True)
     self.socket.connect((host, port))
     socket_stream = self.socket.makefile('rwb')
     self.reader = StreamWrapper(socket_stream)
     self.writer = StreamWrapper(socket_stream)
     self.token = token
     self.writer.write_string(self.token)
     self.writer.flush()
예제 #2
0
class Runner:
    def __init__(self, host, port, token):
        self.socket = socket.socket()
        self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, True)
        self.socket.connect((host, port))
        socket_stream = self.socket.makefile('rwb')
        self.reader = StreamWrapper(socket_stream)
        self.writer = StreamWrapper(socket_stream)
        self.token = token
        self.writer.write_string(self.token)
        self.writer.flush()

    def run(self):
        strategy = MyStrategy()
        debug_interface = DebugInterface(self.reader, self.writer)

        while True:
            message = model.ServerMessage.read_from(self.reader)
            if isinstance(message, model.ServerMessage.GetAction):
                model.ClientMessage.ActionMessage(strategy.get_action(
                    message.player_view, debug_interface if message.debug_available else None)).write_to(self.writer)
                self.writer.flush()
            elif isinstance(message, model.ServerMessage.Finish):
                break
            elif isinstance(message, model.ServerMessage.DebugUpdate):
                strategy.debug_update(message.player_view, debug_interface)
                model.ClientMessage.DebugUpdateDone().write_to(self.writer)
                self.writer.flush()
            else:
                raise Exception("Unexpected server message")
예제 #3
0
파일: main.py 프로젝트: earthshakira/aicup
class Runner:
    def __init__(self, host, port, token):
        self.socket = socket.socket()
        self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, True)
        self.socket.connect((host, port))
        socket_stream = self.socket.makefile('rwb')
        self.reader = StreamWrapper(socket_stream)
        self.writer = StreamWrapper(socket_stream)
        self.token = token
        self.writer.write_string(self.token)
        self.writer.flush()

    def run(self):
        strategy = MyStrategy()
        debug = Debug(self.writer)

        while True:
            message = model.ServerMessageGame.read_from(self.reader)
            if message.player_view is None:
                break
            player_view = message.player_view
            actions = {}
            for unit in player_view.game.units:
                if unit.player_id == player_view.my_id:
                    actions[unit.id] = strategy.get_action(
                        unit, player_view.game, debug)
            model.PlayerMessageGame.ActionMessage(
                model.Versioned(actions)).write_to(self.writer)
            self.writer.flush()
예제 #4
0
class Runner:
    def __init__(self, host, port, token):
        self.socket = socket.socket()
        self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, True)
        self.socket.connect((host, port))
        socket_stream = self.socket.makefile('rwb')
        self.reader = StreamWrapper(socket_stream)
        self.writer = StreamWrapper(socket_stream)
        self.token = token
        self.writer.write_string(self.token)
        self.writer.flush()

    def run(self):
        strategy = MyStrategy()
        debug = Debug(self.writer)

        while True:
            message = model.ServerMessageGame.read_from(self.reader)

            if message.player_view is None:
                print("THE AND")
                break
            player_view = message.player_view

            #print("Level:" , player_view.game.level)

            actions = {}
            cont = 0
            for unit in player_view.game.units:
                #print("cont = ", cont , " >>  my_id", player_view.my_id)
                #print("cont = ", cont , " >>   unit", unit)
                #print("cont = ", cont , " >>units::", player_view.game.units , "XXX")
                #print("cont = ", cont , " >>game::", player_view.game , "YYY")
                cont += 1
                if unit.player_id == player_view.my_id:
                    actions[unit.id] = strategy.get_action(
                        unit, player_view.game, debug)
                else:
                    print("PLAYER:unit.position.x", unit.position.x)
                    print("PLAYER:unit.position.y", unit.position.y)
                    '''
                    print("PLAYER:unit" , unit)
                    print("PLAYER:unit.mines" , unit.mines)
                    print("PLAYER:unit.health" , unit.health)
                    print("PLAYER:unit.weapon" , unit.weapon)
                    print("PLAYER:unit.id" , unit.id)
                    print("PLAYER:unit.jump_state" , unit.jump_state)
                    print("PLAYER:unit.on_ground" , unit.on_ground)
                    print("PLAYER:unit.on_ladder" , unit.on_ladder)
                    print("PLAYER:unit.player_id" , unit.player_id)
                    print("PLAYER:unit.position" , unit.position)
                    print("PLAYER:unit.size" , unit.size)
                    print("PLAYER:unit.stand" , unit.stand)
                    print("PLAYER:unit.walked_right" , unit.walked_right)
                    print("PLAYER:unit_pos.y" , unit.position.y)
                    '''
            model.PlayerMessageGame.ActionMessage(
                model.Versioned(actions)).write_to(self.writer)
            self.writer.flush()
    def from_object(obj, from_repo=False):
        repo = obj.get('repository')

        if (repo is not None) and (not from_repo):
            return ClientModel.FromRepository(repo)

        id = obj.get('id')
        hash = obj.get('hash')
        name = obj.get('name')
        desc = obj.get('desc')
        parameters = obj.get('params')
        commands = obj.get('commands')
        events = obj.get('events')
        streams = obj.get('streams')

        if not name:
            raise WrongObjectException("Can't create client model. Missing name")

        if not desc:
            raise WrongObjectException("Can't create client model. Missing description.")

        if not hash:
            hash = ClientWrapper._create_hash(name + desc)

        client = ClientWrapper(ClientModel(name, desc, hash))

        if id:
            client.model.id = id

        if parameters is not None:
            for param in parameters:
                param_wrapper = ParameterWrapper.from_object(param)
                client.add_parameter(param_wrapper)

        if commands is not None:
            for command in commands:
                cmd_parameters = []

                if 'params' in command:
                    params_names = ClientWrapper.get_parameters_names_from_obj_list(command['params'])
                    cmd_parameters = client.get_parameters_by_names(params_names)
                    del command['params']

                command_wrapper = CommandWrapper.from_object(command)

                for param in cmd_parameters:
                    command_wrapper.add_parameter(param)

                client.add_command(command_wrapper)

        if events is not None:
            for event in events:
                event_parameters = []

                if 'params' in event:
                    params_names = ClientWrapper.get_parameters_names_from_obj_list(event['params'])
                    event_parameters = client.get_parameters_by_names(params_names)
                    del event['params']

                event_wrapper = EventWrapper.from_object(event)

                for event_param in event_parameters:
                    event_wrapper.add_parameter(event_param)

                client.add_event(event_wrapper)

        if streams is not None:
            for stream in streams:
                stream_parameters = []

                if 'params' in stream:
                    params_names = ClientWrapper.get_parameters_names_from_obj_list(stream['params'])
                    stream_parameters = client.get_parameters_by_names(params_names)
                    del stream['params']

                stream_wrapper = StreamWrapper.from_object(stream)

                for param in stream_parameters:
                    stream_wrapper.add_parameter(param)

                client.add_stream(stream_wrapper)

        return client