161 lines
8.0 KiB
TypeScript
161 lines
8.0 KiB
TypeScript
import { Application, BackendSession } from 'pinus';
|
|
import { UserGuildModel } from '../../../db/UserGuild';
|
|
import { GuildModel } from '../../../db/Guild';
|
|
import { WishPoolReportModel } from '../../../db/WishPoolReport';
|
|
import { resResult, genCode } from '../../../pubUtils/util';
|
|
import { STATUS } from '../../../consts';
|
|
import { getArmyWishPoolBaseByLv, getGoodById } from '../../../pubUtils/data';
|
|
import { addItems, checkGoods } from '../../../services/rewardService';
|
|
import { IT_TYPE } from '../../../consts/constModules/itemConst';
|
|
import { GUILD_STRUCTURE } from '../../../consts/constModules/guildConst';
|
|
import { getUserGuildWithRefActive } from '../../../services/guildService';
|
|
import { findIndex, findWhere } from 'underscore';
|
|
import { RoleModel } from '../../../db/Role';
|
|
import { getRedis } from '../../../services/redisService';
|
|
export default function(app: Application) {
|
|
return new WishPoolHandler(app);
|
|
}
|
|
|
|
export class WishPoolHandler {
|
|
constructor(private app: Application) {
|
|
|
|
}
|
|
|
|
async getWishPool(msg: {}, session: BackendSession) {
|
|
const roleId: string = session.get('roleId');
|
|
let userGuild = await getUserGuildWithRefActive(roleId, 'wishDntCnt wishGoods guildCode');
|
|
if (!userGuild)
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
const { guildCode: code, wishDntCnt, wishGoods } = userGuild;
|
|
let userGuilds = await UserGuildModel.getWishPoolGoods(code, ' wishDntCnt wishGoods roleId');
|
|
let list = [];
|
|
userGuilds.map(({ wishGoods, roleId })=>{
|
|
wishGoods.map(({ type, goodId, count, receiveCnt, drawCnt, id })=>{
|
|
list.push({ type, goodId, count, receiveCnt, drawCnt, id, roleId })
|
|
});
|
|
});
|
|
return resResult(STATUS.SUCCESS, { list, wishDntCnt:wishDntCnt||0, wishGoods });
|
|
}
|
|
|
|
// 许愿
|
|
async wishGoods(msg: {goodId: number, type: number}, session: BackendSession) {
|
|
const { goodId, type } = msg;
|
|
const roleId: string = session.get('roleId');
|
|
const serverId: number = parseInt(session.get('serverId'));
|
|
let count;
|
|
let goodInfo = getGoodById(goodId)
|
|
if (!goodInfo)
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
if (!(goodInfo.goodType == IT_TYPE.HERO_PIECE && type == 2 ) && !(goodInfo.goodType == IT_TYPE.EQUIP_PIECE && type == 1 ))
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
let userGuild = await getUserGuildWithRefActive(roleId, ' wishDntCnt wishGoods guildCode wishGoods');
|
|
if (!userGuild)
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
let result = await checkGoods(roleId, [goodId]);
|
|
if (!result) {
|
|
if (goodInfo.goodType == IT_TYPE.HERO_PIECE ) {
|
|
result = await checkGoods(roleId, [goodInfo.hid]);
|
|
if (!result)
|
|
return resResult(STATUS.GUILD_WISH_POOL_NOT_OWN_HERO);
|
|
} else if (goodInfo.goodType == IT_TYPE.EQUIP_PIECE ) {
|
|
result = await checkGoods(roleId, [goodInfo.equipId]);
|
|
if (!result)
|
|
return resResult(STATUS.GUILD_WISH_POOL_NOT_OWN_EQUIP);
|
|
}
|
|
}
|
|
const { guildCode: code, wishGoods } = userGuild;
|
|
let { structure } = await GuildModel.findGuild(code, serverId, 'structure');
|
|
let { lv } = findWhere(structure, {id: GUILD_STRUCTURE.DONATE});
|
|
let { wishGoodsEquips, wishGoodsHeros } = getArmyWishPoolBaseByLv(lv);
|
|
let len = wishGoods.length;
|
|
if (len >= 1) //今日已经许愿过
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
if (type == 1) {
|
|
let wishGoodsEquip = findWhere(wishGoodsEquips, { quality: goodInfo.quality});
|
|
if (!wishGoodsEquip)
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
count = wishGoodsEquip.count;
|
|
} else {
|
|
let wishGoodsHero = findWhere(wishGoodsHeros, { quality: goodInfo.quality});
|
|
if (!wishGoodsHero)
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
count = wishGoodsHero.count;
|
|
}
|
|
|
|
const id = genCode(6);
|
|
let { wishGoods : resWishGoods } = await UserGuildModel.pushAndUpdate(roleId, {}, { wishGoods: { type, goodId, count, receiveCnt: 0, drawCnt: 0, id} }, 'wishGoods');
|
|
return resResult(STATUS.SUCCESS, { wishGoods: resWishGoods });
|
|
}
|
|
|
|
// 捐赠
|
|
async donateGoods(msg: {wishRoleId: string, id: string}, session: BackendSession) {
|
|
let { wishRoleId, id } = msg;
|
|
const dntRoleId: string = session.get('roleId');
|
|
const dntRoleName: string = session.get('roleName');
|
|
if (wishRoleId == dntRoleId)
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
let role = await RoleModel.findByRoleId(wishRoleId);
|
|
if (!role)
|
|
return resResult(STATUS.WRONG_PARMS);//没有玩家
|
|
let wishUserGuild = await getUserGuildWithRefActive(wishRoleId, ' wishDntCnt wishGoods guildCode');
|
|
if (!wishUserGuild)
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
const { guildCode: code, wishGoods } = wishUserGuild;
|
|
let dntRoleGuild = await getUserGuildWithRefActive(dntRoleId, ' wishDntCnt wishGoods guildCode');
|
|
if (!dntRoleGuild)
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
if (dntRoleGuild.guildCode != code)
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
if (dntRoleGuild.wishDntCnt >= 3)
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
let wishGood = findWhere(wishGoods, { id });
|
|
if (!wishGood)
|
|
return resResult(STATUS.WRONG_PARMS);//没有该许愿
|
|
if (wishGood.receiveCnt >= wishGood.count)
|
|
return resResult(STATUS.WRONG_PARMS);//已经收到
|
|
let { wishDntCnt } = await UserGuildModel.donateGoods(dntRoleId, 1, 'wishDntCnt');
|
|
let { wishGoods: resWishGoods } = await UserGuildModel.donateUpdate(wishRoleId, id, 'wishGoods');
|
|
let key = 'login_roleId_' + wishRoleId;
|
|
let sid = await getRedis(key);
|
|
let goods = await addItems(wishRoleId, role.roleName , sid , [{ id : wishGood.goodId, count: 1 }]);
|
|
if (!!sid) {
|
|
this.app.channelService.pushMessageByUids('onWishGoodsRecive', resResult(STATUS.SUCCESS, { goods }), [{uid:wishRoleId, sid}]);
|
|
}
|
|
await WishPoolReportModel.addReport(code, wishRoleId, role.roleName , dntRoleId, dntRoleName, wishGood.goodId, 1);
|
|
return resResult(STATUS.SUCCESS, { wishDntCnt, updateWishGoods: resWishGoods.map(({type, goodId, count, receiveCnt, drawCnt, id})=>{
|
|
return { type, goodId, count, receiveCnt, drawCnt, id, roleId: wishRoleId };
|
|
})});
|
|
}
|
|
|
|
async receiveGoods(msg: { id: string }, session: BackendSession) {
|
|
let { id } = msg;
|
|
const roleId: string = session.get('roleId');
|
|
const roleName: string = session.get('roleName');
|
|
const sid: string = session.get('sid');
|
|
let userGuild = await getUserGuildWithRefActive(roleId, 'wishDntCnt wishGoods guildCode');
|
|
if (!userGuild)
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
const { guildCode: code, wishGoods } = userGuild;
|
|
let index = findIndex(wishGoods, { id });
|
|
if (index == -1)
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
let { drawCnt, goodId } = wishGoods[index];
|
|
if (drawCnt <= 0)
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
let goods = await addItems(roleId, roleName, sid, [{ id : goodId, count: drawCnt }]);
|
|
wishGoods[index].drawCnt = 0;
|
|
await UserGuildModel.updateInfo(code, { wishGoods }, {});
|
|
return resResult(STATUS.SUCCESS, { goods, wishGoods });
|
|
}
|
|
|
|
async getReports(msg: {}, session: BackendSession) {
|
|
const roleId: string = session.get('roleId');
|
|
let userGuild = await getUserGuildWithRefActive(roleId, ' guildCode');
|
|
if (!userGuild)
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
const { guildCode: code } = userGuild;
|
|
const reports = await WishPoolReportModel.getReportsByTime(code, 1);//获得今天的捐赠日报
|
|
return resResult(STATUS.SUCCESS, { reports });
|
|
}
|
|
|
|
}
|