Skip to content

Commit

Permalink
Added nice gacha
Browse files Browse the repository at this point in the history
  • Loading branch information
squaresmile committed Jan 27, 2024
1 parent 336a0d7 commit 3932f02
Show file tree
Hide file tree
Showing 15 changed files with 250 additions and 2 deletions.
53 changes: 53 additions & 0 deletions app/core/nice/gacha.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from sqlalchemy.ext.asyncio import AsyncConnection

from ...db.helpers.gacha import get_all_gacha_entities
from ...schemas.common import Language
from ...schemas.gameenums import COND_TYPE_NAME, GACHA_FLAG_NAME, PAY_TYPE_NAME
from ...schemas.nice import GachaStoryAdjust, NiceGacha
from ...schemas.raw import GachaEntity, MstGachaStoryAdjust
from ..raw import get_gacha_entity
from ..utils import get_flags, get_translation


def get_nice_gacha_story_adjust(gacha_story: MstGachaStoryAdjust) -> GachaStoryAdjust:
return GachaStoryAdjust(
idx=gacha_story.idx,
adjustId=gacha_story.adjustId,
condType=COND_TYPE_NAME[gacha_story.condType],
targetId=gacha_story.targetId,
value=gacha_story.value,
imageId=gacha_story.imageId,
)


def get_nice_gacha(gacha: GachaEntity, lang: Language = Language.jp) -> NiceGacha:
return NiceGacha(
id=gacha.mstGacha.id,
name=get_translation(lang, gacha.mstGacha.name),
imageId=gacha.mstGacha.imageId,
type=PAY_TYPE_NAME[gacha.mstGacha.type],
adjustId=gacha.mstGacha.adjustId,
pickupId=gacha.mstGacha.pickupId,
drawNum1=gacha.mstGacha.drawNum1,
drawNum2=gacha.mstGacha.drawNum2,
maxDrawNum=gacha.mstGacha.maxDrawNum,
openedAt=gacha.mstGacha.openedAt,
closedAt=gacha.mstGacha.closedAt,
detailUrl=gacha.mstGacha.detailUrl,
flags=get_flags(gacha.mstGacha.flag, GACHA_FLAG_NAME),
storyAdjusts=[
get_nice_gacha_story_adjust(story) for story in gacha.mstGachaStoryAdjust
],
)


async def get_nice_gacha_from_id(
conn: AsyncConnection, gacha_id: int, lang: Language = Language.jp
) -> NiceGacha:
raw_gacha = await get_gacha_entity(conn, gacha_id)
return get_nice_gacha(raw_gacha, lang)


async def get_all_nice_gachas(conn: AsyncConnection) -> list[NiceGacha]:
all_raw = await get_all_gacha_entities(conn)
return [get_nice_gacha(gacha) for gacha in all_raw]
23 changes: 22 additions & 1 deletion app/core/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,19 @@

from ..data.custom_mappings import EXTRA_CHARAFIGURES
from ..data.shop import get_shop_cost_item_id
from ..db.helpers import ai, event, fetch, item, quest, script, skill, svt, td, war
from ..db.helpers import (
ai,
event,
fetch,
gacha,
item,
quest,
script,
skill,
svt,
td,
war,
)
from ..redis import Redis
from ..redis.helpers.reverse import RedisReverse, get_reverse_ids
from ..schemas.common import Region, ReverseDepth
Expand Down Expand Up @@ -36,6 +48,7 @@
EventMissionEntity,
FunctionEntity,
FunctionEntityNoReverse,
GachaEntity,
ItemEntity,
MasterMissionEntity,
MstBgm,
Expand Down Expand Up @@ -1510,3 +1523,11 @@ async def get_class_board_entity(
mstItem=mstItem,
mstSkill=skill_entities,
)


async def get_gacha_entity(conn: AsyncConnection, gacha_id: int) -> GachaEntity:
gacha_entity = await gacha.get_gacha_entity(conn, gacha_id)
if gacha_entity is None:
raise HTTPException(status_code=404, detail="Banner not found")

return gacha_entity
1 change: 1 addition & 0 deletions app/data/custom_mappings.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Translation(StrEnum):
CC = "cc_names"
MC = "mc_names"
COSTUME = "costume_names"
GACHA = "gacha_names"
MANUAL = "manual_names"


Expand Down
1 change: 1 addition & 0 deletions app/data/mappings/gacha_names.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
36 changes: 36 additions & 0 deletions app/db/helpers/gacha.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from sqlalchemy.ext.asyncio import AsyncConnection
from sqlalchemy.sql import func, select

from ...models.raw import mstGacha, mstGachaStoryAdjust
from ...schemas.raw import GachaEntity
from .utils import sql_jsonb_agg


