抽卡:修改抽卡奖池逻辑

This commit is contained in:
luying
2022-07-08 14:09:18 +08:00
parent e4d8708898
commit 35c7c57bda
20 changed files with 695 additions and 579 deletions
@@ -1,10 +1,10 @@
import { Application, BackendSession, HandlerService, } from "pinus";
import { resResult, shouldRefresh, getRandSingleEelm } from "../../../pubUtils/util";
import { STATUS, GACHA_ID, HERO_QUALITY_TYPE, TASK_TYPE, ITEM_CHANGE_REASON, } from "../../../consts";
import { resResult, shouldRefresh, getRandSingleEelm, getGachaRemainFloor } from "../../../pubUtils/util";
import { STATUS, GACHA_TYPE, HERO_QUALITY_TYPE, TASK_TYPE, ITEM_CHANGE_REASON, } from "../../../consts";
import { gameData } from "../../../pubUtils/data";
import { GachaResult, GachaData } from "../../../domain/activityField/gachaField";
import { UserGachaModel } from "../../../db/UserGacha";
import { refreshGacha, getGachaList, getVisitedHeroList, getAllHeroByQuality, GachaPull } from "../../../services/activity/gachaService";
import { GachaData, GachaResultIndb } from "../../../domain/activityField/gachaField";
import { SimpleResult, UserGachaModel } from "../../../db/UserGacha";
import { refreshGacha, getGachaList, getVisitedHeroList, GachaPull, getGuideGachaId, GachaResults, getDicGachaByGachaCnt } from "../../../services/activity/gachaService";
import { RoleModel } from "../../../db/Role";
import { HeroModel } from "../../../db/Hero";
import { handleCost, addItems } from "../../../services/role/rewardService";
@@ -61,11 +61,13 @@ export class GachaHandler {
let userGacha = await UserGachaModel.findByRole(roleId, gachaId, activityId);
userGacha = await refreshGacha(dicGacha, userGacha);
let { freeCount, point, pickHero, refFreeTime, count: historyCount } = userGacha;
if ((gachaId == GACHA_ID.ASSIGN || gachaId == GACHA_ID.TIMELIMIT) && !pickHero) return resResult(STATUS.GACHA_NOT_ASSIGN);
if ((gachaId == GACHA_TYPE.ASSIGN || gachaId == GACHA_TYPE.ACTIVITY) && !pickHero) return resResult(STATUS.GACHA_NOT_ASSIGN);
dicGacha = getDicGachaByGachaCnt(dicGacha, historyCount);
if (!dicGacha) return resResult(STATUS.DIC_DATA_NOT_FOUND);
let gachaPull = new GachaPull(dicGacha, userGacha);
let userHeroes = await HeroModel.findByRole(roleId);
let gachaPull = new GachaPull(gachaId);
gachaPull.setByUserGacha(dicGacha, userGacha, false);
let { resultList, heroInfo, items } = gachaPull.pull(count, userHeroes);
let { hope, floor } = gachaPull.getUserGachaParam();
@@ -95,14 +97,15 @@ export class GachaHandler {
});
await UserGachaRecModel.createRec(roleId, gachaId, activityId, count, resultList);
await checkTaskInGacha(serverId, roleId, sid, count, resultHeroes, resultList, false);
if(dicGacha.isTask) await checkTaskInGacha(serverId, roleId, sid, count, resultHeroes, resultList, false);
return resResult(STATUS.SUCCESS, {
gachaId, activityId,
freeCount, refFreeTime: userGacha.refFreeTime, count: userGacha.count, point: userGacha.point, floor, hope,
guideResultCount: userGacha.guideResultCount,
// heroes: resultHeroes,
addHeros: heroes,
result: resultList
result: resultList,
remainFloor: getGachaRemainFloor(gachaId, floor)
});
}
@@ -166,11 +169,11 @@ export class GachaHandler {
let { point, turntable, costPoint } = userGacha;
if (point < RECRUIT.RECRUIT_BONUS_RECRUIT) return resResult(STATUS.GACHA_TURNTABLE_POINT_NOT_ENOUGH);
let turntablePool: { quality: number, count: number }[] = [];
for (let { quality, count } of gameData.gachaTurntable) {
let turntablePool: { quality: number, count: number, planId: number }[] = [];
for (let { quality, count, planId } of gameData.gachaTurntable) {
let myTurntable = turntable.find(cur => cur.quality == quality);
if (!myTurntable || !myTurntable.hasGet) {
turntablePool.push({ quality, count });
turntablePool.push({ quality, count, planId });
}
}
if (turntablePool.length <= 0) { // 重置
@@ -178,32 +181,21 @@ export class GachaHandler {
}
let randTurntable = getRandSingleEelm(turntablePool);
let { quality, count } = randTurntable;
// 根据品质查武将
let pool = getAllHeroByQuality(quality);
let hero = getRandSingleEelm(pool);
let contentId = gameData.gachaContentHero.get(hero);
// 获得或者转成碎片
let heroInfo: { hid: number, count: number }[] = [];
for (let i = 0; i < count; i++) {
heroInfo.push({ hid: hero, count: 1 })
}
let { heroes, resultHeroes, goods } = await createHeroes(roleId, roleName, sid, serverId, heroInfo);
let resultList: GachaResult[] = [];
for (let h of resultHeroes) {
let result = new GachaResult(contentId);
result.setHero(h.hid, 1);
resultList.push(result);
}
for (let g of goods) {
let result = new GachaResult(contentId);
result.setHero(hero, 1);
result.transferToPiece(g.id, g.count);
resultList.push(result);
}
let { quality, count, planId } = randTurntable;
let userHeroes = await HeroModel.findByRole(roleId);
let gachaResults = new GachaResults();
gachaResults.addPlan(planId);
gachaResults.processDetail(null, count);
let { items, heroInfo, resultList } = gachaResults.transferToFinalResult(userHeroes);
// 给东西
// console.log('****', heroInfo)
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);
// 记录
let myTurntable = turntable.find(cur => cur.quality == quality);
if (!myTurntable) {
@@ -235,13 +227,13 @@ export class GachaHandler {
const { gachaId, activityId = 0, pickHero } = msg;
const roleId: string = session.get('roleId');
const serverId: number = session.get('serverId');
if (gachaId != GACHA_ID.ASSIGN && gachaId != GACHA_ID.TIMELIMIT)
if (gachaId != GACHA_TYPE.ASSIGN && gachaId != GACHA_TYPE.ACTIVITY)
return resResult(STATUS.WRONG_PARMS);
let dicHero = gameData.hero.get(pickHero);
if (!dicHero) return resResult(STATUS.DIC_DATA_NOT_FOUND);
let heroes: number[] = [];
if (gachaId == GACHA_ID.TIMELIMIT) {
if (gachaId == GACHA_TYPE.ACTIVITY) {
let activityData = await getActivityById(activityId);
if (!activityData) return resResult(STATUS.ACTIVITY_MISSING);
@@ -299,7 +291,7 @@ export class GachaHandler {
if (index == -1) return resResult(STATUS.GACHA_CAN_NOT_PICK);
let { pieceId } = dicHero;
let { visitedHero, refVisitedTime } = await UserGachaModel.findByRole(roleId, GACHA_ID.NORMAL, 0);
let { visitedHero, refVisitedTime } = await UserGachaModel.findByRole(roleId, GACHA_TYPE.NORMAL, 0);
if (shouldRefresh(refVisitedTime, new Date())) {
visitedHero = [];
refVisitedTime = getZeroPointD();
@@ -312,7 +304,7 @@ export class GachaHandler {
}
visitedHero.push(hid);
let userGacha = await UserGachaModel.updateInfo(roleId, GACHA_ID.NORMAL, 0, { visitedHero, refVisitedTime });
let userGacha = await UserGachaModel.updateInfo(roleId, GACHA_TYPE.NORMAL, 0, { visitedHero, refVisitedTime });
let goods = await addItems(roleId, roleName, sid, [{ id: pieceId, count: RECRUIT.RECRUIT_SHARD_LIMIT }], ITEM_CHANGE_REASON.VISIT_HERO);
return resResult(STATUS.SUCCESS, {
@@ -333,7 +325,7 @@ export class GachaHandler {
let { gachaHasGuide } = await RoleModel.findByRoleId(roleId, 'gachaHasGuide');
let guideCount = 0, guideResultList = [], candidates = [];
if(!gachaHasGuide) {
const userGacha = await UserGachaModel.findByRole(roleId, GACHA_ID.NORMAL);
const userGacha = await UserGachaModel.findByRole(roleId, GACHA_TYPE.NORMAL);
({ guideCount = 0, guideResultList = [], candidates = [] } = userGacha);
}
@@ -354,11 +346,11 @@ export class GachaHandler {
const { } = msg;
const roleId: string = session.get('roleId');
const sid: string = session.get('sid');
const gachaId = GACHA_ID.NORMAL;
const count = 10;
const gachaId = getGuideGachaId();
const dicGacha = gameData.gacha.get(gachaId);
let userGacha = await UserGachaModel.findByRole(roleId, GACHA_ID.NORMAL);
let userGacha = await UserGachaModel.findByRole(roleId, GACHA_TYPE.NORMAL);
let { guideCount = 0 } = userGacha;
if(guideCount >= RECRUIT.RECRUIT_FIRST_RECRUIT) return resResult(STATUS.GACHA_GUIDE_PULL_CNT_LACK);
@@ -370,8 +362,7 @@ export class GachaHandler {
}
let userHeroes = await HeroModel.findByRole(roleId);
let gachaPull = new GachaPull(gachaId);
gachaPull.setByUserGacha(dicGacha, userGacha, true);
let gachaPull = new GachaPull(dicGacha, userGacha);
let { resultList } = gachaPull.pull(count, userHeroes);
userGacha = await UserGachaModel.updateInfo(roleId, gachaId, 0, { guideResultList: resultList, guideCount: guideCount + 1 })
@@ -391,9 +382,9 @@ export class GachaHandler {
async saveToCandidates(msg: { id: number }, session: BackendSession) {
const { id } = msg;
const roleId: string = session.get('roleId');
const gachaId = GACHA_ID.NORMAL;
const gachaId = getGuideGachaId();
let userGacha = await UserGachaModel.findByRole(roleId, GACHA_ID.NORMAL);
let userGacha = await UserGachaModel.findByRole(roleId, gachaId);
let { guideResultList, candidates = [], guideCount } = userGacha;
let index = candidates.findIndex(cur => cur.id == id);
@@ -425,17 +416,18 @@ export class GachaHandler {
const roleName: string = session.get('roleName');
const serverId: number = session.get('serverId');
const sid: string = session.get('sid');
const gachaId = GACHA_ID.NORMAL;
const count = 10;
const gachaId = getGuideGachaId();
const dicGacha = gameData.gacha.get(gachaId);
let role = await RoleModel.findByRoleId(roleId);
if(role.gachaHasGuide) {
return resResult(STATUS.GACHA_GUIDE_HAS_DONE);
}
let userGacha = await UserGachaModel.findByRole(roleId, GACHA_ID.NORMAL);
let userGacha = await UserGachaModel.findByRole(roleId, gachaId);
let { guideResultList = [], candidates = [], } = userGacha;
let simpleResult: { contentId: number, hid: number }[] = []
let simpleResult: SimpleResult[] = []
if(id == 0) {
if(guideResultList.length <= 0) {
return resResult(STATUS.GACHA_GUIDE_NOT_DO);
@@ -448,8 +440,9 @@ export class GachaHandler {
}
let userHeroes = await HeroModel.findByRole(roleId);
let gachaPull = new GachaPull(gachaId);
let { items, heroInfo, resultList } = gachaPull.pullBySimpleResult(simpleResult, userHeroes);
let results = new GachaResults();
results.addBySimpleResult(simpleResult);
let { items, heroInfo, resultList } = results.transferToFinalResult(userHeroes);
userGacha = await UserGachaModel.updateInfo(roleId, gachaId, 0, { guideResultCount: count });
@@ -462,7 +455,7 @@ export class GachaHandler {
await UserGachaRecModel.createRec(roleId, gachaId, 0, count, resultList);
// 任务
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,
@@ -1,6 +1,6 @@
import { Application, BackendSession, HandlerService, } from 'pinus';
import { resResult } from '../../../pubUtils/util';
import { STATUS, GACHA_ID, ITEM_CHANGE_REASON } from '../../../consts';
import { STATUS, GACHA_TYPE, ITEM_CHANGE_REASON } from '../../../consts';
import { getPlayerNewHeroGachaData } from '../../../services/activity/newHeroGachaService';
import { RoleModel } from '../../../db/Role';
import { HeroModel } from '../../../db/Hero';
@@ -62,50 +62,50 @@ export class NewHeroGachaHandler {
* @param {BackendSession} session
* @memberof NewHeroGachaHandler
*/
async pull(msg: { activityId: number, hid: number, count: number }, session: BackendSession) {
const { activityId, hid, count } = msg;
const roleId = session.get('roleId');
const serverId = session.get('serverId');
const sid = session.get('sid');
const roleName = session.get('roleName');
// async pull(msg: { activityId: number, hid: number, count: number }, session: BackendSession) {
// const { activityId, hid, count } = msg;
// const roleId = session.get('roleId');
// const serverId = session.get('serverId');
// const sid = session.get('sid');
// const roleName = session.get('roleName');
if(count < 0) return resResult(STATUS.WRONG_PARMS);
// if(count < 0) return resResult(STATUS.WRONG_PARMS);
let playerData = await getPlayerNewHeroGachaData(activityId, serverId, roleId)
if (!playerData) return resResult(STATUS.ACTIVITY_MISSING);
// let playerData = await getPlayerNewHeroGachaData(activityId, serverId, roleId)
// if (!playerData) return resResult(STATUS.ACTIVITY_MISSING);
let item = playerData.findItem(hid);
if (!item) {
return resResult(STATUS.ACTIVITY_DATA_ERROR);
}
// let item = playerData.findItem(hid);
// if (!item) {
// return resResult(STATUS.ACTIVITY_DATA_ERROR);
// }
let userHeroes = await HeroModel.findByRole(roleId);
// let userHeroes = await HeroModel.findByRole(roleId);
let gachaPull = new GachaPull(GACHA_ID.TIMELIMIT);
gachaPull.setByActivity(item);
let { items, heroInfo, resultList } = gachaPull.pull(count, userHeroes);
let { hasGetFloor, floorCount } = gachaPull.getActivityParam();
// let gachaPull = new GachaPull(GACHA_TYPE.TIMELIMIT);
// gachaPull.setByActivity(item);
// let { items, heroInfo, resultList } = gachaPull.pull(count, userHeroes);
// let { hasGetFloor, floorCount } = gachaPull.getActivityParam();
// 消耗东西
let cost = item.cost.map(cur => { return { id: cur.id, count: cur.count * count } });
let costResult = await handleCost(roleId, sid, cost, ITEM_CHANGE_REASON.NEW_HERO_GACHA_PULL);
if (!costResult) return resResult(STATUS.ACTIVITY_RES_NOT_ENOUGH);
// // 消耗东西
// let cost = item.cost.map(cur => { return { id: cur.id, count: cur.count * count } });
// let costResult = await handleCost(roleId, sid, cost, ITEM_CHANGE_REASON.NEW_HERO_GACHA_PULL);
// if (!costResult) return resResult(STATUS.ACTIVITY_RES_NOT_ENOUGH);
//记录数据
await ActivityNewHeroGachaModel.addRecord(serverId, activityId, roleId, hid, floorCount, JSON.stringify(resultList), hasGetFloor);
// //记录数据
// await ActivityNewHeroGachaModel.addRecord(serverId, activityId, roleId, hid, floorCount, JSON.stringify(resultList), hasGetFloor);
// 给东西
let { heroes, resultHeroes } = await createHeroes(roleId, roleName, sid, serverId, heroInfo);
await addItems(roleId, roleName, sid, items, ITEM_CHANGE_REASON.GACHA_ITEMS);
//固定奖励
let rewardParamArr: Array<RewardParam> = stringToRewardParam(item.commonReward);
let newRewardParamArr: Array<RewardParam> = rewardParamArr.map(obj => { return { id: obj.id, type: obj.type, count: obj.count * count } });
let commonResult = await addReward(roleId, roleName, sid, serverId, newRewardParamArr, ITEM_CHANGE_REASON.NEW_HERO_GACHA_PULL)
// // 给东西
// let { heroes, resultHeroes } = await createHeroes(roleId, roleName, sid, serverId, heroInfo);
// await addItems(roleId, roleName, sid, items, ITEM_CHANGE_REASON.GACHA_ITEMS);
// //固定奖励
// let rewardParamArr: Array<RewardParam> = stringToRewardParam(item.commonReward);
// let newRewardParamArr: Array<RewardParam> = rewardParamArr.map(obj => { return { id: obj.id, type: obj.type, count: obj.count * count } });
// let commonResult = await addReward(roleId, roleName, sid, serverId, newRewardParamArr, ITEM_CHANGE_REASON.NEW_HERO_GACHA_PULL)
return resResult(STATUS.SUCCESS, {
hid, activityId, count, randomReward: { heroes: resultHeroes.map(cur => ({hid: cur.hid})), result: resultList }, commonReward: commonResult
// return resResult(STATUS.SUCCESS, {
// hid, activityId, count, randomReward: { heroes: resultHeroes.map(cur => ({hid: cur.hid})), result: resultList }, commonReward: commonResult
});
}
// });
// }
}
+348 -296
View File
@@ -1,15 +1,13 @@
import { GachaData, Floor, GachaResult, Hope, GachaListReturn } from "../../domain/activityField/gachaField";;
import { GachaData, Floor as UserFloor, Hope as UserHope, GachaListReturn, GachaResultIndb } from "../../domain/activityField/gachaField";;
import { DicGacha } from "../../pubUtils/dictionary/DicGacha";
import { UserGachaType, UserGachaModel } from "../../db/UserGacha";
import { UserGachaType, UserGachaModel, SimpleResult } from "../../db/UserGacha";
import { shouldRefresh, getRandEelm, getRandEelmWithWeight, getRandSingleIndex, getRandSingleEelm } from "../../pubUtils/util";
import { REFRESH_TIME, GACHA_TO_FLOOR, GACHA_FLOOR_TYPE, GACHA_CONTENT_TYPE, HERO_QUALITY_TYPE, GACHA_OCCUPY_HID, IT_TYPE, ITID, CONSUME_TYPE, SPECIAL_ATTR, TIME_OUTPUT_TYPE, GACHA_ID } from "../../consts";
import { REFRESH_TIME, GACHA_FLOOR_TYPE, HERO_QUALITY_TYPE, IT_TYPE, ITID, CONSUME_TYPE, SPECIAL_ATTR, TIME_OUTPUT_TYPE, GACHA_TYPE, GACHA_PLAN_TYPE } from "../../consts";
import { getTimeFunD, getZeroPointD } from "../../pubUtils/timeUtil";
import { gameData, getDicGachaFloor, getGachaContentOfHeroQuality } from "../../pubUtils/data";
import { RoleModel } from "../../db/Role";
import { gameData } from "../../pubUtils/data";
import { RewardInter } from "../../pubUtils/interface";
import { CreateHeroParam } from "../../domain/roleField/hero";
import { HeroType } from "../../db/Hero";
import { NewHeroGachaItem } from "../../domain/activityField/newHeroGachaField";
import { getActivityById } from "./activityService";
import { transPiece } from "../role/util";
import { getRoleCreateTime, getServerCreateTime } from "../redisService";
@@ -22,8 +20,8 @@ export async function getGachaList(roleId: string) {
let userGachaList = await UserGachaModel.findAllByRole(roleId);
let list: GachaListReturn[] = [];
for (let [id, dicGacha] of gameData.gacha) {
if (id == GACHA_ID.TIMELIMIT) continue; // 不包括限时
if(!dicGacha.showInMainPage) continue;
let userGacha = userGachaList.find(cur => cur.gachaId == id);
if (userGacha)
userGacha = await refreshGacha(dicGacha, userGacha);
@@ -53,6 +51,11 @@ export async function getLimitGacha(serverId: number, roleId: string, activityId
return gachaData
}
export function getGuideGachaId() {
let ids = gameData.gachaByType.get(GACHA_TYPE.GUIDE);
return ids[0];
}
/**
* 刷新免费次数
* @param dicGacha
@@ -81,7 +84,7 @@ export async function refreshGacha(dicGacha: DicGacha, userGacha: UserGachaType)
export async function getVisitedHeroList(roleId: string) {
let { visitedHero, refVisitedTime } = await UserGachaModel.findByRole(roleId, GACHA_ID.NORMAL, 0);
let { visitedHero, refVisitedTime } = await UserGachaModel.findByRole(roleId, GACHA_TYPE.NORMAL, 0);
if (shouldRefresh(refVisitedTime, new Date())) {
visitedHero = [];
}
@@ -91,23 +94,20 @@ export async function getVisitedHeroList(roleId: string) {
class PlayerGachaRecord {
public lv: number; // 玩家等级
public hope: Hope[] = []; // 玩家记录的心愿单达成情况
public floor: Floor[] = [];
public hope: UserHope[] = []; // 玩家记录的心愿单达成情况
public floor: UserFloor[] = [];
public pickHero: number = 0;
constructor(userGacha: UserGachaType, item: NewHeroGachaItem) {
constructor(userGacha: UserGachaType ) {
if(userGacha) {
this.floor = userGacha.floor;
this.hope = userGacha.hope;
this.pickHero = userGacha.pickHero;
}
if(item) {
this.floor.push({ id: GACHA_FLOOR_TYPE.ASSIGN, count: item.count, hasGetFloor: item.hasGetFloor });
}
}
public getFloorCountByFloorType(floorType: GACHA_FLOOR_TYPE) {
let userFloor = this.floor.find(cur => cur.id == floorType);
public getFloorCountById(id: number) {
let userFloor = this.floor.find(cur => cur.id == id);
if(userFloor) {
return { floorCount: userFloor.count, hasGetFloor: userFloor.hasGetFloor }
} else {
@@ -115,10 +115,10 @@ class PlayerGachaRecord {
}
}
public setFloorCount(floorType: GACHA_FLOOR_TYPE, count: number, hasGetFloor: boolean) {
let index = this.floor.findIndex(cur => cur.id == floorType);
public setFloorCount(id: number, count: number, hasGetFloor: boolean) {
let index = this.floor.findIndex(cur => cur.id == id);
if(index == -1) {
this.floor.push({ id: floorType, count, hasGetFloor })
this.floor.push({ id, count, hasGetFloor })
} else {
this.floor[index].count = count;
this.floor[index].hasGetFloor = hasGetFloor;
@@ -142,223 +142,113 @@ class PlayerGachaRecord {
}
}
class GachaResults {
export class GachaResult {
planId: number = 0; // 计划id
hid: number = 0; // 武将id
isTransfer: boolean = false; // 是否转换为碎片
id: number = 0; // 道具id
count: number = 0; // 道具数量
pickHero: number = 0; // 玩家选择的武将
heroQuality: number = 0; // planId对应的武将品质
constructor(planId: number) {
this.planId = planId;
this.heroQuality = gameData.gachaPlanQuality.get(planId);
}
public changePlan(planId: number, heroQuality: number) {
this.planId = planId;
this.heroQuality = heroQuality;
}
public isTarget(floor: GachaFloor) {
return floor.heroQuality == this.heroQuality;
}
public setHero(hid: number, count: number) {
this.hid = hid;
this.count = count;
}
public setItem(id: number, count: number) {
this.id = id;
if(count) this.count = count;
}
}
export class GachaResults {
count: number = 0;
list: GachaResult[] = [];
addResult(contentId: number, id?: number) {
public addPlan(planId: number) {
this.count++;
this.list.push(new GachaResult(contentId, id));
this.list.push(new GachaResult(planId));
}
public shouldPurpleFloor(floorCount: number) {
return this.count % floorCount == 0 && !this.hasPurple();
}
public hasPurple() {
return this.list.findIndex(gachaResult => this.checkContentIsHero(gachaResult.contentId, HERO_QUALITY_TYPE.PURPLE)) != -1;
}
private checkContentIsHero(contentId: number, quality: HERO_QUALITY_TYPE) {
let {type, param} = gameData.gachaContent.get(contentId);
return type == GACHA_CONTENT_TYPE.HERO && param[0] >= quality;
}
public getRandItemNotPurple() {
let indexes = this.filterIndex(item => {
return !this.checkContentIsHero(item.contentId, HERO_QUALITY_TYPE.PURPLE);
});
let index = getRandSingleEelm(indexes);
return this.list[index];
}
private filterIndex(callback: (item: GachaResult) => boolean) {
let result: number[] = [];
for(let i = 0; i < this.list.length; i++) {
if(callback(this.list[i])) {
result.push(i);
}
}
return result;
}
}
/**
* 抽卡方法
*/
export class GachaPull {
private gachaType: GACHA_ID; // 抽卡类型
// 字典
private dicFloor: Map<GACHA_FLOOR_TYPE, number> = new Map(); // 保底次数
private percent: { id: number, weight: number, goodId?: number }[] = [];
private bigReward: { id: number, weight: number, goodId?: number }; // 保底的一项,percent中的一项
// 玩家数据
private player: PlayerGachaRecord;
// 结果
private result = new GachaResults();
constructor(gachaType: GACHA_ID, ) {
this.gachaType = gachaType;
}
// 一般抽卡setter
public setByUserGacha(dicGacha: DicGacha, userGacha: UserGachaType, isGuide: boolean) {
// 字典方面
this.percent = dicGacha.percent;
if (dicGacha.floorReward > 0) {
this.bigReward = this.percent[dicGacha.floorReward - 1];
}
if(!isGuide) this.setDicFloor();
// 玩家数据
this.player = new PlayerGachaRecord(userGacha, null);
}
private setDicFloor() {
let dicFloor = GACHA_TO_FLOOR.get(this.gachaType)||[];
for(let id of dicFloor) {
this.dicFloor.set(id, getDicGachaFloor(id));
public addBySimpleResult(simpleResult: SimpleResult[]) {
for(let { planId, hid } of simpleResult) {
let gachaResult = new GachaResult(planId);
gachaResult.setHero(hid, 1);
this.list.push(gachaResult);
}
}
// 新武将活动setter
public setByActivity(item: NewHeroGachaItem) {
this.percent = item.percent;
if (item.floorReward >= 0) {
this.bigReward = item.percent[item.floorReward]
}
this.dicFloor.set(GACHA_FLOOR_TYPE.ASSIGN, item.floorCount);
this.player = new PlayerGachaRecord(null, item);
public getTargetCnt(floor: GachaFloor) {
return this.getTarget(floor).length;
}
public pull(count: number, userHeroes: HeroType[]) {
this.pullContent(count);
this.processFloors();
this.processDetail();
this.processHope();
let { heroInfo, items, resultList } = this.getFinalResult(userHeroes)
return { resultList, heroInfo, items };
public getRandomResultOfTarget(floor: GachaFloor, cnt: number) {
return getRandEelm(this.getTarget(floor), cnt);
}
// 根据基础的percent抽基地
private pullContent(count: number) {
for (let i = 0; i < count; i++) {
// 按照一般概率抽出
let { dic } = getRandEelmWithWeight(this.percent);
if(dic) this.result.addResult(dic.id, dic.goodId);
}
}
// 保底
private processFloors() {
for(let [id, floorCount] of this.dicFloor) {
switch(id) {
case GACHA_FLOOR_TYPE.PURPLE:
this.setPurpleFloor(floorCount);
break;
case GACHA_FLOOR_TYPE.GOLD:
this.setGoldFloor(floorCount);
break;
case GACHA_FLOOR_TYPE.ASSIGN:
this.setAssignFloor(floorCount);
break;
}
}
}
// 紫将保底,10连抽必出一个紫将
private setPurpleFloor(floorCount: number) {
if(this.result.shouldPurpleFloor(floorCount)) {
let item = this.result.getRandItemNotPurple();
item.setContentId(getGachaContentOfHeroQuality(HERO_QUALITY_TYPE.PURPLE));
}
}
// 元宝招募保底,金将保底,按次数给保底,抽到就重新计算次数,单抽也算
public setGoldFloor(dicFloorCount: number) {
let floorType = GACHA_FLOOR_TYPE.GOLD;
let { floorCount: historyCount } = this.player.getFloorCountByFloorType(floorType);
for(let gachaResult of this.result.list) {
let isTarget = this.bigReward.id == gachaResult.contentId;
if(++historyCount >= dicFloorCount || isTarget) {
gachaResult.setContentId(this.bigReward.id, this.bigReward.goodId);
historyCount = 0;
}
}
this.player.setFloorCount(floorType, historyCount, false);
}
// 求贤若渴,和活动抽卡金色保底,伪随机,n次内给且只给一个,单抽也算
public setAssignFloor(dicFloorCount: number) {
let floorType = GACHA_FLOOR_TYPE.ASSIGN;
let { floorCount: historyCount, hasGetFloor } = this.player.getFloorCountByFloorType(floorType);
for(let gachaResult of this.result.list) {
let isTarget = this.bigReward.id == gachaResult.contentId;
if(++historyCount >= dicFloorCount || isTarget) {
if(hasGetFloor) { // 已经获得过一次保底了, 不给,换个给
let notBigReward = this.percent.filter(cur => cur.id != this.bigReward.id);
gachaResult.setContentId(notBigReward[0].id, notBigReward[0].goodId);
} else {
gachaResult.setContentId(this.bigReward.id, this.bigReward.goodId);
hasGetFloor = true;
}
}
if(historyCount >= dicFloorCount) {
hasGetFloor = false;
historyCount = 0;
}
}
this.player.setFloorCount(floorType, historyCount, hasGetFloor);
private getTarget(floor: GachaFloor) {
return this.list.filter(cur => cur.heroQuality == floor.heroQuality);
}
// 具体的卡池
private processDetail() {
for(let gachaResult of this.result.list) {
let detail = new ContentDetail(gachaResult.contentId, gachaResult.pickHero||this.player.pickHero);
let id = detail.getResult();
gachaResult.setHidOrGid(id, detail.count);
public processDetail(pickHero?: number, count?: number) {
for(let gachaResult of this.list) {
let detail = this.getRandomDetail(gachaResult.planId);
if(!detail) continue;
switch(detail.type) {
case GACHA_PLAN_TYPE.HERO:
gachaResult.setHero(detail.content, count||1);
break;
case GACHA_PLAN_TYPE.ASSIGN_HERO:
gachaResult.setHero(pickHero, getPickHeroCnt(pickHero));
break;
case GACHA_PLAN_TYPE.ITEM:
gachaResult.setItem(detail.content, detail.count);
break;
case GACHA_PLAN_TYPE.ASSIGN_ITEM:
gachaResult.setItem(getPickHeroPiece(pickHero), detail.count);
break;
}
}
}
// 心愿单
private processHope() {
if(this.gachaType != GACHA_ID.NORMAL) return;
for(let gachaResult of this.result.list) {
if(gachaResult.contentId != getGachaContentOfHeroQuality(HERO_QUALITY_TYPE.GOLD)) continue; // 只有橙将
let { dic: { id: randHopePosition } } = getRandEelmWithWeight(gameData.gachaHope);
if(randHopePosition > 0) { // 按玩家设置的心愿单塞
let hopeHero = this.player.getHopeHero(randHopePosition)
if(hopeHero > 0) gachaResult.setHidOrGid(hopeHero, 1);
}
this.player.setUserHope(gachaResult.hid); // 设置心愿单中了没有
}
private getRandomDetail(planId: number) {
let details = gameData.gachaPlan.get(planId)||[]
let { dic } = getRandEelmWithWeight(details);
return dic
}
// 创建信息
public getFinalResult(userHeroes: HeroType[]) {
return this.transferToFinalResult(this.result.list, userHeroes);
}
public pullBySimpleResult(simpleResult: {contentId: number, hid: number }[], userHeroes: HeroType[]) {
let results = simpleResult.map(obj => {
let gachaResult = new GachaResult(obj.contentId);
gachaResult.setHidOrGid(obj.hid, 1);
return gachaResult;
});
return this.transferToFinalResult(results, userHeroes);
}
private transferToFinalResult(results: GachaResult[], userHeroes: HeroType[]) {
public transferToFinalResult(userHeroes: HeroType[]) {
let hids = userHeroes.map(cur => cur.hid);
let items: RewardInter[] = [], heroInfo: CreateHeroParam[] = [];
let newResult: GachaResult[] = [];
for(let result of results) {
let cnt = result.hid > 0? result.count: 1;
newResult.push(...new Array(cnt).fill(result.copy()));
let newResult: GachaResultIndb[] = [];
for (let result of this.list) {
if (result.hid > 0) {
for(let i = 0; i < result.count; i++) {
newResult.push(new GachaResultIndb(result));
}
} else {
newResult.push(new GachaResultIndb(result));
}
}
for (let result of newResult) {
for(let result of newResult) {
if (result.hid > 0) {
let hasHero = hids.indexOf(result.hid) != -1;
if (hasHero) { // 已有转换为碎片
@@ -373,83 +263,235 @@ export class GachaPull {
items.push({ id: result.id, count: result.count });
}
}
return { items, heroInfo, resultList: newResult }
}
}
class GachaPlan {
index: number;
planId: number;
weight: number;
heroQuality: number;
constructor(planId: number, weight: number, index: number) {
this.planId = planId;
this.weight = weight;
this.index = index;
this.heroQuality = gameData.gachaPlanQuality.get(planId);
}
}
class GachaPercent {
percents: GachaPlan[] = [];
constructor(percent: { planId: number, weight: number }[]) {
for(let i = 1; i <= percent.length; i++) {
let { planId, weight } = percent[i - 1];
this.percents.push(new GachaPlan(planId, weight, i));
}
}
public getRandomPlan() {
let { dic } = getRandEelmWithWeight(this.percents);
return dic
}
public getReplacePlan(heroQuality: number, isLower: boolean) {
// 低一级顺序按 heroQuality: -1 => weight: 1 => index: -1 排序拿最前面那个
// 高一级顺序按 heroQuality: 1 => weight: -1 => index: 1 排序最前面那个
let arr = this.percents.filter(plan => {
return isLower? plan.heroQuality < heroQuality: plan.heroQuality > heroQuality;
}).sort((a, b) => {
if(a.heroQuality != b.heroQuality) {
return isLower? b.heroQuality - a.heroQuality: a.heroQuality - b.heroQuality;
} else if(a.weight != b.weight) {
return isLower? a.weight - b.weight: b.weight - a.weight;
} else {
return isLower? b.index - a.index: a.index - b.index;
}
});
return arr[0];
}
public getFloorTarget(heroQuality: number) {
let arr = this.percents.filter(plan => plan.heroQuality == heroQuality).sort((a, b) => {
if(a.weight != b.weight) {
return a.weight - b.weight;
} else {
return b.index - a.index;
}
});
return arr[0];
}
}
class GachaFloor {
id: number;
floorType: GACHA_FLOOR_TYPE;
heroQuality: number;
param: number;
constructor(id: number) {
let dicGachaFloor = gameData.gachaFloor.get(id);
if(dicGachaFloor) {
this.id = dicGachaFloor.id;
this.floorType = dicGachaFloor.floorType;
this.heroQuality = dicGachaFloor.heroQuality;
this.param = dicGachaFloor.param;
}
}
isReplaceLower() {
return [
GACHA_FLOOR_TYPE.MAIN_FLOOR,
GACHA_FLOOR_TYPE.ONLY_ONE,
GACHA_FLOOR_TYPE.TEN_MOST_LOWER,
].indexOf(this.floorType) != -1
}
}
/**
* 抽卡方法
*/
export class GachaPull {
private gachaType: GACHA_TYPE; // 抽卡类型
// 字典
private floors: GachaFloor[] = []; // 保底次数
private percent: GachaPercent;
// 玩家数据
private player: PlayerGachaRecord;
// 结果
private result = new GachaResults();
constructor(dicGacha: DicGacha, userGacha: UserGachaType) {
this.gachaType = dicGacha.gachaType;
this.percent = new GachaPercent(dicGacha.percent);
this.player = new PlayerGachaRecord(userGacha);
for(let id of dicGacha.floor) {
let floor = new GachaFloor(id);
if(floor && floor.id) this.floors.push(floor);
}
}
public pull(count: number, userHeroes: HeroType[]) {
this.pullContent(count);
this.processFloors();
this.result.processDetail(this.player.pickHero);
this.processHope();
let { heroInfo, items, resultList } = this.getFinalResult(userHeroes)
return { resultList, heroInfo, items };
}
// 根据基础的percent抽基地
private pullContent(count: number) {
for (let i = 0; i < count; i++) {
// 按照一般概率抽出
let dic = this.percent.getRandomPlan();
if(dic) this.result.addPlan(dic.planId);
}
}
// 保底
private processFloors() {
for(let floor of this.floors) {
switch(floor.floorType) {
case GACHA_FLOOR_TYPE.MAIN_FLOOR:
this.setMainFloor(floor);
break;
case GACHA_FLOOR_TYPE.ONLY_ONE:
this.setAssignFloor(floor);
break;
case GACHA_FLOOR_TYPE.TEN_MOST_HIGHER:
case GACHA_FLOOR_TYPE.TEN_MOST_LOWER:
this.setTenMostFloor(floor);
break;
}
}
}
// 元宝招募保底,金将保底,按次数给保底,抽到就重新计算次数,单抽也算
private setMainFloor(floor: GachaFloor) {
let { floorCount: historyCount } = this.player.getFloorCountById(floor.id);
for(let gachaResult of this.result.list) {
if(++historyCount >= floor.param || gachaResult.isTarget(floor)) {
let replacePlan = this.percent.getFloorTarget(floor.heroQuality);
if(replacePlan) gachaResult.changePlan(replacePlan.planId, replacePlan.heroQuality);
historyCount = 0;
}
}
this.player.setFloorCount(floor.id, historyCount, false);
}
// 求贤若渴,和活动抽卡金色保底,伪随机,n次内给且只给一个,单抽也算
public setAssignFloor(floor: GachaFloor) {
let { floorCount: historyCount, hasGetFloor } = this.player.getFloorCountById(floor.id);
for(let gachaResult of this.result.list) {
if(++historyCount >= floor.param || gachaResult.isTarget(floor)) {
if(hasGetFloor) { // 已经获得过一次保底了, 不给,换个给
let replacePlan = this.percent.getReplacePlan(gachaResult.heroQuality, floor.isReplaceLower());
if(replacePlan) gachaResult.changePlan(replacePlan.planId, replacePlan.heroQuality);
} else { // 设置为保底
let replacePlan = this.percent.getFloorTarget(floor.heroQuality);
if(replacePlan) gachaResult.changePlan(replacePlan.planId, replacePlan.heroQuality);
hasGetFloor = true;
}
}
if(historyCount >= floor.param) {
hasGetFloor = false;
historyCount = 0;
}
}
this.player.setFloorCount(floor.id, historyCount, hasGetFloor);
}
// 10连最多多少个武将
private setTenMostFloor(floor: GachaFloor) {
let targetCnt = this.result.getTargetCnt(floor);
if(this.result.count == 10 && targetCnt >= floor.param) {
let gachaResults = this.result.getRandomResultOfTarget(floor, targetCnt - floor.param);
for(let gachaResult of gachaResults) {
let replacePlan = this.percent.getReplacePlan(gachaResult.heroQuality, floor.isReplaceLower());
if(replacePlan) gachaResult.changePlan(replacePlan.planId, replacePlan.heroQuality)
}
}
}
// 心愿单
private processHope() {
if(this.gachaType != GACHA_TYPE.NORMAL) return;
for(let gachaResult of this.result.list) {
if(gachaResult.heroQuality != HERO_QUALITY_TYPE.GOLD) continue; // 只有橙将
let { dic: { id: randHopePosition } } = getRandEelmWithWeight(gameData.gachaHope);
if(randHopePosition > 0) { // 按玩家设置的心愿单塞
let hopeHero = this.player.getHopeHero(randHopePosition)
if(hopeHero > 0) gachaResult.setHero(hopeHero, 1);
}
this.player.setUserHope(gachaResult.hid); // 设置心愿单中了没有
}
}
// 创建信息
public getFinalResult(userHeroes: HeroType[]) {
return this.result.transferToFinalResult(userHeroes);
}
// 获得需要储存的数据
// 一般抽卡getter
public getUserGachaParam() {
return this.player.getUserGachaParam()
}
// 活动getter
public getActivityParam() {
let { hasGetFloor, floorCount } = this.player.getFloorCountByFloorType(GACHA_FLOOR_TYPE.ASSIGN);
return { hasGetFloor, floorCount };
public clear() {
this.floors = undefined;
this.percent = undefined;
this.player = undefined;
this.result = undefined;
}
}
class ContentDetail {
contentId: number;
type: number;
count: number;
param: number[];
pickHero: number;
constructor(contentId: number, pickHero: number) {
this.contentId = contentId;
let dicGachaContet = gameData.gachaContent.get(contentId);
this.type = dicGachaContet.type;
this.count = dicGachaContet.count;
this.param = dicGachaContet.param;
this.pickHero = pickHero;
}
public getResult() {
let { type } = gameData.gachaContent.get(this.contentId);
switch(type) {
case GACHA_CONTENT_TYPE.HERO:
return this.randomHeroByQuality(this.param[0]);
case GACHA_CONTENT_TYPE.HERO_PIECE:
return this.randomHeroPieceByQuality(this.param[0]);
case GACHA_CONTENT_TYPE.STONE:
return this.randomStoneByLv(this.param[0]);
case GACHA_CONTENT_TYPE.ITEMS:
return getRandSingleEelm(this.param);
default:
return 0
}
}
private randomHeroByQuality(quality: number) {
if(quality == 0) {
this.setPickHeroCnt();
return this.pickHero;
}
let heroes = getAllHeroByQuality(quality);
return getRandSingleEelm(heroes);
}
private setPickHeroCnt() {
if(this.pickHero && gameData.hero.has(this.pickHero)) {
let dicHero = gameData.hero.get(this.pickHero);
this.count = gameData.gachaPickHeroCnt.get(dicHero.quality)??1;
}
}
private randomHeroPieceByQuality(quality: number) {
let pieces = getAllItemByQuality(IT_TYPE.HERO_PIECE, quality);
return getRandSingleEelm(pieces);
}
private randomStoneByLv(lv: number) {
let stones = getAllStoneByLv(lv);
return getRandSingleEelm(stones);
}
}
/**
* 根据品质获得武将池
@@ -473,22 +515,32 @@ export function getAllHeroByQuality(quality: number) {
return allHero;
}
export function getAllItemByQuality(itid: number, quality: number) {
let allPiece: number[] = [];
for (let [id, dicGoods] of gameData.goods) {
if (dicGoods.itid == itid) {
if (quality == 0 || dicGoods.quality == quality) {
allPiece.push(id);
}
}
}
return allPiece;
function getPickHeroCnt(hid: number) {
let dicHero = gameData.hero.get(hid);
let cnt = gameData.gachaPickHeroCnt.get(dicHero?.quality)||0;
return cnt
}
export function getAllStoneByLv(lv: number) {
let items: number[] = [];
for(let [id, dicStone] of gameData.stone) {
if(dicStone.lv == lv) items.push(id);
}
return items;
function getPickHeroPiece(hid: number) {
let dicHero = gameData.hero.get(hid);
return dicHero?.pieceId??0
}
export function getDicGachaByGachaCnt(dicGacha: DicGacha, historyCount: number) {
if(isGachaCntIn(historyCount, dicGacha.gachaCnt.min, dicGacha.gachaCnt.max)) {
return dicGacha;
}
let gachaIds = gameData.gachaByType.get(dicGacha.gachaType);
for(let gachaId of gachaIds) {
let dicGacha = gameData.gacha.get(gachaId);
if(isGachaCntIn(historyCount, dicGacha.gachaCnt.min, dicGacha.gachaCnt.max)) {
return dicGacha;
}
}
return null
}
function isGachaCntIn(historyCount: number, min: number, max: number) {
let nowCnt = historyCount + 1;
return min <= nowCnt && (max >= nowCnt || max == -1);
}
@@ -12,7 +12,7 @@ import { HeroType } from '../../db/Hero';
import { pinus } from 'pinus';
import { getRandEelm, getRandSingleEelm } from '../../pubUtils/util';
import { getAllOnlineRoles } from '../redisService';
import { GachaResult } from '../../domain/activityField/gachaField';
import { GachaResultIndb } from '../../domain/activityField/gachaField';
import { gameData } from '../../pubUtils/data';
/**
@@ -44,7 +44,7 @@ export async function getPopUpShopDataShow(activityId: number, serverId: number,
return null
}
export async function checkPopUpConditionInCreateHero(serverId: number, roleId: string, result: GachaResult[]) {
export async function checkPopUpConditionInCreateHero(serverId: number, roleId: string, result: GachaResultIndb[]) {
let conditions = result.map(gachaResult => {
let dicHero = gameData.hero.get(gachaResult?.hid);
return { conditionType: POP_UP_SHOP_CONDITION_TYPE.GET_HERO_BY_QUALITY, param: { quality: dicHero?.quality } };
+2 -2
View File
@@ -19,7 +19,7 @@ import { JewelType } from '../../db/Jewel';
import { checkPopUpCondition, checkPopUpConditionInCreateHero, checkPopUpConditionInEntry } from '../activity/popUpShopService';
import { sendMessageToUserWithSuc } from '../pushService';
import { getGoldId } from '../role/rewardService';
import { GachaResult } from '../../domain/activityField/gachaField';
import { GachaResultIndb } from '../../domain/activityField/gachaField';
export async function checkTaskWithRoles(serverId: number, roleId: string, sid: string, taskType: TASK_TYPE, roles: RoleType[]) {
for (let role of roles) {
@@ -219,7 +219,7 @@ export async function checkTaskInPvpEnd(serverId: number, roleId: string, sid: s
}
}
export async function checkTaskInGacha(serverId: number, roleId: string, sid: string, count: number, heroes: HeroType[], result: GachaResult[], isGuide: boolean) {
export async function checkTaskInGacha(serverId: number, roleId: string, sid: string, count: number, heroes: HeroType[], result: GachaResultIndb[], isGuide: boolean) {
let task = new CheckTask(serverId, roleId);
task.setParam(TASK_TYPE.GACHA, { count });
task.setParam(TASK_TYPE.GACHA_QUALITY_COUNT, { heroes });
+8 -8
View File
@@ -3,7 +3,7 @@ import 'mocha';
import { PinusWSClient } from 'pinus-robot-plugin';
import { expect } from 'chai';
import { checkSuccessResponse, checkDisplayItems, checkHero, addItem } from './CheckPatten';
import { GACHA_ID } from '../../shared/consts';
import { GACHA_TYPE } from '../../shared/consts';
describe('抽卡测试', function () {
@@ -40,12 +40,12 @@ describe('抽卡测试', function () {
expect(gacha.refFreeTime).to.be.a('number');
expect(gacha.count).to.be.a('number');
checkFloor(gacha.floor);
if(gacha.gachaId == GACHA_ID.NORMAL) {
if(gacha.gachaId == GACHA_TYPE.NORMAL) {
checkHope(gacha.hope);
expect(gacha.point).to.be.a('number');
checkTurntable(gacha.turntable);
}
if(gacha.gachaId == GACHA_ID.ASSIGN) {
if(gacha.gachaId == GACHA_TYPE.ASSIGN) {
expect(gacha.pickHero).to.be.a('number');
}
});
@@ -54,27 +54,27 @@ describe('抽卡测试', function () {
});
it('检查元宝招募', function (done) {
let gachaId = GACHA_ID.ASSIGN;
let gachaId = GACHA_TYPE.ASSIGN;
let hid1 = 1, hid2 = 2;
checkSetHope(pinusClient, gachaId, hid1, hid2, function(res) {
addItem(pinusClient, 22001, 10, function(res) {
checkPull(pinusClient, done, GACHA_ID.NORMAL, 0, 10);
checkPull(pinusClient, done, GACHA_TYPE.NORMAL, 0, 10);
});
})
});
it('检查友情点招募', function (done) {
addItem(pinusClient, 40003, 1000, function(res) {
checkPull(pinusClient, done, GACHA_ID.FRDPOINT, 0, 10);
checkPull(pinusClient, done, GACHA_TYPE.FRDPOINT, 0, 10);
});
});
it('检查求贤若渴招募', function (done) {
let gachaId = GACHA_ID.ASSIGN;
let gachaId = GACHA_TYPE.ASSIGN;
let pickHero = 1;
addItem(pinusClient, 22002, 10, function(res) {
checkSetPickHero(pinusClient, gachaId, 0, pickHero, (res) => {
checkPull(pinusClient, done, GACHA_ID.ASSIGN, 0, 5);
checkPull(pinusClient, done, GACHA_TYPE.ASSIGN, 0, 5);
})
});
});