抽卡:添加抽卡接口
This commit is contained in:
@@ -1,10 +1,17 @@
|
||||
import { Application, BackendSession } from "pinus";
|
||||
import { resResult } from "../../../pubUtils/util";
|
||||
import { STATUS, GACHA_ID } from "../../../consts";
|
||||
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 } from "../../../domain/activityField/gachaField";
|
||||
import { GachaListReturn, Floor, Hope, GachaResult } from "../../../domain/activityField/gachaField";
|
||||
import { UserGachaModel } from "../../../db/UserGacha";
|
||||
import { refreshFreeCount } from "../../../services/gachaService";
|
||||
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) {
|
||||
@@ -31,7 +38,8 @@ export class GachaHandler {
|
||||
if(id == GACHA_ID.TIMELIMIT) continue; // 不包括限时
|
||||
|
||||
let userGacha = userGachaList.find(cur => cur.gachaId == id);
|
||||
userGacha = await refreshFreeCount(dicGacha, userGacha);
|
||||
userGacha = await refreshGacha(dicGacha, userGacha);
|
||||
console.log(JSON.stringify(userGacha))
|
||||
let param = new GachaListReturn(dicGacha, userGacha);
|
||||
list.push(param);
|
||||
}
|
||||
@@ -47,9 +55,81 @@ export class 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');
|
||||
|
||||
return resResult(STATUS.SUCCESS);
|
||||
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
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user