Exemplo n.º 1
0
    def entity2response_block(cls, entity, packet, ):
        logger = HenriqueLogger.func_level2logger(cls.packet2response, logging.DEBUG)

        chatroom = Chatroom.codename2chatroom(KhalaPacket.packet2chatroom(packet))
        if Chatroom.chatroom2codename(chatroom) != ChatroomKakaotalk.codename():
            return

        locale = Chatroom.chatroom2locale(chatroom)
        lang = LocaleTool.locale2lang(locale)

        v = FoxylibEntity.entity2value(entity)
        codename = ChatroomuserEntity.value_packet2codename(v, packet)
        logger.debug({"codename": codename,
                      "entity": entity,
                      "v": v,
                      })

        chatroomuser = Chatroomuser.codename2chatroomuser(codename)

        comments = Chatroomuser.chatroomuser2comments(chatroomuser)
        comment = choice(comments) if comments else None

        filepath = os.path.join(FILE_DIR, "tmplt.{}.part.txt".format(lang))
        data = {"name": codename,
                "comment": comment,
                "str_aliases": ", ".join(Chatroomuser.chatroomuser2aliases(chatroomuser)),
                }
        text_out = str2strip(HenriqueJinja2.textfile2text(filepath, data))

        return text_out
Exemplo n.º 2
0
    def packet2response(cls, packet):
        chatroom = Chatroom.codename2chatroom(
            KhalaPacket.packet2chatroom(packet))
        locale = Chatroom.chatroom2locale(chatroom)
        lang = LocaleTool.locale2lang(locale)

        text_in = KhalaPacket.packet2text(packet)
        config = {HenriqueEntity.Config.Field.LOCALE: locale}

        def entity2is_valid(entity):
            if FoxylibEntity.entity2value(
                    entity) != HenriqueSkill.Codename.HELP:
                return True

            span = FoxylibEntity.entity2span(entity)
            if len(str2strip(text_in[:span[0]])) > 1:
                return True

        entity_list_skill = lfilter(
            entity2is_valid,
            SkillEntity.text2entity_list(text_in, config=config))

        def entity_list2codename_list(entity_list):
            codename_list = luniq(
                map(SkillEntity.entity2skill_codename, entity_list))
            if codename_list:
                return codename_list

            return [HenriqueSkill.Codename.HELP]

        codename_list = entity_list2codename_list(entity_list_skill)
        clazz_list = lmap(HenriqueSkill.codename2class, codename_list)

        blocks = [clazz.lang2description(lang) for clazz in clazz_list]
        return Rowsblock.blocks2text(blocks)
Exemplo n.º 3
0
    def packet2response(cls, packet):
        chatroom = Chatroom.codename2chatroom(KhalaPacket.packet2chatroom(packet))
        locale = Chatroom.chatroom2locale(chatroom)
        lang = LocaleTool.locale2lang(locale)

        entity_classes = cls.target_entity_classes()
        text_in = KhalaPacket.packet2text(packet)

        config = {HenriqueEntity.Config.Field.LOCALE: locale}
        entity_list = HenriqueEntity.text_classes2entity_list(text_in, entity_classes, config=config)

        blocks = [cls.entity_lang2response_block(entity, lang) for entity in entity_list]

        return Rowsblock.blocks2text(blocks)
Exemplo n.º 4
0
    def packet2response(cls, packet):
        chatroom = Chatroom.codename2chatroom(
            KhalaPacket.packet2chatroom(packet))
        locale = Chatroom.chatroom2locale(chatroom)

        entity_classes = cls.target_entity_classes()
        text_in = KhalaPacket.packet2text(packet)
        config = {HenriqueEntity.Config.Field.LOCALE: locale}
        entity_list_raw = lchain(*[
            c.text2entity_list(text_in, config=config) for c in entity_classes
        ])

        entity_list = sorted(entity_list_raw, key=FoxylibEntity.entity2span)

        blocks = [
            cls.entity2response_block(packet, entity) for entity in entity_list
        ]

        return Rowsblock.blocks2text(blocks)
