抽卡:500抽逻辑完成

This commit is contained in:
luying
2022-07-29 16:17:41 +08:00
parent 40af0edc28
commit 1ad6163bb5
11 changed files with 196 additions and 126 deletions
@@ -3,7 +3,7 @@ import { resResult, shouldRefresh, getRandSingleEelm, getGachaRemainFloor } from
import { STATUS, GACHA_TYPE, HERO_QUALITY_TYPE, TASK_TYPE, ITEM_CHANGE_REASON, } from "../../../consts";
import { gameData } from "../../../pubUtils/data";
import { UserGachaModel } from "../../../db/UserGacha";
import { refreshGacha, getGachaList, getVisitedHeroList, GachaPull, getGuideGachaId, GachaResults, getDicGachaByGachaCnt, getNormalGachaId } from "../../../services/activity/gachaService";
import { refreshGacha, getGachaList, getVisitedHeroList, GachaPull, GachaResults, getDicGachaByGachaCnt, getNormalGachaId } from "../../../services/activity/gachaService";
import { RoleModel } from "../../../db/Role";
import { HeroModel } from "../../../db/Hero";
import { handleCost, addItems } from "../../../services/role/rewardService";
@@ -13,8 +13,7 @@ import { RECRUIT } from "../../../pubUtils/dicParam";
import { getActivityById } from "../../../services/activity/activityService";
import { checkTaskInGacha } from "../../../services/task/taskService";
import { createHeroes } from "../../../services/role/createHero";
import { getRoleCreateTime, getServerCreateTime } from "../../../services/redisService";
import { getGuideGachaData } from "../../../services/activity/guideGachaService";
import { getGuideGachaData } from "../../../services/activity/gachaService";
export default function (app: Application) {
new HandlerService(app, {});
@@ -305,6 +304,8 @@ export class GachaHandler {
const serverId: number = session.get('serverId');
const { activityId } = msg;
if(!activityId) return resResult(STATUS.SUCCESS);
let playerData = await getGuideGachaData(serverId, activityId, roleId);
return resResult(STATUS.SUCCESS, { playerData });
@@ -317,34 +318,43 @@ export class GachaHandler {
* @memberof GachaHandler
*/
async guidePull(msg: { activityId: number; }, session: BackendSession) {
// const { } = msg;
// const roleId: string = session.get('roleId');
// const sid: string = session.get('sid');
// const count = 10;
// const gachaId = getGuideGachaId();
// const dicGacha = gameData.gacha.get(gachaId);
const roleId: string = session.get('roleId');
const serverId: number = session.get('serverId');
const sid: string = session.get('sid');
const count = 10;
// let userGacha = await UserGachaModel.findByRole(roleId, gachaId);
// let { guideCount = 0 } = userGacha;
// if(guideCount >= RECRUIT.RECRUIT_FIRST_RECRUIT) return resResult(STATUS.GACHA_GUIDE_PULL_CNT_LACK);
const { activityId } = msg;
let playerData = await getGuideGachaData(serverId, activityId, roleId);
// if(guideCount == 0) {
// // 消耗东西
// let cost = dicGacha.cost.map(cur => { return { id: cur.id, count: cur.count * count } });
// let costResult = await handleCost(roleId, sid, cost, ITEM_CHANGE_REASON.GACHA_PULL);
// if (!costResult) return resResult(STATUS.GACHA_COST_NOT_ENOUGH);
// }
const gachaId = playerData.gachaId;
const dicGacha = gameData.gacha.get(gachaId);
if (!dicGacha) return resResult(STATUS.DIC_DATA_NOT_FOUND);
// let userHeroes = await HeroModel.findByRole(roleId);
// let gachaPull = new GachaPull(dicGacha, userGacha);
// let { resultList } = gachaPull.pull(count, userHeroes);
if(!playerData.canPull()) return resResult(STATUS.GACHA_GUIDE_PULL_CNT_LACK);
// userGacha = await UserGachaModel.updateInfo(roleId, gachaId, 0, { guideResultList: resultList, guideCount: guideCount + 1 })
if(playerData.pullCnt == 0) {
// 消耗东西
let cost = dicGacha.cost.map(cur => { return { id: cur.id, count: cur.count * count } });
let costResult = await handleCost(roleId, sid, cost, ITEM_CHANGE_REASON.GACHA_PULL);
if (!costResult) return resResult(STATUS.GACHA_COST_NOT_ENOUGH);
}
// return resResult(STATUS.SUCCESS, {
// guideCount: userGacha.guideCount,
// latest: userGacha.guideResultList
// });
let userHeroes = await HeroModel.findByRole(roleId);
let gachaPull = new GachaPull(dicGacha);
let { resultList } = gachaPull.pull(count, userHeroes);
let userGacha = await UserGachaModel.updateInfo(roleId, gachaId, activityId, { guideResultList: resultList }, { pullCnt: 1 });
playerData.setPlayerData(userGacha);
if(playerData.autoSave()) { // 最后几次自动保存
await UserGachaModel.updateInfo(roleId, gachaId, activityId, { candidates: playerData.candidates });
}
return resResult(STATUS.SUCCESS, {
activityId,
pullCnt: playerData.pullCnt,
result: playerData.latest,
candidates: playerData.candidates
});
}
/**
@@ -353,29 +363,27 @@ export class GachaHandler {
* @param {BackendSession} session
* @memberof GachaHandler
*/
async saveToCandidates(msg: { id: number }, session: BackendSession) {
// const { id } = msg;
// const roleId: string = session.get('roleId');
// const gachaId = getGuideGachaId();
async saveToCandidates(msg: { activityId: number, id: number }, session: BackendSession) {
const roleId: string = session.get('roleId');
const serverId: number = session.get('serverId');
// let userGacha = await UserGachaModel.findByRole(roleId, gachaId);
// let { guideResultList, candidates = [], guideCount } = userGacha;
const { activityId, id } = msg;
let playerData = await getGuideGachaData(serverId, activityId, roleId);
let gachaId = playerData.gachaId;
const dicGacha = gameData.gacha.get(gachaId);
if (!dicGacha) return resResult(STATUS.DIC_DATA_NOT_FOUND);
// let index = candidates.findIndex(cur => cur.id == id);
// if(index == -1) {
// candidates.push({
// id, guideCount, list: guideResultList
// });
// } else {
// candidates[index].list = guideResultList;
// candidates[index].guideCount = guideCount;
// }
let result = playerData.setCandidate(id);
if(!result) return resResult(STATUS.GACHA_GUIDE_NOT_DO)
// userGacha = await UserGachaModel.updateInfo(roleId, gachaId, 0, { candidates })
let userGacha = await UserGachaModel.updateInfo(roleId, gachaId, activityId, { candidates: playerData.candidates });
playerData.setPlayerData(userGacha);
// return resResult(STATUS.SUCCESS, {
// candidates: userGacha.candidates
// });
return resResult(STATUS.SUCCESS, {
activityId,
pullCnt: playerData.pullCnt,
candidates: playerData.candidates
});
}
/**
@@ -384,64 +392,55 @@ export class GachaHandler {
* @param {BackendSession} session
* @memberof GachaHandler
*/
async decide(msg: { id: number }, session: BackendSession) {
// const { id } = msg;
// const roleId: string = session.get('roleId');
// const roleName: string = session.get('roleName');
// const serverId: number = session.get('serverId');
// const sid: string = session.get('sid');
// const count = 10;
// const gachaId = getGuideGachaId();
// const dicGacha = gameData.gacha.get(gachaId);
async decide(msg: { activityId: number, id: number }, session: BackendSession) {
const { activityId, id } = msg;
const roleId: string = session.get('roleId');
const roleName: string = session.get('roleName');
const serverId: number = session.get('serverId');
const sid: string = session.get('sid');
const count = 10;
// let role = await RoleModel.findByRoleId(roleId);
// if(role.gachaHasGuide) {
// return resResult(STATUS.GACHA_GUIDE_HAS_DONE);
// }
// let userGacha = await UserGachaModel.findByRole(roleId, gachaId);
// let { guideResultList = [], candidates = [], } = userGacha;
let playerData = await getGuideGachaData(serverId, activityId, roleId);
if(!playerData.canDecide()) return resResult(STATUS.GACHA_GUIDE_HAS_DONE);
let gachaId = playerData.gachaId;
const dicGacha = gameData.gacha.get(gachaId);
if (!dicGacha) return resResult(STATUS.DIC_DATA_NOT_FOUND);
// let simpleResult: SimpleResult[] = []
// if(id == 0) {
// if(guideResultList.length <= 0) {
// return resResult(STATUS.GACHA_GUIDE_NOT_DO);
// }
// simpleResult = guideResultList;
// } else {
// let candidate = candidates.find(cur => cur.id == id);
// if(!candidate) return resResult(STATUS.WRONG_PARMS);
// simpleResult = candidate.list;
// }
let candidate = playerData.chooseCandidate(id);
if(!candidate) return resResult(STATUS.GACHA_GUIDE_NO_CANDIDATE);
// let userHeroes = await HeroModel.findByRole(roleId);
// let results = new GachaResults();
// results.addBySimpleResult(simpleResult);
// let { items, heroInfo, resultList } = results.transferToFinalResult(userHeroes);
let userHeroes = await HeroModel.findByRole(roleId);
let results = new GachaResults();
results.addBySimpleResult(candidate.list);
let { items, heroInfo, resultList } = results.transferToFinalResult(userHeroes);
// userGacha = await UserGachaModel.updateInfo(roleId, gachaId, 0, { guideResultCount: count });
let userGacha = await UserGachaModel.updateInfo(roleId, gachaId, activityId, { candidates: playerData.candidates });
playerData.setPlayerData(userGacha);
// // 给东西
// // console.log('****', heroInfo)
// let { heroes, resultHeroes } = await createHeroes(roleId, roleName, sid, serverId, heroInfo);
// await addItems(roleId, roleName, sid, items, ITEM_CHANGE_REASON.GACHA_ITEMS);
// // 更新数据
// role = await RoleModel.updateRoleInfo(roleId, { gachaHasGuide: true });
// await UserGachaRecModel.createRec(roleId, gachaId, 0, count, resultList);
// 给东西
let { heroes, resultHeroes } = await createHeroes(roleId, roleName, sid, serverId, heroInfo);
await addItems(roleId, roleName, sid, items, ITEM_CHANGE_REASON.GACHA_ITEMS);
// 更新数据
await UserGachaRecModel.createRec(roleId, gachaId, 0, count, resultList);
// // 任务
// if(dicGacha.isTask) await checkTaskInGacha(serverId, roleId, sid, count, resultHeroes, resultList, true);
// 任务
if(dicGacha.isTask) await checkTaskInGacha(serverId, roleId, sid, count, resultHeroes, resultList, true);
// return resResult(STATUS.SUCCESS, {
// hasInit: !!role.gachaHasGuide,
// guideResultCount: userGacha.guideResultCount,
// count: userGacha.count,
// gachaId,
// // heroes: resultHeroes,
// addHeros: heroes,
// result: resultList
// });
return resResult(STATUS.SUCCESS, {
activityId,
pullCnt: playerData.pullCnt,
addHeros: heroes,
result: resultList
});
}
async debugClearGachaRecord(msg: {}, session: BackendSession) {
const roleId = session.get('roleId');
let serverId = session.get('serverId');
await UserGachaModel.deleteMany({ roleId });
return resResult(STATUS.SUCCESS);
}
async test(msg: { gachaId: number, count: number, isGuide: boolean }, session: BackendSession) {
const { gachaId, count, isGuide } = msg;