Files
ZYZ/game-server/app/services/gvg/gvgItemService.ts
2023-02-17 10:58:47 +08:00

98 lines
3.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { GVG_RETURN_ITEM_TYPE, ITEM_CHANGE_REASON, LEAGUE_ITEM_REFRESH_TYPE, GVG_ITEM, PUSH_ROUTE } from "../../consts";
import { GVGUserItemModel } from "../../db/GVGUserItem";
import { gameData } from "../../pubUtils/data";
import { RewardInter } from "../../pubUtils/interface";
import { DAY_TO_SECOND, getZeroPoint, getFutureTime } from "../../pubUtils/timeUtil";
import { sendMessageToUserWithSuc } from "../pushService";
import { addItems, handleCost } from "../role/rewardService";
import { getGVGConfig } from "./gvgService";
/**
* 获取道具必须在guild进程下使用
* @param roleId
* @param roleName
* @param leagueCode
* @param sid
* @param leagueItems GVG内专用道具
* @param items 普通道具
* @param reason
* @returns
*/
export async function addGVGReward(roleId: string, roleName: string, leagueCode: string, sid: string, leagueItems: RewardInter[], items: RewardInter[], reason: ITEM_CHANGE_REASON) {
let returnGoods: { id: number, count: number, itemType: number }[] = [], pushGoods: { id: number, count: number, expireTime: number }[] = [];
let { configId } = getGVGConfig();
if(items.length > 0) {
const goods = await addItems(roleId, roleName, sid, items, reason);
for(let { id, count } of goods) {
returnGoods.push({ id, count, itemType: GVG_RETURN_ITEM_TYPE.NORMAL_ITEM });
}
}
for(let { id, count } of leagueItems) {
let dicItem = gameData.gvgItem.get(id);
if(!dicItem) continue;
let item = await GVGUserItemModel.increaseItem(configId, leagueCode, roleId, id, count, getExpireTime(dicItem.refreshType));
returnGoods.push({ id, count, itemType: GVG_RETURN_ITEM_TYPE.GVG_ITEM });
pushGoods.push({ id, count: item.count, expireTime: item.expireTime });
}
if(pushGoods.length > 0) {
await sendMessageToUserWithSuc(roleId, PUSH_ROUTE.LEAGUE_ITEM_UPDATE, { items: pushGoods }, sid);
}
return returnGoods;
}
function getExpireTime(refreshType: number) {
if(refreshType == LEAGUE_ITEM_REFRESH_TYPE.DAILY) {
return getZeroPoint() + DAY_TO_SECOND;
}
return getFutureTime();
}
/**
* 消耗道具
* @param roleId
* @param leagueCode
* @param sid
* @param leagueItems GVG内专用道具
* @param items 普通背包道具
* @param reason
* @returns
*/
export async function handleGVGCost(roleId: string, leagueCode: string, sid: string, leagueItems: RewardInter[], items: RewardInter[], reason: ITEM_CHANGE_REASON) {
let { configId } = getGVGConfig();
// 检查本地道具数量
const curItems = await GVGUserItemModel.findByRoleAndIds(configId, leagueCode, roleId, leagueItems.map(cur => cur.id));
for(let { id, count } of leagueItems) {
let curItem = curItems.find(cur => cur.id == id);
if(!curItem || curItem.count < count) {
return false;
}
}
const itemResult = await handleCost(roleId, sid, items, reason);
if(!itemResult) return false;
const decreaseResult = await GVGUserItemModel.decreaseItem(configId, leagueCode, roleId, leagueItems);
const pushGoods = decreaseResult.map(item => ({ id: item.id, count: item.count, expireTime: item.expireTime }));
if(pushGoods.length > 0) await sendMessageToUserWithSuc(roleId, PUSH_ROUTE.LEAGUE_ITEM_UPDATE, { items: pushGoods }, sid);
return true;
}
export function getProduceCoinCnt(items: RewardInter[]) {
let produceCoinCnt = 0;
for(let { id, count } of items) {
if(id == GVG_ITEM.PRODUCE_COIN) {
produceCoinCnt = count; break;
}
}
return produceCoinCnt;
}
export function combinePushItem(targetItems: RewardInter[], dicItems: RewardInter[]) {
for(let { id, count } of dicItems) {
let obj = targetItems.find(obj => obj.id == id);
if(!obj) {
targetItems.push({ id, count });
} else {
obj.count += count;
}
}
}