pvp结算保存战报
This commit is contained in:
@@ -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');
|
||||
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 });
|
||||
|
||||
72
shared/db/PvpRecord.ts
Normal file
72
shared/db/PvpRecord.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import BaseModel from './BaseModel';
|
||||
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
|
||||
|
||||
|
||||
export class HeroesRecord {
|
||||
@prop({ required: true, default: 0 })
|
||||
hid: number; // 武将id
|
||||
@prop({ required: true, default: 0 })
|
||||
quality: number; // 品质
|
||||
@prop({ required: true, default: 0 })
|
||||
star: number; // 星级
|
||||
@prop({ required: true, default: 0 })
|
||||
colorStar: number; // 觉醒
|
||||
@prop({ required: true, default: 0 })
|
||||
lv: number; // 等级
|
||||
@prop({ required: true, default: 0 })
|
||||
damage: number; // 伤害
|
||||
@prop({ required: true, default: 0 })
|
||||
heal: number; // 治疗
|
||||
@prop({ required: true, default: 0 })
|
||||
underDamage: number; // 承伤
|
||||
}
|
||||
|
||||
export class PlayerInfo {
|
||||
@prop({ required: true })
|
||||
roleId: string; // 角色 id
|
||||
@prop({ required: true })
|
||||
roleName: string; // 角色 名
|
||||
@prop({ required: true, default: 0 })
|
||||
lv: number; // 等级
|
||||
@prop({ required: true, default: 0 })
|
||||
sHid: number; // 形象
|
||||
@prop({ required: true, default: 0 })
|
||||
headHid: number; // 头像
|
||||
@prop({ required: true, default: 0 })
|
||||
title: number; // 爵位
|
||||
@prop({ required: true, default: 0 })
|
||||
ce: number; // 最高五人战力
|
||||
@prop({ required: true, default: false })
|
||||
isSuccess: boolean; // 是否胜利
|
||||
@prop({ required: true, default: 0 })
|
||||
score: number; // 获得的总积分
|
||||
@prop({ required: true, type: HeroesRecord, default: [], _id: false })
|
||||
heroes: HeroesRecord[]; // 获得的总积分
|
||||
}
|
||||
|
||||
@index({ roleId1: 1, updatedAt: -1 })
|
||||
@index({ roleId2: 1, updatedAt: -1 })
|
||||
|
||||
export default class PvpRecord extends BaseModel {
|
||||
@prop({ required: true })
|
||||
roleId1: string; // 角色 id
|
||||
@prop({ required: true })
|
||||
roleId2: string; // 角色 id
|
||||
|
||||
@prop({ required: true, default: 0 })
|
||||
warId: number; // 这一关使用的地图
|
||||
@prop({ required: true, type: PlayerInfo, default: {}, _id: false })
|
||||
attackInfo: PlayerInfo; // 攻方信息
|
||||
@prop({ required: true, type: PlayerInfo, default: {}, _id: false })
|
||||
defenseInfo: PlayerInfo; // 守方信息
|
||||
|
||||
|
||||
public static async createRec(param: { roleId1: string, roleId2: string, warId: number, attackInfo: PlayerInfo, defenseInfo: PlayerInfo}, lean = true) {
|
||||
const result = await PvpRecordModel.findOneAndUpdate({}, param, { new: true, upsert: true }).lean();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
export const PvpRecordModel = getModelForClass(PvpRecord);
|
||||
|
||||
export interface PvpRecordType extends Pick<DocumentType<PvpRecord>, keyof PvpRecord>{};
|
||||
@@ -94,5 +94,5 @@ export interface pvpEndParamInter {
|
||||
hid: number;
|
||||
damage: number;
|
||||
heal: number;
|
||||
hurt: number;
|
||||
underDamage: number;
|
||||
}
|
||||
Reference in New Issue
Block a user