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'; /** * 获得军团捐献,并检查是否刷新捐献数据 * @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, guild: GuildType) { let refTime = getZeroPoint(); let donation = await DonationModel.donate(code, refTime, fund, { id: donateId, roleName, time: refTime }, getDonateLv(guild)); 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 } }