Files
ZYZ/game-server/app/services/gachaService.ts
2021-04-21 21:10:26 +08:00

73 lines
2.0 KiB
TypeScript

import { GachaData, Floor } from "../domain/activityField/gachaField";;
import { ActivityModelType, ActivityModel } from "../db/Activity";
import { DicGacha } from "../pubUtils/dictionary/DicGacha";
import { UserGachaType, UserGachaModel } from "../db/UserGacha";
import { shouldRefresh } from "../pubUtils/util";
import { REFRESH_HOUR, GACHA_ID, GACHA_TO_FLOOR } from "../consts";
import { getNextDayByGap } from "../pubUtils/timeUtil";
/**
* 获取活动页签里的限时卡池
*
* @param aid 活动id
*/
export async function getLimitGacha(activityId: number) {
let activityData: ActivityModelType = await ActivityModel.findActivity(activityId, true);
if(!activityData) return false;
let gachaData = new GachaData(activityData);
let { heroes, gachaId } = gachaData;
return {
gachaId,
heroes,
pickHero: 0,
freeCount: 0,
refFreeTime: 0,
count: 0,
floor: [{
id: 3,
count: 0
}]
}
}
/**
* 刷新免费次数
* @param dicGacha
* @param userGacha
*/
export async function refreshFreeCount(dicGacha: DicGacha, userGacha: UserGachaType) {
let { day, count } = dicGacha.free;
if(count > 0) {
return userGacha;
}
let { roleId, gachaId, refFreeTime } = userGacha;
if(shouldRefresh(refFreeTime, new Date(), REFRESH_HOUR, day)) {
let ref = getNextDayByGap(refFreeTime, new Date(), day);
userGacha = await UserGachaModel.refreshFreeCount(roleId, gachaId, 0, ref);
}
return userGacha
}
/**
* @description 获取保底状态
* @param type 招募类型
* @param floor 玩家保底
*/
export function getFloorStatus(type: number, floor: Floor[]) {
let floorMap = new Map<number, number>();
for(let { id, count } of floor) {
floorMap.set(id, count);
}
let dicFloorType = GACHA_TO_FLOOR.get(type);
return dicFloorType.map(id => {
return {
id,
count: floorMap.get(id)||0
}
});
}