Files
ZYZ/game-server/app/servers/activity/handler/gachaHandler.ts
2021-04-23 15:46:04 +08:00

174 lines
7.2 KiB
TypeScript

import { Application, BackendSession } from "pinus";
import { resResult, getRandomWithWeight } from "../../../pubUtils/util";
import { STATUS, GACHA_ID, GACHA_CONTENT_TYPE, GACHA_OCCUPY_HID } from "../../../consts";
import { gameData } from "../../../pubUtils/data";
import { GachaListReturn, Floor, Hope, GachaResult } from "../../../domain/activityField/gachaField";
import { UserGachaModel } from "../../../db/UserGacha";
import { refreshGacha, getFloorResult, getResultFromContentId, transPiece } from "../../../services/gachaService";
import { RoleModel } from "../../../db/Role";
import { HeroModel, HeroUpdate } from "../../../db/Hero";
import { RewardInter } from "../../../pubUtils/interface";
import { handleCost, createHeroes, addItems } from "../../../services/rewardService";
import { CounterModel } from "../../../db/Counter";
import { getNextTime, getAfterDateByDay } from "../../../pubUtils/timeUtil";
import { UserGachaRecModel } from "../../../db/UserGachaRec";
export default function (app: Application) {
return new GachaHandler(app);
}
export class GachaHandler {
constructor(private app: Application) {
}
/**
* @description 获取抽卡列表 元宝招募、友情点招募、求贤若渴
* @param {{}} msg
* @param {BackendSession} session
* @memberof GachaHandler
*/
async getGachaList(msg: {}, session: BackendSession) {
const { } = msg;
const roleId: string = session.get('roleId');
let userGachaList = await UserGachaModel.findAllByRole(roleId);
let list: GachaListReturn[] = [];
for(let [id, dicGacha] of gameData.gacha) {
if(id == GACHA_ID.TIMELIMIT) continue; // 不包括限时
let userGacha = userGachaList.find(cur => cur.gachaId == id);
userGacha = await refreshGacha(dicGacha, userGacha);
console.log(JSON.stringify(userGacha))
let param = new GachaListReturn(dicGacha, userGacha);
list.push(param);
}
return resResult(STATUS.SUCCESS, { list });
}
/**
* @description 抽卡,所有抽卡类型都
* @param {{ gachaId: number, activityId: number, count: number }} msg
* @param {BackendSession} session
* @memberof GachaHandler
*/
async pull(msg: { gachaId: number, activityId: number, count: number }, session: BackendSession) {
const { gachaId, activityId, count } = msg;
if(gachaId == undefined|| activityId == undefined || count == undefined) return resResult(STATUS.WRONG_PARMS);
const roleId: string = session.get('roleId');
const roleName: string = session.get('roleName');
const sid: string = session.get('sid');
const serverId: number = session.get('serverId');
let { lv } = await RoleModel.findByRoleId(roleId);
let dicGacha = gameData.gacha.get(gachaId);
if(!dicGacha) return resResult(STATUS.DIC_DATA_NOT_FOUND);
if(!dicGacha.count.includes(count)) return resResult(STATUS.WRONG_PARMS);
let userGacha = await UserGachaModel.findByRole(roleId, gachaId, activityId);
let { floor, freeCount, hope, point, pickHero, refFreeTime } = await refreshGacha(dicGacha, userGacha);
if((gachaId == GACHA_ID.ASSIGN|| gachaId == GACHA_ID.TIMELIMIT) && pickHero) return resResult(STATUS.GACHA_NOT_ASSIGN);
let userHeroes = await HeroModel.findByRole(roleId);
let items: RewardInter[] = [], heroInfo: HeroUpdate[] = [], resultList: GachaResult[] = [];
for(let i = 0; i < count; i++) {
// 按照一般概率抽出
let { dic: { id: base } } = getRandomWithWeight(dicGacha.percent);
let contentId = getFloorResult(gachaId, base, floor);
if(contentId == false) return resResult(STATUS.DIC_DATA_NOT_FOUND);
let result = getResultFromContentId(contentId, lv, hope);
if(result.hid > 0) {
result.setSetPickHero(pickHero);
let hasHero = userHeroes.find(cur => cur.hid == result.hid);
if(hasHero) { // 已有转换为碎片
let { pieceId, count } = transPiece(result.hid);
result.transferToPiece(pieceId, count);
items.push({ id: pieceId, count });
} else {
let { heroId: hid, name: hName, initialStars: star, quality, jobid: job, initialSkin } = gameData.hero.get(result.hid);
heroInfo.push({ roleId, serverId, roleName, hid, hName, star, quality, job, skins:[{id: initialSkin, enable: true}] })
}
} else {
items.push({ id: result.id, count });
}
resultList.push(result);
}
console.log('***', dicGacha.free.count, dicGacha.free.day, freeCount, count);
let costNum = count;
if(dicGacha.free.count > 0) {
if(count > dicGacha.free.count - freeCount) {
costNum = count - dicGacha.free.count + freeCount;
freeCount = dicGacha.free.count;
} else {
costNum = 0;
freeCount += count;
}
}
// 消耗东西
if(costNum > 0) {
let cost = dicGacha.cost.map(cur => { return { id: cur.id, count: cur.count * costNum }});
let costResult = await handleCost(roleId, sid, cost);
if(!costResult) return resResult(STATUS.GACHA_COST_NOT_ENOUGH);
}
// 给东西
let heroes = await createHeroes(roleId, sid, serverId, heroInfo);
await addItems(roleId, roleName, sid, items);
// 更新数据
point += count;
userGacha = await UserGachaModel.updateInfo(roleId, gachaId, activityId, {
freeCount, hope, floor, count, point
});
await UserGachaRecModel.createRec(roleId, gachaId, activityId, count, resultList);
return resResult(STATUS.SUCCESS, {
gachaId, activityId,
freeCount, refFreeTime: getAfterDateByDay(refFreeTime, dicGacha.free.day), count, point, floor,
heroes, result: resultList
});
}
/**
* @description 设置心愿单
* @param {{ gachaId: number, hope: { id: number, hid: number }[] }} msg
* @param {BackendSession} session
* @memberof GachaHandler
*/
async setHope(msg: { gachaId: number, hope: { id: number, hid: number }[] }, session: BackendSession) {
const { gachaId, hope } = msg;
const roleId: string = session.get('roleId');
return resResult(STATUS.SUCCESS);
}
/**
* @description 转盘
* @param {{ gachaId: number }} msg
* @param {BackendSession} session
* @memberof GachaHandler
*/
async drawTurnTable(msg: { gachaId: number }, session: BackendSession) {
const { gachaId } = msg;
const roleId: string = session.get('roleId');
return resResult(STATUS.SUCCESS);
}
/**
* @description 求贤若渴&限时招募里面设置pick武将
* @param {{ gachaId: number, pickHero: number }} msg
* @param {BackendSession} session
* @memberof GachaHandler
*/
async setPickHero(msg: { gachaId: number, pickHero: number }, session: BackendSession) {
const { gachaId, pickHero } = msg;
const roleId: string = session.get('roleId');
return resResult(STATUS.SUCCESS);
}
}