Skip to content

模块 nonebot_plugin_marshoai.plugins.twisuki_petcat.pc_token

猫对象属性存储编码Token 名字: 3位长度 + 8位ASCII字符 - 67b 年龄: 0 ~ 15 - 4b 种类: 8种 - 3b 生命值: 0 ~ 127 - 7b 饱食度: 0 ~ 127 - 7b 活力值: 0 ~ 127 - 7b 技能: 8种任选 - 8b 时间: 0 ~ 131017d > 2025-1-1 - 17b

总计120b有效数据 总计120b数据, 15字节, 每3字节(utf-8一个字符)转换为4个Base64字符 总计20个Base64字符的字符串


func bool_to_int(bool_array: List[bool]) -> int

源代码在GitHub上查看
python
def bool_to_int(bool_array: List[bool]) -> int:
    result = 0
    for index, bit in enumerate(bool_array[::-1]):
        if bit:
            result |= 1 << index
    return result

func int_to_bool(integer: int, length: int = 0) -> List[bool]

源代码在GitHub上查看
python
def int_to_bool(integer: int, length: int=0) -> List[bool]:
    bit_length = integer.bit_length()
    bool_array = [False] * bit_length
    for i in range(bit_length):
        if integer & 1 << i:
            bool_array[bit_length - 1 - i] = True
    if len(bool_array) >= length:
        return bool_array
    else:
        return [*[False] * (length - len(bool_array)), *bool_array]

func bool_to_byte(bool_array: List[bool]) -> bytes

源代码在GitHub上查看
python
def bool_to_byte(bool_array: List[bool]) -> bytes:
    byte_data = bytearray()
    for i in range(0, len(bool_array), 8):
        byte = 0
        for j in range(8):
            if i + j < len(bool_array) and bool_array[i + j]:
                byte |= 1 << 7 - j
        byte_data.append(byte)
    return bytes(byte_data)

func byte_to_bool(byte_data: bytes, length: int = 0) -> List[bool]

源代码在GitHub上查看
python
def byte_to_bool(byte_data: bytes, length: int=0) -> List[bool]:
    bool_array = []
    for byte in byte_data:
        for bit in format(byte, '08b'):
            bool_array.append(bit == '1')
    if len(bool_array) >= length:
        return bool_array
    else:
        return [*[False] * (length - len(bool_array)), *bool_array]

func token_to_dict(token: str) -> dict

源代码在GitHub上查看
python
def token_to_dict(token: str) -> dict:
    logger.info(f'开始解码...\n{token}')
    data = {'name': 'Default0', 'age': 0, 'type': 0, 'health': 0, 'saturation': 0, 'energy': 0, 'skill': [False] * 8, 'date': 0}
    try:
        token_byte = base64.b64decode(token.encode())
        code = byte_to_bool(token_byte)
    except ValueError:
        logger.error('token b64解码错误!')
        return ERROR_DICT
    name_length = bool_to_int(code[0:3]) + 1
    name_code = code[3:67]
    age = bool_to_int(code[67:71])
    type = bool_to_int(code[71:74])
    health = bool_to_int(code[74:81])
    saturation = bool_to_int(code[81:88])
    energy = bool_to_int(code[88:95])
    skill = code[95:103]
    date = bool_to_int(code[103:120])
    name: str = ''
    try:
        for i in range(name_length):
            character_code = bool_to_byte(name_code[8 * i:8 * i + 8])
            name += character_code.decode('ASCII')
    except UnicodeDecodeError:
        logger.error('token ASCII解析错误!')
        return ERROR_DICT
    data['name'] = name
    data['age'] = age
    data['type'] = type
    data['health'] = health
    data['saturation'] = saturation
    data['energy'] = energy
    data['skill'] = skill
    data['date'] = date
    logger.success(f'解码完成, 数据为\n{data}')
    return data

func dict_to_token(data: dict) -> str

源代码在GitHub上查看
python
def dict_to_token(data: dict) -> str:
    logger.info(f'开始编码...\n{data}')
    code = [False] * 120
    name_length = len(data['name'])
    if name_length > 8:
        logger.error('name过长')
        return ERROR_TOKEN
    name = data['name']
    age = data['age']
    type = data['type']
    health = data['health']
    saturation = data['saturation']
    energy = data['energy']
    skill = data['skill']
    date = data['date']
    code[0:3] = int_to_bool(name_length - 1, 3)
    name_code = [False] * 64
    try:
        for i in range(name_length):
            character_code = byte_to_bool(name[i].encode('ASCII'), 8)
            name_code[8 * i:8 * i + 8] = character_code
    except UnicodeEncodeError:
        logger.error('name内含有非法字符!')
        return ERROR_TOKEN
    code[3:67] = name_code
    code[67:71] = int_to_bool(age, 4)
    code[71:74] = int_to_bool(type, 3)
    code[74:81] = int_to_bool(health, 7)
    code[81:88] = int_to_bool(saturation, 7)
    code[88:95] = int_to_bool(energy, 7)
    code[95:103] = skill
    code[103:120] = int_to_bool(date, 17)
    token_byte = bool_to_byte(code)
    token = base64.b64encode(token_byte).decode()
    logger.success(f'编码完成, token为\n{token}')
    return token

文档完善中,欢迎提出建议或帮助我们完善。