战斗开始和战斗结算
This commit is contained in:
172
game-server/app/servers/battle/handler/normalBattleHandler.ts
Normal file
172
game-server/app/servers/battle/handler/normalBattleHandler.ts
Normal file
@@ -0,0 +1,172 @@
|
||||
import { Application, BackendSession } from 'pinus';
|
||||
import { BattleRecordModel } from '../../../../../shared/db/BattleRecord';
|
||||
import { getWarById, getGoodById } from '../../../util/gamedata';
|
||||
import { CounterModel } from '../../../../../shared/db/Counter';
|
||||
import { HeroModel } from '../../../../../shared/db/Hero';
|
||||
import { EquipModel } from '../../../../../shared/db/Equip';
|
||||
|
||||
export default function(app: Application) {
|
||||
return new NormalBattleHandler(app);
|
||||
}
|
||||
|
||||
export class NormalBattleHandler {
|
||||
constructor(private app: Application) {
|
||||
}
|
||||
|
||||
// 进入关卡前,设置status=0
|
||||
async checkBattle(msg: {battleId: number}, session: BackendSession) {
|
||||
const { battleId } = msg;
|
||||
let roleId = session.get('roleId');
|
||||
let roleName = session.get('roleName');
|
||||
let warInfo = getWarById(battleId);
|
||||
if(!warInfo) {
|
||||
return {
|
||||
code: 202,
|
||||
data: "缺少关卡信息"
|
||||
}
|
||||
}
|
||||
|
||||
const BattleRecord = await BattleRecordModel.updateBattleRecordByRole(roleId, battleId, {
|
||||
$set: {
|
||||
roleName,
|
||||
status: 0,
|
||||
warName: warInfo.gk_name,
|
||||
warType: warInfo.war_type
|
||||
},
|
||||
$inc: {
|
||||
count: 1
|
||||
}
|
||||
});
|
||||
|
||||
let { count, status} = BattleRecord;
|
||||
return {
|
||||
code: 200,
|
||||
data: {
|
||||
battleId, count, status
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 关卡结算,记录使用的武将,获得奖励
|
||||
async battleEnd(msg: {battleId: number, isSuccess: boolean, heroes: Array<any>, }, session: BackendSession) {
|
||||
|
||||
const { battleId, isSuccess, heroes } = msg;
|
||||
let roleId = session.get('roleId');
|
||||
let roleName = session.get('roleName');
|
||||
let warInfo = getWarById(battleId);
|
||||
|
||||
const BattleRecord = await BattleRecordModel.getBattleRecordByRole(roleId, battleId);
|
||||
if(!BattleRecord || BattleRecord.status != 0) {
|
||||
return {
|
||||
code: 202,
|
||||
data: '关卡状态错误,未开启挑战'
|
||||
}
|
||||
}
|
||||
|
||||
let params = {}, reward: Array<any>;
|
||||
if(isSuccess) { // 挑战胜利
|
||||
params = {
|
||||
$set: {
|
||||
status: 1,
|
||||
record: { heroes }
|
||||
},
|
||||
$inc: {
|
||||
successCount: 1
|
||||
}
|
||||
}
|
||||
reward = await this.handleReward(roleId, roleName, warInfo.reward);
|
||||
} else { // 挑战失败
|
||||
params = {
|
||||
$set: {
|
||||
status: 2,
|
||||
record: { heroes }
|
||||
},
|
||||
$inc: {
|
||||
failCount: 1
|
||||
}
|
||||
}
|
||||
reward = [];
|
||||
}
|
||||
|
||||
const result = await BattleRecordModel.updateBattleRecordByRole(roleId, battleId, params);
|
||||
let {count, status} = result;
|
||||
|
||||
return {
|
||||
code: 200,
|
||||
data: {
|
||||
battleId, count, status,
|
||||
goods: reward
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async handleReward(roleId: string, roleName: string, rewardStr:string) {
|
||||
let {weapons, armors, items, souls} = this.decodeReward(rewardStr);
|
||||
let addWeapons = await this.rewardWeapons(roleId, roleName, weapons);
|
||||
// 暂时只处理装备
|
||||
// let addArmors = await this.rewardArmors(roleId, roleName, armors);
|
||||
// let addItems = await this.rewardItems(roleId, roleName, items);
|
||||
// let addSouls = await this.rewardSouls(roleId, roleName, souls);
|
||||
|
||||
let result = [].concat(addWeapons);
|
||||
return result;
|
||||
}
|
||||
|
||||
private async rewardWeapons (roleId: string, roleName:string, weapons: Array<{id:number,cnt:number, type: number}>) {
|
||||
|
||||
let weaponsData = [];
|
||||
for (let weapon of weapons) {
|
||||
let cnt = weapon.cnt;
|
||||
let g = getGoodById(weapon.id);
|
||||
while (cnt > 0) {
|
||||
const seqId = await CounterModel.getNewCounter('eid');
|
||||
const equipInfo = {
|
||||
roleId,
|
||||
roleName,
|
||||
eid: weapon.id,
|
||||
eName: g.name,
|
||||
seqId,
|
||||
type: weapon.type,
|
||||
lv: g.lv
|
||||
}
|
||||
const equip = await EquipModel.createEquip(equipInfo);
|
||||
cnt -= 1;
|
||||
weaponsData.push(equip);
|
||||
}
|
||||
}
|
||||
return weaponsData;
|
||||
}
|
||||
|
||||
private decodeReward(rewardStr: string, multiple=1) {
|
||||
let weapons = [];
|
||||
let armors = [];
|
||||
let items = [];
|
||||
let souls = [];
|
||||
rewardStr.split('|').forEach((rStr) => {
|
||||
// r[0]: type, r[1]: id, r[2]: count
|
||||
let r = rStr.split('&');
|
||||
let type = parseInt(r[0] || '')||0;
|
||||
let id = parseInt(r[1] || '') || 0;
|
||||
let cnt = (parseInt(r[2] || '') || 0) * multiple;
|
||||
if (id !== 0 && cnt !== 0) {
|
||||
switch (r[0]) {
|
||||
case '0':
|
||||
items.push({id, cnt, type});
|
||||
break;
|
||||
case '1':
|
||||
weapons.push({id, cnt, type});
|
||||
break;
|
||||
case '2':
|
||||
armors.push({id, cnt, type});
|
||||
break;
|
||||
case '3':
|
||||
souls.push({id, cnt, type});
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
return {weapons, armors, items, souls};
|
||||
}
|
||||
}
|
||||
44
game-server/app/util/gamedata.ts
Normal file
44
game-server/app/util/gamedata.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
var gamedata = {};
|
||||
|
||||
function initData () {
|
||||
fs.readdirSync(__dirname + '/../../config/resource')
|
||||
.filter(function(file) {
|
||||
return (file.indexOf(".") !== 0) && (file !== "index.js");
|
||||
})
|
||||
//筛选有文件名且不是index进行遍历
|
||||
.forEach(function(file) {
|
||||
var name = file.split('.')[0];
|
||||
try {
|
||||
gamedata[name] = JSON.parse(
|
||||
fs.readFileSync(path.resolve(__dirname, "../../config/resource/" + file))
|
||||
);
|
||||
} catch(e) {
|
||||
console.error('【文件缺少】:' + file);
|
||||
gamedata[name] = [];
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
initData();
|
||||
|
||||
export function getGamedata(key) {
|
||||
return gamedata[key];
|
||||
}
|
||||
|
||||
export function getWarById(warid) {
|
||||
let warInfo = gamedata['dic_zyz_gk']||[];
|
||||
return warInfo.find(cur => {
|
||||
return cur.war_id == warid
|
||||
});
|
||||
}
|
||||
|
||||
export function getGoodById(gid) {
|
||||
console.log(gid)
|
||||
let goodsInfo = gamedata['goods']||[];
|
||||
return goodsInfo.find(cur => {
|
||||
return cur.good_id == gid
|
||||
});
|
||||
}
|
||||
@@ -7,5 +7,8 @@ module.exports = [{
|
||||
}, {
|
||||
'type': 'gate',
|
||||
'token': 'agarxhqb98rpajloaxn34ga8xrunpagkjwlaw3ruxnpaagl29w4rxn'
|
||||
}, {
|
||||
'type': 'battle',
|
||||
'token': 'agarxhqb98rpajloaxn34ga8xrunpagkjwlaw3ruxnpaagl29w4rxn'
|
||||
}
|
||||
];
|
||||
2102
game-server/config/resource/dic_zyz_gk.json
Normal file
2102
game-server/config/resource/dic_zyz_gk.json
Normal file
File diff suppressed because it is too large
Load Diff
6602
game-server/config/resource/goods.json
Normal file
6602
game-server/config/resource/goods.json
Normal file
File diff suppressed because it is too large
Load Diff
@@ -4,4 +4,5 @@ npm run build
|
||||
node generatePm2Config.js #使用pm2来做进程管理,生成进程配置文件
|
||||
rm -rf /game-server/shared
|
||||
cp -r /game-server/dist/shared /game-server
|
||||
cp -r /game-server/config/resource /game-server/dist/game-server/config
|
||||
pm2-runtime pomeloPm2Start.json #pm2 启动游戏服务器
|
||||
|
||||
Reference in New Issue
Block a user