Exemplo n.º 5
0
    def packet2response(cls, packet):
        logger = HenriqueLogger.func_level2logger(cls.packet2response, logging.DEBUG)

        logger.debug({"packet":packet})

        chatroom = Chatroom.codename2chatroom(KhalaPacket.packet2chatroom(packet))
        if Chatroom.chatroom2codename(chatroom) != ChatroomKakaotalk.codename():
            return

        text_in = KhalaPacket.packet2text(packet)
        # config = {HenriqueEntity.Config.Field.LOCALE: locale}

        # config = {HenriqueEntity.Config.Field.LOCALE: locale}
        # entity_list_raw = lchain(*[c.text2entity_list(text_in, config=config) for c in entity_classes])
        #
        entity_list_chatroomuser = sorted(ChatroomuserEntity.text2entity_list(text_in), key=FoxylibEntity.entity2span)
        blocks = [cls.entity2response_block(entity, packet,) for entity in entity_list_chatroomuser]

        return Rowsblock.blocks2text(blocks)
Exemplo n.º 6
0
    def entity2response_block(
        cls,
        packet,
        entity,
    ):
        chatroom = Chatroom.codename2chatroom(
            KhalaPacket.packet2chatroom(packet))
        locale = Chatroom.chatroom2locale(chatroom)
        lang = LocaleTool.locale2lang(locale)

        entity_type = FoxylibEntity.entity2type(entity)

        h_type2func = {
            PortEntity.entity_type():
            partial(PortSkill.entity_lang2response_block, lang=lang),
            TradegoodEntity.entity_type():
            partial(TradegoodSkill.entity_lang2response_block, lang=lang),
            CultureEntity.entity_type():
            partial(CultureSkill.entity_lang2response_block, lang=lang),
            ChatroomuserEntity.entity_type():
            partial(
                WhoSkill.entity2response_block,
                packet=packet,
            ),
        }

        assert_equals(
            set(h_type2func.keys()),
            smap(lambda c: c.entity_type(), cls.target_entity_classes()))

        entity2response = h_type2func.get(entity_type)
        if not entity2response:
            raise NotImplementedError(
                "Invalid entity_type: {}".format(entity_type))

        return entity2response(entity)
Exemplo n.º 7
0
    def packet2response(cls, packet):
        logger = HenriqueLogger.func_level2logger(cls.packet2response,
                                                  logging.DEBUG)
        logger.debug({"packet": packet})

        server_codename = HenriquePacket.packet2server(packet)
        chatroom = Chatroom.codename2chatroom(
            KhalaPacket.packet2chatroom(packet))
        locale = Chatroom.chatroom2locale(chatroom) or HenriqueLocale.DEFAULT
        lang = LocaleTool.locale2lang(locale)
        tz = pytz.timezone(HenriqueLocale.lang2tzdb(lang))
        dt_now = datetime.now(tz)

        text_in = KhalaPacket.packet2text(packet)
        config = {HenriqueEntity.Config.Field.LOCALE: locale}
        # entity_list = RelativeTimedeltaEntity.text2entity_list(text_in, config=config)

        entity_list = HenriqueEntity.text_extractors2entity_list(
            text_in,
            cls.config2extractors(config),
        )
        logger.debug({
            "len(entity_list)": len(entity_list),
            "entity_list": entity_list,
        })

        if not entity_list:
            return cls.server_lang2lookup(server_codename, lang)

        if len(entity_list) != 1:
            return  # Invalid request

        entity = l_singleton2obj(entity_list)
        if FoxylibEntity.entity2type(
                entity) == RelativeTimedeltaEntity.entity_type():
            reldelta = RelativeTimedeltaEntity.entity2relativedelta(entity)
            dt_in = cls.relativedelta2nanban_datetime(
                server_codename,
                reldelta,
            )

            # raise Exception({"dt_in":dt_in, "reldelta":reldelta})

            if dt_in is None:
                msg_error = NanbanSkillError.codename_lang2text(
                    NanbanSkillError.Codename.NO_PREV_NANBAN_ERROR, lang)
                raise HenriqueCommandError(msg_error)

            logger.debug({
                "reldelta": reldelta,
            })

        elif FoxylibEntity.entity2type(entity) == TimeEntity.entity_type():
            time_in = TimeEntity.value2datetime_time(
                FoxylibEntity.entity2value(entity))
            dt_in = PytzTool.localize(datetime.combine(dt_now.date(), time_in),
                                      tz)
            logger.debug({
                "time_in": time_in,
                "dt_in": dt_in,
            })
        else:
            raise RuntimeError({
                "Invalid entity type: {}".format(
                    FoxylibEntity.entity2type(entity))
            })

        dt_nearest = DatetimeTool.datetime2nearest(dt_in, dt_now,
                                                   NanbanTimedelta.period(),
                                                   Nearest.COMING)

        logger.debug({
            "text_in": text_in,
            "dt_now": dt_now,
            "dt_in": dt_in,
            "dt_nearest": dt_nearest,
        })

        cls.nanban_datetime2upsert_mongo(packet, dt_nearest)
        return cls.server_lang2lookup(server_codename, lang)
