import { GachaData, Floor, GachaResult, Hope, GachaListReturn } from "../../domain/activityField/gachaField";; import { DicGacha } from "../../pubUtils/dictionary/DicGacha"; import { UserGachaType, UserGachaModel } 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 { getTimeFunD, getZeroPointD } from "../../pubUtils/timeUtil"; import { gameData, getDicGachaFloor, getGachaContentOfHeroQuality } from "../../pubUtils/data"; import { RoleModel } from "../../db/Role"; 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"; /** * 获取招募列表 * @param roleId */ 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; // 不包括限时 let userGacha = userGachaList.find(cur => cur.gachaId == id); if (userGacha) userGacha = await refreshGacha(dicGacha, userGacha); let param = new GachaListReturn(dicGacha, userGacha); list.push(param); } return list; } /** * 获取活动页签里的限时卡池 * * @param roleId 玩家id * @param aid 活动id */ export async function getLimitGacha(serverId: number, roleId: string, activityId: number) { let activityData = await getActivityById(activityId); if (!activityData) return false; let createTime = await getRoleCreateTime(roleId); let serverTime = await getServerCreateTime(serverId); let gachaData = new GachaData(activityData, createTime, serverTime); let userGacha = await UserGachaModel.findByRole(roleId, gachaData.gachaId, activityId); userGacha = await refreshGacha(gameData.gacha.get(gachaData.gachaId), userGacha); gachaData.setUserGacha(userGacha); return gachaData } /** * 刷新免费次数 * @param dicGacha * @param userGacha */ export async function refreshGacha(dicGacha: DicGacha, userGacha: UserGachaType) { let { day, count } = dicGacha.free; if (count <= 0) { return userGacha; } let { roleId, gachaId, refFreeTime, refHopeTime, hope } = userGacha; if (shouldRefresh(refFreeTime, new Date(), REFRESH_TIME, day)) { let ref = getTimeFunD().getAfterDayByGap(refFreeTime, day); userGacha = await UserGachaModel.refreshFreeCount(roleId, gachaId, 0, ref); } if (shouldRefresh(refHopeTime, new Date(), REFRESH_TIME)) { hope = hope.map(cur => { return { ...cur, hasGet: false } }); let ref = getZeroPointD(); userGacha = await UserGachaModel.refreshHopeCount(roleId, gachaId, 0, hope, ref); } return userGacha } export async function getVisitedHeroList(roleId: string) { let { visitedHero, refVisitedTime } = await UserGachaModel.findByRole(roleId, GACHA_ID.NORMAL, 0); if (shouldRefresh(refVisitedTime, new Date())) { visitedHero = []; } return visitedHero; } class PlayerGachaRecord { public lv: number; // 玩家等级 public hope: Hope[] = []; // 玩家记录的心愿单达成情况 public floor: Floor[] = []; public pickHero: number = 0; constructor(userGacha: UserGachaType, item: NewHeroGachaItem) { 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); if(userFloor) { return { floorCount: userFloor.count, hasGetFloor: userFloor.hasGetFloor } } else { return { floorCount: 0, hasGetFloor: false } } } public setFloorCount(floorType: GACHA_FLOOR_TYPE, count: number, hasGetFloor: boolean) { let index = this.floor.findIndex(cur => cur.id == floorType); if(index == -1) { this.floor.push({ id: floorType, count, hasGetFloor }) } else { this.floor[index].count = count; this.floor[index].hasGetFloor = hasGetFloor; } } public getHopeHero(randHopePosition: number) { let userHope = this.hope.find(cur => cur.id == randHopePosition); if(!userHope || userHope.hasGet) return 0; return userHope.hid||0; } public setUserHope(hid: number) { if(hid <= 0) return; let userHope = this.hope.find(cur => cur.hid == hid); if(userHope && !userHope.hasGet) userHope.hasGet = true; } public getUserGachaParam() { return { hope: this.hope, floor: this.floor } } } class GachaResults { count: number = 0; list: GachaResult[] = []; addResult(contentId: number, id?: number) { this.count++; this.list.push(new GachaResult(contentId, id)); } 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 = 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)); } } // 新武将活动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 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 }; } // 根据基础的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) { // 已经获得过一次保底了, 不给,换个给 gachaResult.setContentId(getGachaContentOfHeroQuality(HERO_QUALITY_TYPE.PURPLE)); } 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 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); } } // 心愿单 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); } this.player.setUserHope(gachaResult.hid); // 设置心愿单中了没有 } } // 创建信息 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); return gachaResult; }); return this.transferToFinalResult(results, userHeroes); } private transferToFinalResult(results: GachaResult[], userHeroes: HeroType[]) { let hids = userHeroes.map(cur => cur.hid); let items: RewardInter[] = [], heroInfo: CreateHeroParam[] = []; for (let result of results) { if (result.hid > 0) { let hasHero = hids.indexOf(result.hid) != -1; if (hasHero) { // 已有转换为碎片 let { pieceId, count } = transPiece(result.hid); result.transferToPiece(pieceId, count); items.push({ id: pieceId, count }); } else { heroInfo.push({ hid: result.hid, count: 1 })//默认1个英雄 hids.push(result.hid); } } else { items.push({ id: result.id, count: result.count }); } } return { items, heroInfo, resultList: results } } // 获得需要储存的数据 // 一般抽卡getter public getUserGachaParam() { return this.player.getUserGachaParam() } // 活动getter public getActivityParam() { let { hasGetFloor, floorCount } = this.player.getFloorCountByFloorType(GACHA_FLOOR_TYPE.ASSIGN); return { hasGetFloor, floorCount }; } } 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) return this.pickHero; let heroes = getAllHeroByQuality(quality); return getRandSingleEelm(heroes); } 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); } } /** * 根据品质获得武将池 * @param quality 品质 */ export function getAllHeroByQuality(quality: number) { let allHero: number[] = []; let allQuality: number[] = []; for (let { actorId } of gameData.recruit) { let dicHero = gameData.hero.get(actorId); if (dicHero.quality == quality) { allHero.push(actorId); } if (allQuality.indexOf(dicHero.quality) == -1) allQuality.push(dicHero.quality); } allQuality.sort((a, b) => a - b); if (allQuality.length == 0) return []; if (allHero.length == 0) { allHero = getAllHeroByQuality(allQuality[0]); } 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; } export function getAllStoneByLv(lv: number) { let items: number[] = []; for(let [id, dicStone] of gameData.stone) { if(dicStone.lv == lv) items.push(id); } return items; }