拍卖行:出价、关注接口和测试用例

This commit is contained in:
liangtongchuan
2021-03-18 20:27:00 +08:00
parent 46a625761c
commit 180f147951
7 changed files with 225 additions and 64 deletions
@@ -1,13 +1,14 @@
import { DividendModel } from './../../../db/Dividend';
import { Application, BackendSession, ChannelService } from "pinus";
import { AUCTION_STAGE, DEBUG_MAGIC_WORD, STATUS, OFFER_RATIO, CURRENCY_BY_TYPE, CURRENCY_TYPE } from "../../../consts";
import { AUCTION_STAGE, DEBUG_MAGIC_WORD, STATUS, OFFER_RATIO, CURRENCY_BY_TYPE, CURRENCY_TYPE, DATA_NAME, LOT_STATUS } from "../../../consts";
import { LotModel } from "../../../db/Lot";
import { ItemReward } from "../../../domain/dbGeneral";
import { resResult } from "../../../pubUtils/util";
import { auctionBegin, auctionStage, calculateDividend, genAuction, todayGuildBegin } from "../../../services/auctionService";
import { auctionStage, calculateDividend, genAuction, todayGuildBegin } from "../../../services/auctionService";
import { addItems, handleCost } from '../../../services/rewardService';
import { getSimpleRoleInfo } from '../../../services/roleService';
import { getRoleOnlineInfo } from '../../../services/redisService';
import { lockData } from '../../../services/redLockService';
export default function (app: Application) {
return new AuctionHandler(app);
@@ -37,41 +38,79 @@ export class AuctionHandler {
async offer(msg: { code: string, max: boolean }, session: BackendSession) {
const { code, max } = msg;
let maxFlag = max;
const lot = await LotModel.findLot(code);
if (!lot) return resResult(STATUS.GUILD_LOT_NOT_FOUND);
const roleId = session.get('roleId');
const roleName = session.get('roleName');
const sid = session.get('sid');
const { curBuyer, curPrice, maxPrice, gid, count, bidRoles } = lot;
if (curBuyer === roleId) return resResult(STATUS.LOT_OFFER_SERIAL);
let newPrice = parseInt((curPrice * OFFER_RATIO).toFixed(0));
if (newPrice >= maxPrice) {
newPrice = maxPrice;
maxFlag = true;
}
const costRes = await handleCost(roleId, sid, [{ id: CURRENCY_BY_TYPE.get(CURRENCY_TYPE.GOLD), count: newPrice }]);
if (!costRes) return resResult(STATUS.ROLE_COIN_NOT_ENOUGH);
const { roleName: buyerName } = await getSimpleRoleInfo(curBuyer);
const { sid: buyerSid } = await getRoleOnlineInfo(buyerName);
await addItems(curBuyer, buyerName, buyerSid, [{ id: CURRENCY_BY_TYPE.get(CURRENCY_TYPE.GOLD), count: curPrice }]);
// TODO 元宝返还纪录
if (maxFlag) {
newPrice = maxPrice;
await addItems(roleId, roleName, sid, [{ id: gid, count }]);
}
bidRoles.push({roleId, price: newPrice, time: new Date()});
const newLot = await LotModel.updateLot({ code, curBuyer: roleId, curPrice: newPrice, bidRoles});
const serverId = session.get('serverId');
let res: any = await lockData(serverId, DATA_NAME.AUCTION_LOT, code);// 加锁
const incPrice = newPrice - (curBuyer ? curPrice : 0);
const dividend = await DividendModel.updateLot(code, gid, newPrice, incPrice);
const newDividend = await calculateDividend(dividend);
// TODO 加关注
return resResult(STATUS.SUCCESS, { lot: newLot, dividend: newDividend });
try {
if (!res) {
return resResult(STATUS.REDLOCK_ERR);
}
const lot = await LotModel.findLot(code);
if (!lot || lot.status === LOT_STATUS.SOLD) {
res.releaseCallback();
return resResult(STATUS.GUILD_LOT_NOT_FOUND);
}
const { curBuyer, curPrice, maxPrice, gid, count, bidRoles, watchingRoles } = lot;
if (curBuyer === roleId) {
res.releaseCallback();
return resResult(STATUS.LOT_OFFER_SERIAL);
}
let newPrice = parseInt((curPrice * OFFER_RATIO).toFixed(0));
let maxFlag = max;
if (newPrice >= maxPrice) {
newPrice = maxPrice;
maxFlag = true;
}
const costRes = await handleCost(roleId, sid, [{ id: CURRENCY_BY_TYPE.get(CURRENCY_TYPE.GOLD), count: newPrice }]);
if (!costRes) {
res.releaseCallback();
return resResult(STATUS.ROLE_COIN_NOT_ENOUGH);
}
if (curBuyer) {
const { roleName: buyerName } = await getSimpleRoleInfo(curBuyer);
const { sid: buyerSid } = await getRoleOnlineInfo(buyerName);
await addItems(curBuyer, buyerName, buyerSid, [{ id: CURRENCY_BY_TYPE.get(CURRENCY_TYPE.GOLD), count: curPrice }]);
}
// TODO 元宝返还纪录
if (maxFlag) {
newPrice = maxPrice;
await addItems(roleId, roleName, sid, [{ id: gid, count }]);
}
bidRoles.push({roleId, price: newPrice, time: new Date()});
const newLot = await LotModel.updateLot({ code, curBuyer: roleId, curPrice: newPrice, bidRoles, status: maxFlag ? LOT_STATUS.SOLD : LOT_STATUS.ING, watchingRoles: Array.from(new Set([...watchingRoles, roleId])) });
res.releaseCallback();
const incPrice = newPrice - (curBuyer ? curPrice : 0);
const dividend = await DividendModel.updateLot(code, gid, newPrice, incPrice);
const newDividend = await calculateDividend(dividend);
return resResult(STATUS.SUCCESS, { lot: newLot, dividend: newDividend });
} catch (e) {
console.log('offer got err:', e);
res.releaseCallback();
return resResult(STATUS.INTERNAL_ERR);
}
}
async watchLot(msg: { code: string }, session: BackendSession) {
return resResult(STATUS.SUCCESS);
const roleId = session.get('roleId');
const { code } = msg;
const lot = await LotModel.watchLot(code, roleId);
if (!lot) return resResult(STATUS.WRONG_PARMS);
return resResult(STATUS.SUCCESS, { lot });
}
async unWatchLot(msg: { code: string }, session: BackendSession) {
const roleId = session.get('roleId');
const { code } = msg;
const lot = await LotModel.unWatchLot(code, roleId);
if (!lot) return resResult(STATUS.WRONG_PARMS);
return resResult(STATUS.SUCCESS, { lot });
}
async checkDividend(msg: {}, session: BackendSession) {