305 lines
13 KiB
TypeScript
305 lines
13 KiB
TypeScript
import {Application, BackendSession, createTcpMailBox, ChannelService} from 'pinus';
|
||
import { handleCost } from '../../../services/rewardService';
|
||
import { calPlayerCeAndSave } from '../../../services/playerCeService';
|
||
import { resResult, getItems } from '../../../pubUtils/util';
|
||
import { STATUS } from '../../../consts/statusCode';
|
||
import {HeroModel} from '../../../db/Hero';
|
||
import {CURRENCY_BY_TYPE, CURRENCY_TYPE, ITID, CONSUME_TYPE} from '../../../consts/const';
|
||
import {getJobInfoById, getMaxGradeByjobClass , getJobByGradeAndClass, getFriendShipById, getHeroInfoById, getFriendShipLevels, getGoodById} from '../../../pubUtils/gamedata';
|
||
import { ITID, CONSUME_TYPE } from '../../../consts/consts';
|
||
|
||
const _ = require('underscore');
|
||
|
||
export default function(app: Application) {
|
||
return new HeroHandler(app);
|
||
}
|
||
|
||
export class HeroHandler {
|
||
constructor(private app: Application) {
|
||
}
|
||
|
||
private channelService: ChannelService = this.app.get('channelService');
|
||
|
||
public async test(msg: { id: number, count: number}, session: BackendSession) {
|
||
let roleId: string = session.get('roleId');
|
||
let roleName: string = session.get('roleName');
|
||
let sid: string = session.get('sid');
|
||
|
||
let {id, count} = msg;
|
||
|
||
let result = await handleCost(roleId, sid, [{id, count}] );
|
||
if(!result) {
|
||
return resResult(STATUS.BATTLE_CONSUMES_NOT_ENOUGH);
|
||
}
|
||
return resResult(STATUS.SUCCESS);
|
||
|
||
}
|
||
|
||
// 武将碎片合成
|
||
public async combine(msg: { hid: number}, session: BackendSession) {
|
||
let roleId: string = session.get('roleId');
|
||
let roleName: string = session.get('roleName');
|
||
let sid: string = session.get('sid');
|
||
|
||
let {hid} = msg;
|
||
|
||
// 检查是否存在武将
|
||
let hasHero = await HeroModel.findByHidAndRole(hid, roleId);
|
||
if(hasHero) return resResult(STATUS.ROLE_HERO_EXISTS);
|
||
// 根据dic_hero 获得 1. 碎片id 2. 碎片数量 3. 初始武将星级 4. 初始品质
|
||
let dicHero = getHeroInfoById(hid);
|
||
if(!dicHero) return resResult(STATUS.ROLE_INFO_NOT_FOUND);
|
||
let {pieceId, quality, initialStars: star, pieceCount, jobid: job, name: hName} = dicHero;
|
||
// 碎片数量是否足够
|
||
let costResult = await handleCost(roleId, sid, [{id: pieceId, count: pieceCount}]);
|
||
if(!costResult) return resResult(STATUS.ROLE_MATERIAL_NOT_ENOUGH);
|
||
// createHero
|
||
let curHero = await HeroModel.createHero({
|
||
roleId, roleName, hid, hName, star, quality, job
|
||
});
|
||
await calPlayerCeAndSave(sid, roleId, [curHero]);
|
||
return resResult(STATUS.SUCCESS, {curHero});
|
||
}
|
||
|
||
// 武将升级
|
||
public async lvUp(msg: { hid: number, type: number, material: Array<{id: number, count: number}>}, session: BackendSession) {
|
||
let roleId: string = session.get('roleId');
|
||
let roleName: string = session.get('roleName');
|
||
let sid: string = session.get('sid');
|
||
|
||
let { hid, type, material } = msg;
|
||
|
||
// 根据dic_goods 计算得材料可转换的经验
|
||
let allExp = 0;
|
||
for(let {id, count} of material) {
|
||
let dicGoods = getGoodById(id);
|
||
if(!dicGoods) return resResult(STATUS.ROLE_INFO_NOT_FOUND);
|
||
let dicItid = ITID.get(dicGoods.itid);
|
||
if(!dicItid || dicItid.type != CONSUME_TYPE.EXP) {
|
||
return resResult(STATUS.ROLE_METERIAL_ERROR);
|
||
}
|
||
}
|
||
// 根据dic_zyz_charexp 计算武将可以升的级数
|
||
// 检查材料是否满足升级需求
|
||
// 检查是否超出主公等级
|
||
// 进行升级,溢出经验保留
|
||
// handleCost
|
||
}
|
||
|
||
// 武将升星
|
||
public async starUp(msg: { hid: number, star: number, starStage: number}, session: BackendSession) {
|
||
// 根据dic_hero 获得 碎片id
|
||
// 根据dic_zyz_hero_star 计算需要花的碎片并检查碎片数量
|
||
// 检查当前星级和星阶和客户端传参是否符合
|
||
// 升星阶,满6自动升星。最高6星
|
||
// handleCost
|
||
}
|
||
|
||
// 武将觉醒
|
||
public async wakeUp(msg: { hid: number, colorStar: number, fireStage: number}, session: BackendSession) {
|
||
// 根据dic_hero 获得 碎片id
|
||
// 根据dic_zyz_hero_wake 获得需要花费的碎片和材料
|
||
// 特殊处理,初次觉醒fireStage传0,保存为 colorStar = 1, fireStage = 0,花费的材料取的0星的
|
||
// 检查品质是否是橙色
|
||
// 升星阶,满6自动升星
|
||
// handleCost
|
||
}
|
||
|
||
// 武将升品
|
||
public async qualityUp(msg: { hid: number, quality: number }, session: BackendSession) {
|
||
// 根据dic_hero 获得 碎片id
|
||
// 根据dic_zyz_hero_quality_up 获得需要的材料
|
||
// 检查是否达到6星
|
||
// 升品,满品3级
|
||
// handleCost
|
||
}
|
||
//训练
|
||
async heroJobTrain (msg: {hid:number}, session: BackendSession) {
|
||
let roleId: string = session.get('roleId');
|
||
let sid: string = session.get('sid');
|
||
let { hid } = msg;
|
||
let hero = await HeroModel.findByHidAndRole(hid, roleId);
|
||
if (!hero)
|
||
return resResult(STATUS.HERO_NOT_FIND);
|
||
let heroJob = getJobInfoById(hero.job);
|
||
let nextHeroJob = getJobByGradeAndClass(heroJob.job_class, heroJob.grade + 1);
|
||
if (nextHeroJob.unlockLevel > hero.lv)
|
||
return resResult(STATUS.NOT_REACH_UNLOCK_LEVEL);
|
||
if (hero.jobStage >= 6)
|
||
return resResult(STATUS.HERO_JOB_STAGE_REACH_MAX_STAGE);
|
||
if (hero.job >= getMaxGradeByjobClass(heroJob.job_class))
|
||
return resResult(STATUS.HERO_JOB_REACH_MAX_STAGE);
|
||
let cousumeGoods = getItems(heroJob.trainingConsume);
|
||
let result = await handleCost(roleId, sid, cousumeGoods);
|
||
if(!result) {
|
||
return resResult(STATUS.BATTLE_CONSUMES_NOT_ENOUGH);
|
||
}
|
||
hero.jobStage = hero.jobStage ++;
|
||
//重算战力并下发
|
||
let heros = await calPlayerCeAndSave(sid, roleId, [hero]);
|
||
return resResult(STATUS.SUCCESS, { hid : heros[0].hid, job : heros[0].job, jobStage : heros[0].jobStage});
|
||
}
|
||
//进阶
|
||
async heroJobStageUp(msg: {hid:number}, session: BackendSession) {
|
||
let roleId: string = session.get('roleId');
|
||
let sid: string = session.get('sid');
|
||
let { hid } = msg;
|
||
let hero = await HeroModel.findByHidAndRole(hid, roleId);
|
||
if (!hero)
|
||
return resResult(STATUS.HERO_NOT_FIND);
|
||
let heroJob = getJobInfoById(hero.job);
|
||
let nextHeroJob = getJobByGradeAndClass(heroJob.job_class, heroJob.grade + 1);
|
||
if (nextHeroJob.unlockLevel > hero.lv)
|
||
return resResult(STATUS.NOT_REACH_UNLOCK_LEVEL);
|
||
if (hero.job >= getMaxGradeByjobClass(heroJob.job_class))
|
||
return resResult(STATUS.HERO_JOB_REACH_MAX_STAGE);
|
||
let cousumeGoods = getItems(heroJob.upGradeConsume);
|
||
let result = await handleCost(roleId, sid, cousumeGoods);
|
||
if(!result) {
|
||
return resResult(STATUS.BATTLE_CONSUMES_NOT_ENOUGH);
|
||
}
|
||
hero.job = nextHeroJob.job;
|
||
hero.jobStage = 0;
|
||
//重算战力并下发
|
||
let heros = await calPlayerCeAndSave(sid, roleId, [hero]);
|
||
return resResult(STATUS.SUCCESS, { hid : heros[0].hid, job : heros[0].job, jobStage : heros[0].jobStage});
|
||
}
|
||
|
||
//激活羁绊
|
||
async heroConectionActivate(msg: {hid:number, id:number}, session: BackendSession) {
|
||
let roleId: string = session.get('roleId');
|
||
let sid: string = session.get('sid');
|
||
let { hid, id } = msg;
|
||
let friendShip = getFriendShipById(id);
|
||
if (friendShip.actorId != hid)
|
||
return resResult(STATUS.WRONG_PARMS);
|
||
let hero = await HeroModel.findByHidAndRole(hid, roleId);
|
||
if (!hero)
|
||
return resResult(STATUS.HERO_NOT_FIND);
|
||
if (!!_.findWhere(hero.conections, {id:id}))
|
||
return resResult(STATUS.HERO_CONECTION_IS_EXIT);
|
||
if (hero.star < friendShip.level)
|
||
return resResult(STATUS.NOT_REACH_UNLOCK_LEVEL);
|
||
let heroIds = friendShip.memberId.split('|');
|
||
for (let heroId of heroIds) {
|
||
let member = await HeroModel.findByHidAndRole(heroId, roleId);
|
||
if(!member)
|
||
return resResult(STATUS.HERO_NOT_FIND);
|
||
if (member.star < friendShip.level)
|
||
return resResult(STATUS.NOT_REACH_UNLOCK_LEVEL);
|
||
}
|
||
let result = await handleCost(roleId, sid, [{id: CURRENCY_BY_TYPE.get(CURRENCY_TYPE.COIN), count:5000}]);
|
||
if(!result) {
|
||
return resResult(STATUS.BATTLE_CONSUMES_NOT_ENOUGH);
|
||
}
|
||
hero.conections.push({
|
||
id : id
|
||
});
|
||
//重算战力并下发
|
||
let heros = await calPlayerCeAndSave(sid, roleId, [hero]);
|
||
return resResult(STATUS.SUCCESS, { hid : heros[0].hid, conections : heros[0].conections});
|
||
}
|
||
|
||
//赠送(包括一键赠送)
|
||
async heroGiveFavor(msg: {hid:number, items:Array<{id : number,count : number}>}, session: BackendSession) {
|
||
let roleId: string = session.get('roleId');
|
||
let sid: string = session.get('sid');
|
||
let { hid, items } = msg;
|
||
let hero = await HeroModel.findByHidAndRole(hid, roleId);
|
||
if (!hero)
|
||
return resResult(STATUS.HERO_NOT_FIND);
|
||
let friendShipLevels = getFriendShipLevels();
|
||
if (friendShipLevels[friendShipLevels.lenth - 1].level <= hero.favourLv)
|
||
return resResult(STATUS.HERO_FAVOUR_LEVEL_REACH_MAXT);
|
||
//计算消耗物品转化的经验
|
||
let exp:number = 0;
|
||
for (let item of items) {
|
||
let itemInfo = getGoodById(item.id);
|
||
if (itemInfo.itid == CONSUME_TYPE.FAVOUR) {
|
||
exp += itemInfo.value;
|
||
} else {
|
||
return resResult(STATUS.WRONG_PARMS);
|
||
}
|
||
}
|
||
hero.favour += exp;
|
||
for (let friendShipLevel of friendShipLevels) {
|
||
if (friendShipLevel.level < hero.favourLv)
|
||
continue;
|
||
if (friendShipLevel.exp > hero.favour)
|
||
break;
|
||
hero.favour -= friendShipLevel.exp;
|
||
hero.favourLv++;
|
||
}
|
||
let result = await handleCost(roleId, sid, items);
|
||
if(!result) {
|
||
return resResult(STATUS.BATTLE_CONSUMES_NOT_ENOUGH);
|
||
}
|
||
//重算战力并下发
|
||
let heros = await calPlayerCeAndSave(sid, roleId, [hero]);
|
||
return resResult(STATUS.SUCCESS, { hid : heros[0].hid, favour : heros[0].favour, favourLv : heros[0].favourLv});
|
||
}
|
||
|
||
//购买时装
|
||
async buyHeroSkin(msg: {id:number}, session: BackendSession) {
|
||
let roleId: string = session.get('roleId');
|
||
let sid: string = session.get('sid');
|
||
let { id } = msg;
|
||
let skinInfo;//TODO查询时装
|
||
if (!skinInfo)
|
||
resResult(STATUS.HERO_SKIN_NOT_FIND);
|
||
let hid;//TODO获得时装的武将id
|
||
let hero = await HeroModel.findByHidAndRole(hid, roleId);
|
||
if (!hero)
|
||
resResult(STATUS.HERO_NOT_FIND);
|
||
if (!!_.findWhere(hero.skins, {id:id}))
|
||
resResult(STATUS.HERO_SKIN_IS_EXIST);
|
||
let items;//TODO物品消耗
|
||
let result = await handleCost(roleId, sid, items);
|
||
if(!result) {
|
||
return resResult(STATUS.BATTLE_CONSUMES_NOT_ENOUGH);
|
||
}
|
||
hero.skins.push({
|
||
id:id,
|
||
enable:false
|
||
});
|
||
let heros = await HeroModel.findByRole(roleId);
|
||
for (let i = 0; i < heros.lenth; i++) {
|
||
if (heros[i].hid == hero.hid) {
|
||
heros[i] = hero;
|
||
}
|
||
}
|
||
//重算战力并下发
|
||
await calPlayerCeAndSave(sid, roleId, heros);
|
||
return resResult(STATUS.SUCCESS, { hid : hero.hid, skins : hero.skins});
|
||
}
|
||
|
||
//穿带时装
|
||
async heroWearSkin(msg: {id:number}, session: BackendSession) {
|
||
let roleId: string = session.get('roleId');
|
||
let sid: string = session.get('sid');
|
||
let { id } = msg;
|
||
let skinInfo;//TODO查询时装
|
||
if (!skinInfo)
|
||
resResult(STATUS.HERO_SKIN_NOT_FIND);
|
||
let hid;
|
||
let hero = await HeroModel.findByHidAndRole(hid, roleId);
|
||
if (!hero)
|
||
resResult(STATUS.HERO_NOT_FIND);
|
||
let result = false;
|
||
for (let skin of hero.skins) {
|
||
if (skin.id == id) {
|
||
skin.enable = true;
|
||
result = true;
|
||
} else {
|
||
skin.enable = false;
|
||
}
|
||
}
|
||
if (!result) {
|
||
resResult(STATUS.HERO_SKIN_NOT_FIND);
|
||
}
|
||
await calPlayerCeAndSave(sid, roleId, [hero]);
|
||
return resResult(STATUS.SUCCESS, { hid : hero.hid, skins : hero.skins});
|
||
}
|
||
|
||
}
|