SELECT_GACHA_ENTITY = select(
func.to_jsonb(mstGacha.table_valued()).label(mstGacha.name),
sql_jsonb_agg(mstGachaStoryAdjust),
).select_from(
mstGacha.outerjoin(
mstGachaStoryAdjust, mstGacha.c.id == mstGachaStoryAdjust.c.gachaId
)
)


async def get_gacha_entity(conn: AsyncConnection, gacha_id: int) -> GachaEntity | None:
stmt = SELECT_GACHA_ENTITY.where(mstGacha.c.id == gacha_id).group_by(
mstGacha.table_valued()
)
res = (await conn.execute(stmt)).fetchone()

if res is not None:
return GachaEntity.from_orm(res)
else:
return None


async def get_all_gacha_entities(conn: AsyncConnection) -> list[GachaEntity]:
stmt = SELECT_GACHA_ENTITY.group_by(mstGacha.table_valued())

return [
GachaEntity.from_orm(gacha) for gacha in ((await conn.execute(stmt)).fetchall())
]
14 changes: 13 additions & 1 deletion app/models/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,18 @@
Column("flag", Integer),
)

mstGachaStoryAdjust = Table(
"mstGachaStoryAdjust",
metadata,
Column("gachaId", Integer),
Column("idx", Integer),
Column("adjustId", Integer),
Column("condType", Integer),
Column("targetId", Integer),
Column("value", Integer),
Column("imageId", Integer),
)


mstEvent = Table(
"mstEvent",
Expand Down Expand Up @@ -2648,5 +2660,5 @@
[mstClassBoardBase, mstClassBoardClass, mstClassBoardLine, mstFuncDisp],
[mstClassBoardLock, mstClassBoardSquare],
[mstFuncTypeDetail, mstBuffTypeDetail],
[mstGacha],
[mstGacha, mstGachaStoryAdjust],
]
20 changes: 20 additions & 0 deletions app/routers/nice.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
cc,
class_board,
enemy_master,
gacha,
gift,
item,
mc,
Expand Down Expand Up @@ -40,6 +41,7 @@
NiceEquip,
NiceEvent,
NiceEventMission,
NiceGacha,
NiceGift,
NiceItem,
NiceMasterMission,
Expand Down Expand Up @@ -1037,3 +1039,21 @@ async def get_gift(
) -> Response:
async with get_db(region) as conn:
return list_response(await gift.get_nice_gifts_from_id(conn, region, [gift_id]))


