抽卡:修改抽卡概率

This commit is contained in:
luying
2021-07-01 17:27:32 +08:00
parent 363b2f9263
commit 2338acda54
7 changed files with 277 additions and 109 deletions

View File

@@ -4,18 +4,15 @@ import { STATUS, GACHA_ID, HERO_QUALITY_TYPE, TASK_TYPE, REFRESH_TIME, TIME_OUTP
import { gameData } from "../../../pubUtils/data";
import { GachaListReturn, GachaResult, GachaData } from "../../../domain/activityField/gachaField";
import { UserGachaModel } from "../../../db/UserGacha";
import { refreshGacha, getFloorResult, getResultFromContentId, getGachaList, getVisitedHeroList, getAllHeroByQuality } from "../../../services/activity/gachaService";
import { refreshGacha, getFloorResult, getGachaList, getVisitedHeroList, getAllHeroByQuality, GachaPull } from "../../../services/activity/gachaService";
import { RoleModel } from "../../../db/Role";
import { HeroModel } from "../../../db/Hero";
import { RewardInter } from "../../../pubUtils/interface";
import { handleCost, createHeroes, addItems } from "../../../services/rewardService";
import { getZeroPointD, getTimeFun } from "../../../pubUtils/timeUtil";
import { UserGachaRecModel } from "../../../db/UserGachaRec";
import { ActivityModel } from "../../../db/Activity";
import { checkActivityTask, checkTask } from "../../../services/taskService";
import { RECRUIT } from "../../../pubUtils/dicParam";
import { transPiece } from "../../../pubUtils/itemUtils";
import { CreateHeroParam } from "../../../domain/roleField/hero";
export default function (app: Application) {
return new GachaHandler(app);
@@ -61,37 +58,16 @@ export class GachaHandler {
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, count: historyCount } = await refreshGacha(dicGacha, userGacha);
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);
let userHeroes = await HeroModel.findByRole(roleId);
let items: RewardInter[] = [], heroInfo: CreateHeroParam[] = [], resultList: GachaResult[] = [];
let activityData = [];//活动需要统计抽中的英雄、碎片品质
for (let i = 0; i < count; i++) {
// 按照一般概率抽出
let { dic: { id: base } } = getRandEelmWithWeight(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) { // 已有转换为碎片
activityData.push({ hid: hasHero.hid, quality: hasHero.quality });
let { pieceId, count } = transPiece(result.hid);
result.transferToPiece(pieceId, count);
items.push({ id: pieceId, count });
} else {
heroInfo.push({ hid: result.hid, count: 1 })//默认1个英雄
}
} else {
items.push({ id: result.id, count: result.count });
}
resultList.push(result);
}
let gachaPull = new GachaPull(gachaId, lv, userHeroes);
gachaPull.setByUserGacha(dicGacha, userGacha);
let { items, heroInfo, resultList, activityData } = gachaPull.pull(count);
let { hope, floor } = gachaPull.getUserGachaParam(userGacha);
let costNum = count;
if (count == 1 && dicGacha.free.count > 0) { // 单抽的时候免费
@@ -110,6 +86,7 @@ export class GachaHandler {
if (!costResult) return resResult(STATUS.GACHA_COST_NOT_ENOUGH);
}
// 给东西
console.log('****', heroInfo)
let { heroes } = await createHeroes(roleId, roleName, sid, serverId, funcs, heroInfo);
await addItems(roleId, roleName, sid, items);
// 更新数据

View File

@@ -2,11 +2,16 @@ import { GachaData, Floor, GachaResult, Hope, GachaListReturn } from "../../doma
import { ActivityModel } from "../../db/Activity";
import { DicGacha } from "../../pubUtils/dictionary/DicGacha";
import { UserGachaType, UserGachaModel } from "../../db/UserGacha";
import { shouldRefresh, getRandEelm, getRandEelmWithWeight } from "../../pubUtils/util";
import { shouldRefresh, getRandEelm, getRandEelmWithWeight, getFloorStatus } 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 { getTimeFunD, getZeroPointD } from "../../pubUtils/timeUtil";
import { gameData, getDicGachaFloor } from "../../pubUtils/data";
import { RoleModel } from "../../db/Role";
import { RewardInter } from "../../pubUtils/interface";
import { CreateHeroParam } from "../../domain/roleField/hero";
import { UserGuildType } from "../../db/UserGuild";
import { transPiece } from "../../pubUtils/itemUtils";
import { HeroType } from "../../db/Hero";
/**
* 获取招募列表
@@ -82,6 +87,260 @@ export async function getVisitedHeroList(roleId: string) {
return visitedHero;
}
/**
* 抽卡方法
*/
export class GachaPull {
private gachaType: GACHA_ID; // 抽卡类型
private dicFloorType: GACHA_FLOOR_TYPE; // 保底类型 金将保底,指定保底
// 字典
private dicFloorCount: number; // 保底次数
private percent: { id: number, weight: number, goodId?: number }[];
private dicFloor: { id: number, weight: number, goodId?: number }; // 保底的一项percent中的一项
// 玩家数据
private lv: number; // 玩家等级
private userHeroes: HeroType[]; // 玩家的所有武将
private floorCount: number = 0; // 记录下的保底次数
private hasGetFloor: boolean = false; // 玩家是否达到保底
private hope: Hope[] = []; // 玩家记录的心愿单达成情况
constructor(gachaType: GACHA_ID, lv: number, userHeroes: HeroType[]) {
this.gachaType = gachaType;
if(gachaType == GACHA_ID.NORMAL) { // 元宝招募,金色武将保底,按次数给保底,抽到就重新计算次数,单抽也算
this.dicFloorType = GACHA_FLOOR_TYPE.GOLD;
} else if (gachaType == GACHA_ID.ASSIGN || gachaType == GACHA_ID.TIMELIMIT) { // 求贤若渴和活动抽卡金色保底伪随机n次内给且只给一个单抽也算
this.dicFloorType = GACHA_FLOOR_TYPE.ASSIGN;
}
this.userHeroes = userHeroes;
this.lv = lv;
}
// 一般抽卡setter
public setByUserGacha(dicGacha: DicGacha, userGacha: UserGachaType) {
// 字典方面
let percent = dicGacha.percent;
// 设置pickup的武将
let newPercent = [];
for(let { id, weight } of percent) {
if(id == gameData.gachaContentHero.get(0)) {
newPercent.push({ id, weight, goodId: userGacha.pickHero })
} else {
newPercent.push({ id, weight });
}
}
this.percent = newPercent;
if(dicGacha.floorReward > 0) {
this.dicFloor = this.percent[dicGacha.floorReward - 1];
}
this.dicFloorCount = getDicGachaFloor(this.dicFloorType);
// 玩家数据
let userFloor = userGacha.floor.find(cur => cur.id == this.dicFloorType);
if(userFloor) {
this.floorCount = userFloor.count;
this.hasGetFloor = userFloor.hasGetFloor||false;
}
this.hope = userGacha.hope;
}
// 新武将活动setter
public setByActivity() {
// this.floorCount =
// this.hasGetFloor =
// this.percent =
// this.dicFloorCount =
// this.dicFloor =
}
private getFloorResult(base: { id: number, weight: number, goodId?: number }) {
if(!this.dicFloor) return base;
let isTarget = base.id == this.dicFloor.id; // 是否抽到的就是那个保底
if(this.dicFloorType == GACHA_FLOOR_TYPE.GOLD) { // 元宝招募 橙色 按次数保底,抽到就重计
if(isTarget || this.floorCount + 1 >= this.dicFloorCount) {
this.floorCount = 0;
return this.dicFloor;
} else {
this.floorCount ++;
return base;
}
} else if (this.dicFloorType == GACHA_FLOOR_TYPE.ASSIGN) {
this.floorCount ++;
console.log('******', this.floorCount, this.dicFloorCount, this.hasGetFloor )
if(this.floorCount >= this.dicFloorCount) {
if(!this.hasGetFloor) isTarget = true;
this.hasGetFloor = false;
this.floorCount = 0;
}
console.log('******2', this.floorCount, this.dicFloorCount, this.hasGetFloor, isTarget )
if(isTarget) {
if(this.hasGetFloor) { // 已经获得过一次保底了, 不给,换个给
let { dic } = getRandEelmWithWeight(this.percent.filter(cur => cur.id != this.dicFloor.id));
return dic
} else {
this.hasGetFloor = true;
return this.dicFloor;
}
} else {
return base
}
} else {
return base
}
}
/**
* 根据contentId获得抽卡结果包括心愿单
* @param contentId dic_zyz_gachaContent的id
* @param lv 玩家等级
* @param hope 玩家心愿单
*/
private getResultFromContentId(contentId: number, goodId?: number) {
let dic = gameData.gachaContent.get(contentId);
let { type, param, count } = dic;
if (type == GACHA_CONTENT_TYPE.HERO) {
let pool: number[] = [];
if(this.hope.length > 0) {
pool = this.getPoolByHope(this.hope, param[0]); // 根据心愿单获得卡池
} else {
pool = getAllHeroByQuality(param[0]); // 没有心愿单就根据武将颜色
}
let hero = getRandEelm(pool);
let result = new GachaResult(contentId);
result.setHero(hero[0]);
if (goodId > 0) {
result.setHero(goodId);
}
return result
} else {
let pool: number[] = [];
if (type == GACHA_CONTENT_TYPE.HERO_PIECE) {
pool = getAllItemByQuality(IT_TYPE.HERO_PIECE, param[0], this.lv);
} else if (type == GACHA_CONTENT_TYPE.BLUEPRT) {
pool = getAllItemByQuality(IT_TYPE.BLUEPRT, param[0], this.lv);
} else if (type == GACHA_CONTENT_TYPE.JEWEL) {
pool = getAllJewelByLv(param[0]);
} else if (type == GACHA_CONTENT_TYPE.TERAPH_MATERIAL) {
pool = param;
} else if (type == GACHA_CONTENT_TYPE.SUIT_PAPER) {
pool = getSuitPaper(this.lv);
}
let item = getRandEelm(pool);
let result = new GachaResult(contentId);
result.setItem(item[0], count);
if (goodId > 0) {
result.setItem(goodId, count);
}
return result;
}
}
/**
* 心愿
* @param hope 玩家数据里的心愿单
* @param qualtiy 武将品质
*/
private getPoolByHope(hope: Hope[], qualtiy: number) {
if (qualtiy == HERO_QUALITY_TYPE.GOLD) {
let hopeMap = new Map<number, Hope>();
let hasGetHope: number[] = [];
for (let h of hope) {
if (h.hid > 0) {
hopeMap.set(h.id, h);
if (h.hasGet) hasGetHope.push(h.id);
}
}
let list = gameData.gachaHope.map(cur => {
return { ...cur, hasGet: hopeMap.get(cur.id)?.hasGet || false };
})
let { dic: { id, hasGet } } = getRandEelmWithWeight(list);
if (id == 0 || hasGet) {
return getAllHeroByQuality(qualtiy).filter(cur => !hasGetHope.includes(cur));
} else {
if (hopeMap.has(id)) {
let curHope = hopeMap.get(id);
curHope.hasGet = true;
return [curHope.hid];
} else {
return getAllHeroByQuality(qualtiy).filter(cur => !hasGetHope.includes(cur));
}
}
} else {
return getAllHeroByQuality(qualtiy);
}
}
public pull(count: number) {
let items: RewardInter[] = [], heroInfo: CreateHeroParam[] = [], resultList: GachaResult[] = [];
let activityData = [];//活动需要统计抽中的英雄、碎片品质
for (let i = 0; i < count; i++) {
// 按照一般概率抽出
let { dic } = getRandEelmWithWeight(this.percent);
let { id: contentId, goodId } = this.getFloorResult(dic); // 根据保底替换
contentId = this.getPurpleFloor(count, i, contentId, resultList);
let result = this.getResultFromContentId(contentId, goodId);
if (result.hid > 0) {
let hasHero = this.userHeroes.find(cur => cur.hid == result.hid);
if (hasHero) { // 已有转换为碎片
activityData.push({ hid: hasHero.hid, quality: hasHero.quality });
let { pieceId, count } = transPiece(result.hid);
result.transferToPiece(pieceId, count);
items.push({ id: pieceId, count });
} else {
heroInfo.push({ hid: result.hid, count: 1 })//默认1个英雄
}
} else {
items.push({ id: result.id, count: result.count });
}
resultList.push(result);
}
return { items, heroInfo, resultList, activityData }
}
private getPurpleFloor(count: number, index: number, contentId: number, resultList: GachaResult[]) {
if(count == 10 && index == count - 1) { // 10连到最后一抽
let { type, param } = gameData.gachaContent.get(contentId);
if(type != GACHA_CONTENT_TYPE.HERO || param[0] < HERO_QUALITY_TYPE.PURPLE) {
let hasPurple = false;
for(let { contentId } of resultList) {
let { type, param } = gameData.gachaContent.get(contentId);
if(type == GACHA_CONTENT_TYPE.HERO && param[0] >= HERO_QUALITY_TYPE.PURPLE) {
hasPurple = true; break;
}
}
if(!hasPurple) {
return gameData.gachaContentHero.get(HERO_QUALITY_TYPE.PURPLE);
}
}
}
return contentId
}
// 获得需要储存的数据
// 一般抽卡getter
public getUserGachaParam(userGacha: UserGachaType) {
let floor = userGacha.floor||[];
let curFloor = floor.find(cur => cur.id == this.dicFloorType);
if(curFloor) {
curFloor.hasGetFloor = this.hasGetFloor;
curFloor.count = this.floorCount;
} else {
floor.push({ id: this.dicFloorType, hasGetFloor: this.hasGetFloor, count: this.floorCount })
}
return { hope: this.hope, floor }
}
// 活动getter
public getActivityParam() {
// this.hasGetFloor
// this.floorCount
}
}
/**
* @description 获得保底结果
* @param gachaId 招募表id
@@ -177,78 +436,6 @@ function getHeroQuality(id: number) {
}
}
/**
* 根据contentId获得抽卡结果包括心愿单
* @param contentId dic_zyz_gachaContent的id
* @param lv 玩家等级
* @param hope 玩家心愿单
*/
export function getResultFromContentId(contentId: number, lv: number, hope: Hope[]) {
let dic = gameData.gachaContent.get(contentId);
let { type, param, count } = dic;
if (type == GACHA_CONTENT_TYPE.HERO) {
let pool: number[] = getPoolByHope(hope, param[0]);
let hero = getRandEelm(pool);
let result = new GachaResult(contentId);
result.setHero(hero[0]);
return result
} else {
let pool: number[] = [];
if (type == GACHA_CONTENT_TYPE.HERO_PIECE) {
pool = getAllItemByQuality(IT_TYPE.HERO_PIECE, param[0], lv);
} else if (type == GACHA_CONTENT_TYPE.BLUEPRT) {
pool = getAllItemByQuality(IT_TYPE.BLUEPRT, param[0], lv);
} else if (type == GACHA_CONTENT_TYPE.JEWEL) {
pool = getAllJewelByLv(param[0]);
} else if (type == GACHA_CONTENT_TYPE.TERAPH_MATERIAL) {
pool = param;
} else if (type == GACHA_CONTENT_TYPE.SUIT_PAPER) {
pool = getSuitPaper(lv);
}
let item = getRandEelm(pool);
let result = new GachaResult(contentId);
result.setItem(item[0], count);
return result;
}
}
/**
* 心愿
* @param hope 玩家数据里的心愿单
* @param qualtiy 武将品质
*/
function getPoolByHope(hope: Hope[], qualtiy: number) {
if (qualtiy == HERO_QUALITY_TYPE.GOLD) {
let hopeMap = new Map<number, Hope>();
let hasGetHope: number[] = [];
for (let h of hope) {
if (h.hid > 0) {
hopeMap.set(h.id, h);
if (h.hasGet) hasGetHope.push(h.id);
}
}
let list = gameData.gachaHope.map(cur => {
return { ...cur, hasGet: hopeMap.get(cur.id)?.hasGet || false };
})
let { dic: { id, hasGet } } = getRandEelmWithWeight(list);
if (id == 0 || hasGet) {
return getAllHeroByQuality(qualtiy).filter(cur => !hasGetHope.includes(cur));
} else {
if (hopeMap.has(id)) {
let curHope = hopeMap.get(id);
curHope.hasGet = true;
return [curHope.hid];
} else {
return getAllHeroByQuality(qualtiy).filter(cur => !hasGetHope.includes(cur));
}
}
} else {
return getAllHeroByQuality(qualtiy);
}
}
/**
* 根据品质获得武将池
* @param quality 品质

View File

@@ -105,7 +105,7 @@ export const DAILY_CONST = {
export const DUNGEON_CONST = {
REFRESH_TIME: 5, // 每天加点刷新
MAX_CNT: 10, // 最大挑战次数
MAX_CNT: 100, // 最大挑战次数
MAX_BUY_CNT: 10 // 最大购买次数
}

View File

@@ -643,9 +643,9 @@ export enum GACHA_ID {
// 抽卡保底类型
export enum GACHA_FLOOR_TYPE {
PURPLE = 1, // 元宝招募,保底紫色
GOLD = 2, // 元宝招募,保底金色
ASSIGN = 3, // 求贤若渴,保底指定武将
PURPLE = 1, // 紫色保底10连必定会出一个紫的单抽不算
GOLD = 2, // 金色武将保底,按次数给保底,抽到就重新计算次数,单抽也算
ASSIGN = 3, // 求贤若渴,和活动抽卡金色保底伪随机n次内给且只给一个单抽也算
}
// 抽卡对应保底类型

View File

@@ -8,6 +8,8 @@ class Floor {
id: number; // 保底类型 1-紫将保底 2-金将保底 3-指定将保底
@prop({ required: true })
count: number; // 抽卡次数
@prop({ required: true })
hasGetFloor: boolean; // 是否抽到保底了
}
/**

View File

@@ -4,7 +4,7 @@ import { prop } from '@typegoose/typegoose';
import { UserGachaType } from '../../db/UserGacha';
import { getTimeFun } from '../../pubUtils/timeUtil';
import { DicGacha } from '../../pubUtils/dictionary/DicGacha';
import { GACHA_OCCUPY_HID } from '../../consts';
import { GACHA_OCCUPY_HID, GACHA_ID } from '../../consts';
import { gameData } from '../../pubUtils/data';
import { getFloorStatus } from '../../pubUtils/util';

View File

@@ -13,6 +13,8 @@ export interface DicGacha {
readonly cost: RewardInter[];
// 概率
readonly percent: { id: number, weight: number }[];
// 哪一个是大奖
readonly floorReward: number;
}
export const dicGacha = new Map<number, DicGacha>(); // id => dic