战斗开始和战斗结算
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -3,3 +3,4 @@ game-server/dist
|
||||
game-server/logs
|
||||
*.DS_Store
|
||||
.vscode/*
|
||||
shared/**/*.js
|
||||
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 启动游戏服务器
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { prop, pre } from '@typegoose/typegoose';
|
||||
import { TimeStamps } from '@typegoose/typegoose/lib/defaultClasses';
|
||||
|
||||
/**
|
||||
* BaseModel
|
||||
@@ -12,7 +13,7 @@ import { prop, pre } from '@typegoose/typegoose';
|
||||
next();
|
||||
})
|
||||
|
||||
export default class BaseModel {
|
||||
export default class BaseModel extends TimeStamps {
|
||||
|
||||
_id?: string
|
||||
|
||||
|
||||
42
shared/db/BattleRecord.ts
Normal file
42
shared/db/BattleRecord.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { COUNTER } from '../consts/consts';
|
||||
import { CounterModel } from './Counter';
|
||||
import BaseModel from './BaseModel';
|
||||
import { index, getModelForClass, prop } from '@typegoose/typegoose';
|
||||
import { timeStamp } from 'console';
|
||||
import { TimeStamps } from '@typegoose/typegoose/lib/defaultClasses';
|
||||
|
||||
|
||||
@index({ roleId: 1, hid: 1, eid: 1 })
|
||||
@index({ seqId: 1 })
|
||||
|
||||
export default class BattleRecord extends BaseModel {
|
||||
@prop({ required: true })
|
||||
roleId: string; // 角色 id
|
||||
@prop({ required: true })
|
||||
roleName: string; // 角色名称
|
||||
|
||||
@prop({ required: true })
|
||||
battleId: number; // 关卡 id
|
||||
@prop({ required: true })
|
||||
status: number; // 关卡状态 0-挑战中 1-挑战成功 2-挑战失败
|
||||
@prop({ required: true })
|
||||
count: number; // 挑战次数
|
||||
@prop({ required: true })
|
||||
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);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static async getBattleRecordByRole(roleId: string, battleId: number, lean = true) {
|
||||
const result = await BattleRecordModel.findOne({roleId, battleId}).lean(lean);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
export const BattleRecordModel = getModelForClass(BattleRecord);
|
||||
@@ -1,4 +1,5 @@
|
||||
import { EggAppConfig, EggAppInfo, PowerPartial } from 'egg';
|
||||
const path = require('path');
|
||||
|
||||
export default (appInfo: EggAppInfo) => {
|
||||
const config = {} as PowerPartial<EggAppConfig>;
|
||||
@@ -31,6 +32,20 @@ export default (appInfo: EggAppInfo) => {
|
||||
packages: [ '/root/zyz/web-server/package.json' ],
|
||||
};
|
||||
|
||||
config.view = {
|
||||
root: path.join(appInfo.baseDir, '/app/public'),
|
||||
defaultViewEngine: 'nunjucks',
|
||||
mapping: {
|
||||
'.html': 'nunjucks' //左边写成.html后缀,会自动渲染.html文件
|
||||
},
|
||||
};
|
||||
|
||||
config.static = {
|
||||
prefix: '/',
|
||||
dir: path.join(appInfo.baseDir, '/app/public'),
|
||||
};
|
||||
|
||||
|
||||
// add your special config in here
|
||||
const bizConfig = {
|
||||
sourceUrl: `https://github.com/eggjs/examples/tree/master/${appInfo.name}`,
|
||||
|
||||
Reference in New Issue
Block a user