🐞 fix(gvg): 批量保存数据后返回有时无法查询
This commit is contained in:
@@ -123,33 +123,32 @@ export default class GVGLeagueFarm extends BaseModel {
|
||||
}
|
||||
|
||||
public static async plant(configId: number, leagueCode: string, farmId: number, fields: { fieldId: number, seedType: number, time: number, output: number, outputStr: string, index: number }[], roleId: string) {
|
||||
const batchCode = genCode(10);
|
||||
const result = await GVGLeagueFarmModel.bulkWrite(fields.map(({ fieldId, seedType, time, output, outputStr, index }) => {
|
||||
return { updateOne: { filter: { configId, leagueCode, farmId, fieldId, lockRoleId: roleId }, update: { $set: { seedType, unlockTime: getFutureTime(), harvestTime: nowSeconds() + time, batchCode, output, outputStr, index }} } }
|
||||
}));
|
||||
if(result.modifiedCount == 0) return [];
|
||||
const fieldResult: GVGLeagueFarmType[] = await GVGLeagueFarmModel.find({ batchCode }).sort({ fieldId: 1 }).select('-_id -createdAt -updatedAt -__v').lean();
|
||||
const fieldResult: GVGLeagueFarmType[] = [];
|
||||
for(let { fieldId, seedType, time, output, outputStr, index } of fields) {
|
||||
let obj: GVGLeagueFarmType = await GVGLeagueFarmModel.findOneAndUpdate({ configId, leagueCode, farmId, fieldId, lockRoleId: roleId }, {$set: { seedType, unlockTime: getFutureTime(), harvestTime: nowSeconds() + time, output, outputStr, index }}, { new: true }).lean();
|
||||
if(obj) fieldResult.push(obj);
|
||||
}
|
||||
return fieldResult;
|
||||
}
|
||||
|
||||
public static async harvest(configId: number, leagueCode: string, farmId: number, fieldIds: number[], roleId: string) {
|
||||
const batchCode = genCode(10);
|
||||
const result = await GVGLeagueFarmModel.bulkWrite(fieldIds.map(fieldId => {
|
||||
return { updateOne: { filter: { configId, leagueCode, farmId, fieldId, lockRoleId: roleId, harvestTime: { $lt: nowSeconds() } }, update: { $set: { unlockTime: 0, harvestTime: 0, lockRoleId: '', lockRoleName: '', batchCode }} } }
|
||||
}));
|
||||
if(result.modifiedCount == 0) return [];
|
||||
const fieldResult: GVGLeagueFarmType[] = await GVGLeagueFarmModel.find({ batchCode }).sort({ fieldId: 1 }).select('-_id -createdAt -updatedAt -__v -configId -leagueCode').lean();
|
||||
await GVGLeagueFarmModel.updateMany({ batchCode }, { $set: { seedType: 0 } });
|
||||
const fieldResult: GVGLeagueFarmType[] = [], _ids: string[] = [];
|
||||
for(let fieldId of fieldIds) {
|
||||
let obj: GVGLeagueFarmType = await GVGLeagueFarmModel.findOneAndUpdate(
|
||||
{ configId, leagueCode, farmId, fieldId, lockRoleId: roleId, harvestTime: { $lt: nowSeconds() } },
|
||||
{ $set: { unlockTime: 0, harvestTime: 0, lockRoleId: '', lockRoleName: '', index: 0 } }, {new: true}).lean();
|
||||
if(obj) { fieldResult.push(obj); _ids.push(obj._id);}
|
||||
}
|
||||
await GVGLeagueFarmModel.updateMany({ _id: { $in: _ids } }, { $set: { seedType: 0 } });
|
||||
return fieldResult;
|
||||
}
|
||||
|
||||
public static async helpHarvest(configId: number, leagueCode: string, farmId: number, roleId: string) {
|
||||
const batchCode = genCode(10);
|
||||
const result = await GVGLeagueFarmModel.updateMany({ configId, leagueCode, farmId, lockRoleId: roleId, harvestTime: { $lt: nowSeconds() } },
|
||||
{ $set: { unlockTime: 0, harvestTime: 0, lockRoleId: '', lockRoleName: '', batchCode }}, { new: true });
|
||||
const fieldResult: GVGLeagueFarmType[] = await GVGLeagueFarmModel.find({ configId, leagueCode, farmId, lockRoleId: roleId, harvestTime: { $lt: nowSeconds() } }).lean();
|
||||
const _ids = fieldResult.map(cur => cur._id);
|
||||
const result = await GVGLeagueFarmModel.updateMany({ _id: { $in: _ids } },
|
||||
{ $set: { unlockTime: 0, harvestTime: 0, lockRoleId: '', lockRoleName: '', seedType: 0, index: 0 }}, { new: true });
|
||||
if(result.modifiedCount == 0) return [];
|
||||
const fieldResult: GVGLeagueFarmType[] = await GVGLeagueFarmModel.find({ batchCode }).lean();
|
||||
await GVGLeagueFarmModel.updateMany({ batchCode }, { $set: { seedType: 0 } });
|
||||
return fieldResult
|
||||
}
|
||||
|
||||
|
||||
@@ -122,9 +122,6 @@ export default class GVGTeam extends BaseModel {
|
||||
@prop({ required: true, default: false })
|
||||
isBroken: boolean; // 机器人是否被击破,如果没有被击破,那么这个位置上就还是机器人,如果被击破了,这个位置就空出给玩家
|
||||
|
||||
@prop({ required: false })
|
||||
batchCode: string; // 批量编号
|
||||
|
||||
originPointId: number;
|
||||
curTeamBreak: boolean;
|
||||
|
||||
@@ -319,15 +316,14 @@ export default class GVGTeam extends BaseModel {
|
||||
|
||||
// 投石车伤害
|
||||
public static async attackByCatapult(teamCodes: string[], inc: number, rebirthAreaId: number) {
|
||||
let batchCode = genCode(8);
|
||||
await GVGTeamModel.updateMany({ teamCode: { $in: teamCodes } }, { $inc: { durability: -inc }, $set: { batchCode } });
|
||||
let brokenTeams: GVGTeamType[] = await GVGTeamModel.find({ batchCode, durability: { $lte: 0 } }).lean();
|
||||
await GVGTeamModel.updateMany({ teamCode: { $in: teamCodes } }, { $inc: { durability: -inc } });
|
||||
let brokenTeams: GVGTeamType[] = await GVGTeamModel.find({ teamCode: { $in: teamCodes }, durability: { $lte: 0 } }).lean();
|
||||
if(brokenTeams.length > 0) {
|
||||
await GVGTeamModel.bulkWrite(brokenTeams.map(({ teamCode, maxDurability }) => {
|
||||
return { updateOne: { filter: { teamCode }, update: { $set: { areaId: rebirthAreaId, pointId: 0, durability: maxDurability } }}}
|
||||
}));
|
||||
}
|
||||
let teams: GVGTeamType[] = await GVGTeamModel.find({ batchCode }).lean();
|
||||
let teams: GVGTeamType[] = await GVGTeamModel.find({ teamCode: { $in: teamCodes } }).lean();
|
||||
return teams.map(team => {
|
||||
let originTeam = brokenTeams.find(origin => origin.teamCode == team.teamCode);
|
||||
if(originTeam) {
|
||||
|
||||
@@ -2,10 +2,8 @@ import BaseModel from './BaseModel';
|
||||
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
|
||||
import { nowSeconds } from '../pubUtils/timeUtil';
|
||||
import { RewardInter } from '../pubUtils/interface';
|
||||
import { genCode } from '../pubUtils/util';
|
||||
|
||||
@index({ leagueCode: 1, roleId: 1, configId: 1, status: 1 })
|
||||
@index({ batchCode: 1 })
|
||||
export default class GVGUserItem extends BaseModel {
|
||||
|
||||
@prop({ required: true })
|
||||
@@ -24,10 +22,7 @@ export default class GVGUserItem extends BaseModel {
|
||||
count: number; // 数量
|
||||
|
||||
@prop({ required: true, default: 0 })
|
||||
expireTime: number;
|
||||
|
||||
@prop({ required: true, default: 0 })
|
||||
batchCode: string;
|
||||
expireTime: number;
|
||||
|
||||
public static async findByRole(configId: number, leagueCode: string, roleId: string) {
|
||||
const result: GVGUserItemType[] = await GVGUserItemModel.find({ configId, leagueCode, roleId, expireTime: { $gte: nowSeconds() } }, { _id: 0, id: 1, count: 1 }).lean();
|
||||
@@ -45,12 +40,11 @@ export default class GVGUserItem extends BaseModel {
|
||||
}
|
||||
|
||||
public static async decreaseItem(configId: number, leagueCode: string, roleId: string, items: RewardInter[]) {
|
||||
let batchCode = genCode(10);
|
||||
await GVGUserItemModel.bulkWrite(items.map(({ id, count }) => {
|
||||
let filter = { configId, leagueCode, roleId, id, count: { $gte: count }, expireTime: { $gte: nowSeconds() } }
|
||||
return { updateOne: { filter, update: { $inc: { count: -1 * count }, $set: { batchCode } } } }
|
||||
}));
|
||||
const result: GVGUserItemType[] = await GVGUserItemModel.find({ batchCode }).lean();
|
||||
const result: GVGUserItemType[] = [];
|
||||
for(let { id, count } of items) {
|
||||
let item: GVGUserItemType = await GVGUserItemModel.findOneAndUpdate({ configId, leagueCode, roleId, id, count: { $gte: count }, expireTime: { $gte: nowSeconds() } }, { $inc: { count: -1 * count } }, { new: true }).lean();
|
||||
if(item) result.push(item);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user