Exemplo n.º 8
0
    def packet2rowsblocks(cls, packet):
        from henrique.main.skill.price.price_skill_clique import PriceSkillClique as Clique
        Param = PriceSkillParameter

        text = KhalaPacket.packet2text(packet)
        server_codename = HenriquePacket.packet2server(packet)

        chatroom = Chatroom.codename2chatroom(
            KhalaPacket.packet2chatroom(packet))

        config = {
            HenriqueEntity.Config.Field.LOCALE:
            Chatroom.chatroom2locale(chatroom)
        }
        extractors = Clique.config2extractors(config)
        entity_list = HenriqueEntity.text_extractors2entity_list(
            text, extractors)

        clique_list = Clique.text_entity_list2clique_list(text, entity_list)
        clique_list_update = lfilter(
            lambda x: Clique.clique2type(x) == Clique.Type.UPDATE, clique_list)

        # raise Exception({"entity_list": entity_list,
        #                  "clique_list": clique_list,
        #                  "clique_list_update":clique_list_update,
        #                  })
        if clique_list_update:
            Clique.clique_list2update_mongodb(packet, clique_list_update)

        h_port2indexes = Clique.cliques2dict_port2indexes(clique_list)
        h_tradegood2indexes = Clique.cliques2dict_tradegood2indexes(
            clique_list)

        def _groupby_paramter_type():
            entity_list_portlike = lfilter(
                lambda x: FoxylibEntity.entity2type(x) in Portlike.
                entity_types(), entity_list)
            entity_list_tradegood = lfilter(
                lambda x: FoxylibEntity.entity2type(x) == TradegoodEntity.
                entity_type(), entity_list)

            if not entity_list_portlike:
                return Param.Type.TRADEGOOD

            if not entity_list_tradegood:
                return Param.Type.PORTLIKE

            if len(h_port2indexes) > 1:
                return Param.Type.TRADEGOOD

            if len(h_tradegood2indexes) > 1:
                return Param.Type.PORTLIKE

            span_portlike = FoxylibEntity.entity2span(entity_list_portlike[0])
            span_tradegood = FoxylibEntity.entity2span(
                entity_list_tradegood[0])

            if span_portlike[0] < span_tradegood[0]:
                return Param.Type.PORTLIKE
            else:
                return Param.Type.TRADEGOOD

        groupby_parameter_type = _groupby_paramter_type()

        port_tradegood_list = lchain(
            *map(Clique.clique2port_tradegood_iter, clique_list))
        price_dict = MarketpriceDict.port_tradegood_iter2price_dict(
            server_codename, port_tradegood_list)

        # raise Exception({"port_tradegood_list": port_tradegood_list,
        #                  "price_dict": price_dict,
        #                  "groupby_parameter_type": groupby_parameter_type,
        #                  })
        chatroom = Chatroom.codename2chatroom(
            KhalaPacket.packet2chatroom(packet))
        lang = LocaleTool.locale2lang(Chatroom.chatroom2locale(chatroom))
        block_list = cls.port_tradegood_lists2blocks(port_tradegood_list,
                                                     price_dict, lang,
                                                     groupby_parameter_type)
        return block_list