活动:寻宝骑兵-每日物资

This commit is contained in:
qiaoxin
2021-05-25 22:05:28 +08:00
parent ebeb22f630
commit f8f9d9d120
4 changed files with 98 additions and 17 deletions

View File

@@ -1,7 +1,11 @@
import { Application, BackendSession } from 'pinus';
import { resResult } from '../../../pubUtils/util';
import { STATUS, ACTIVITY_RESOURCES_TYPE, ACTIVITY_TYPE } from '../../../consts';
import { getPlayerTreasureHuntData, getTreasureHuntData } from '../../../services/treasureHuntService';
import { getPlayerTreasureHuntData, getTreasureHuntData, getPlayerTreasureHuntShopData } from '../../../services/treasureHuntService';
import { ActivityTreasureHuntShopModel } from '../../../db/ActivityTreasureHuntShop';
import { handleCost } from '../../../services/rewardService';
import { addReward, stringToConsumeParam, stringToRewardParam } from '../../../services/giftPackageService';
import { RewardParam } from '../../../domain/activityField/rewardField';
export default function (app: Application) {
@@ -38,10 +42,14 @@ export class TreasureHuntHandler {
* @param {BackendSession} session
* @memberof TreasureHuntHandler
*/
async buyGoods(msg: { activityId: number }, session: BackendSession) {
const { activityId } = msg;
async buyGoods(msg: { activityId: number, cellIndex: number }, session: BackendSession) {
const { activityId, cellIndex } = msg;
const roleId = session.get('roleId');
const serverId = session.get('serverId');
const sid = session.get('sid');
const roleName = session.get('roleName');
const funcs: number[] = session.get('funcs');
let { huntActivityId, huntBeginTime, huntEndTime, huntRoundIndex, activityData } = await getTreasureHuntData(serverId);
if (!activityData) {
return resResult(STATUS.ACTIVITY_MISSING, {});
@@ -50,9 +58,34 @@ export class TreasureHuntHandler {
return resResult(STATUS.ACTIVITY_MISSING, {});
}
let playerData = await getPlayerTreasureHuntData(activityId, serverId, roleId, huntRoundIndex, huntBeginTime, huntEndTime,);
let playerData = await getPlayerTreasureHuntShopData(activityId, serverId, roleId, huntRoundIndex, huntBeginTime, huntEndTime,);
if (!playerData) return resResult(STATUS.ACTIVITY_MISSING);
let item = playerData.shop.getItem(cellIndex);
if (!item) {
return resResult(STATUS.ACTIVITY_MISSING, {});
}
if (item.price >= 0) {
return resResult(STATUS.ACTIVITY_NEED_PAY, {});
}
if (item.buyCount >= item.countMax) {
return resResult(STATUS.ACTIVITY_MAX_COUNT, {});
}
return resResult(STATUS.SUCCESS, playerData);
let consumeStr = item.getConsume();
let consume = stringToConsumeParam(consumeStr)
let resourceResult = await handleCost(roleId, sid, consume);
if (!resourceResult) return resResult(STATUS.ROLE_MATERIAL_NOT_ENOUGH);
//添加购买记录
await ActivityTreasureHuntShopModel.buyShopRecord(activityId, roleId, huntRoundIndex, playerData.todayIndex, cellIndex);
let rewardParamArr: Array<RewardParam> = stringToRewardParam(item.reward);
let result = await addReward(roleId, roleName, sid, serverId, funcs, rewardParamArr)
item.buyCount += 1;
return resResult(STATUS.SUCCESS, Object.assign(result, {
param: { activityId, cellIndex },
item: item,
}));
}
}

View File

@@ -36,15 +36,29 @@ export async function treasureHuntActivity(serverId: number, roleId: string) {
*/
export async function getPlayerTreasureHuntData(activityId: number, serverId: number, roleId: string, huntRoundIndex: number, huntBeginTime: Date, huntEndTime: Date) {
let activityData: ActivityModelType = await ActivityModel.findActivity(serverId, activityId, true);
let dayIndex = 0;
let playerRecord: ActivityTreasureHuntShopModelType = await ActivityTreasureHuntShopModel.findTreasureData(serverId, activityId, roleId, huntRoundIndex, dayIndex);
let playerData = new TreasureHuntData(activityData);
playerData.beginTime = moment(huntBeginTime).valueOf();
playerData.endTime = moment(huntEndTime).valueOf();
playerData.todayIndex = deltaDays(huntBeginTime, new Date) + 1;;
playerData.roundIndex = huntRoundIndex;
// playerData.setPlayerRecords(playerRecord);
let playerShopRecord: ActivityTreasureHuntShopModelType = await ActivityTreasureHuntShopModel.findTreasureData(serverId, activityId, roleId, huntRoundIndex, playerData.todayIndex);
playerData.shop.setPlayerRecords(playerShopRecord);
return playerData;
}
export async function getPlayerTreasureHuntShopData(activityId: number, serverId: number, roleId: string, huntRoundIndex: number, huntBeginTime: Date, huntEndTime: Date) {
let activityData: ActivityModelType = await ActivityModel.findActivity(serverId, activityId, true);
let playerData = new TreasureHuntData(activityData);
playerData.beginTime = moment(huntBeginTime).valueOf();
playerData.endTime = moment(huntEndTime).valueOf();
playerData.todayIndex = deltaDays(huntBeginTime, new Date) + 1;;
playerData.roundIndex = huntRoundIndex;
let playerShopRecord: ActivityTreasureHuntShopModelType = await ActivityTreasureHuntShopModel.findTreasureData(serverId, activityId, roleId, huntRoundIndex, playerData.todayIndex);
playerData.shop.setPlayerRecords(playerShopRecord);
return playerData;
}
@@ -56,12 +70,18 @@ export async function getTreasureHuntData(serverId: number) {
if (!tempData) {//开始新周期
let { huntActivityId, huntBeginTime, huntEndTime, huntRoundIndex } = await getNewActivityData(serverId);
if (huntActivityId === 0) {
return { huntActivityId, huntBeginTime, huntEndTime, huntRoundIndex, activityData }
}
tempData = await ServerTempModel.updateTreasureHuntData(serverId, huntActivityId, huntBeginTime, huntEndTime, huntRoundIndex);
activityData = await ActivityModel.findActivity(serverId, huntActivityId, true);
return { huntActivityId, huntBeginTime, huntEndTime, huntRoundIndex, activityData }
}
if (now > tempData.huntEndTime) {//活动过期
let { huntActivityId, huntBeginTime, huntEndTime, huntRoundIndex } = await getNextActivityData(serverId, tempData.huntActivityId, tempData.huntEndTime, tempData.huntRoundIndex);
if (huntActivityId === 0) {
return { huntActivityId, huntBeginTime, huntEndTime, huntRoundIndex, activityData }
}
tempData = await ServerTempModel.updateTreasureHuntData(serverId, huntActivityId, huntBeginTime, huntEndTime, huntRoundIndex);
activityData = await ActivityModel.findActivity(serverId, huntActivityId, true);
return { huntActivityId, huntBeginTime, huntEndTime, huntRoundIndex, activityData }

View File

@@ -10,14 +10,21 @@ import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoos
@index({ roleId: 1 })
export default class Activity_Treasure_Hunt_Shop extends ActivityShop {
@prop({ required: true })
dayIndex: number; // 第几天
@prop({ required: true })
dayIndex: number; // 第几天
//根据活动id查询活动数据
public static async findTreasureData(serverId: number, activityId: number, roleId: string, roundIndex: number, dayIndex: number) {
let result: ActivityTreasureHuntShopModelType = await ActivityTreasureHuntShopModel.findOne({ roleId, activityId, roundIndex, dayIndex }).lean(true);
return result;
}
//根据活动id查询活动数据
public static async findTreasureData(serverId: number, activityId: number, roleId: string, roundIndex: number, dayIndex: number) {
let result: ActivityTreasureHuntShopModelType = await ActivityTreasureHuntShopModel.findOne({ roleId, activityId, roundIndex, dayIndex }).lean(true);
return result;
}
//购买领取奖励的记录
public static async buyShopRecord(activityId: number, roleId: string, roundIndex: number, dayIndex: number, id: number) {
let result: ActivityTreasureHuntShopModelType = await ActivityTreasureHuntShopModel.findOneAndUpdate({ roleId, activityId, roundIndex, dayIndex },
{ $push: { records: { id, time: new Date() } } }, { upsert: true, new: true }).lean(true);
return result;
}
}
export const ActivityTreasureHuntShopModel = getModelForClass(Activity_Treasure_Hunt_Shop);

View File

@@ -1,5 +1,5 @@
import { ActivityModelType } from '../../db/Activity';
import { UserOrderModelType } from '../../db/UserOrder';
import { ActivityTreasureHuntShopModelType } from '../../db/ActivityTreasureHuntShop';
import { ActivityBase } from './activityField';
/************************************************************/
@@ -41,6 +41,11 @@ export class TreasureHuntShopItem {
}
this.buyCount = 0;
}
public getConsume() {
let index = this.consume.findIndex(obj => { return obj && obj.count === this.buyCount + 1 });
return (index != -1) ? this.consume[index].consume : '';
}
}
// 商店数据
@@ -48,11 +53,25 @@ export class TreasureHuntShopData {
name: string = '';//页签名字
list: Array<TreasureHuntShopItem> = [];//商品
public getItem(cellIndex: number): TreasureHuntShopItem {
let index = this.list.findIndex(obj => { return obj && obj.cellIndex === cellIndex })
return (index != -1) ? this.list[index] : null;
}
public setPlayerRecords(record: ActivityTreasureHuntShopModelType) {
if (!record) {
return;
}
for (let item of this.list) {
let buyRecords = record.records.filter(obj => { obj.id === item.cellIndex });
item.buyCount = buyRecords.length;
}
}
public initData(data: any) {
this.name = data.name;
let arr = data.data;
for (let obj of arr) {
console.log(JSON.stringify(obj))
this.list.push(new TreasureHuntShopItem(obj))
}
}
@@ -122,6 +141,8 @@ export class TreasureHuntData extends ActivityBase {
roundIndex = 0;//周期数
shop: TreasureHuntShopData = null;
public initData(data: any) {
let dataObj = JSON.parse(data);
this.name = dataObj.name;