Skip to content

Commit

Permalink
Unit: Implement feature to sell SIS.
Browse files Browse the repository at this point in the history
  • Loading branch information
MikuAuahDark committed Apr 18, 2024
1 parent 3e9bf04 commit e3b3f91
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
41 changes: 41 additions & 0 deletions npps4/game/unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,20 @@ class UnitFavoriteRequest(pydantic.BaseModel):
favorite_flag: int


class UnitRemovableSkillSellData(unit_model.EquipRemovableSkillInfoDetail):
amount: int


class UnitRemovableSkillSellRequest(pydantic.BaseModel):
selling_list: list[UnitRemovableSkillSellData]


class UnitRemovableSkillSellResponse(pydantic.BaseModel):
after_user_info: user.UserInfoData
total: int
reward_box_flag: bool


@idol.register("unit", "accessoryAll")
async def unit_accessoryall(context: idol.SchoolIdolUserParams) -> UnitAccessoryInfoResponse:
# TODO
Expand Down Expand Up @@ -749,3 +763,30 @@ async def unit_favorite(context: idol.SchoolIdolUserParams, request: UnitFavorit
source_unit = await unit.get_unit(context, request.unit_owning_user_id)
unit.validate_unit(current_user, source_unit)
source_unit.favorite_flag = request.favorite_flag > 0


@idol.register("unit", "removableSkillSell")
async def unit_removableskillsell(
context: idol.SchoolIdolUserParams, request: UnitRemovableSkillSellRequest
) -> UnitRemovableSkillSellResponse:
current_user = await user.get_current(context)
total = 0

for info in request.selling_list:
sis = await unit.get_removable_skill_game_info(context, info.unit_removable_skill_id)
if sis is not None:
# FIXME: Check if amount is sensible across units and their assigned SIS
new_amount = await unit.sub_unit_removable_skill(
context, current_user, info.unit_removable_skill_id, info.amount
)
if new_amount < 0:
raise idol.error.IdolError(
detail=f"cannot sell SIS {info.unit_removable_skill_id} amount {-new_amount}"
)

total = sis.selling_price * info.amount

success_add = bool(await advanced.add_item(context, current_user, item.game_coin(total)))
return UnitRemovableSkillSellResponse(
after_user_info=await user.get_user_info(context, current_user), total=total, reward_box_flag=not success_add
)
17 changes: 17 additions & 0 deletions npps4/system/unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,10 @@ async def get_removable_skill_info(context: idol.BasicSchoolIdolContext, user: m
return result.scalar()


async def get_removable_skill_game_info(context: idol.BasicSchoolIdolContext, /, removable_skill_id: int):
return await db.get_decrypted_row(context.db.unit, unit.RemovableSkill, removable_skill_id)


async def get_unit_removable_skill_count(
context: idol.BasicSchoolIdolContext, user: main.User, removable_skill_id: int
):
Expand Down Expand Up @@ -710,6 +714,19 @@ async def add_unit_removable_skill(
return removable_skill.amount


async def sub_unit_removable_skill(
context: idol.BasicSchoolIdolContext, /, user: main.User, removable_skill_id: int, amount: int = 1
):
removable_skill = await get_removable_skill_info(context, user, removable_skill_id)
if removable_skill is None:
return -amount

removable_skill.amount = removable_skill.amount - amount
if removable_skill.amount >= 0:
await context.db.main.flush()
return removable_skill.amount


async def attach_unit_removable_skill(context: idol.BasicSchoolIdolContext, unit: main.Unit, removable_skill_id: int):
q = sqlalchemy.select(main.UnitRemovableSkill).where(
main.UnitRemovableSkill.unit_owning_user_id == unit.id,
Expand Down

0 comments on commit e3b3f91

Please sign in to comment.