满足条件开启功能推送,详细功能校验暂缺

This commit is contained in:
luying
2020-11-26 19:29:28 +08:00
parent f2e288f25d
commit 1f05043b4b
14 changed files with 158 additions and 44 deletions

View File

@@ -262,7 +262,7 @@ export class ExpeditionBattleHandler {
let warReward = new WarReward(roleId, roleName, battleId, isSuccess);
let reward = await warReward.saveReward(1);
let actordata = await roleLevelup(roleId, isSuccess?warInfo.kingExp:0);// 主公升级经验
let actordata = await roleLevelup(roleId, isSuccess?warInfo.kingExp:0, this.app, session);// 主公升级经验
if(isSuccess) {
// 更新下一关状态
await ExpeditionWarRecordModel.updateStatus(expeditionCode, expeditionId + 1, EXPEDITION_WAR_RECORD_STATUS.WAITING);

View File

@@ -3,7 +3,7 @@ import { BattleRecordModel } from '../../../db/BattleRecord';
import { BattleSweepRecordModel } from '../../../db/BattleSweepRecord';
import { getWarById, } from '../../../pubUtils/gamedata';
import { genCode } from '../../../pubUtils/util';
import { WAR_TYPE, EVENT_STATUS } from '../../../consts/consts';
import { WAR_TYPE, EVENT_STATUS, FUNC_OPT_TYPE } from '../../../consts/consts';
import { checkDaily, checkDailyAndIncrease } from '../../../services/dailyBattleService';
import { checkTowerWar, towerBattleEnd } from '../../../services/battleService';
import { WarReward } from '../../../services/warRewardService';
@@ -16,6 +16,7 @@ import { RoleModel } from '../../../db/Role';
import { RScriptRecordModel } from '../../../db/RScriptRecord';
import { updateWarStar, checkBattleHeroes, roleLevelup } from '../../../services/normalBattleService';
import { checkDungeonNum, checkDungeonAndIncrease } from '../../../services/dungeonService';
import { switchOnFunc } from '../../../services/funcSwitchService';
export default function(app: Application) {
return new NormalBattleHandler(app);
@@ -224,7 +225,10 @@ export class NormalBattleHandler {
// 是否首通
let {warStar} = await RoleModel.findByRoleId(roleId);
let condition1 = warStar.find(cur => cur.id == battleId);
if(!condition1) warReward.setCondition(0, true);
if(!condition1) {
await switchOnFunc(roleId, FUNC_OPT_TYPE.BATTLE_END, battleId, this.app, session);
warReward.setCondition(0, true);
}
// 是否首次3星
if(star == 3 && (!condition1 || condition1.star != 3)) {
warReward.setCondition(1, true);
@@ -240,7 +244,7 @@ export class NormalBattleHandler {
}, true);
let { status } = updateResult;
let actordata = await roleLevelup(roleId, isSuccess?warInfo.kingExp:0);// 主公升级经验
let actordata = await roleLevelup(roleId, isSuccess?warInfo.kingExp:0, this.app, session);// 主公升级经验
// 返回值:
// towerStatus: false-本层未通过, true-本层已通过
@@ -300,7 +304,7 @@ export class NormalBattleHandler {
let warReward = new WarReward(roleId, roleName, battleId, true);
let result = await warReward.saveReward(count);
let actordata = await roleLevelup(roleId, warInfo.kingExp * count)// 主公升级经验
let actordata = await roleLevelup(roleId, warInfo.kingExp * count, this.app, session)// 主公升级经验
// 扫荡记录
await BattleSweepRecordModel.saveBattleSweepRecordById(roleId, battleId, {

View File

@@ -1,10 +1,10 @@
import { Application, ChannelService, FrontendSession, RemoterClass } from 'pinus';
export default function (app: Application) {
return new EventBattleRemote(app);
return new BattleRemote(app);
}
export class EventBattleRemote {
export class BattleRemote {
constructor(private app: Application) {
this.app = app;
@@ -21,9 +21,9 @@ export class EventBattleRemote {
* @param {boolean} flag channel parameter
*
*/
public async add(uid: string, sid: string, flag: boolean) {
let name = uid;
console.log('EventBattleRemote add: ', name, flag);
public async add(uid: string, sid: string, serverId: number, flag: boolean) {
let name = `server-${serverId}`;
console.log('BattleRemote add: ', name, flag);
let channel = this.channelService.getChannel(name, flag);
if (!!channel && !this.get(name, false).includes(uid)) {
if (!!channel) {
@@ -61,8 +61,8 @@ export class EventBattleRemote {
* @param {String} sid server id
*
*/
public async kick(uid: string, sid: string) {
let name = uid;
public async kick(uid: string, sid: string, serverId: number) {
let name = `server-${serverId}`;
let channel = this.channelService.getChannel(name, false);
// leave channel
if (!!channel) {

View File

@@ -8,10 +8,11 @@ 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 { EVENT_START_LV, FUNC_OPT_TYPE } from '../../../consts/consts';
import { getAp } from '../../../services/actionPointService';
import Actor from '../../../pubUtils/actor';
import { ItemModel } from '../../../db/Item';
import { switchOnFunc } from '../../../services/funcSwitchService';
export default function (app: Application) {
return new EntryHandler(app);
@@ -54,11 +55,13 @@ export class EntryHandler {
session.set('eventStatus', role.eventStatus);
session.set('sid', self.app.get('serverId'));
session.set('serverId', role.serverId);
session.set('funcs', role.funcs||[]);
session.push('sid', () => {});
session.push('roleId', () => {});
session.push('roleName', () => {});
session.push('eventStatus', () => {});
session.push('serverId', () => {});
session.push('funcs', () => {});
// session.push('rid', function (err) {
// if (err) {
// console.error('set rid for session service failed! error is : %j', err.stack);
@@ -66,20 +69,15 @@ 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);
let channel = channelService.getChannel(`server-${role.serverId}`, true);
if (channel.getMembers().indexOf(role.roleId) === -1) {
channel.add(role.roleId, self.app.get('serverId'));
}
// put user into channel
await self.app.rpc.battle.eventBattleRemote.add.route(session)(role.roleId, self.app.get('serverId'), true);
console.log(JSON.stringify(self.app.rpc.battle))
await self.app.rpc.battle.BattleRemote.add.route(session)(role.roleId, self.app.get('serverId'), role.serverId, true);
// let users = await self.app.rpc.chat.chatRemote.add.route(session)(role.roleId, self.app.get('serverId'), rid, true);
let heros = await HeroModel.findByRole(role.roleId);
let equips = await EquipModel.findbyRole(role.roleId);
@@ -96,6 +94,8 @@ export class EntryHandler {
role['consumeGoods'] = items;
let apJson = await getAp(Date.now(), role.roleId);
role['apJson'] = apJson;
await switchOnFunc(role.roleId, FUNC_OPT_TYPE.LEVEL_UP, role.lv, self.app, session);
return resResult(STATUS.SUCCESS, { role });
}
@@ -113,12 +113,13 @@ export class EntryHandler {
let roleId = session.get('roleId');
let sid = session.get('sid');
let serverId = session.get('serverId');
let channelService = this.app.get('channelService');
let channel = channelService.getChannel(roleId, true);
channel.leave(roleId, sid);
this.app.rpc.battle.eventBattleRemote.kick.route(session)(roleId, this.app.get('serverId'));
this.app.rpc.battle.BattleRemote.kick.route(session)(roleId, this.app.get('serverId'), serverId);
// this.app.rpc.chat.chatRemote.kick.route(session, true)(session.uid, this.app.get('serverId'), session.get('rid'));
}

View File

@@ -48,4 +48,39 @@ export class GmHandler {
await EventRecordModel.deleteAccount(roleId);
return resResult(STATUS.SUCCESS);
}
async pushMessage(msg: { uid: number, serverId: number, eventName: string, message: string }, session: BackendSession) {
let sid = session.get('sid');
let { uid, serverId, eventName, message } = msg;
let content = {};
try {
content = JSON.parse(message);
} catch(e) {
return resResult(STATUS.GM_JSON_FORMAT_ERR);
}
console.log(uid, serverId)
let role = await RoleModel.findByUid(uid, serverId);
if(!role) return resResult(STATUS.GM_ROLE_NOT_FOUND);
let {roleId } = role;
let channelService = this.app.get('channelService');
let channel = channelService.getChannel(roleId, true);
let users = channel.getMembers();
if (users.indexOf(roleId) === -1) {
channel.add(roleId, sid);
}
let tsid = channel.getMember(roleId)['sid'];
channelService.pushMessageByUids(eventName, {
msg: content
}, [{
uid: roleId,
sid: tsid
}]);
return resResult(STATUS.SUCCESS, { msg: content });
}
}

View File

@@ -5,7 +5,7 @@
import { FrontendSession, RemoterClass } from 'pinus';
import { ChatRemote } from './chat/remote/chatRemote';
import { ComBattleRemote } from './battle/remote/comBattleRemote';
import { EventBattleRemote } from './battle/remote/eventBattleRemote';
import { BattleRemote } from './battle/remote/battleRemote';
declare global {
interface UserRpc {
@@ -14,7 +14,7 @@ declare global {
};
battle: {
comBattleRemote: RemoterClass<FrontendSession, ComBattleRemote>;
eventBattleRemote: RemoterClass<FrontendSession, EventBattleRemote>;
BattleRemote: RemoterClass<FrontendSession, BattleRemote>;
};
}
}

View File

@@ -3,7 +3,7 @@ import { getGamedata } from '../pubUtils/gamedata';
import EventRecord, { EventRecordModel } from '../db/EventRecord';
import { RoleModel } from '../db/Role';
import { genCode, decodeStrSingle, decodeStr, getRandomWithWeight, resResult, setLocalHours } from '../pubUtils/util';
import { EVENT_STATUS, EVENT_RECORD_STATUS, EVENT_TYPE, EVENT_RANDOM_TYPE_ONE_OPEN, EVENT_QUIZ_NUM, EVENT_ANSWER_STATUS } from '../consts/consts';
import { EVENT_STATUS, EVENT_RECORD_STATUS, EVENT_TYPE, EVENT_RANDOM_TYPE_ONE_OPEN, EVENT_QUIZ_NUM, EVENT_ANSWER_STATUS, FUNCS_ID } from '../consts/consts';
import { EVENT_REFRESH_NUM } from '../consts/consts';
import { STATUS } from '../consts/statusCode';
@@ -87,7 +87,8 @@ export async function startEvent(app: Application, session: FrontendOrBackendSes
// console.log('*******setEventStatus')
let roleId = session.get('roleId');
let roleName = session.get('roleName');
let channelName = roleId;
let serverId = session.get('serverId');
let channelName = `server-${serverId}`;
let event = await refreshEvent(1, roleId, roleName, 0, []); // 刷新初始的一件
await RoleModel.setEventStatus(roleId, EVENT_STATUS.STARTING);
session.set('eventStatus', EVENT_STATUS.STARTING);
@@ -108,25 +109,31 @@ export async function checkEvent(app: Application, session: FrontendOrBackendSes
try {
let roleId = session.get('roleId');
let serverId = session.get('serverId');
let funcs = session.get('funcs');
if(roleId) {
let roleName = session.get('roleName');
let channelName = roleId;
let channelName = `server-${serverId}`;
let eventStatus = session.get('eventStatus')||EVENT_STATUS.WAITING;
let eventTime = session.get('getEventTime')||0;
let now = new Date();
let t = getEventTime(now);
let channel = app.get('channelService').getChannel(channelName, false);
if(!!channel && (eventTime < t || isForce)) { // 第一次登陆后可以刷新了
let event = await getEvent(eventStatus, roleId, roleName);
if (eventStatus == EVENT_STATUS.STARTING || eventStatus == EVENT_STATUS.OPEN) {
pushEventMsg(app, roleId, channelName, { event }); // 推送
session.set('getEventTime', t);
session.push('getEventTime', () => {});
if(funcs && funcs.includes(FUNCS_ID.EVENT) && eventStatus == EVENT_STATUS.WAITING) {
await startEvent(app, session);
} else {
let eventTime = session.get('getEventTime')||0;
let now = new Date();
let t = getEventTime(now);
let channel = app.get('channelService').getChannel(channelName, false);
if(!!channel && (eventTime < t || isForce)) { // 第一次登陆后可以刷新了
let event = await getEvent(eventStatus, roleId, roleName);
if (eventStatus == EVENT_STATUS.STARTING || eventStatus == EVENT_STATUS.OPEN) {
pushEventMsg(app, roleId, channelName, { event }); // 推送
session.set('getEventTime', t);
session.push('getEventTime', () => {});
}
}
}
}

View File

@@ -0,0 +1,35 @@
import { Application, BackendSession, FrontendSession } from "pinus";
import { getGamedata } from "../pubUtils/gamedata";
import Role, { RoleModel } from "../db/Role";
import { FUNC_OPT_TYPE } from "../consts/consts";
// 开启功能
export async function switchOnFunc(roleId: string, type: number, param: number, app: Application, session: (BackendSession|FrontendSession)) {
const serverId = session.get('serverId');
const dataFuncs = session.get('funcs');
const dicFuncSwitch = getGamedata('dic_func_switch');
let funcs = new Array<{id: number, desc: string, script: string}>(), addFuncs = new Array<number>();
for(let obj of dicFuncSwitch) {
if(obj.conditionType == type && !dataFuncs.includes(obj.id) && obj.param == param) {
funcs.push({id: obj.id, desc: obj.desc, script: obj.script});
addFuncs.push(obj.id);
}
}
let channelService = app.get('channelService');
let channel = channelService.getChannel(`server-${serverId}`, false);
if(!!channel && funcs.length > 0) {
let tsid = channel.getMember(roleId)['sid'];
channelService.pushMessageByUids('onFuncSwitchOn', {funcs}, [{
uid: roleId,
sid: tsid
}]);
}
const recs = await RoleModel.pushFuncs(roleId, addFuncs);
session.set('funcs', recs.funcs||[]);
session.push('funcs', () => {});
return recs
}

View File

@@ -3,8 +3,11 @@ import { HeroModel } from '../db/Hero';
import Role, { RoleModel } from '../db/Role'
import { getLvByExp, getExpByLv } from '../pubUtils/gamedata';
import { redisUserInfoUpdate } from './redisService';
import { switchOnFunc } from './funcSwitchService';
import { FUNC_OPT_TYPE } from '../consts/consts';
import { Application, BackendSession } from 'pinus';
export async function roleLevelup(roleId: string, kingExp: number) {
export async function roleLevelup(roleId: string, kingExp: number, app: Application, session: BackendSession) {
let role = await RoleModel.findByRoleId(roleId);
let {lv = 1, exp = 0} = role;
let newExp = exp + kingExp;
@@ -15,8 +18,9 @@ export async function roleLevelup(roleId: string, kingExp: number) {
newExp = curExpObj.sum;
}
await RoleModel.levelup(roleId, newLv, newExp);
role = await RoleModel.levelup(roleId, newLv, newExp);
if(newLv > lv) { // 升级
await switchOnFunc(roleId, FUNC_OPT_TYPE.LEVEL_UP, newLv, app, session);
await redisUserInfoUpdate(roleId, [{field: 'lv', value: newLv}])
}
let actordata = [];

View File

@@ -32,7 +32,7 @@ export async function initAllRank() {
}
export async function initRank(serverId: number) {
console.log('*****', 'initRank')
// console.log('*****', 'initRank')
await redisExpire(getKeyName(REDIS_KEY.TOWER_RANK, serverId), 30 * 24 * 60 * 60);
await redisExpire(REDIS_KEY.USER_INFO, 30 * 24 * 60 * 60);

View File

@@ -292,3 +292,12 @@ export const REDIS_KEY = {
USER_INFO: "userInfo", // 玩家缓存信息
TOWER_RANK: "towerRank" // 天梯排行榜
}
export const FUNC_OPT_TYPE = {
LEVEL_UP: 1,
BATTLE_END: 2
}
export const FUNCS_ID = {
EVENT: 1
}

View File

@@ -121,5 +121,6 @@ export const STATUS = {
GM_ROLE_NOT_FOUND: { code: 60010, simStr: '未找到该玩家' },
GM_HERO_NOT_FOUND: { code: 60011, simStr: '未找到武将' },
GM_PVP_DEFENSE_NOT_FOUND: { code: 60012, simStr: '该玩家未保存防守阵' },
GM_PVP_DEFENSE_HERO_NOT_FOUND: { code: 60013, simStr: '该守阵没有该武将' }
GM_PVP_DEFENSE_HERO_NOT_FOUND: { code: 60013, simStr: '该守阵没有该武将' },
GM_JSON_FORMAT_ERR: { code: 60005, simStr: 'json格式错误' }
}

View File

@@ -119,6 +119,9 @@ export default class Role extends BaseModel {
@prop({ required: true })
loginTime: Date; // 更新 / 登录时间
@prop({ required: true, type: Number, default: [] })
funcs: Array<number>; // 开启了的功能
// 天梯相关
@prop({ required: true, default: 1 })
towerLv: number; // 天梯当前层数
@@ -366,6 +369,12 @@ export default class Role extends BaseModel {
}
}
}
// 保存开启功能
public static async pushFuncs(roleId: string, funcs: Array<number>, lean = true) {
const recs = await RoleModel.findOneAndUpdate({roleId}, {$push: {funcs: {$each: funcs}}}, {new: true}).lean(lean);
return recs
}
}
export const RoleModel = getModelForClass(Role);

View File

@@ -0,0 +1,9 @@
[
{
"id": 1,
"desc": "奇遇",
"conditionType": 1,
"param": 1,
"script": "&"
}
]