修改关卡记录逻辑
This commit is contained in:
1
game-server/app/resource
Symbolic link
1
game-server/app/resource
Symbolic link
@@ -0,0 +1 @@
|
|||||||
|
../../shared/resource
|
||||||
@@ -1,9 +1,10 @@
|
|||||||
import { Application, BackendSession } from 'pinus';
|
import { Application, BackendSession } from 'pinus';
|
||||||
import { BattleRecordModel } from '../../../../../shared/db/BattleRecord';
|
import { BattleRecordModel } from '../../../db/BattleRecord';
|
||||||
import { getWarById, getGoodById } from '../../../util/gamedata';
|
import { getWarById, getGoodById } from '../../../util/gamedata';
|
||||||
import { CounterModel } from '../../../../../shared/db/Counter';
|
import { CounterModel } from '../../../db/Counter';
|
||||||
import { HeroModel } from '../../../../../shared/db/Hero';
|
import { HeroModel } from '../../../db/Hero';
|
||||||
import { EquipModel } from '../../../../../shared/db/Equip';
|
import { EquipModel } from '../../../db/Equip';
|
||||||
|
import { genCode } from '../../../util/util';
|
||||||
|
|
||||||
export default function(app: Application) {
|
export default function(app: Application) {
|
||||||
return new NormalBattleHandler(app);
|
return new NormalBattleHandler(app);
|
||||||
@@ -13,9 +14,9 @@ export class NormalBattleHandler {
|
|||||||
constructor(private app: Application) {
|
constructor(private app: Application) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 进入关卡前,设置status=0
|
// 进入关卡前,记录信息,生成唯一标识
|
||||||
async checkBattle(msg: {battleId: number}, session: BackendSession) {
|
async checkBattle(msg: {battleId: number, heroes: Array<any> }, session: BackendSession) {
|
||||||
const { battleId } = msg;
|
const { battleId, heroes } = msg;
|
||||||
let roleId = session.get('roleId');
|
let roleId = session.get('roleId');
|
||||||
let roleName = session.get('roleName');
|
let roleName = session.get('roleName');
|
||||||
let warInfo = getWarById(battleId);
|
let warInfo = getWarById(battleId);
|
||||||
@@ -26,40 +27,54 @@ export class NormalBattleHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const BattleRecord = await BattleRecordModel.updateBattleRecordByRole(roleId, battleId, {
|
const battleCode = genCode(8);
|
||||||
|
const BattleRecord = await BattleRecordModel.updateBattleRecordByCode(battleCode, {
|
||||||
$set: {
|
$set: {
|
||||||
|
roleId,
|
||||||
roleName,
|
roleName,
|
||||||
|
battleId,
|
||||||
status: 0,
|
status: 0,
|
||||||
warName: warInfo.gk_name,
|
warName: warInfo.gk_name,
|
||||||
warType: warInfo.war_type
|
warType: warInfo.war_type,
|
||||||
},
|
record: { heroes }
|
||||||
$inc: {
|
|
||||||
count: 1
|
|
||||||
}
|
}
|
||||||
});
|
}, true);
|
||||||
|
|
||||||
|
let {status} = BattleRecord;
|
||||||
|
|
||||||
let { count, status} = BattleRecord;
|
|
||||||
return {
|
return {
|
||||||
code: 200,
|
code: 200,
|
||||||
data: {
|
data: {
|
||||||
battleId, count, status
|
battleId, battleCode, status
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 关卡结算,记录使用的武将,获得奖励
|
// 关卡结算,记录使用的武将,获得奖励
|
||||||
async battleEnd(msg: {battleId: number, isSuccess: boolean, heroes: Array<any>, }, session: BackendSession) {
|
async battleEnd(msg: {battleCode: string, battleId: number, isSuccess: boolean, heroes: Array<any>, }, session: BackendSession) {
|
||||||
|
|
||||||
const { battleId, isSuccess, heroes } = msg;
|
const { battleCode, battleId, isSuccess, heroes } = msg;
|
||||||
let roleId = session.get('roleId');
|
let roleId = session.get('roleId');
|
||||||
let roleName = session.get('roleName');
|
let roleName = session.get('roleName');
|
||||||
let warInfo = getWarById(battleId);
|
let warInfo = getWarById(battleId);
|
||||||
|
|
||||||
const BattleRecord = await BattleRecordModel.getBattleRecordByRole(roleId, battleId);
|
const BattleRecord = await BattleRecordModel.getBattleRecordByCode(battleCode, true);
|
||||||
if(!BattleRecord || BattleRecord.status != 0) {
|
if(!BattleRecord || BattleRecord.status != 0) {
|
||||||
return {
|
return {
|
||||||
code: 202,
|
code: 202,
|
||||||
data: '关卡状态错误,未开启挑战'
|
data: '关卡状态错误'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let flag = 1; // 对比hero信息
|
||||||
|
let { record: { heroes: dbHeroes } } = BattleRecord;
|
||||||
|
for(let hid of heroes) {
|
||||||
|
if(dbHeroes.indexOf(hid) == -1) flag = 0;
|
||||||
|
}
|
||||||
|
if(!flag) {
|
||||||
|
return {
|
||||||
|
code: 202,
|
||||||
|
data: '关卡信息不同'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,9 +84,6 @@ export class NormalBattleHandler {
|
|||||||
$set: {
|
$set: {
|
||||||
status: 1,
|
status: 1,
|
||||||
record: { heroes }
|
record: { heroes }
|
||||||
},
|
|
||||||
$inc: {
|
|
||||||
successCount: 1
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
reward = await this.handleReward(roleId, roleName, warInfo.reward);
|
reward = await this.handleReward(roleId, roleName, warInfo.reward);
|
||||||
@@ -80,21 +92,18 @@ export class NormalBattleHandler {
|
|||||||
$set: {
|
$set: {
|
||||||
status: 2,
|
status: 2,
|
||||||
record: { heroes }
|
record: { heroes }
|
||||||
},
|
|
||||||
$inc: {
|
|
||||||
failCount: 1
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
reward = [];
|
reward = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = await BattleRecordModel.updateBattleRecordByRole(roleId, battleId, params);
|
const updateResult = await BattleRecordModel.updateBattleRecordByCode(battleCode, params, true);
|
||||||
let {count, status} = result;
|
let { status } = updateResult;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
code: 200,
|
code: 200,
|
||||||
data: {
|
data: {
|
||||||
battleId, count, status,
|
battleCode, battleId, status,
|
||||||
goods: reward
|
goods: reward
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ const path = require('path');
|
|||||||
var gamedata = {};
|
var gamedata = {};
|
||||||
|
|
||||||
function initData () {
|
function initData () {
|
||||||
fs.readdirSync(__dirname + '/../../config/resource')
|
fs.readdirSync(__dirname + '/../resource')
|
||||||
.filter(function(file) {
|
.filter(function(file) {
|
||||||
return (file.indexOf(".") !== 0) && (file !== "index.js");
|
return (file.indexOf(".") !== 0) && (file !== "index.js");
|
||||||
})
|
})
|
||||||
@@ -13,7 +13,7 @@ function initData () {
|
|||||||
var name = file.split('.')[0];
|
var name = file.split('.')[0];
|
||||||
try {
|
try {
|
||||||
gamedata[name] = JSON.parse(
|
gamedata[name] = JSON.parse(
|
||||||
fs.readFileSync(path.resolve(__dirname, "../../config/resource/" + file))
|
fs.readFileSync(path.resolve(__dirname, "../resource/" + file))
|
||||||
);
|
);
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
console.error('【文件缺少】:' + file);
|
console.error('【文件缺少】:' + file);
|
||||||
|
|||||||
10
game-server/app/util/util.ts
Normal file
10
game-server/app/util/util.ts
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
|
||||||
|
export function genCode(len) {
|
||||||
|
const chars = '123456789ABCDEFGHJKLMNPQRSTWXYZabcdefghijklmnopqrstuvwxyz';
|
||||||
|
const charArr = chars.split('');
|
||||||
|
let code = '';
|
||||||
|
for (let i = 0; i < len; i++) {
|
||||||
|
code += charArr[Math.floor(Math.random() * charArr.length)];
|
||||||
|
}
|
||||||
|
return code;
|
||||||
|
}
|
||||||
@@ -7,6 +7,7 @@
|
|||||||
],
|
],
|
||||||
"module": "commonjs", //指定生成哪个模块系统代码
|
"module": "commonjs", //指定生成哪个模块系统代码
|
||||||
"target": "es2017",
|
"target": "es2017",
|
||||||
|
"resolveJsonModule": true,
|
||||||
"lib": [
|
"lib": [
|
||||||
"es2015",
|
"es2015",
|
||||||
"es2016",
|
"es2016",
|
||||||
@@ -24,6 +25,7 @@
|
|||||||
"watch":false //在监视模式下运行编译器。会监视输出文件,在它们改变时重新编译。
|
"watch":false //在监视模式下运行编译器。会监视输出文件,在它们改变时重新编译。
|
||||||
},
|
},
|
||||||
"include":[
|
"include":[
|
||||||
|
"./app/**/*.json",
|
||||||
"./app/**/*.ts",
|
"./app/**/*.ts",
|
||||||
"./config/**/*.ts",
|
"./config/**/*.ts",
|
||||||
"./app.ts",
|
"./app.ts",
|
||||||
|
|||||||
@@ -15,28 +15,28 @@ export default class BattleRecord extends BaseModel {
|
|||||||
@prop({ required: true })
|
@prop({ required: true })
|
||||||
roleName: string; // 角色名称
|
roleName: string; // 角色名称
|
||||||
|
|
||||||
|
@prop({ required: true })
|
||||||
|
battleCode: string; // 关卡记录唯一标识
|
||||||
@prop({ required: true })
|
@prop({ required: true })
|
||||||
battleId: number; // 关卡 id
|
battleId: number; // 关卡 id
|
||||||
@prop({ required: true })
|
@prop({ required: true })
|
||||||
status: number; // 关卡状态 0-挑战中 1-挑战成功 2-挑战失败
|
status: number; // 关卡状态 0-挑战中 1-挑战成功 2-挑战失败
|
||||||
@prop({ required: true })
|
@prop({ required: true })
|
||||||
count: number; // 挑战次数
|
record: { // 使用的武将等记录
|
||||||
@prop({ required: true })
|
heroes: Array <number>; // 武将id
|
||||||
successCount: number; // 成功次数
|
};
|
||||||
@prop({ required: true })
|
|
||||||
failCount: number; // 失败次数
|
|
||||||
@prop({ required: true })
|
|
||||||
record: object; // 失败次数
|
|
||||||
|
|
||||||
public static async updateBattleRecordByRole( roleId: string, battleId: number, params: object, lean = true) {
|
|
||||||
const result = await BattleRecordModel.findOneAndUpdate({roleId, battleId}, params, {new: true, upsert: true}).lean(lean);
|
public static async getBattleRecordByCode(battleCode: string, lean = true) {
|
||||||
|
const result = await BattleRecordModel.findOne({ battleCode }).lean(lean);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async getBattleRecordByRole(roleId: string, battleId: number, lean = true) {
|
public static async updateBattleRecordByCode( battleCode: string, params: object, lean = true) {
|
||||||
const result = await BattleRecordModel.findOne({roleId, battleId}).lean(lean);
|
const result = await BattleRecordModel.findOneAndUpdate({ battleCode }, params, {new: true, upsert: true}).lean(lean);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const BattleRecordModel = getModelForClass(BattleRecord);
|
export const BattleRecordModel = getModelForClass(BattleRecord);
|
||||||
|
|||||||
Reference in New Issue
Block a user