Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions sdk/py/petcode/create_and_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,12 @@ def create_petcode_message(
display_mode: PetCodeMessage.DisplayMode,
seer_set: PetCodeMessage.SeerSet,
pets: list[PetInfo],
battle_fires: list[PetCodeMessage.BattleFire] | None = None,
) -> PetCodeMessage:
return PetCodeMessage(
server=server,
display_mode=display_mode,
seer_set=seer_set,
pets=pets,
battle_fires=battle_fires or [],
)
4 changes: 2 additions & 2 deletions sdk/py/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ build-backend = "uv_build"

[project]
name = "petcode"
version = "1.0.0"
version = "1.1.0"
description = "PetCode SDK for Python"
readme = "README.md"
requires-python = ">=3.10"
dependencies = [
"seerbp-petcode-protocolbuffers-python==33.2.0.1.20251225073453+b0513d4afebb",
"seerbp-petcode-protocolbuffers-python==33.2.0.1.20260110152342+c9e05318fd42",
]

[tool.uv.build-backend]
Expand Down
89 changes: 79 additions & 10 deletions sdk/py/tests/test_create_and_read.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""测试辅助创建和读取函数"""

import pytest

from petcode.create_and_read import (
create_ability_mintmark,
create_petcode_message,
Expand All @@ -11,6 +9,7 @@
create_universal_mintmark,
read_mintmark,
)
import pytest
from seerbp.petcode.v1.message_pb2 import (
MintmarkInfo,
PetAbilityValue,
Expand Down Expand Up @@ -107,7 +106,12 @@ def test_create_universal_mintmark_with_gem(self):
def test_create_universal_mintmark_with_ability(self):
"""测试带自定义能力值的全能刻印"""
ability = PetAbilityValue(
hp=100, attack=120, defense=80, special_attack=90, special_defense=85, speed=110
hp=100,
attack=120,
defense=80,
special_attack=90,
special_defense=85,
speed=110,
)
mintmark = create_universal_mintmark(id=40001, level=5, ability=ability)

Expand All @@ -120,7 +124,12 @@ def test_create_universal_mintmark_with_ability(self):
def test_create_universal_mintmark_with_gem_and_ability(self):
"""测试同时带宝石和能力值的全能刻印"""
ability = PetAbilityValue(
hp=100, attack=120, defense=80, special_attack=90, special_defense=85, speed=110
hp=100,
attack=120,
defense=80,
special_attack=90,
special_defense=85,
speed=110,
)
mintmark = create_universal_mintmark(
id=40001, level=5, gem_id=1800011, bind_skill_id=24708, ability=ability
Expand Down Expand Up @@ -207,7 +216,7 @@ def test_read_quanxiao_mintmark(self):
def test_read_empty_mintmark(self):
"""测试读取空刻印"""
mintmark = MintmarkInfo()
with pytest.raises(ValueError, match="Unknown mintmark type"):
with pytest.raises(ValueError, match='Unknown mintmark type'):
read_mintmark(mintmark)


Expand Down Expand Up @@ -316,7 +325,9 @@ def test_create_petcode_message_all_display_modes(
)
assert message.display_mode == mode

def test_create_petcode_message_multiple_pets(self, sample_pet_info, sample_seer_set):
def test_create_petcode_message_multiple_pets(
self, sample_pet_info, sample_seer_set
):
"""测试多只宠物"""
from seerbp.petcode.v1.message_pb2 import PetAbilityValue, PetInfo

Expand All @@ -325,7 +336,12 @@ def test_create_petcode_message_multiple_pets(self, sample_pet_info, sample_seer
level=100,
dv=31,
ability_total=PetAbilityValue(
hp=110, attack=130, defense=90, special_attack=100, special_defense=95, speed=120
hp=110,
attack=130,
defense=90,
special_attack=100,
special_defense=95,
speed=120,
),
)

Expand Down Expand Up @@ -362,18 +378,30 @@ def test_create_petcode_message_full_featured(self):
level=100,
dv=31,
ability_total=PetAbilityValue(
hp=100, attack=120, defense=80, special_attack=90, special_defense=85, speed=110
hp=100,
attack=120,
defense=80,
special_attack=90,
special_defense=85,
speed=110,
),
evs=PetAbilityValue(
hp=255, attack=255, defense=255, special_attack=255, special_defense=255, speed=255
hp=255,
attack=255,
defense=255,
special_attack=255,
special_defense=255,
speed=255,
),
effects=[PetInfo.Effect(id=67, status=1, args=[1, 5])],
skills=[24708, 31567, 31568, 31569],
mintmarks=[create_universal_mintmark(id=40001, level=5)],
nature=1,
)

seer_set = PetCodeMessage.SeerSet(equips=[200001, 300001, 400001, 500001, 600001])
seer_set = PetCodeMessage.SeerSet(
equips=[200001, 300001, 400001, 500001, 600001]
)

message = create_petcode_message(
server=PetCodeMessage.Server.SERVER_OFFICIAL,
Expand All @@ -386,3 +414,44 @@ def test_create_petcode_message_full_featured(self):
assert len(message.pets[0].skills) == 4
assert len(message.pets[0].mintmarks) == 1
assert message.pets[0].mintmarks[0].WhichOneof('mintmark') == 'universal'

def test_create_petcode_message_with_battle_fires(
self, sample_pet_info, sample_seer_set
):
"""测试带战斗火焰的消息创建"""
battle_fires = [
PetCodeMessage.BattleFire.BATTLE_FIRE_GREEN,
PetCodeMessage.BattleFire.BATTLE_FIRE_BLUE,
]
message = create_petcode_message(
server=PetCodeMessage.Server.SERVER_OFFICIAL,
display_mode=PetCodeMessage.DisplayMode.DISPLAY_MODE_PVP,
seer_set=sample_seer_set,
pets=[sample_pet_info],
battle_fires=battle_fires,
)

assert len(message.battle_fires) == 2
assert message.battle_fires[0] == PetCodeMessage.BattleFire.BATTLE_FIRE_GREEN
assert message.battle_fires[1] == PetCodeMessage.BattleFire.BATTLE_FIRE_BLUE

def test_create_petcode_message_with_all_battle_fires(
self, sample_pet_info, sample_seer_set
):
"""测试所有类型的战斗火焰"""
battle_fires = [
PetCodeMessage.BattleFire.BATTLE_FIRE_GREEN,
PetCodeMessage.BattleFire.BATTLE_FIRE_BLUE,
PetCodeMessage.BattleFire.BATTLE_FIRE_PURPLE,
PetCodeMessage.BattleFire.BATTLE_FIRE_GOLD,
]
message = create_petcode_message(
server=PetCodeMessage.Server.SERVER_OFFICIAL,
display_mode=PetCodeMessage.DisplayMode.DISPLAY_MODE_PVP,
seer_set=sample_seer_set,
pets=[sample_pet_info],
battle_fires=battle_fires,
)

assert len(message.battle_fires) == 4
assert PetCodeMessage.BattleFire.BATTLE_FIRE_GOLD in message.battle_fires
55 changes: 44 additions & 11 deletions sdk/py/tests/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import base64

import pytest

import petcode
import pytest
from seerbp.petcode.v1.message_pb2 import PetCodeMessage


Expand Down Expand Up @@ -35,7 +34,10 @@ def test_binary_roundtrip(self, sample_petcode_message):
# 验证完整性
assert restored.pets[0].id == sample_petcode_message.pets[0].id
assert restored.pets[0].level == sample_petcode_message.pets[0].level
assert restored.pets[0].ability_total.hp == sample_petcode_message.pets[0].ability_total.hp
assert (
restored.pets[0].ability_total.hp
== sample_petcode_message.pets[0].ability_total.hp
)
assert restored.pets[0].skills == sample_petcode_message.pets[0].skills

def test_binary_compression(self, sample_petcode_message):
Expand All @@ -50,7 +52,7 @@ def test_binary_compression(self, sample_petcode_message):
def test_from_binary_invalid_data(self):
"""测试使用无效数据进行反序列化"""
with pytest.raises(Exception): # gzip 或 protobuf 解析异常
petcode.from_binary(b"invalid_data")
petcode.from_binary(b'invalid_data')

def test_binary_empty_message(self):
"""测试空消息的序列化"""
Expand Down Expand Up @@ -78,7 +80,7 @@ def test_to_base64_valid_format(self, sample_petcode_message):
try:
base64.b64decode(b64_str)
except Exception as e:
pytest.fail(f"Base64 字符串格式无效: {e}")
pytest.fail(f'Base64 字符串格式无效: {e}')

def test_from_base64_basic(self, sample_petcode_message):
"""测试基本的 Base64 反序列化"""
Expand All @@ -98,12 +100,14 @@ def test_base64_roundtrip(self, sample_petcode_message):
assert restored.pets[0].id == sample_petcode_message.pets[0].id
assert restored.pets[0].level == sample_petcode_message.pets[0].level
assert restored.pets[0].nature == sample_petcode_message.pets[0].nature
assert list(restored.pets[0].skills) == list(sample_petcode_message.pets[0].skills)
assert list(restored.pets[0].skills) == list(
sample_petcode_message.pets[0].skills
)

def test_from_base64_invalid_string(self):
"""测试使用无效 Base64 字符串进行反序列化"""
with pytest.raises(Exception): # base64 解码或 gzip/protobuf 解析异常
petcode.from_base64("invalid!!!base64")
petcode.from_base64('invalid!!!base64')

def test_base64_empty_message(self):
"""测试空消息的 Base64 序列化"""
Expand Down Expand Up @@ -236,8 +240,12 @@ def test_large_pet_list(self):
level=100,
dv=31,
ability_total=PetAbilityValue(
hp=100, attack=120, defense=80,
special_attack=90, special_defense=85, speed=110
hp=100,
attack=120,
defense=80,
special_attack=90,
special_defense=85,
speed=110,
),
)
)
Expand Down Expand Up @@ -283,8 +291,9 @@ def test_special_values(self):
id=0,
level=1,
dv=0,
ability_total=PetAbilityValue(hp=0, attack=0, defense=0,
special_attack=0, special_defense=0, speed=0),
ability_total=PetAbilityValue(
hp=0, attack=0, defense=0, special_attack=0, special_defense=0, speed=0
),
)

msg = PetCodeMessage(
Expand All @@ -297,3 +306,27 @@ def test_special_values(self):
restored = petcode.from_binary(binary)
assert restored.pets[0].dv == 0
assert restored.pets[0].id == 0

def test_battle_fires_serialization(self):
"""测试战斗火焰的序列化"""
msg = PetCodeMessage(
server=PetCodeMessage.Server.SERVER_OFFICIAL,
battle_fires=[
PetCodeMessage.BattleFire.BATTLE_FIRE_GREEN,
PetCodeMessage.BattleFire.BATTLE_FIRE_GOLD,
],
)

# Base64 往返测试
b64 = petcode.to_base64(msg)
restored = petcode.from_base64(b64)
assert len(restored.battle_fires) == 2
assert PetCodeMessage.BattleFire.BATTLE_FIRE_GREEN in restored.battle_fires
assert PetCodeMessage.BattleFire.BATTLE_FIRE_GOLD in restored.battle_fires

# Dict 往返测试
data = petcode.to_dict(msg)
assert 'battleFires' in data
assert len(data['battleFires']) == 2
restored_dict = petcode.from_dict(data)
assert len(restored_dict.battle_fires) == 2
Loading
Loading