HJ_Server/src/api_s2c/ganhai/ApiFight.ts
DESKTOP-15R5JU0\legu 97e070ea81 init
2023-11-17 12:02:12 +08:00

52 lines
2.0 KiB
TypeScript

import { ApiCall } from "tsrpc";
import { FightFun } from '../../public/fight';
import { PlayerFun } from '../../public/player';
import { ReqFight, ResFight } from "../../shared/protocols/ganhai/PtlFight";
import { prizeType } from '../../shared/protocols/type';
import { HongDianChange } from "../hongdian/fun";
export default async function (call: ApiCall<ReqFight, ResFight>) {
let dbData = await G.mongodb.collection('ganhai').findOne({ uid: call.uid });
let index = call.req.index;
let fightNum = dbData.useFightNum + 1;
let toShipUid = dbData.ships[index]?.player?.player?.uid;
let toShip
if(toShipUid){
toShip = (await G.mongodb.collection('ganhai').findOne({ uid: toShipUid })).ship;
}
if (fightNum > G.gc.ganhai.fightNum) return call.error('', { code: -1 });
if (!toShip) return call.error('', { code: -2 });
if (toShip.beFightNum >= G.gc.ganhai.ships[toShip.index].ldNum) return call.error('', { code: -3 });
G.mongodb.collection('ganhai').updateOne({ uid: call.uid }, { $set: { ships: dbData.ships, useFightNum: fightNum } });
let change: ResFight['change'] = {
useFightNum: fightNum,
ships: dbData.ships
};
let result = FightFun.fight([await call.conn.getDefaultFightData(), toShip.player]);
result.otherData = { shipIndex: toShip.index };
FightFun.saveLog(call.uid, 'ganhai', result);
FightFun.saveLog(toShip.player.player.uid, 'ganhai', {...result,winSide:result.winSide?0:1});
let prize: prizeType[] = [];
if (result.winSide == 0) {
// 我方胜利加1
toShip.beFightNum++;
G.mongodb.collection('ganhai').updateOne({ uid: toShip.player.player.uid }, { $inc: { 'ship.beFightNum': 1 } });
let conf = G.gc.ganhai.ships[toShip.index];
prize = conf.prize.slice(1).map(atn => ({ ...atn, n: Math.floor(atn.n * conf.ldRatio) }));
await PlayerFun.sendPrize(call, prize);
}
HongDianChange.sendChangeKey(call.uid, ['gonghuihd']);
call.succ({
prize: prize,
result: result,
change: change
});
}