添加保存R剧本接口

This commit is contained in:
luying
2020-10-22 11:05:49 +08:00
parent 3d031088b7
commit 318d45e50e
8 changed files with 202 additions and 37 deletions

View File

@@ -3,15 +3,18 @@ import { BattleRecordModel } from '../../../db/BattleRecord';
import { BattleSweepRecordModel } from '../../../db/BattleSweepRecord';
import { getWarById, } from '../../../pubUtils/gamedata';
import { genCode } from '../../../pubUtils/util';
import { WAR_TYPE, EVENT_START_BATTLE } from '../../../consts/consts';
import { WAR_TYPE } from '../../../consts/consts';
import { checkDaily, checkDailyAndIncrease } from '../../../services/dailyBattleService';
import { checkTowerWar, towerBattleEnd, roleLevelup } from '../../../services/battleService';
import { checkTowerWar, towerBattleEnd } from '../../../services/battleService';
import { WarReward } from '../../../services/warRewardService';
import { getAp, setAp } from '../../../services/actionPointService';
import { setBattleStatus, startEvent } from '../../../services/eventSercive';
import { setBattleStatus } from '../../../services/eventSercive';
import { STATUS } from '../../../consts/statusCode';
import { resResult } from '../../../pubUtils/util';
import { HeroModel } from '../../../db/Hero';
import { RoleModel } from '../../../db/Role';
import { RScriptRecordModel } from '../../../db/RScriptRecord';
import { updateWarStar, checkBattleHeroes, roleLevelup } from '../../../services/normalBattleService';
export default function(app: Application) {
return new NormalBattleHandler(app);
@@ -47,6 +50,9 @@ export class NormalBattleHandler {
if(!preBattle) return resResult(STATUS.BATTLE_NEED_PREVIOUS_GK);
}
let checkHeroes = checkBattleHeroes(roleId, heroes);
if(!checkHeroes) return resResult(STATUS.BATTLE_HERO_NOT_FOUND);
let dailyNum = {};
let towerData = {};
if(warInfo.warType == WAR_TYPE.DAILY) {
@@ -86,14 +92,40 @@ export class NormalBattleHandler {
const { type } = msg;
let roleId = session.get('roleId');
const BattleRecord = await BattleRecordModel.getBattleList(roleId, type);
let role = await RoleModel.findByRoleId(roleId);
let {warStar} = role;
let scripts = await RScriptRecordModel.findbyRole(roleId, type);
let result = []; // 去重
for(let br of BattleRecord) {
let index = result.findIndex(cur => cur.battleId == br.battleId);
if(index == -1) {
result.push(br);
for(let {battleId, scriptBefore = '', scriptAfter = ''} of scripts) {
result.push({
battleId,
status: 0,
star: 0,
scriptBefore,
scriptAfter
});
}
for(let {id, star, warType} of warStar) {
if(warType == type) {
let curResult = result.find(cur => cur.battleId == id);
if(curResult) {
curResult.status = 1;
curResult.star = star;
} else {
result.push({
battleId: id,
status: 1,
star,
scriptBefore: '',
scriptAfter: ''
});
}
}
}
result = result.sort((a, b) => {return a.battleId - b.battleId});
return resResult(STATUS.SUCCESS, {
list: result
@@ -181,6 +213,8 @@ export class NormalBattleHandler {
}, true);
let { status } = updateResult;
await updateWarStar(roleId, battleId, warInfo.warType, star);
let actordata = await roleLevelup(roleId, warInfo.kingExp)// 主公升级经验
let curHeroes = [];
@@ -189,12 +223,6 @@ export class NormalBattleHandler {
curHeroes.push(hero);
}
// 主线关卡某个关卡触发事件开启
let eventStatus = session.get('eventStatus')||0;
if(battleId == EVENT_START_BATTLE && eventStatus == 0) {
// console.log('*******startEvent')
await startEvent(this.app, session);
}
// 返回值:
// towerStatus: false-本层未通过, true-本层已通过
// towerBonus: 天梯每隔几层的额外宝箱
@@ -269,4 +297,38 @@ export class NormalBattleHandler {
});
}
async saveScript(msg: {battleId: number, type: number, script: string }, session: BackendSession) {
const { battleId, type, script } = msg;
let roleId = session.get('roleId');
let warInfo = getWarById(battleId);
let result = await RScriptRecordModel.setScript(roleId, battleId, warInfo.warType, type, script);
if(result) {
if(!result.scriptBefore) result.scriptBefore = '';
if(!result.scriptAfter) result.scriptAfter = '';
return resResult(STATUS.SUCCESS, result);
} else {
console.error('script not created');
return resResult(STATUS.INTERNAL_ERR);
}
}
async getScriptByBattle(msg: {battleIds: Array<number> }, session: BackendSession) {
const { battleIds } = msg;
let roleId = session.get('roleId');
let list = new Array();
for(let battleId of battleIds) {
let result = await RScriptRecordModel.findbyRoleAndBattle(roleId, battleId);
if(result) {
let {scriptBefore = '', scriptAfter = ''} = result;
list.push({battleId, scriptBefore, scriptAfter});
} else {
list.push({battleId, scriptBefore: '', scriptAfter: ''});
}
}
return resResult(STATUS.SUCCESS, { list });
}
}

View File

@@ -7,6 +7,9 @@ import { Application, Session } from 'pinus';
import {FrontendSession} from 'pinus';
import { HeroModel } from './../../../db/Hero';
import { resResult } from '../../../pubUtils/util';
import { startEvent } from '../../../services/eventSercive';
import { EVENT_START_LV } from '../../../consts/consts';
import { getAp } from '../../../services/actionPointService';
export default function (app: Application) {
return new EntryHandler(app);
@@ -24,6 +27,7 @@ export class EntryHandler {
*/
async enter(msg: { token: string, serverId: number }, session: FrontendSession) {
console.log('******enter')
let self = this;
let serverId = msg.serverId;
let sessionService = self.app.get('sessionService');
@@ -43,7 +47,6 @@ export class EntryHandler {
console.log('duplicate log in', role.roleId);
return resResult(STATUS.DUP_LOGIN);
}
await session.abind(role.roleId);
session.set('uid', role.roleId);
session.set('roleId', role.roleId);
@@ -61,6 +64,11 @@ export class EntryHandler {
// });
session.on('closed', this.onUserLeave.bind(this));
if(role.lv >= EVENT_START_LV && !role.eventStatus) {
startEvent(self.app, session);
}
let channelService = self.app.get('channelService');
let channel = channelService.getChannel(role.roleId, true);
if (channel.getMembers().indexOf(role.roleId) === -1) {
@@ -74,6 +82,8 @@ export class EntryHandler {
let equips = await EquipModel.findbyRole(role.roleId);
role['heros'] = heros;
role['equips'] = equips;
let apJson = await getAp(Date.now(), role.roleId);
role['apJson'] = apJson;
return resResult(STATUS.SUCCESS, { role });
}

View File

@@ -5,7 +5,7 @@ import { HANG_UP_CONSTS } from './../consts/consts';
import { BattleRecordModel } from './../db/BattleRecord';
import { TowerRecordModel } from './../db/TowerRecord';
import { RoleModel } from './../db/Role';
import { getHeroInfoById, getJobInfoById, getTowerDataByLv, getLvByExp, getExpByLv } from "../pubUtils/gamedata"
import { getHeroInfoById, getJobInfoById, getTowerDataByLv } from "../pubUtils/gamedata"
import { decodeArrayStr, shouldRefresh, resResult } from '../pubUtils/util';
import { handleFixedReward } from './rewardService';
import { STATUS } from '../consts/statusCode';
@@ -183,23 +183,3 @@ export async function checkTaskConditions(roleId: string, heroes: Array<number>,
return res;
}
export async function roleLevelup(roleId: string, kingExp: number) {
let role = await RoleModel.findByRoleId(roleId);
let {lv = 1, exp = 0} = role;
exp += kingExp;
let newLv = getLvByExp(exp);
await RoleModel.levelup(roleId, newLv, exp);
let actordata = [];
for(let i = lv; i <= newLv; i++) {
let {sum, cur} = getExpByLv(i);
let getExp = sum > exp? sum - exp: 0;
actordata.push({
lv : i,
exp : cur - getExp,
getExp : getExp,
mostExp : cur
})
}
return {actordata, lv, exp, kingExp};
}

View File

@@ -0,0 +1,50 @@
import { HeroModel } from '../db/Hero';
import Role, { RoleModel } from '../db/Role'
import { getLvByExp, getExpByLv } from '../pubUtils/gamedata';
export async function roleLevelup(roleId: string, kingExp: number) {
let role = await RoleModel.findByRoleId(roleId);
let {lv = 1, exp = 0} = role;
exp += kingExp;
let newLv = getLvByExp(exp);
await RoleModel.levelup(roleId, newLv, exp);
let actordata = [];
for(let i = lv; i <= newLv; i++) {
let {sum, cur} = getExpByLv(i);
let getExp = sum > exp? sum - exp: 0;
actordata.push({
lv : i,
exp : cur - getExp,
getExp : getExp,
mostExp : cur
})
}
return {actordata, lv, exp, kingExp};
}
export async function checkBattleHeroes(roleId: string, heroes: Array<number>) {
let flag = true;
for(let hid of heroes) {
let hero = await HeroModel.findByHidAndRole(hid, roleId);
if(!hero) flag = false;
}
return flag;
}
export async function updateWarStar(roleId: string, battleId: number, warType: number, star: number) {
let role = await RoleModel.findByRoleId(roleId);
let { warStar = new Array<{id: number, warType: number, star: number}>() } = role;
let curStar = warStar.find(cur => cur.id == battleId);
let result: Role;
if(curStar) {
if(star > curStar.star) {
result = await Role.updateWarStar(roleId, battleId, star);
}
} else {
result = await RoleModel.pushWarStar(roleId, battleId, warType, star);
}
return result
}

View File

@@ -93,6 +93,8 @@ export const WAR_TYPE = {
export const EVENT_RANDOM_TYPE_ONE_OPEN = false;
// 奇遇事件每次刷新几个
export const EVENT_REFRESH_NUM = 3;
// 奇遇事件开启等级
export const EVENT_START_LV = 1;
// 存于用户Role表事件开启状态
export const EVENT_STATUS = {

View File

@@ -19,6 +19,7 @@ export const STATUS = {
BATTLE_NEED_PREVIOUS_GK: { code: 20003, simStr: '需要完成上一关才可以挑战' },
BATTLE_ID_NOT_MATCH: { code: 20004, simStr: '战斗数据错误' },
BATTLE_STATUS_WRONG: { code: 20005, simStr: '关卡状态错误' },
BATTLE_HERO_NOT_FOUND: { code: 20006, simStr: '使用的武将不存在' },
// 主线 20100 - 20199
BATTLE_INFO_VALIDATE_ERR: { code: 20101, simStr: '关卡信息不同' },

View File

@@ -0,0 +1,46 @@
import BaseModel from './BaseModel';
import { index, getModelForClass, prop } from '@typegoose/typegoose';
@index({ roleId: 1, battleId: 1 })
export default class RScriptRecord extends BaseModel {
@prop({ required: true, default: '' })
roleId: string; // 角色 id
@prop({ required: true, default: 0 })
battleId: number; // 关卡 id
@prop({ required: true, default: 0 })
warType: number; // 关卡 id
@prop({ required: true, default: '' })
scriptBefore: string; // 战场前剧本
@prop({ required: true, default: '' })
scriptAfter: string; // 战场后剧本
public static async setScript(roleId: string, battleId: number, warType: number, type: number, script: string, lean = true) {
let update = { warType };
if(type == 1) {
update['scriptBefore'] = script;
} else if (type == 2) {
update['scriptAfter'] = script;
}
const items = await RScriptRecordModel.findOneAndUpdate({ roleId, battleId }, update, {upsert: true, new: true}).lean(lean);
return items;
}
public static async findbyRoleAndBattle(roleId: string, battleId: number, lean = true) {
const items = await RScriptRecordModel.findOne({ roleId, battleId }).select('battleId scriptBefore scriptAfter').sort({battleId: 1}).lean(lean);
return items;
}
public static async findbyRole(roleId: string, warType: number, lean = true) {
const items = await RScriptRecordModel.find({ roleId, warType }).select('battleId scriptBefore scriptAfter').lean(lean);
return items;
}
}
export const RScriptRecordModel = getModelForClass(RScriptRecord);

View File

@@ -86,6 +86,7 @@ export default class Role extends BaseModel {
@prop({ required: true, default: [] })
warStar: [{ // 关卡星级
id: number; // 关卡 id
warType: number; // 关卡类型
star: number; // 星级
}];
@@ -197,7 +198,6 @@ export default class Role extends BaseModel {
let update = {};
update[`${field}.$.cnt`] = cnt;
console.log(JSON.stringify(condition), update)
let result = await RoleModel.findOneAndUpdate(condition, { $inc: update }, { "new": true, "upsert": true}).lean(lean);
return result;
}
@@ -211,6 +211,20 @@ export default class Role extends BaseModel {
let result = await RoleModel.findOneAndUpdate({roleId}, { $inc: { gold: cnt, giftGold: cnt } }, { "new": true, "upsert": true}).lean(lean);
return result;
}
public static async pushWarStar(roleId: string, battleId: number, warType: number, star: number, lean = true) {
let result = await RoleModel.findOneAndUpdate({roleId}, { $addToSet: { warStar: {id: battleId, warType, star }} }, { "new": true, "upsert": true}).lean(lean);
return result;
}
public static async updateWarStar(roleId: string, battleId: number, star: number, lean = true) {
let result = await RoleModel.findOneAndUpdate(
{roleId, 'warStar.id': battleId},
{ $set: { 'warStar.$.star': star } },
{ "new": true, "upsert": true}
).lean(lean);
return result;
}
}
export const RoleModel = getModelForClass(Role);