活动:清理数据
This commit is contained in:
@@ -1,163 +1,33 @@
|
||||
import { ITID, CONSUME_TYPE, ITEM_TABLE, CURRENCY, CURRENCY_TYPE, MAIL_TYPE, HANDLE_REWARD_TYPE, HERO_SYSTEM_TYPE, CURRENCY_BY_TYPE, ITEM_CHANGE_REASON, TA_USERSET_TYPE, TA_EVENT, POP_UP_SHOP_CONDITION_TYPE } from '../../consts';
|
||||
import { getRandSingleEelm, resResult } from '../../pubUtils/util';
|
||||
import { ITID, CONSUME_TYPE, ITEM_TABLE, CURRENCY, CURRENCY_TYPE, MAIL_TYPE, HANDLE_REWARD_TYPE, HERO_SYSTEM_TYPE, CURRENCY_BY_TYPE, ITEM_CHANGE_REASON, TA_USERSET_TYPE, TA_EVENT, POP_UP_SHOP_CONDITION_TYPE, ROLE_SELECT, FIGURE_UNLOCK_CONDITION } from '../../consts';
|
||||
import { getDecimalCnt, getRandEelm, getRandEelmWithWeight, getRandSingleEelm, getRandValueByMinMax, resResult } from '../../pubUtils/util';
|
||||
import { RoleModel, RoleType } from '../../db/Role';
|
||||
import { setAp } from '../actionPointService';
|
||||
import { pushCalAllHeroCe, calPlayerCeAndSave } from '../playerCeService';
|
||||
import { pushCalAllHeroCe, calPlayerCeAndSave, calAllHeroCe } from '../playerCeService';
|
||||
import { ItemModel, ItemType } from '../../db/Item';
|
||||
import { STATUS } from '../../consts/statusCode';
|
||||
import { pinus } from 'pinus';
|
||||
import { addJewels, addBags, addSkin, addFigure, unlockFigure as pubUnlockFigure, transPiece, getGoldObject, getCoinObject, getApObject } from '../../pubUtils/itemUtils';
|
||||
import { ItemInter, RewardInter, } from '../../pubUtils/interface';
|
||||
import { gameData } from '../../pubUtils/data';
|
||||
import { uniq } from 'underscore';
|
||||
import { EPlace, HeroModel, HeroType, HeroUpdate } from '../../db/Hero';
|
||||
import { Figure } from '../../domain/dbGeneral';
|
||||
import { Rank } from '../rankService';
|
||||
import { pushTaskUpdate } from '../task/taskService';
|
||||
import { CreateHeroParam, HeroShowParam } from '../../domain/roleField/hero';
|
||||
import { HeroSkin } from '../../db/Hero';
|
||||
import { errlogger } from '../../util/logger';
|
||||
import { BAG } from '../../pubUtils/dicParam';
|
||||
import { sendMailByContent } from '../mailService';
|
||||
import { SkinUpdate } from '../../db/Skin';
|
||||
import { SkinModel, SkinUpdate } from '../../db/Skin';
|
||||
import { getInitHeroById } from '../roleService';
|
||||
import { getActivities } from '../activity/activityService';
|
||||
import { reportTAEvent, reportTAUserSet } from '../sdkService';
|
||||
import { saveCoinChangeLog, saveFigureInfoLog, saveGoldChangeLog, saveItemChangeLog } from '../../pubUtils/logUtil';
|
||||
import { JewelModel, JewelType } from '../../db/Jewel';
|
||||
import { JewelModel, JewelType, jewelUpdate, RandSe } from '../../db/Jewel';
|
||||
import { updateEplaces } from '../equipService';
|
||||
import { checkPopUpConditionInCreateHero } from '../activity/popUpShopService';
|
||||
import { CreateHeroes } from './createHero';
|
||||
import { combineItems, getCoinEventProperties, getGoldEventProperties, sortItems } from './util';
|
||||
import { nowSeconds } from '../../pubUtils/timeUtil';
|
||||
|
||||
export class CheckMeterial {
|
||||
private roleId: string;
|
||||
private itemsIndb: Map<number, number> = new Map(); // 玩家已有的东西
|
||||
private notEnoughItems: Map<number, number> = new Map(); // 缺少的数量
|
||||
private consumes: ItemInter[] = []; // 消耗,正向数
|
||||
|
||||
private goldId = CURRENCY_BY_TYPE.get(CURRENCY_TYPE.GOLD);
|
||||
private coinId = CURRENCY_BY_TYPE.get(CURRENCY_TYPE.COIN);
|
||||
|
||||
constructor(roleId: string, role?: RoleType, items?: ItemType[]) {
|
||||
this.roleId = roleId;
|
||||
if(role) {
|
||||
this.itemsIndb.set(this.goldId, role.gold);
|
||||
this.itemsIndb.set(this.coinId, role.coin);
|
||||
}
|
||||
if(items && items.length) {
|
||||
for(let {id, count} of items) {
|
||||
this.itemsIndb.set(id, count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private pushToNotEnoughItems(id: number, count: number) {
|
||||
if(!this.notEnoughItems.has(id)) {
|
||||
this.notEnoughItems.set(id, 0);
|
||||
}
|
||||
this.notEnoughItems.set(id, this.notEnoughItems.get(id) + count);
|
||||
}
|
||||
|
||||
private getNotEnoughItems() {
|
||||
let map = new Map<number, number>();
|
||||
for(let [ id, count ] of this.notEnoughItems) {
|
||||
map.set(id, count);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
public async decrease(goods: {id: number, count: number}[]) {
|
||||
this.notEnoughItems.clear();
|
||||
let { items, gold, coin } = sortItems(goods, HANDLE_REWARD_TYPE.COST);
|
||||
let isEnough = true;
|
||||
for(let { id, count} of items) {
|
||||
if(!this.itemsIndb.has(id)) {
|
||||
let item = await ItemModel.findbyRoleAndGid(this.roleId, id);
|
||||
if(!item) {
|
||||
this.pushToNotEnoughItems(id, count);
|
||||
isEnough = false; break;
|
||||
}
|
||||
this.itemsIndb.set(id, item.count);
|
||||
}
|
||||
if(this.itemsIndb.get(id) < count) {
|
||||
this.pushToNotEnoughItems(id, count - this.itemsIndb.get(id));
|
||||
isEnough = false; break;
|
||||
}
|
||||
this.itemsIndb.set(id, this.itemsIndb.get(id) - count);
|
||||
}
|
||||
|
||||
if(gold.length > 0) {
|
||||
if(!this.itemsIndb.has(this.goldId)) {
|
||||
let role = await RoleModel.findByRoleId(this.roleId, 'gold coin');
|
||||
this.itemsIndb.set(this.goldId, role.gold);
|
||||
this.itemsIndb.set(this.coinId, role.coin);
|
||||
}
|
||||
let goldCost = gold.reduce((pre, cur) => { return pre + cur.count }, 0);
|
||||
if(this.itemsIndb.get(this.goldId) < goldCost) {
|
||||
this.pushToNotEnoughItems(this.goldId, goldCost - this.itemsIndb.get(this.goldId));
|
||||
isEnough = false;
|
||||
}
|
||||
this.itemsIndb.set(this.goldId, this.itemsIndb.get(this.goldId) - goldCost);
|
||||
}
|
||||
if(isEnough && coin.length > 0) {
|
||||
if(!this.itemsIndb.has(this.coinId)) {
|
||||
let role = await RoleModel.findByRoleId(this.roleId, 'gold coin');
|
||||
this.itemsIndb.set(this.goldId, role.gold);
|
||||
this.itemsIndb.set(this.coinId, role.coin);
|
||||
}
|
||||
let coinCost = coin.reduce((pre, cur) => pre + cur, 0);
|
||||
if(this.itemsIndb.get(this.coinId) < coinCost) {
|
||||
this.pushToNotEnoughItems(this.coinId, coinCost - this.itemsIndb.get(this.coinId));
|
||||
isEnough = false;
|
||||
}
|
||||
this.itemsIndb.set(this.coinId, this.itemsIndb.get(this.coinId) - coinCost);
|
||||
}
|
||||
|
||||
if(isEnough) this.consumes.push(...goods);
|
||||
return isEnough;
|
||||
}
|
||||
|
||||
private getMaterialEnough(materials: RewardInter[], notEnoughItems: Map<number, number>) {
|
||||
let newMaterials: RewardInter[] = [];
|
||||
for(let {id, count} of materials) {
|
||||
if(notEnoughItems.has(id)) {
|
||||
newMaterials.push({ id, count: count - notEnoughItems.get(id) });
|
||||
} else {
|
||||
newMaterials.push({ id, count });
|
||||
}
|
||||
}
|
||||
return newMaterials;
|
||||
}
|
||||
|
||||
// 检查地玉石是否可以合成
|
||||
public async composeStone(id: number, count: number) {
|
||||
let dicStone = gameData.stone.get(id);
|
||||
if(!dicStone || dicStone.composeMaterial.length <= 0) return false; // 1阶石头不能再从下合成返回不行
|
||||
let materials = dicStone.composeMaterial.map(cur => ({...cur, count: cur.count * count }));
|
||||
let isEnough = await this.decrease(materials);
|
||||
if(!isEnough) {
|
||||
let notEnoughItems = this.getNotEnoughItems();
|
||||
let newMaterials = this.getMaterialEnough(materials, notEnoughItems);
|
||||
let isEnough = await this.decrease(newMaterials); // 消耗掉除了不足的部分以外的其他部分
|
||||
if(!isEnough) return false;
|
||||
|
||||
let isAllOK = true; // 如果有石头不足,向下补充,isAllOK标识不足的都能补充上
|
||||
for(let [id, count] of notEnoughItems) {
|
||||
let isEnough = await this.composeStone(id, count);
|
||||
if(!isEnough) {
|
||||
isAllOK = false; break;
|
||||
}
|
||||
}
|
||||
return isAllOK;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public getConsume() {
|
||||
return this.consumes;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export async function handleCost(roleId: string, sid: string, goods: Array<ItemInter>, reason: ITEM_CHANGE_REASON) {
|
||||
@@ -264,7 +134,7 @@ export async function addItems(roleId: string, roleName: string, sid: string, go
|
||||
}
|
||||
|
||||
// 直接加的
|
||||
let { jewels: jewelInfos, pushMessages } = await addJewels(roleId, roleName, <{id: number, hid?: number}[]>incJewels, reason);
|
||||
let { jewels: jewelInfos } = await addJewels(roleId, roleName, <{id: number, hid?: number}[]>incJewels, reason);
|
||||
for (let jewel of jewelInfos) {
|
||||
showItems.push({ seqId: jewel.seqId, id: jewel.id, count: 1, isBag: true });
|
||||
}
|
||||
@@ -274,7 +144,6 @@ export async function addItems(roleId: string, roleName: string, sid: string, go
|
||||
//装备推送
|
||||
if (!!jewelInfos.length)
|
||||
pinus.app.get('channelService').pushMessageByUids('onJewelAdd', resResult(STATUS.SUCCESS, { jewelInfos }), uids);
|
||||
pushTaskUpdate(roleId, sid, pushMessages);
|
||||
//统计装备
|
||||
if (jewelInfos.length > 0) {
|
||||
saveItemChangeLog(roleId, jewelInfos, reason);
|
||||
@@ -301,7 +170,7 @@ export async function addItems(roleId: string, roleName: string, sid: string, go
|
||||
|
||||
// 3. 货币推送
|
||||
if(gold.length > 0 || coin.length > 0 || ap > 0) {
|
||||
await setAp(roleId, null, role.lv, ap, sid, reason);
|
||||
await setAp(role.serverId, roleId, null, role.lv, ap, sid, reason);
|
||||
let incCoin = coin.reduce((pre, cur) => pre + cur, 0);
|
||||
let incGold = gold.reduce((pre, cur) => pre + cur.count, 0);
|
||||
role = await RoleModel.increaseGoldAndCoin(roleId, gold, incCoin);
|
||||
@@ -337,15 +206,10 @@ export async function addItems(roleId: string, roleName: string, sid: string, go
|
||||
let calAllHeroResult = undefined; // 全局战力变化推送
|
||||
|
||||
for (let skinId of skins) {//皮肤推送
|
||||
let result = await addSkin(roleId, roleName, skinId, false);
|
||||
if (!!result) {
|
||||
let hero = await addSkin(roleId, roleName, sid, skinId, false);
|
||||
if (!!hero) {
|
||||
showItems.push({ id: skinId, count: 1 });
|
||||
figureInfos.push(result.figureInfo);
|
||||
if(result.hero) {
|
||||
heroskins.push({ skins: result.hero.skins, hid: result.hero.hid });
|
||||
if(result.calAllHeroResult) calAllHeroResult = result.calAllHeroResult;
|
||||
}
|
||||
skinInfos.push({ id: skinId, hid: result.hero?.hid||0, count: 1, inc: 1, reason })
|
||||
skinInfos.push({ id: skinId, hid: hero?.hid||0, count: 1, inc: 1, reason })
|
||||
}
|
||||
}
|
||||
if (!!skinInfos.length) {
|
||||
@@ -376,127 +240,6 @@ export async function addItems(roleId: string, roleName: string, sid: string, go
|
||||
return showItems;
|
||||
}
|
||||
|
||||
function getGoldEventProperties(inc: number, count: number, reason: ITEM_CHANGE_REASON) {
|
||||
let id = CURRENCY_BY_TYPE.get(CURRENCY_TYPE.GOLD);
|
||||
let dicGoods = gameData.goods.get(id);
|
||||
return { item_id: id, item_name: dicGoods.name, item_itid: dicGoods.itid, change_count: inc, change_after: count, change_reason: reason }
|
||||
}
|
||||
|
||||
function getCoinEventProperties(inc: number, count: number, reason: ITEM_CHANGE_REASON) {
|
||||
let id = CURRENCY_BY_TYPE.get(CURRENCY_TYPE.COIN);
|
||||
let dicGoods = gameData.goods.get(id);
|
||||
return { item_id: id, item_name: dicGoods.name, item_itid: dicGoods.itid, change_count: inc, change_after: count, change_reason: reason }
|
||||
}
|
||||
|
||||
export function combineItems(items: { id?: number, count?: number }[]) {
|
||||
let result: { id: number, count: number }[] = [];
|
||||
for(let { id, count = 1 } of items) {
|
||||
let index = result.findIndex(cur => cur.id == id);
|
||||
if(index == -1) {
|
||||
result.push({ id, count });
|
||||
} else {
|
||||
result[index].count += count;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export function combineItemAndJewels(items: { id?: number, count?: number }[]) {
|
||||
let result: { id: number, count: number }[] = [];
|
||||
for(let { id, count = 1 } of items) {
|
||||
let dicGoods = gameData.goods.get(id);
|
||||
let dicItid = ITID.get(dicGoods.itid);
|
||||
if(dicItid.table != 'jewel') {
|
||||
let index = result.findIndex(cur => cur.id == id);
|
||||
if(index == -1) {
|
||||
result.push({ id, count });
|
||||
} else {
|
||||
result[index].count += count;
|
||||
}
|
||||
} else {
|
||||
result.push({ id, count });
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function sortItems(goods: ItemInter[], handleType: HANDLE_REWARD_TYPE) {
|
||||
let items: { id: number, count: number }[] = []; // 可叠加道具
|
||||
let jewels: { seqId?: number, id?: number, hid?: number }[] = []; // 不可叠加装备
|
||||
let gold: { count: number, isPay: boolean }[] = []; // 金币
|
||||
let coin: number[] = [];
|
||||
let ap: number = 0;
|
||||
let skins: number[] = [];
|
||||
let figures: number[] = [];
|
||||
|
||||
for(let good of goods) {
|
||||
if(good.count == 0) continue;
|
||||
let dicGood = gameData.goods.get(good.id);
|
||||
if(!dicGood) {
|
||||
errlogger.error(`物品 ${good.id} 未配置`);
|
||||
continue;
|
||||
}
|
||||
let dicItid = ITID.get(dicGood.itid);
|
||||
if(!dicItid) {
|
||||
errlogger.error(`itid ${dicGood.itid} 未配置`);
|
||||
continue;
|
||||
}
|
||||
let { type, table, isCurrency } = dicItid;
|
||||
if(table == ITEM_TABLE.JEWEL) { // 装备
|
||||
if(handleType == HANDLE_REWARD_TYPE.RECEIVE) {
|
||||
for(let i = 0; i < good.count; i++) {
|
||||
jewels.push({ id: good.id, hid: good.hid })
|
||||
}
|
||||
} else {
|
||||
if(!!good.seqId) {
|
||||
jewels.push({ seqId: good.seqId });
|
||||
}
|
||||
}
|
||||
} else if (table == ITEM_TABLE.ITEM) { // 可叠加道具
|
||||
let index = items.findIndex(cur => cur.id == good.id);
|
||||
if(index > 0) {
|
||||
items[index].count += good.count;
|
||||
} else {
|
||||
items.push({ id: good.id, count: good.count });
|
||||
}
|
||||
} else if (table == ITEM_TABLE.SKIN) { // 皮肤,不可重复获得,不可删
|
||||
if(handleType == HANDLE_REWARD_TYPE.RECEIVE) {
|
||||
let index = skins.indexOf(good.id);
|
||||
if (index == -1) {
|
||||
skins.push(good.id);
|
||||
}
|
||||
}
|
||||
} else if (table == ITEM_TABLE.ROLE) {
|
||||
if(isCurrency) { // 3种货币
|
||||
let dicCurrency = CURRENCY.get(good.id);
|
||||
if(dicCurrency) {
|
||||
if(dicCurrency.type == CURRENCY_TYPE.GOLD) { // 金币,区分付费和免费,默认免费
|
||||
let index = gold.findIndex(cur => cur.isPay == !!good.isPay);
|
||||
if(index > 0) {
|
||||
gold[index].count += good.count;
|
||||
} else {
|
||||
gold.push({ count: good.count, isPay: !!good.isPay });
|
||||
}
|
||||
} else if (dicCurrency.type == CURRENCY_TYPE.COIN) { // 铜钱
|
||||
coin.push(good.count);
|
||||
} else if (dicCurrency.type == CURRENCY_TYPE.ACTION_POINT) { // 体力
|
||||
ap += good.count;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (type == CONSUME_TYPE.HEAD || type == CONSUME_TYPE.FRAME || type == CONSUME_TYPE.SPINE) { // 头像等,不可重复获得,不可删
|
||||
let index = figures.indexOf(good.id);
|
||||
if (index == -1) {
|
||||
figures.push(good.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return { items, jewels, gold, coin, ap, skins, figures }
|
||||
}
|
||||
|
||||
export async function checkGoods(roleId: string, goodIds: Array<number>) {
|
||||
let jewelSeqIds: Array<number> = [];
|
||||
@@ -546,79 +289,24 @@ export async function checkHeroEquips(roleId: string, quality: number) {
|
||||
return await HeroModel.checkEquipByQuality(roleId, quality);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解锁头像/相框
|
||||
* @param roleId 玩家id
|
||||
* @param conditions 解锁条件
|
||||
* @param role 如果已查询过role表就直接可以使用
|
||||
*/
|
||||
export async function unlockFigure(sid: string, roleId: string, conditions: { type: number, paramHid?: number, paramFavourLv?: number, paramSkinId?: number, paramWinStreakNum?: number }[], role?: RoleType) {
|
||||
let figureInfo = await pubUnlockFigure(roleId, conditions, role);
|
||||
await pushFigureUpdate(roleId, sid, figureInfo);
|
||||
}
|
||||
|
||||
export async function pushFigureUpdate(roleId: string, sid: string, figureInfo: { heads: Figure[], frames: Figure[], spines: Figure[] }) {
|
||||
if (!role || !role.heads || !role.frames) {
|
||||
role = await RoleModel.findByRoleId(roleId, ROLE_SELECT.GET_HEADS);
|
||||
}
|
||||
let { figureInfo, heads, frames, spines } = unlockFigureWithoutSave(conditions, role);
|
||||
role = await RoleModel.updateRoleInfo(roleId, { heads, frames, spines });
|
||||
if (!!figureInfo && (figureInfo.heads.length > 0 || figureInfo.frames.length > 0 || figureInfo.spines.length > 0)) {
|
||||
let uids = [{ uid: roleId, sid }];
|
||||
pinus.app.get('channelService').pushMessageByUids('onHeadChange', resResult(STATUS.SUCCESS, { ...figureInfo }), uids);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建多个武将
|
||||
* @param roleId
|
||||
* @param sid
|
||||
* @param serverId
|
||||
* @param heroInfo
|
||||
*/
|
||||
export async function createHeroes(roleId: string, roleName: string, sid: string, serverId: number, heroInfo: CreateHeroParam[]) {
|
||||
let hids = heroInfo.map(cur => cur.hid);
|
||||
let userHeroesMap = await HeroModel.findMapByHidRange(hids, roleId);
|
||||
|
||||
let infos: Map<number, { heroInfo: HeroUpdate, skinInfo: SkinUpdate }> = new Map(), pieces: ItemInter[] = [];
|
||||
for (let h of heroInfo) {
|
||||
let heroCount = h.count || 1;
|
||||
if (userHeroesMap.has(h.hid)) {
|
||||
let { pieceId, count } = transPiece(h.hid);
|
||||
pieces.push({ id: pieceId, count: count * heroCount });
|
||||
} else {
|
||||
let initInfo: { heroInfo: HeroUpdate, skinInfo: SkinUpdate };
|
||||
if(pinus.app.getServerType() == 'role') {
|
||||
initInfo = getInitHeroById(h.hid);
|
||||
} else {
|
||||
let roleServers = pinus.app.getServersByType('role');
|
||||
let server = getRandSingleEelm(roleServers);
|
||||
initInfo = await pinus.app.rpc.role.roleRemote.getInitHeroById.toServer(server.id, h.hid);
|
||||
}
|
||||
|
||||
initInfo.heroInfo = { ...initInfo.heroInfo, ...h };
|
||||
|
||||
infos.set(h.hid, initInfo);
|
||||
userHeroesMap.set(h.hid, null);
|
||||
if (heroCount > 1) {
|
||||
let { pieceId, count } = transPiece(h.hid);
|
||||
pieces.push({ id: pieceId, count: count * (heroCount - 1) });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let resultHeroes: HeroType[] = [], resultItems: RewardInter[] = [], heroes: HeroShowParam[] = [];
|
||||
if (infos.size > 0) {
|
||||
let createHero = new CreateHeroes(roleId, roleName, serverId);
|
||||
await createHero.createWithHeroInfo(infos);
|
||||
await createHero.pushMessage(sid);
|
||||
await createHero.updateRedisRank();
|
||||
heroes = createHero.getShowHeroes();
|
||||
resultHeroes = createHero.getResultHeroes();
|
||||
|
||||
await checkPopUpConditionInCreateHero(serverId, roleId, resultHeroes);
|
||||
}
|
||||
|
||||
if (pieces.length > 0) {
|
||||
let goods = await addItems(roleId, roleName, sid, pieces, ITEM_CHANGE_REASON.HERO_TRANSFER_PIECE);
|
||||
resultItems = goods;
|
||||
}
|
||||
return { heroes, resultHeroes, goods: resultItems }
|
||||
}
|
||||
|
||||
export async function createHero(roleId: string, roleName: string, sid: string, serverId: number, heroInfo: CreateHeroParam) {
|
||||
let result = await createHeroes(roleId, roleName, sid, serverId, [heroInfo]);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 皮肤数据变化去重、推送
|
||||
@@ -640,4 +328,335 @@ function pushHeroSkinMsg(heroskins: {skins: HeroSkin[], hid: number}[], skinInf
|
||||
if(pushSkinInfos.length > 0 || skinInfos.length > 0) {
|
||||
pinus.app.get('channelService').pushMessageByUids('onHeroSkinChange', resResult(STATUS.SUCCESS, { heros: heroskins, skins: skinInfos }), uids);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 只插入皮肤,不管那么多的
|
||||
* @param roleId
|
||||
* @param roleName
|
||||
* @param skinId
|
||||
* @returns
|
||||
*/
|
||||
export async function increaseSkin(roleId:string, roleName: string, skinId: number) {
|
||||
let dicSkin = gameData.fashion.get(skinId);
|
||||
if (!dicSkin) return false;
|
||||
|
||||
let skin = await SkinModel.increaseSkin(roleId, skinId, { roleId, roleName, id: skinId, skinName: dicSkin.name, hid: dicSkin.actorId, skinId: dicSkin.heroId });
|
||||
if(!skin) return false; // 插入失败
|
||||
return skin
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加皮肤
|
||||
* @param roleId 玩家id
|
||||
* @param roleName 玩家名
|
||||
* @param id 皮肤id
|
||||
* @param hero 武将,如果已经查询过这个武将就不用再查询一次,主意要select skins字段
|
||||
* @returns {{ hero, figureInfo, calAllHeroResult }} hero:添加皮肤后的武将 figureInfo: 触发头像添加信息 calAllHeroResult:全局战力加成后结果
|
||||
*/
|
||||
export async function addSkin(roleId: string, roleName: string, sid: string, skinId: number, enable: boolean, hero?: HeroType) {
|
||||
let skin = await increaseSkin(roleId, roleName, skinId);
|
||||
if(!skin) return false;
|
||||
|
||||
if(skin.hid && !hero) hero = await HeroModel.findByHidAndRole(skin.hid, roleId);
|
||||
let condition = { type: FIGURE_UNLOCK_CONDITION.GET_SKIN, paramSkinId: skinId };
|
||||
await unlockFigure(sid, roleId, [condition]); // 解锁头像
|
||||
await calAllHeroCe(HERO_SYSTEM_TYPE.ADD_SKIN, sid, roleId, {}, [skinId]); // 全局加成
|
||||
|
||||
if (hero) { // 有武将的,将皮肤链接到武将上
|
||||
let curSkin = hero.skins.find(cur => cur.id == skinId);
|
||||
if (!curSkin) {
|
||||
hero.skins.push({ id: skinId, skin: skin._id, enable, skinId: skin.skinId });
|
||||
await HeroModel.updateHeroInfo(roleId, hero.hid, hero);
|
||||
}
|
||||
return hero;
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export async function addBags(roleId: string, roleName: string, datas: { id: number, count: number }[], reason: number) {
|
||||
let items: { id: number, count: number, inc: number }[] = [];
|
||||
for(let data of datas) {
|
||||
let item = await addBag(roleId, roleName, data, reason);
|
||||
items.push(item)
|
||||
}
|
||||
return { items }
|
||||
}
|
||||
|
||||
export async function addBag(roleId: string, roleName: string, data: { id: number, count: number }, reason: number) {
|
||||
let { id, count } = data;
|
||||
let { name: itemName, itid, hid } = gameData.goods.get(id);
|
||||
let { type } = ITID.get(itid);
|
||||
|
||||
let item = await ItemModel.increaseItem(roleId, id, count, { roleId, roleName, itemName, id, type, hid });
|
||||
return { id: item.id, count: item.count, inc: count, reason };
|
||||
}
|
||||
|
||||
|
||||
export async function addJewels(roleId: string, roleName: string, jewels: { id: number, }[], reason: number) {
|
||||
let jewelInfo: jewelUpdate[] = [];
|
||||
for(let jewel of jewels) {
|
||||
let info = await getAddJewelInfo(roleId, roleName, jewel);
|
||||
jewelInfo.push(info);
|
||||
}
|
||||
|
||||
const jewelResult = await JewelModel.createJewels(roleId, jewelInfo);
|
||||
return { jewels: jewelResult.map(jewel => {
|
||||
return { ...jewel, count: 1, inc: 1, reason }
|
||||
})}
|
||||
}
|
||||
|
||||
export async function getAddJewelInfo(roleId: string, roleName: string, jewel: { id: number, }) {
|
||||
let { id, } = jewel;
|
||||
let { name, randomEffect, effectCount } = gameData.jewel.get(id);
|
||||
|
||||
// 随机属性
|
||||
let randomResult: number[] = getRandEelm(randomEffect, effectCount);
|
||||
|
||||
let randSe: Array<RandSe> = randomResult.map((id: number, index: number) => {
|
||||
return getJewelRandSe(index + 1, id);
|
||||
});
|
||||
|
||||
return { roleId, roleName, id, name, randSe };
|
||||
}
|
||||
|
||||
/**
|
||||
* 天晶石已知词条随机值
|
||||
* @param id 词条位置,第几条
|
||||
* @param seid 词条id
|
||||
* @returns
|
||||
*/
|
||||
export function getJewelRandSe(id: number, seid: number) {
|
||||
let dicRandom = gameData.randomEffectPool.get(seid)
|
||||
let rand = 0;
|
||||
if (dicRandom.id > 0) {
|
||||
let randRange = getRandEelmWithWeight(dicRandom.rate);
|
||||
let randResult = getRandValueByMinMax(randRange.dic.min, randRange.dic.max, getDecimalCnt(dicRandom.gap));
|
||||
let n = Math.floor((randResult - dicRandom.Min)/dicRandom.gap);
|
||||
rand = dicRandom.Min + n * dicRandom.gap;
|
||||
}
|
||||
return new RandSe(id, dicRandom.id, rand);
|
||||
}
|
||||
|
||||
export function getGoldId() {
|
||||
return CURRENCY_BY_TYPE.get(CURRENCY_TYPE.GOLD);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取元宝物品 { id, count }
|
||||
* @param count 元宝数量
|
||||
*/
|
||||
export function getGoldObject(count: number) {
|
||||
return { id: CURRENCY_BY_TYPE.get(CURRENCY_TYPE.GOLD), count };
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取金币物品 { id, count }
|
||||
* @param count 元宝数量
|
||||
*/
|
||||
export function getCoinObject(count: number) {
|
||||
return { id: CURRENCY_BY_TYPE.get(CURRENCY_TYPE.COIN), count };
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取体力物品 { id, count }
|
||||
* @param count 体力数量
|
||||
*/
|
||||
export function getApObject(count: number) {
|
||||
return { id: CURRENCY_BY_TYPE.get(CURRENCY_TYPE.ACTION_POINT), count };
|
||||
}
|
||||
/**
|
||||
* @description 获取友情点物品 { id, count }
|
||||
* @param count 友情点数量
|
||||
*/
|
||||
export function getFriendPointObject(count: number) {
|
||||
return { id: CURRENCY_BY_TYPE.get(CURRENCY_TYPE.FRIEND_POINT), count };
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取功勋物品 { id, count }
|
||||
* @param count 功勋数量
|
||||
*/
|
||||
export function getHonourObject(count: number) {
|
||||
return { id: CURRENCY_BY_TYPE.get(CURRENCY_TYPE.HONOUR), count };
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回 解锁头像/相框
|
||||
* @param conditions 解锁条件
|
||||
* @param role 如果已查询过role表就直接可以使用
|
||||
*/
|
||||
export function unlockFigureWithoutSave(conditions: { type: number, paramHid?: number, paramFavourLv?: number, paramSkinId?: number, paramWinStreakNum?: number }[], role: RoleType) {
|
||||
|
||||
let { heads, frames, spines } = role;
|
||||
let figureInfo = { heads: new Array<Figure>(), frames: new Array<Figure>(), spines: new Array<Figure>() };
|
||||
for (let { type, paramHid, paramFavourLv, paramSkinId, paramWinStreakNum } of conditions) {
|
||||
let canUnLockList = gameData.figureCondition.get(type);
|
||||
if (canUnLockList) {
|
||||
let reason = 0;
|
||||
|
||||
for (let { id, params, gid } of canUnLockList) {
|
||||
let flag = false; // 是否达成条件
|
||||
if (type == FIGURE_UNLOCK_CONDITION.GET_HERO) {
|
||||
let [hid] = params;
|
||||
if (paramHid == hid) flag = true;
|
||||
reason = ITEM_CHANGE_REASON.GET_HERO_UNLOCK_FIGURE;
|
||||
} else if (type == FIGURE_UNLOCK_CONDITION.HERO_FAVOR) {
|
||||
let [hid, favourLv] = params;
|
||||
if (paramHid == hid && paramFavourLv >= favourLv) flag = true;
|
||||
reason = ITEM_CHANGE_REASON.HERO_FAVOR_UNLOCK_FIGURE;
|
||||
} else if (type == FIGURE_UNLOCK_CONDITION.GET_SKIN) {
|
||||
let [id] = params;
|
||||
if (paramSkinId == id) flag = true;
|
||||
reason = ITEM_CHANGE_REASON.ADD_SKIN_UNLOCK_FIGURE;
|
||||
} else if (type == FIGURE_UNLOCK_CONDITION.PVP_WIN_SERIES) {
|
||||
let [winStreakNum] = params;
|
||||
if (paramWinStreakNum >= winStreakNum) flag = true;
|
||||
reason = ITEM_CHANGE_REASON.PVP_SERIES_UNLOCK_FIGURE;
|
||||
}
|
||||
if (!flag) continue;
|
||||
let dicGood = gameData.goods.get(gid);
|
||||
if (!dicGood) continue;
|
||||
let dicItid = ITID.get(dicGood.itid);
|
||||
if (!dicItid) continue;
|
||||
|
||||
if (dicItid.type == CONSUME_TYPE.HEAD) {
|
||||
let figure = unlockSingleFigure(heads, gid, reason, false, id);
|
||||
if (figure && figure.unlocked) figureInfo.heads.push(figure);
|
||||
} else if (dicItid.type == CONSUME_TYPE.FRAME) {
|
||||
let figure = unlockSingleFigure(frames, gid, reason, false, id);
|
||||
if (figure && figure.unlocked) figureInfo.frames.push(figure);
|
||||
} else if (dicItid.type == CONSUME_TYPE.SPINE) {
|
||||
let figure = unlockSingleFigure(spines, gid, reason, false, id);
|
||||
if (figure && figure.unlocked) figureInfo.spines.push(figure);
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return { figureInfo, heads, frames, spines };
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 直接获得形象/相框
|
||||
export async function addFigure(roleId: string, ids: number[], reason: number) {
|
||||
let role = await RoleModel.findByRoleId(roleId, ROLE_SELECT.GET_HEADS);
|
||||
|
||||
if (!role) return false;
|
||||
let { heads, frames, spines } = role;
|
||||
|
||||
let figureInfo = { heads: [], frames: [], spines: [] };
|
||||
for (let gid of ids) {
|
||||
let dicGoods = gameData.goods.get(gid);
|
||||
if (!dicGoods) continue;
|
||||
let dicItid = ITID.get(dicGoods.itid);
|
||||
if (!dicItid) continue;
|
||||
|
||||
if (dicItid.type == CONSUME_TYPE.HEAD) {
|
||||
let figure = unlockSingleFigure(heads, gid, reason, true);
|
||||
if (figure && figure.unlocked) figureInfo.heads.push(figure);
|
||||
} else if (dicItid.type == CONSUME_TYPE.FRAME) {
|
||||
let figure = unlockSingleFigure(frames, gid, reason, true);
|
||||
if (figure && figure.unlocked) figureInfo.frames.push(figure);
|
||||
} else if (dicItid.type == CONSUME_TYPE.SPINE) {
|
||||
let figure = unlockSingleFigure(spines, gid, reason, true);
|
||||
if (figure && figure.unlocked) figureInfo.spines.push(figure);
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
role = await RoleModel.updateRoleInfo(roleId, { heads, frames, spines });
|
||||
return figureInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据物品id解锁/获得玩家数据
|
||||
* @param dbFigures 数据库内字段
|
||||
* @param id 物品id
|
||||
* @param unlockDirect 是否不计算解锁条件直接解锁
|
||||
* @param conditionId 条件id
|
||||
*/
|
||||
function unlockSingleFigure(dbFigures: Figure[], id: number, reason: number, unlockDirect = false, conditionId?: number) {
|
||||
let index = dbFigures.findIndex(cur => cur.id == id);
|
||||
let figure = dbFigures[index];
|
||||
if (index == -1) {
|
||||
figure = new Figure(id, false);
|
||||
dbFigures.push(figure);
|
||||
index = dbFigures.length - 1;
|
||||
}
|
||||
if (figure.unlocked) return; // 已解锁过
|
||||
if (!figure.unlockedId) figure.unlockedId = new Array<number>();
|
||||
|
||||
let dicGoods = gameData.goods.get(id);
|
||||
|
||||
let hasUnlockedAll = true;
|
||||
if (!unlockDirect) { // 不能直接获得,需要通过type解锁
|
||||
if (figure.unlockedId.includes(conditionId)) return;
|
||||
|
||||
figure.unlockedId.push(conditionId);
|
||||
|
||||
for (let { id: cid } of dicGoods.condition) {
|
||||
if (!figure.unlockedId.includes(cid)) {
|
||||
hasUnlockedAll = false; break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (hasUnlockedAll) {
|
||||
figure.unlocked = true;
|
||||
delete figure.unlockedId;
|
||||
|
||||
if (dicGoods.timeLimit) {
|
||||
figure.time = nowSeconds() + dicGoods.timeLimit * 86400
|
||||
}
|
||||
}
|
||||
|
||||
figure.inc = 1;
|
||||
figure.reason = reason;
|
||||
dbFigures[index] = figure;
|
||||
return figure
|
||||
}
|
||||
|
||||
// async function getSkinsOfThisHero(roleId: string, roleName: string, hid: number, initialSkin: number, allSkins?: SkinType[]) {
|
||||
// if(!allSkins) allSkins = await SkinModel.findbyRoleAndHid(roleId, hid);
|
||||
// let skin = await increaseSkin(roleId, roleName, initialSkin);
|
||||
// if(skin) allSkins.push(skin);
|
||||
// let skins: { id: number, skin: string, enable: boolean }[] = [];
|
||||
// for(let skin of allSkins) {
|
||||
// skins.push({ id: skin.id, skin: skin._id, enable: skin.id == initialSkin });
|
||||
// }
|
||||
|
||||
// return skins
|
||||
// }
|
||||
|
||||
export function combineFigureInfo(figureInfos: { heads: Figure[], frames: Figure[], spines: Figure[] }[]) {
|
||||
let figureInfo = { heads: new Array<Figure>(), frames: new Array<Figure>(), spines: new Array<Figure>() };
|
||||
for(let {heads, frames, spines} of figureInfos) {
|
||||
for(let head of heads) {
|
||||
figureInfo.heads.push(head);
|
||||
}
|
||||
for(let frame of frames) {
|
||||
figureInfo.frames.push(frame);
|
||||
}
|
||||
for(let spine of spines) {
|
||||
figureInfo.spines.push(spine);
|
||||
}
|
||||
}
|
||||
return figureInfo;
|
||||
}
|
||||
|
||||
// export class CreateHero {
|
||||
// roleId: string;
|
||||
// roleName: string;
|
||||
// serverId: number;
|
||||
|
||||
// constructor(roleId: string, roleName: string, serverId: number) {
|
||||
|
||||
// }
|
||||
// }
|
||||
Reference in New Issue
Block a user