2023-02-17 07:02:21 +01:00
|
|
|
from typing import Any, List, Dict
|
2023-03-15 21:03:22 +01:00
|
|
|
from random import randint
|
2023-02-17 07:02:21 +01:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
import pytz
|
|
|
|
import json
|
|
|
|
|
|
|
|
from core.config import CoreConfig
|
2023-07-15 22:51:54 +02:00
|
|
|
from titles.mai2.splashplus import Mai2SplashPlus
|
2023-02-17 07:02:21 +01:00
|
|
|
from titles.mai2.const import Mai2Constants
|
|
|
|
from titles.mai2.config import Mai2Config
|
|
|
|
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-07-15 22:51:54 +02:00
|
|
|
class Mai2Universe(Mai2SplashPlus):
|
2023-02-17 07:02:21 +01:00
|
|
|
def __init__(self, cfg: CoreConfig, game_cfg: Mai2Config) -> None:
|
|
|
|
super().__init__(cfg, game_cfg)
|
2023-03-09 17:38:58 +01:00
|
|
|
self.version = Mai2Constants.VER_MAIMAI_DX_UNIVERSE
|
2023-03-15 21:03:22 +01:00
|
|
|
|
2024-01-09 09:07:04 +01:00
|
|
|
async def handle_cm_get_user_preview_api_request(self, data: Dict) -> Dict:
|
2024-01-09 20:42:17 +01:00
|
|
|
p = await self.data.profile.get_profile_detail(data["userId"], self.version)
|
2023-03-15 21:03:22 +01:00
|
|
|
if p is None:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
return {
|
|
|
|
"userName": p["userName"],
|
|
|
|
"rating": p["playerRating"],
|
2023-10-15 19:04:15 +02:00
|
|
|
# hardcode lastDataVersion for CardMaker
|
2023-03-15 21:03:22 +01:00
|
|
|
"lastDataVersion": "1.20.00",
|
2023-10-15 19:04:15 +02:00
|
|
|
# checks if the user is still logged in
|
2023-03-15 21:03:22 +01:00
|
|
|
"isLogin": False,
|
2023-10-15 19:04:15 +02:00
|
|
|
"isExistSellingCard": True,
|
2023-03-15 21:03:22 +01:00
|
|
|
}
|
|
|
|
|
2024-01-09 09:07:04 +01:00
|
|
|
async def handle_cm_get_user_data_api_request(self, data: Dict) -> Dict:
|
2023-03-15 21:03:22 +01:00
|
|
|
# user already exists, because the preview checks that already
|
2024-01-09 20:42:17 +01:00
|
|
|
p = await self.data.profile.get_profile_detail(data["userId"], self.version)
|
2023-03-15 21:03:22 +01:00
|
|
|
|
2024-01-09 20:42:17 +01:00
|
|
|
cards = await self.data.card.get_user_cards(data["userId"])
|
2023-03-15 21:03:22 +01:00
|
|
|
if cards is None or len(cards) == 0:
|
|
|
|
# This should never happen
|
|
|
|
self.logger.error(
|
|
|
|
f"handle_get_user_data_api_request: Internal error - No cards found for user id {data['userId']}"
|
|
|
|
)
|
|
|
|
return {}
|
|
|
|
|
|
|
|
# get the dict representation of the row so we can modify values
|
|
|
|
user_data = p._asdict()
|
|
|
|
|
|
|
|
# remove the values the game doesn't want
|
|
|
|
user_data.pop("id")
|
|
|
|
user_data.pop("user")
|
|
|
|
user_data.pop("version")
|
|
|
|
|
|
|
|
return {"userId": data["userId"], "userData": user_data}
|
|
|
|
|
2024-01-09 09:07:04 +01:00
|
|
|
async def handle_cm_login_api_request(self, data: Dict) -> Dict:
|
2023-03-15 21:03:22 +01:00
|
|
|
return {"returnCode": 1}
|
|
|
|
|
2024-01-09 09:07:04 +01:00
|
|
|
async def handle_cm_logout_api_request(self, data: Dict) -> Dict:
|
2023-03-15 21:03:22 +01:00
|
|
|
return {"returnCode": 1}
|
|
|
|
|
2024-01-09 09:07:04 +01:00
|
|
|
async def handle_cm_get_selling_card_api_request(self, data: Dict) -> Dict:
|
2024-01-09 20:42:17 +01:00
|
|
|
selling_cards = await self.data.static.get_enabled_cards(self.version)
|
2023-03-15 21:03:22 +01:00
|
|
|
if selling_cards is None:
|
|
|
|
return {"length": 0, "sellingCardList": []}
|
|
|
|
|
|
|
|
selling_card_list = []
|
|
|
|
for card in selling_cards:
|
|
|
|
tmp = card._asdict()
|
|
|
|
tmp.pop("id")
|
|
|
|
tmp.pop("version")
|
|
|
|
tmp.pop("cardName")
|
|
|
|
tmp.pop("enabled")
|
|
|
|
|
2023-10-15 19:04:15 +02:00
|
|
|
tmp["startDate"] = datetime.strftime(
|
|
|
|
tmp["startDate"], Mai2Constants.DATE_TIME_FORMAT
|
|
|
|
)
|
|
|
|
tmp["endDate"] = datetime.strftime(
|
|
|
|
tmp["endDate"], Mai2Constants.DATE_TIME_FORMAT
|
|
|
|
)
|
2023-03-15 21:03:22 +01:00
|
|
|
tmp["noticeStartDate"] = datetime.strftime(
|
2023-07-15 22:51:54 +02:00
|
|
|
tmp["noticeStartDate"], Mai2Constants.DATE_TIME_FORMAT
|
2023-03-15 21:03:22 +01:00
|
|
|
)
|
|
|
|
tmp["noticeEndDate"] = datetime.strftime(
|
2023-07-15 22:51:54 +02:00
|
|
|
tmp["noticeEndDate"], Mai2Constants.DATE_TIME_FORMAT
|
2023-03-15 21:03:22 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
selling_card_list.append(tmp)
|
|
|
|
|
|
|
|
return {"length": len(selling_card_list), "sellingCardList": selling_card_list}
|
|
|
|
|
2024-01-09 09:07:04 +01:00
|
|
|
async def handle_cm_get_user_card_api_request(self, data: Dict) -> Dict:
|
2024-01-09 20:42:17 +01:00
|
|
|
user_cards = await self.data.item.get_cards(data["userId"])
|
2023-03-15 21:03:22 +01:00
|
|
|
if user_cards is None:
|
|
|
|
return {"returnCode": 1, "length": 0, "nextIndex": 0, "userCardList": []}
|
|
|
|
|
|
|
|
max_ct = data["maxCount"]
|
|
|
|
next_idx = data["nextIndex"]
|
|
|
|
start_idx = next_idx
|
|
|
|
end_idx = max_ct + start_idx
|
|
|
|
|
|
|
|
if len(user_cards[start_idx:]) > max_ct:
|
|
|
|
next_idx += max_ct
|
|
|
|
else:
|
|
|
|
next_idx = 0
|
|
|
|
|
|
|
|
card_list = []
|
|
|
|
for card in user_cards:
|
|
|
|
tmp = card._asdict()
|
|
|
|
tmp.pop("id")
|
|
|
|
tmp.pop("user")
|
|
|
|
|
2023-05-30 12:14:18 +02:00
|
|
|
tmp["startDate"] = datetime.strftime(
|
|
|
|
tmp["startDate"], Mai2Constants.DATE_TIME_FORMAT
|
|
|
|
)
|
|
|
|
tmp["endDate"] = datetime.strftime(
|
|
|
|
tmp["endDate"], Mai2Constants.DATE_TIME_FORMAT
|
|
|
|
)
|
2023-03-15 21:03:22 +01:00
|
|
|
card_list.append(tmp)
|
|
|
|
|
|
|
|
return {
|
|
|
|
"returnCode": 1,
|
|
|
|
"length": len(card_list[start_idx:end_idx]),
|
|
|
|
"nextIndex": next_idx,
|
|
|
|
"userCardList": card_list[start_idx:end_idx],
|
|
|
|
}
|
|
|
|
|
2024-01-09 09:07:04 +01:00
|
|
|
async def handle_cm_get_user_item_api_request(self, data: Dict) -> Dict:
|
2023-03-15 21:03:22 +01:00
|
|
|
super().handle_get_user_item_api_request(data)
|
|
|
|
|
2024-01-09 09:07:04 +01:00
|
|
|
async def handle_cm_get_user_character_api_request(self, data: Dict) -> Dict:
|
2024-01-09 20:42:17 +01:00
|
|
|
characters = await self.data.item.get_characters(data["userId"])
|
2023-03-15 21:03:22 +01:00
|
|
|
|
|
|
|
chara_list = []
|
|
|
|
for chara in characters:
|
|
|
|
chara_list.append(
|
|
|
|
{
|
|
|
|
"characterId": chara["characterId"],
|
|
|
|
# no clue why those values are even needed
|
|
|
|
"point": 0,
|
|
|
|
"count": 0,
|
|
|
|
"level": chara["level"],
|
|
|
|
"nextAwake": 0,
|
|
|
|
"nextAwakePercent": 0,
|
|
|
|
"favorite": False,
|
|
|
|
"awakening": chara["awakening"],
|
|
|
|
"useCount": chara["useCount"],
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
return {
|
|
|
|
"returnCode": 1,
|
|
|
|
"length": len(chara_list),
|
|
|
|
"userCharacterList": chara_list,
|
|
|
|
}
|
|
|
|
|
2024-01-09 09:07:04 +01:00
|
|
|
async def handle_cm_get_user_card_print_error_api_request(self, data: Dict) -> Dict:
|
2023-03-15 21:03:22 +01:00
|
|
|
return {"length": 0, "userPrintDetailList": []}
|
|
|
|
|
2024-01-09 09:07:04 +01:00
|
|
|
async def handle_cm_upsert_user_print_api_request(self, data: Dict) -> Dict:
|
2023-03-15 21:03:22 +01:00
|
|
|
user_id = data["userId"]
|
|
|
|
upsert = data["userPrintDetail"]
|
|
|
|
|
|
|
|
# set a random card serial number
|
|
|
|
serial_id = "".join([str(randint(0, 9)) for _ in range(20)])
|
|
|
|
|
2023-05-30 12:14:18 +02:00
|
|
|
# calculate start and end date of the card
|
|
|
|
start_date = datetime.utcnow()
|
|
|
|
end_date = datetime.utcnow() + timedelta(days=15)
|
|
|
|
|
2023-03-15 21:03:22 +01:00
|
|
|
user_card = upsert["userCard"]
|
2024-01-09 20:42:17 +01:00
|
|
|
await self.data.item.put_card(
|
2023-03-15 21:03:22 +01:00
|
|
|
user_id,
|
|
|
|
user_card["cardId"],
|
|
|
|
user_card["cardTypeId"],
|
|
|
|
user_card["charaId"],
|
|
|
|
user_card["mapId"],
|
2023-05-30 12:14:18 +02:00
|
|
|
# add the correct start date and also the end date in 15 days
|
|
|
|
start_date,
|
|
|
|
end_date,
|
2023-03-15 21:03:22 +01:00
|
|
|
)
|
|
|
|
|
2023-05-30 12:14:18 +02:00
|
|
|
# get the profile extend to save the new bought card
|
2024-01-09 20:42:17 +01:00
|
|
|
extend = await self.data.profile.get_profile_extend(user_id, self.version)
|
2023-05-30 12:14:18 +02:00
|
|
|
if extend:
|
|
|
|
extend = extend._asdict()
|
|
|
|
# parse the selectedCardList
|
|
|
|
# 6 = Freedom Pass, 4 = Gold Pass (cardTypeId)
|
2023-07-05 16:47:43 +02:00
|
|
|
selected_cards: List = extend["selectedCardList"]
|
2023-05-30 12:14:18 +02:00
|
|
|
|
|
|
|
# if no pass is already added, add the corresponding pass
|
|
|
|
if not user_card["cardTypeId"] in selected_cards:
|
|
|
|
selected_cards.insert(0, user_card["cardTypeId"])
|
|
|
|
|
|
|
|
extend["selectedCardList"] = selected_cards
|
2024-01-09 20:42:17 +01:00
|
|
|
await self.data.profile.put_profile_extend(user_id, self.version, extend)
|
2023-05-30 12:14:18 +02:00
|
|
|
|
2023-03-15 21:03:22 +01:00
|
|
|
# properly format userPrintDetail for the database
|
|
|
|
upsert.pop("userCard")
|
|
|
|
upsert.pop("serialId")
|
|
|
|
upsert["printDate"] = datetime.strptime(upsert["printDate"], "%Y-%m-%d")
|
|
|
|
|
2024-01-09 20:42:17 +01:00
|
|
|
await self.data.item.put_user_print_detail(user_id, serial_id, upsert)
|
2023-03-15 21:03:22 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
"returnCode": 1,
|
|
|
|
"orderId": 0,
|
|
|
|
"serialId": serial_id,
|
2023-05-30 12:14:18 +02:00
|
|
|
"startDate": datetime.strftime(start_date, Mai2Constants.DATE_TIME_FORMAT),
|
|
|
|
"endDate": datetime.strftime(end_date, Mai2Constants.DATE_TIME_FORMAT),
|
2023-03-15 21:03:22 +01:00
|
|
|
}
|
|
|
|
|
2024-01-09 09:07:04 +01:00
|
|
|
async def handle_cm_upsert_user_printlog_api_request(self, data: Dict) -> Dict:
|
2023-03-15 21:03:22 +01:00
|
|
|
return {
|
|
|
|
"returnCode": 1,
|
|
|
|
"orderId": 0,
|
|
|
|
"serialId": data["userPrintlog"]["serialId"],
|
|
|
|
}
|
|
|
|
|
2024-01-09 09:07:04 +01:00
|
|
|
async def handle_cm_upsert_buy_card_api_request(self, data: Dict) -> Dict:
|
2023-03-15 21:03:22 +01:00
|
|
|
return {"returnCode": 1}
|