141 lines
5.3 KiB
TypeScript
141 lines
5.3 KiB
TypeScript
import { DividendModel } from './../db/Dividend';
|
||
import { LOT_CODE_LEN, AUCTION_STAGE, AUCTION_TIME, DIVIDEND_CODE_LEN, DIVIDEND_STATUS, LOT_STATUS } from './../consts';
|
||
import { DividendRec, ItemReward } from "../domain/dbGeneral";
|
||
import { genCode } from '../pubUtils/util';
|
||
import { LotModel, LotParam } from '../db/Lot';
|
||
import { getNextTime, getTodayZeroDate } from '../pubUtils/timeUtil';
|
||
import { getGoodById } from '../pubUtils/data';
|
||
import { DividendParam, DividendType } from '../db/Dividend';
|
||
|
||
// ! 获取底价,假数据
|
||
function getBasePrice(gid: number, count: number) {
|
||
const good = getGoodById(gid);
|
||
return (good ? good.quality * 100 : 100) * count;
|
||
}
|
||
|
||
// ! 获取一口价,假数据
|
||
function getMaxPrice(gid: number, count: number) {
|
||
const good = getGoodById(gid);
|
||
return (good ? good.quality * 200 : 200) * count;
|
||
}
|
||
|
||
export function auctionStage() {
|
||
const curTime = new Date().getTime();
|
||
if (curTime < todayGuildBegin().getTime()) return AUCTION_STAGE.DEFAULT;
|
||
if (curTime < todayWorldBegin().getTime() && curTime > todayGuildBegin().getTime()) return AUCTION_STAGE.GUILD;
|
||
if (curTime > todayWorldBegin().getTime() && curTime < todayWorldEnd().getTime()) return AUCTION_STAGE.WORLD;
|
||
if (curTime > todayWorldEnd().getTime()) return AUCTION_STAGE.END;
|
||
}
|
||
|
||
export function auctionBegin() {
|
||
return getNextTime(new Date(), AUCTION_TIME.GUILD_BEGIN_HOUR, AUCTION_TIME.GUILD_BEGIN_MIN);
|
||
}
|
||
|
||
export function auctionEnd() {
|
||
return getNextTime(new Date(), AUCTION_TIME.WORLD_END_HOUR, AUCTION_TIME.WORLD_END_MIN);
|
||
}
|
||
|
||
export function todayGuildBegin() {
|
||
return getNextTime(getTodayZeroDate(), AUCTION_TIME.GUILD_BEGIN_HOUR, AUCTION_TIME.GUILD_BEGIN_MIN);
|
||
}
|
||
|
||
export function todayWorldBegin() {
|
||
return getNextTime(getTodayZeroDate(), AUCTION_TIME.WORLD_BEGIN_HOUR, AUCTION_TIME.WORLD_BEGIN_MIN);
|
||
}
|
||
|
||
export function todayWorldEnd() {
|
||
return getNextTime(getTodayZeroDate(), AUCTION_TIME.WORLD_END_HOUR, AUCTION_TIME.WORLD_END_MIN);
|
||
}
|
||
|
||
/**
|
||
* @description 生成拍卖数据
|
||
* @export
|
||
* @param {string} guildCode
|
||
* @param {number} sourceType
|
||
* @param {string} sourceCode
|
||
* @param {number} serverId
|
||
* @param {ItemReward[]} rewards
|
||
*/
|
||
export async function genAuction(guildCode: string, sourceType: number, sourceCode: string, serverId: number, rewards: ItemReward[]) {
|
||
const begin = auctionBegin();
|
||
const end = auctionEnd();
|
||
const lotsData: LotParam[] = rewards.map(reward => {
|
||
const { id, count } = reward;
|
||
const code = genCode(LOT_CODE_LEN);
|
||
return {
|
||
auctionStage: AUCTION_STAGE.DEFAULT, sourceType,
|
||
sourceCode, serverId, guildCode, code, gid: id, count, begin, end, status: LOT_STATUS.DEFAULT,
|
||
maxPrice: getMaxPrice(id, count), curPrice: getBasePrice(id, count),
|
||
}
|
||
});
|
||
const lots = await LotModel.createRecs(lotsData);
|
||
const dividendCode = genCode(DIVIDEND_CODE_LEN);
|
||
const dividendData: DividendParam = {
|
||
guildCode, sourceType, sourceCode, serverId, code: dividendCode, dividends: [], totalPrice: 0, begin, lots: lots.map(lot => {
|
||
const { code, gid } = lot;
|
||
return { code, gid, price: 0 }
|
||
}),
|
||
};
|
||
const dividend = await DividendModel.createDividend(dividendData);
|
||
return { lots, dividend };
|
||
}
|
||
|
||
/**
|
||
* ! 目前返回假数据
|
||
* @description 获取活动参加者
|
||
* @export
|
||
* @param {string} guildCode
|
||
* @param {number} sourceType
|
||
* @param {string} sourceCode
|
||
*/
|
||
export async function participants(guildCode: string, sourceType: number, sourceCode: string) {
|
||
return [{
|
||
roleId: 'rM8NZtlfzD',
|
||
position: 1,
|
||
}, {
|
||
roleId: 'EjX79SEddK',
|
||
position: 2,
|
||
}];
|
||
}
|
||
|
||
function baseDividend(totalPrice: number, roleNum: number) {
|
||
return roleNum > 10 ? totalPrice / roleNum : totalPrice / 10;
|
||
}
|
||
|
||
function posDividend(totalPrice: number, roleNum: number, position: number) {
|
||
return totalPrice / roleNum * position;
|
||
}
|
||
|
||
function weekendDividend(totalPrice: number, roleNum: number, date: Date) {
|
||
const day = date.getDay();
|
||
return (day === 0 || day === 6) ? totalPrice / roleNum : 0;
|
||
}
|
||
|
||
export async function calculateDividend(dividend: DividendType) {
|
||
if (!dividend) return null;
|
||
const { code, guildCode, sourceType, sourceCode, lots, totalPrice, status, begin } = dividend;
|
||
if (status === DIVIDEND_STATUS.SENT) return dividend;
|
||
const calcuTotalPrice = lots.reduce((acc, lot) => { return acc + lot.price }, 0);
|
||
if (calcuTotalPrice !== totalPrice) {
|
||
await DividendModel.updateDividend(code, { totalPrice: calcuTotalPrice });
|
||
// 更新 totalPrice
|
||
}
|
||
const participantsData = await participants(guildCode, sourceType, sourceCode);
|
||
const dividends: DividendRec[] = participantsData.map(data => {
|
||
const { roleId, position } = data;
|
||
const roleNum = participantsData.length;
|
||
const baseNum = baseDividend(calcuTotalPrice, roleNum);
|
||
const posNum = posDividend(calcuTotalPrice, roleNum, position);
|
||
const weekendNum = weekendDividend(calcuTotalPrice, roleNum, begin);
|
||
return {
|
||
roleId,
|
||
baseNum, // 基础分红
|
||
posNum, // 职位分红
|
||
weekendNum, // 额外分红,周末
|
||
total: baseNum + posNum + weekendNum, // 总分红
|
||
status: 0, // 0:未领取,1:已领取
|
||
};
|
||
});
|
||
return await DividendModel.updateDividend(code, { dividends });
|
||
}
|