@router.get(
"/{region}/gacha/{gacha_id}",
summary="Get Gacha data",
response_description="Nice Gacha Entity",
response_model=NiceGacha,
response_model_exclude_unset=True,
responses=get_error_code([404]),
)
@cache()
async def get_gacha(
region: Region,
gacha_id: int,
lang: Language = Depends(language_parameter),
) -> Response:
async with get_db(region) as conn:
return item_response(await gacha.get_nice_gacha_from_id(conn, gacha_id, lang))
22 changes: 22 additions & 0 deletions app/routers/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
EventEntity,
EventMissionEntity,
FunctionEntity,
GachaEntity,
ItemEntity,
MasterMissionEntity,
MstEventAlloutBattle,
Expand Down Expand Up @@ -846,3 +847,24 @@ async def get_class_board(
async with get_db(region) as conn:
class_board_entity = await raw.get_class_board_entity(conn, class_board_id)
return item_response(class_board_entity)


@router.get(
"/{region}/gacha/{gacha_id}",
summary="Get Banner data",
response_description="Gacha entity",
response_model=GachaEntity,
response_model_exclude_unset=True,
responses=get_error_code([404]),
)
@cache()
async def get_gacha(
region: Region,
gacha_id: int,
) -> Response:
"""
Get Gacha info from ID
"""
async with get_db(region) as conn:
gacha_entity = await raw.get_gacha_entity(conn, gacha_id)
return item_response(gacha_entity)
18 changes: 18 additions & 0 deletions app/schemas/gameenums.py
Original file line number Diff line number Diff line change
Expand Up @@ -5388,3 +5388,21 @@ class NiceServantOverwriteType(StrEnum):
SERVANT_OVERWRITE_TYPE_NAME: dict[int, NiceServantOverwriteType] = {
1: NiceServantOverwriteType.treasureDevice,
}


class GachaFlag(IntEnum):
PC_MESSAGE = 2
BONUS_SELECT = 8


class NiceGachaFlag(StrEnum):
"""Gacha Flag"""

pcMessage = "pcMessage"
bonusSelect = "bonusSelect"


GACHA_FLAG_NAME: dict[int, NiceGachaFlag] = {
2: NiceGachaFlag.pcMessage,
8: NiceGachaFlag.bonusSelect,
}
27 changes: 27 additions & 0 deletions app/schemas/nice.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
NiceFrequencyType,
NiceFuncTargetType,
NiceFuncType,
NiceGachaFlag,
NiceGender,
NiceGiftType,
NiceItemType,
Expand Down Expand Up @@ -2814,3 +2815,29 @@ class NiceBuffTypeDetail(BaseModelORJson):
buffType: NiceBuffType
ignoreValueUp: bool
script: dict[str, Any]


class GachaStoryAdjust(BaseModelORJson):
idx: int
adjustId: int
condType: NiceCondType
targetId: int
value: int
imageId: int


class NiceGacha(BaseModelORJson):
id: int
name: str
imageId: int
type: NicePayType
adjustId: int
pickupId: int
drawNum1: int
drawNum2: int
maxDrawNum: int
openedAt: int
closedAt: int
detailUrl: str
flags: list[NiceGachaFlag]
storyAdjusts: list[GachaStoryAdjust]
15 changes: 15 additions & 0 deletions app/schemas/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,16 @@ class MstGacha(BaseModelORJson):
flag: int


class MstGachaStoryAdjust(BaseModelORJson):
gachaId: int
idx: int
adjustId: int
condType: int
targetId: int
value: int
imageId: int


class MstEventReward(BaseModelORJson):
eventId: int # 80305
groupId: int # 0
Expand Down Expand Up @@ -2357,6 +2367,11 @@ class ClassBoardEntity(BaseModelORJson):
# mstFuncDisp: list[MstFuncDisp]


class GachaEntity(BaseModelORJson):
mstGacha: MstGacha
mstGachaStoryAdjust: list[MstGachaStoryAdjust]


class AssetStorageLine(BaseModelORJson):
first: str = Field(
...,
Expand Down
8 changes: 8 additions & 0 deletions app/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from pydantic import DirectoryPath
from sqlalchemy.ext.asyncio import AsyncConnection, AsyncEngine

from app.core.nice.gacha import get_all_nice_gachas

from .config import Settings, app_info, logger, project_root
from .core.basic import (
get_all_basic_ccs,
Expand Down Expand Up @@ -251,6 +253,11 @@ async def dump_nice_bgms(
await util.dump_orjson("nice_bgm", all_bgm_data)


async def dump_nice_gachas(util: ExportUtil) -> None:
all_gacha = await get_all_nice_gachas(util.conn)
await util.dump_orjson("nice_gacha", all_gacha)


async def get_nice_mms_from_raw(
util: ExportUtil, mms: list[MstMasterMission]
) -> list[NiceMasterMission]: # pragma: no cover
Expand Down Expand Up @@ -524,6 +531,7 @@ async def generate_exports(
await dump_nice_shops(util, nice_shops)
await dump_nice_enemy_masters(util, mstEnemyMasters)
await dump_nice_class_boards(util, mstClassBoardBases)
await dump_nice_gachas(util)

util_en = ExportUtil(conn, redis, region, export_path, Language.en)
nice_items_lang_en: list[NiceItem] = []
Expand Down
5 changes: 5 additions & 0 deletions scripts/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1828,3 +1828,8 @@ export enum NiceBattleFieldEnvironmentGrantType {
export enum NiceServantOverwriteType {
TREASURE_DEVICE = "treasureDevice",
}

export enum NiceGachaFlag {
PC_MESSAGE = "pcMessage",
BONUS_SELECT = "bonusSelect",
}
7 changes: 7 additions & 0 deletions scripts/extract_enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,13 @@ def cs_enum_to_ts(cs_enums: list[str], raw_class: str, nice_class: str) -> list[
"Servant Overwrite Type",
"SERVANT_OVERWRITE_TYPE_NAME",
),
(
"GachaEntity.Flag",
"GachaFlag",
"NiceGachaFlag",
"Gacha Flag",
"GACHA_FLAG_NAME",
),
]


Expand Down
2 changes: 2 additions & 0 deletions scripts/update_ce_translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"cc_names",
"mc_names",
"costume_names",
"gacha_names",
)
LOGIN_ITEM_REGEX = re.compile(r"(\d+)月交換券\((\d+)\)")
VOICE_NAME_REGEX = re.compile(r"^(.*?)(\d+)$", re.DOTALL)
Expand Down Expand Up @@ -429,6 +430,7 @@ def main(jp_master: Path, na_master: Path) -> None:
"costume_names", jp_master, na_master, "mstSvtCostume", get_costume_names
)
update_translation("cv_names", jp_master, na_master, "mstCv", get_names)
update_translation("gacha_names", jp_master, na_master, "mstGacha", get_names)
update_translation(
"overwrite_voice_names",
jp_master,
Expand Down

0 comments on commit 3932f02

Please sign in to comment.