88 lines
3.3 KiB
TypeScript
88 lines
3.3 KiB
TypeScript
|
||
import { DonationModel, Report } from '../db/Donation';
|
||
import { getZeroPoint, nowSeconds } from '../pubUtils/timeUtil';
|
||
import { GuildModel, GuildType } from '../db/Guild';
|
||
import { findWhere } from 'underscore';
|
||
import { GUILD_STRUCTURE } from '../consts/constModules/guildConst';
|
||
import { gameData } from '../pubUtils/data';
|
||
import { shouldRefresh } from '../pubUtils/util';
|
||
import { recordGuildFund } from './activity/timeLimitRankService';
|
||
import { pushGuildInfoUpdate } from './guildService';
|
||
import { getArmyDonateBoxBaseById, getArmyDonateBoxBaseByLvAndIndex } from '../pubUtils/data';
|
||
/**
|
||
* 获得军团捐献,并检查是否刷新捐献数据
|
||
* @param code
|
||
* @param serverId
|
||
*/
|
||
export async function getDonation(code: string, guild: GuildType) {
|
||
let refTime = getZeroPoint();
|
||
let donation = await DonationModel.findOrCreateDonationByRefTime(code, refTime, getDonateLv(guild));
|
||
return donation;
|
||
}
|
||
|
||
export async function donate(code: string, fund: number, donateId: number, roleName: string, donateLv: number) {
|
||
let refTime = getZeroPoint();
|
||
let donation = await DonationModel.donate(code, refTime, fund, { id: donateId, roleName, time: refTime, fund }, donateLv);
|
||
return donation;
|
||
}
|
||
|
||
export function getDonateLv(guild: GuildType) {
|
||
let structure = guild.structure.find(cur => cur.id == GUILD_STRUCTURE.DONATE);
|
||
return structure? structure.lv: 0;
|
||
}
|
||
|
||
export async function getGuildFundByRefTime(guildCode: string, refTime: number) {
|
||
let donation = await DonationModel.findDonationByRefTime(guildCode, refTime);
|
||
return { donationLv: donation? donation.donationLv: 0, fund: donation? donation.donateFund: 0}
|
||
}
|
||
|
||
// 增加资金
|
||
export async function addFund(code: string, serverId: number, fund: number) {
|
||
try {
|
||
let guild = await GuildModel.findByCode(code, serverId, 'lv todayFund refTodayFund');
|
||
let {lv, todayFund, refTodayFund} = guild;
|
||
if(!guild) return null
|
||
|
||
let dicStructure = gameData.centerBase.get(lv);
|
||
if(!dicStructure) return null
|
||
if(shouldRefresh(refTodayFund, new Date())) {
|
||
todayFund = 0;
|
||
refTodayFund = new Date();
|
||
}
|
||
if(todayFund + fund > dicStructure.maxFund) {
|
||
fund = dicStructure.maxFund - todayFund;
|
||
todayFund = dicStructure.maxFund;
|
||
} else {
|
||
todayFund += fund;
|
||
}
|
||
if(fund < 0) return null
|
||
|
||
guild = await GuildModel.updateInfo(code, { todayFund, refTodayFund }, { fund });
|
||
await pushGuildInfoUpdate(code, { fund: guild.fund });
|
||
await recordGuildFund(serverId, guild, fund);
|
||
|
||
return guild
|
||
} catch(e) {
|
||
return null
|
||
}
|
||
|
||
}
|
||
|
||
/**
|
||
* 将已经领取的其他等级捐赠宝箱id转为donationLv对应等级相同index的宝箱id
|
||
*
|
||
* @export
|
||
* @param {number[]} receiveBoxs 已领取的宝箱id
|
||
* @param {number} donationLv 当前捐赠等级
|
||
*/
|
||
export function changeReceiveBoxIdByLvAndIndex(receiveBoxs: number[], donationLv: number) {
|
||
let haveGotBoxIds: number[] = [];
|
||
receiveBoxs.forEach((boxId) => {
|
||
// 获取已领取箱子的index
|
||
let { index } = getArmyDonateBoxBaseById(boxId);
|
||
// 获取donationLv等级,第index个箱子的id
|
||
let donateInfo = getArmyDonateBoxBaseByLvAndIndex(donationLv, index);
|
||
haveGotBoxIds.push(donateInfo ? donateInfo.id : 0);
|
||
});
|
||
return haveGotBoxIds;
|
||
} |