diff --git a/game-server/app/servers/battle/handler/auctionHandler.ts b/game-server/app/servers/battle/handler/auctionHandler.ts index 15c3851a9..ed115c0f2 100644 --- a/game-server/app/servers/battle/handler/auctionHandler.ts +++ b/game-server/app/servers/battle/handler/auctionHandler.ts @@ -53,11 +53,11 @@ export class AuctionHandler { const serverId: number = session.get('serverId'); if (!guildCode) return resResult(STATUS.GUILD_NOT_FOUND) - const lots = await genAuction(guildCode, sourceType, sourceCode, serverId, rewards); - if (!lots) { + const result = await genAuction(guildCode, sourceType, sourceCode, serverId, rewards); + if (!result) { return resResult(STATUS.WRONG_PARMS); } - return resResult(STATUS.SUCCESS, { lots }); + return resResult(STATUS.SUCCESS, result); } } diff --git a/game-server/app/services/auctionService.ts b/game-server/app/services/auctionService.ts index 8faba9742..64f2cea5c 100644 --- a/game-server/app/services/auctionService.ts +++ b/game-server/app/services/auctionService.ts @@ -1,9 +1,11 @@ -import { LOT_CODE_LEN, AUCTION_STAGE, AUCTION_TIME } from './../consts'; -import { ItemReward } from "../domain/dbGeneral"; +import { DividendModel } from './../db/Dividend'; +import { LOT_CODE_LEN, AUCTION_STAGE, AUCTION_TIME, DIVIDEND_CODE_LEN } from './../consts'; +import { DividendRec, ItemReward } from "../domain/dbGeneral"; import { genCode } from '../pubUtils/util'; import { LotModel, LotParam } from '../db/Lot'; import { getNextTime } from '../pubUtils/timeUtil'; import { getGoodById } from '../pubUtils/data'; +import { DividendParam, DividendType } from '../db/Dividend'; // ! 获取底价,假数据 function getBasePrice(gid: number, count: number) { @@ -29,7 +31,7 @@ function getMaxPrice(gid: number, count: number) { export async function genAuction(guildCode: string, sourceType: number, sourceCode: string, serverId: number, rewards: ItemReward[]) { const begin = getNextTime(new Date(), AUCTION_TIME.GUILD_BEGIN_HOUR, AUCTION_TIME.GUILD_BEGIN_MIN); const end = getNextTime(new Date(), AUCTION_TIME.WORLD_END_HOUR, AUCTION_TIME.WORLD_END_MIN); - const lots: LotParam[] = rewards.map(reward => { + const lotsData: LotParam[] = rewards.map(reward => { const { id, count } = reward; const code = genCode(LOT_CODE_LEN); return { @@ -38,8 +40,13 @@ export async function genAuction(guildCode: string, sourceType: number, sourceCo maxPrice: getMaxPrice(id, count), curPrice: getBasePrice(id, count), } }); - const result = await LotModel.createRecs(lots); - return result; + const lots = await LotModel.createRecs(lotsData); + const dividendCode = genCode(DIVIDEND_CODE_LEN); + const dividendData: DividendParam = { + guildCode, sourceType, sourceCode, serverId, code: dividendCode, lots: [], dividends: [], totalPrice: 0, end + }; + const dividend = await DividendModel.createDividend(dividendData); + return { lots, dividend }; } /** @@ -59,3 +66,43 @@ export async function participants(guildCode: string, sourceType: number, source 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) { + const { code, guildCode, sourceType, sourceCode, lots, totalPrice, status, end } = dividend; + if (status === 3) return null; + 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, end); + return { + roleId, + baseNum, // 基础分红 + posNum, // 职位分红 + weekendNum, // 额外分红,周末 + total: baseNum + posNum + weekendNum, // 总分红 + status: 0, // 0:未领取,1:已领取 + }; + }); + await DividendModel.updateDividend(code, { dividends }); +} diff --git a/game-server/test/auction.test.ts b/game-server/test/auction.test.ts index e0784987c..3348078ef 100644 --- a/game-server/test/auction.test.ts +++ b/game-server/test/auction.test.ts @@ -113,7 +113,7 @@ describe('拍卖行测试', function() { }); it('查看我的拍卖纪录', function(done) { - pinusClient.request('battle.auctionHandler.getAuction', { count: 10 }, (res) => { + pinusClient.request('battle.auctionHandler.offerRecs', { count: 10 }, (res) => { checkSuccessResponse(res, false); done(); }); diff --git a/shared/consts/constModules/auctionConst.ts b/shared/consts/constModules/auctionConst.ts index 5ad8d97b4..b9b39023c 100644 --- a/shared/consts/constModules/auctionConst.ts +++ b/shared/consts/constModules/auctionConst.ts @@ -14,6 +14,7 @@ export const AUCTION_SOURCE = { }; export const LOT_CODE_LEN = 8; +export const DIVIDEND_CODE_LEN = 8; // 世界拍卖开始时间需晚于军团拍卖结束时间 export const AUCTION_TIME = { @@ -25,4 +26,11 @@ export const AUCTION_TIME = { GUILD_END_MIN: 30, // 军团拍卖结束 WORLD_END_HOUR: 10, // 世界拍卖结束 WORLD_END_MIN: 0, // 世界拍卖结束 -} \ No newline at end of file +}; + +export const DIVIDEND_STATUS = { + DEFAULT: 0, // 0:未开始 + ING: 1, // 1:进行中 + END: 2, // 2:已结束 + SENT: 3, // 3:已发放 +}; diff --git a/shared/db/Dividend.ts b/shared/db/Dividend.ts index 90ed6067c..353464020 100644 --- a/shared/db/Dividend.ts +++ b/shared/db/Dividend.ts @@ -11,9 +11,11 @@ import { genCode } from '../pubUtils/util'; @index({ guildCode: 1 }) export default class Dividend extends BaseModel { @prop({ required: true, default: 0 }) - type: number; // 0:初始值,1:演武;2:蛮夷入侵;3:诸侯混战;4:粮草先行 + serverId: number; // 区服编号 @prop({ required: true, default: '' }) guildCode: string; // 军团编号 + @prop({ required: true, default: 0 }) + sourceType: number; // 0:初始值,1:演武;2:蛮夷入侵;3:诸侯混战;4:粮草先行 @prop({ required: true, default: '' }) sourceCode: string; // 来源的唯一标识,如活动编号 @prop({ required: true, default: '' }) @@ -24,6 +26,10 @@ export default class Dividend extends BaseModel { totalPrice: number; // 分红总金额 @prop({ required: true, type: DividendRec, default: [] }) dividends: DividendRec[]; + @prop({ required: true, default: 0 }) + status: number; // 0:未开始;1:进行中;2:已结束;3:已发放 + @prop({ required: true }) + end: Date; public static async createDividend(data: DividendParam) { const code = genCode(8); @@ -36,6 +42,11 @@ export default class Dividend extends BaseModel { const result = await DividendModel.findOne({ code }).select('-_id -__v').lean(); return result; } + + public static async updateDividend(code: string, update: DividendParam) { + const result = await DividendModel.findOneAndUpdate({ code }, { ...update }, { new: true }).select('-_id -__v').lean(); + return result; + } } export const DividendModel = getModelForClass(Dividend); diff --git a/shared/db/Lot.ts b/shared/db/Lot.ts index f76c82835..e2ad1a6c3 100644 --- a/shared/db/Lot.ts +++ b/shared/db/Lot.ts @@ -56,8 +56,8 @@ export default class Lot extends BaseModel { return result; } - public static async findGuildLotsByTime(guildCode: string, time: Date) { - const results = await LotModel.find({ guildCode, createdAt: { $gte: time } }).select('-_id -__v').lean(); + public static async findGuildLotsByTime(guildCode: string, sourceType: number, time: Date) { + const results = await LotModel.find({ guildCode, sourceType, createdAt: { $gte: time } }).select('-_id -__v').lean(); return results; } } diff --git a/shared/domain/dbGeneral.ts b/shared/domain/dbGeneral.ts index 452db7cc3..ec2049b7f 100644 --- a/shared/domain/dbGeneral.ts +++ b/shared/domain/dbGeneral.ts @@ -273,4 +273,6 @@ export class DividendRec { weekendNum: number; // 额外分红,周末 @prop({ required: true, default: 0 }) total: number; // 总分红 + @prop({ required: true, default: 0 }) + status: number; // 0:未领取,1:已领取 } \ No newline at end of file