HJ_Server/src/api_s2c/hero/ApiChangePos.ts
2023-12-22 14:07:55 +08:00

78 lines
3.2 KiB
TypeScript

import {ApiCall} from "tsrpc";
import {Wjjl} from '../../module/collection_wjjl';
import {PlayerFun} from '../../public/player';
import {ReqChangePos, ResChangePos} from "../../shared/protocols/hero/PtlChangePos";
import kfjsFun from "../../public/kaifujingsai";
import {HeroFun} from "../../public/hero";
export const maxPosNum = 6;
export default async function (call: ApiCall<ReqChangePos, ResChangePos>) {
let hero = await HeroFun.getHero(call, call.req.id)
if (!hero) return call.error(globalThis.lng.hero_1);
let heroPos = Object.assign({}, call.conn.heroPos);
let fightHeros = call.conn.gud.fightHeros.map(id => id);
let posArr = Object.keys(heroPos).filter(pos => call.conn.gud.lv >= G.gc.herocom.szHeroLimit[pos]);
let idArr = Object.values(heroPos).filter(v => v != '');
if (call.conn.gud.helpHeros.find(v => v._id == call.req.id)) return call.error('', {code: -1});
if (idArr.includes(call.req.id)) return call.error(globalThis.lng.hero_2);
if (call.req.pos && !posArr.includes(call.req.pos)) return call.error(globalThis.lng.hero_3);
let matrixPos = JSON.parse(JSON.stringify(call.conn.gud.matrixPos));
if (call.req.state == 'set') {
if (idArr.length == posArr.length) return call.error(posArr.length < maxPosNum ? globalThis.lng.hero_5 : globalThis.lng.hero_4);
if (fightHeros.includes(hero.heroId.toString())) return call.errorCode(-2);
let setPos = call.req.pos;
heroPos[setPos] = call.req.id;
for (let type in matrixPos) {
if (!matrixPos[type][setPos]) matrixPos[type][setPos] = call.req.id;
// else {
// for (let pos = 1; pos <= maxPosNum; pos++) {
// if (!matrixPos[type][pos]) matrixPos[type][pos] = call.req.id;
// }
// }
}
} else if (call.req.state == 'change') {
if (heroPos[call.req.pos]) {
let unLoadHero = await HeroFun.getHero(call, heroPos[call.req.pos]);
unLoadHero && fightHeros.removeOne(id => id == unLoadHero.heroId);
}
if (fightHeros.includes(hero.heroId.toString())) return call.errorCode(-2);
// todo 替换逻辑有问题
for (let type in matrixPos) {
for (let pos in matrixPos[type]) {
if (pos == call.req.pos) {
matrixPos[type][pos] = call.req.id;
}
}
}
heroPos[call.req.pos] = call.req.id;
}
fightHeros.push(hero.heroId.toString());
fightHeros = await checkFightHerosAndFix(call.uid, heroPos, fightHeros)
Wjjl.setVal(call.uid, 'fight_hero_colour_4', fightHeros.map(id => G.gc.hero[id].colour).filter(c => c >= 4).length);
let checkMatrixPos = HeroFun.checkMatrixPosAndChange(call, heroPos, matrixPos)
await PlayerFun.addAttr(call, {heroPos: heroPos, fightHeros: fightHeros, matrixPos: checkMatrixPos});
call.succ(heroPos);
call.conn.refreshPower();
kfjsFun.setHeroLvCount(call)
}
async function checkFightHerosAndFix(uid, heroPos, fightHeros) {
if (fightHeros.length == R.values(heroPos).length) {
return fightHeros
}
let heros = await HeroFun.getHeros({uid}, R.values(heroPos));
return heros.map(i => i.heroId)
}