pvp随机地图
This commit is contained in:
+51
-51
@@ -1,7 +1,7 @@
|
||||
import BaseModel from './BaseModel';
|
||||
import { index, getModelForClass, prop, DocumentType, modelOptions } from '@typegoose/typegoose';
|
||||
const _ = require('underscore');
|
||||
|
||||
const Transaction = require('mongoose-transactions');
|
||||
@index({ roleId: 1, id: 1 })
|
||||
@index({ seqId: 1 })
|
||||
@modelOptions({ schemaOptions: { id: false } })
|
||||
@@ -50,59 +50,59 @@ export default class Item extends BaseModel {
|
||||
return items;
|
||||
}
|
||||
|
||||
public static async decreaseItems(roleId: string, items: Array<{ id: number, count: number, ratio?: number }>, lean = true) {
|
||||
let updateItems = new Array<{ id: number, count: number, ratio?: number }>(), hasError: boolean = false, result = new Array();
|
||||
for (let { id, count, ratio } of items) {
|
||||
let rec: ItemType;
|
||||
if (!ratio) {
|
||||
rec = await ItemModel.findOneAndUpdate({ roleId, id, count: { $gte: count } }, { $inc: { count: -1 * count } }, { new: true }).lean(lean);
|
||||
} else {
|
||||
rec = await ItemModel.findOneAndUpdate({ roleId, id }, { $inc: { count: ratio * count } }, { new: true, upsert: true }).lean(lean);
|
||||
}
|
||||
if (!!rec) {
|
||||
let index = _.findIndex(result,{id})
|
||||
if (!!result[index]) {
|
||||
result[index] = { id: rec.id, count: rec.count }
|
||||
} else {
|
||||
result.push({ id: rec.id, count: rec.count });
|
||||
}
|
||||
updateItems.push({ id, count, ratio });
|
||||
} else {
|
||||
hasError = true; break;
|
||||
}
|
||||
}
|
||||
if (hasError) { // 数量不足
|
||||
for (let { id, count, ratio } of updateItems) {
|
||||
if (!ratio) ratio = -1;
|
||||
await ItemModel.findOneAndUpdate({ roleId, id }, { $inc: { count: -ratio * count } }, { new: true }).lean(lean);
|
||||
}
|
||||
return { hasError: true }
|
||||
} else {
|
||||
return { hasError: false, result }
|
||||
}
|
||||
}
|
||||
// public static async decreaseItems(roleId: string, items: Array<{ id: number, count: number, ratio?: number}>, lean = true) {
|
||||
// const transaction = new Transaction();
|
||||
// let hasError: boolean = false, result = new Array();
|
||||
// try {
|
||||
// for (let { id, count, ratio } of items) {
|
||||
// if (ratio) ratio = -1;
|
||||
// await transaction.update({ roleId, id, count: { $gte: count } }, { $inc: { count: ratio * count } }, { new: true});
|
||||
// //const rec: ItemType = await transaction.findOneAndUpdate({ roleId, id, count: { $gte: count } }, { $inc: { count: ratio * count } }, { new: true}).lean(lean);
|
||||
// // if (!!rec) {
|
||||
// // result.push({ id: rec.id, count: rec.count });
|
||||
// // } else {
|
||||
// // hasError = true; break;
|
||||
// // }
|
||||
// public static async decreaseItems(roleId: string, items: Array<{ id: number, count: number, ratio?: number }>, lean = true) {
|
||||
// let updateItems = new Array<{ id: number, count: number, ratio?: number }>(), hasError: boolean = false, result = new Array();
|
||||
// for (let { id, count, ratio } of items) {
|
||||
// let rec: ItemType;
|
||||
// if (!ratio) {
|
||||
// rec = await ItemModel.findOneAndUpdate({ roleId, id, count: { $gte: count } }, { $inc: { count: -1 * count } }, { new: true }).lean(lean);
|
||||
// } else {
|
||||
// rec = await ItemModel.findOneAndUpdate({ roleId, id }, { $inc: { count: ratio * count } }, { new: true, upsert: true }).lean(lean);
|
||||
// }
|
||||
// await transaction.run();
|
||||
// return {hasError, result};
|
||||
// } catch (error) {
|
||||
// await transaction.rollback().catch(console.error);
|
||||
// transaction.clean();
|
||||
// return {error};
|
||||
// if (!!rec) {
|
||||
// let index = _.findIndex(result,{id})
|
||||
// if (!!result[index]) {
|
||||
// result[index] = { id: rec.id, count: rec.count }
|
||||
// } else {
|
||||
// result.push({ id: rec.id, count: rec.count });
|
||||
// }
|
||||
// updateItems.push({ id, count, ratio });
|
||||
// } else {
|
||||
// hasError = true; break;
|
||||
// }
|
||||
// }
|
||||
// if (hasError) { // 数量不足
|
||||
// for (let { id, count, ratio } of updateItems) {
|
||||
// if (!ratio) ratio = -1;
|
||||
// await ItemModel.findOneAndUpdate({ roleId, id }, { $inc: { count: -ratio * count } }, { new: true }).lean(lean);
|
||||
// }
|
||||
// return { hasError: true }
|
||||
// } else {
|
||||
// return { hasError: false, result }
|
||||
// }
|
||||
// }
|
||||
public static async decreaseItems(roleId: string, items: Array<{ id: number, count: number, ratio?: number}>, lean = true) {
|
||||
const transaction = new Transaction();
|
||||
let hasError: boolean = false, result = new Array();
|
||||
try {
|
||||
for (let { id, count, ratio } of items) {
|
||||
if (ratio) ratio = -1;
|
||||
await transaction.update({ roleId, id, count: { $gte: count } }, { $inc: { count: ratio * count } }, { new: true});
|
||||
const rec: ItemType = await transaction.findOneAndUpdate({ roleId, id, count: { $gte: count } }, { $inc: { count: ratio * count } }, { new: true}).lean(lean);
|
||||
if (!!rec) {
|
||||
result.push({ id: rec.id, count: rec.count });
|
||||
} else {
|
||||
hasError = true; break;
|
||||
}
|
||||
}
|
||||
await transaction.run();
|
||||
return {hasError, result};
|
||||
} catch (error) {
|
||||
await transaction.rollback().catch(console.error);
|
||||
transaction.clean();
|
||||
return {error};
|
||||
}
|
||||
}
|
||||
|
||||
public static async deleteAccount(roleId: string) {
|
||||
let result = await ItemModel.deleteMany({ roleId });
|
||||
|
||||
+15
-3
@@ -7,7 +7,9 @@ import { PVP_PLAYER_POS, PVP_HERO_POS } from '../consts';
|
||||
|
||||
interface pvpUpdateInter {
|
||||
_id?: string;
|
||||
oppPlayers: OppPlayers[]
|
||||
oppPlayers?: OppPlayers[];
|
||||
score?: number;
|
||||
pLv?:number;
|
||||
}
|
||||
|
||||
export class Heroes {
|
||||
@@ -123,7 +125,8 @@ export default class PvpDefense extends BaseModel {
|
||||
challengeRefTime: number; // 上一次刷新的时间
|
||||
@prop({ required: true, default: [] })
|
||||
receivedBox: Array<number>;
|
||||
|
||||
@prop({ required: true, default: 0 })
|
||||
seasonNum: number;
|
||||
public static async findByRoleId(roleId: string, lean = true) {
|
||||
const result: PvpDefenseType = await PvpDefenseModel.findOne({ roleId }).lean(lean);
|
||||
return result;
|
||||
@@ -170,7 +173,7 @@ export default class PvpDefense extends BaseModel {
|
||||
return result;
|
||||
}
|
||||
|
||||
public static async findByRoleIdIncludeAll(roleId: string, lean = true) {
|
||||
public static async findByRoleIdIncludeAll(roleId: string) {
|
||||
const result: PvpDefenseType = await PvpDefenseModel.findOne({ roleId })
|
||||
.populate('role', 'headHid sHid topFiveCe roleId roleName')
|
||||
.populate('heroes.hero')
|
||||
@@ -201,6 +204,15 @@ export default class PvpDefense extends BaseModel {
|
||||
}).lean();
|
||||
return result;
|
||||
}
|
||||
public static async updateInfo(roleId: string, update: pvpUpdateInter, lean = true) {
|
||||
let result: PvpDefenseType = await PvpDefenseModel.findOneAndUpdate({roleId}, {$set:update}, {new: true}).lean(lean);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static async getPvpDef(limitNum: number, lean = true) {
|
||||
let result: Array<PvpDefenseType> = await PvpDefenseModel.find().sort({ score: -1 }).limit(limitNum).lean(lean);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
export const PvpDefenseModel = getModelForClass(PvpDefense);
|
||||
|
||||
@@ -15,6 +15,21 @@ export default class SystemConfig extends BaseModel {
|
||||
const result: SystemConfigType = await SystemConfigModel.findOne({ id: 1 }).lean(lean);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static async createSystemConfig(seasonEndTime: number, warId: number, lean = true) {
|
||||
const result: SystemConfigType = await SystemConfigModel.findOneAndUpdate({ id: 1}, { seasonEndTime, warId }, { upsert: true, new: true }).lean(lean);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static async updateSystemConfig(update: { seasonEndTime?: number, warId?: number, seasonNum?:number }, lean = true) {
|
||||
const result: SystemConfigType = await SystemConfigModel.findOneAndUpdate({ id: 1}, update, { upsert: true, new: true }).lean(lean);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static async updateSeason( seasonEndTime: number, lean = true) {
|
||||
const result: SystemConfigType = await SystemConfigModel.findOneAndUpdate({ id: 1}, { $seasonEndTime: seasonEndTime, $inc: { seasonNum:1 } }, { upsert: true, new: true }).lean(lean);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
export const SystemConfigModel = getModelForClass(SystemConfig);
|
||||
|
||||
Reference in New Issue
Block a user