335 lines
16 KiB
TypeScript
335 lines
16 KiB
TypeScript
import * as taskUtil from '../pubUtils/taskUtil';
|
|
import { RoleModel, RoleType } from '../db/Role';
|
|
import { pinus, FrontendOrBackendSession } from 'pinus';
|
|
import { resResult, shouldRefresh } from '../pubUtils/util';
|
|
import { STATUS, TASK_TYPE, TASK_FUN_TYPE, SHOP_REFRESH_TYPE, WAR_TYPE } from '../consts';
|
|
import { TaskParam, TaskListReturn } from '../domain/roleField/task';
|
|
import { EPlace, HeroType } from '../db/Hero';
|
|
import { getRoleOnlineInfo } from './redisService';
|
|
import { HeroScores } from '../db/PvpHistoryOpp';
|
|
import { ItemInter } from '../pubUtils/interface';
|
|
import { UserTaskModel, UserTaskType } from '../db/UserTask';
|
|
import { UserTaskRecModel } from '../db/UserTaskRec';
|
|
import { UserTaskHistoryModel } from '../db/UserTaskHistory';
|
|
import { gameData } from '../pubUtils/data';
|
|
import { getSeconds, getZeroPointD } from '../pubUtils/timeUtil';
|
|
import { RoleStatus } from '../db/ComBattleTeam';
|
|
import { getActivities } from './activity/activityService';
|
|
|
|
export async function checkTaskWithRoles(serverId: number, roleId: string, sid: string, taskType: number, roles: RoleType[]) {
|
|
for (let role of roles) {
|
|
if (role) {
|
|
await checkTaskWithRole(serverId, role.roleId, role.roleId == roleId ? sid : null, taskType, role);
|
|
}
|
|
}
|
|
}
|
|
|
|
export async function checkTaskWithRole(serverId: number, roleId: string, sid: string, taskType: number, role: RoleType) {
|
|
let pushMessage = await taskUtil.checkTaskWithRole(serverId, roleId, taskType, role);
|
|
pushTaskUpdate(roleId, sid, pushMessage);
|
|
}
|
|
|
|
export async function checkTaskWithHero(roleId: string, sid: string, taskType: number, hero: HeroType, args?: number[]) {
|
|
let pushMessage = await taskUtil.checkTaskWithHero(roleId, taskType, hero, args);
|
|
pushTaskUpdate(roleId, sid, pushMessage);
|
|
}
|
|
|
|
export async function checkTaskWithEplaces(roleId: string, sid: string, taskType: number, eplace: EPlace[], newEplace: EPlace[], eplaceIds: number[], params?: any) {
|
|
let pushMessage = await taskUtil.checkTaskWithEplaces(roleId, taskType, eplace, newEplace, eplaceIds, params);
|
|
pushTaskUpdate(roleId, sid, pushMessage);
|
|
}
|
|
|
|
export async function checkTaskWithEplace(roleId: string, sid: string, taskType: number, oldEquip: EPlace, newEplace: EPlace, params?: any) {
|
|
let pushMessage = await taskUtil.checkTaskWithEplace(roleId, taskType, oldEquip, newEplace, params);
|
|
pushTaskUpdate(roleId, sid, pushMessage);
|
|
}
|
|
|
|
export async function checkTaskWithArgs(roleId: string, sid: string, taskType: number, args: number[]) {
|
|
let pushMessage = await taskUtil.checkTaskWithArgs(roleId, taskType, args);
|
|
pushTaskUpdate(roleId, sid, pushMessage);
|
|
}
|
|
|
|
export async function checkTaskWithWar(roleId: string, sid: string, taskType: number, warId: number, heroes: number[], count: number, star: number) {
|
|
let pushMessage = await taskUtil.checkTaskWithWar(roleId, taskType, warId, heroes, count, star);
|
|
pushTaskUpdate(roleId, sid, pushMessage);
|
|
}
|
|
|
|
export async function checkTaskWithGoods(roleId: string, sid: string, taskType: number, goods: ItemInter[]) {
|
|
let pushMessage = await taskUtil.checkTaskWithGoods(roleId, taskType, goods);
|
|
pushTaskUpdate(roleId, sid, pushMessage);
|
|
}
|
|
|
|
export async function checkTask(roleId: string, sid: string, taskType: number, count: number, isInc: boolean, param: TaskParam) {
|
|
let pushMessage = await taskUtil.checkTask(roleId, taskType, count, isInc, param);
|
|
pushTaskUpdate(roleId, sid, pushMessage);
|
|
}
|
|
|
|
export async function pushTaskUpdate(roleId: string, sid: string, pushMessage: TaskListReturn[]) {
|
|
if (pushMessage.length > 0) {
|
|
if (!sid) {
|
|
let onlineUser = await getRoleOnlineInfo(roleId);
|
|
sid = onlineUser.sid;
|
|
}
|
|
if (!!sid) {
|
|
let uids = [{ uid: roleId, sid }];
|
|
pinus.app.get('channelService').pushMessageByUids('onTaskUpdate', resResult(STATUS.SUCCESS, pushMessage), uids);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
export async function checkActivityTask(serverId: number, sid: string, roleId: string, taskType: TASK_TYPE, count: number, parma?: any) {
|
|
let pushMessage = await taskUtil.accomplishTask(serverId, roleId, taskType, count, parma, await getActivities());
|
|
pushActivityUpdate(roleId, sid, pushMessage);
|
|
return pushMessage;
|
|
}
|
|
|
|
export async function pushActivityUpdate(roleId: string, sid: string, pushMessage: any[]) {
|
|
// console.log('pushActivityUpdate', JSON.stringify(pushMessage))
|
|
if (pushMessage?.length > 0) {
|
|
if (!sid) {
|
|
let onlineUser = await getRoleOnlineInfo(roleId);
|
|
sid = onlineUser.sid;
|
|
}
|
|
if (!!sid) {
|
|
let uids = [{ uid: roleId, sid }];
|
|
pinus.app.get('channelService').pushMessageByUids('onActivityTaskUpdate', resResult(STATUS.SUCCESS, pushMessage), uids);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* battle.normalBattleHandler.battleEnd 中会触发的任务,因为有点多提出来了
|
|
*/
|
|
export async function checkTaskInBattleEnd(serverId: number, roleId: string, sid: string, battleId: number, heroes: number[], star: number) {
|
|
|
|
await checkTaskWithWar(roleId, sid, TASK_TYPE.BATTLE_WITH_HERO, battleId, heroes, 1, star);
|
|
await checkTaskWithWar(roleId, sid, TASK_TYPE.BATTLE_MAIN, battleId, heroes, 1, star);
|
|
await checkTaskWithWar(roleId, sid, TASK_TYPE.BATTLE_DAILY_STAR, battleId, heroes, 1, star);
|
|
await checkTaskWithWar(roleId, sid, TASK_TYPE.BATTLE_DAILY, battleId, heroes, 1, star);
|
|
await checkTaskWithWar(roleId, sid, TASK_TYPE.BATTLE_DUNGEON, battleId, heroes, 1, star);
|
|
await checkTaskWithWar(roleId, sid, TASK_TYPE.BATTLE_DUNGEON_WAR, battleId, heroes, 1, star);
|
|
await checkTaskWithWar(roleId, sid, TASK_TYPE.BATTLE_TOWER, battleId, heroes, 1, star);
|
|
await checkTaskWithWar(roleId, sid, TASK_TYPE.BATTLE_VESTIGE, battleId, heroes, 1, star);
|
|
await checkTaskWithWar(roleId, sid, TASK_TYPE.BATTLE_EXPEDITION, battleId, heroes, 1, star);
|
|
//成长任务
|
|
let dicWar = gameData.war.get(battleId);
|
|
if (dicWar) {
|
|
if (dicWar.warType == WAR_TYPE.NORMAL) {
|
|
await checkActivityTask(serverId, sid, roleId, TASK_TYPE.BATTLE_MAIN, 1, { warId: battleId })
|
|
} else if (dicWar.warType == WAR_TYPE.EXPEDITION) {
|
|
await checkActivityTask(serverId, sid, roleId, TASK_TYPE.BATTLE_EXPEDITION, 1)
|
|
} else if (dicWar.warType == WAR_TYPE.MYSTERY || dicWar.warType == WAR_TYPE.MYSTERY_ELITE) {
|
|
await checkActivityTask(serverId, sid, roleId, TASK_TYPE.BATTLE_DUNGEON, 1)
|
|
await checkActivityTask(serverId, sid, roleId, TASK_TYPE.BATTLE_DUNGEON_WAR, 1, { warId: battleId })
|
|
} else if (dicWar.warType == WAR_TYPE.TOWER) {
|
|
await checkActivityTask(serverId, sid, roleId, TASK_TYPE.BATTLE_TOWER, 1)
|
|
} else if (dicWar.warType == WAR_TYPE.DAILY) {
|
|
await checkActivityTask(serverId, sid, roleId, TASK_TYPE.BATTLE_DAILY, 1, { dailyType: dicWar.dailyType })
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
export async function checkTaskInBattleSweep(serverId: number, roleId: string, sid: string, battleId: number, count: number) {
|
|
|
|
// 任务
|
|
await checkTaskWithWar(roleId, sid, TASK_TYPE.BATTLE_MAIN_SWEEP, battleId, [], count, 0);
|
|
//活动任务
|
|
await checkActivityTask(serverId, sid, roleId, TASK_TYPE.BATTLE_MAIN_SWEEP, count, { battleId })
|
|
|
|
await checkTaskWithWar(roleId, sid, TASK_TYPE.BATTLE_MAIN, battleId, [], count, 0);
|
|
await checkTaskWithWar(roleId, sid, TASK_TYPE.BATTLE_DAILY_STAR, battleId, [], count, 0);
|
|
await checkTaskWithWar(roleId, sid, TASK_TYPE.BATTLE_DAILY, battleId, [], count, 0);
|
|
await checkTaskWithWar(roleId, sid, TASK_TYPE.BATTLE_DUNGEON, battleId, [], count, 0);
|
|
await checkTaskWithWar(roleId, sid, TASK_TYPE.BATTLE_DUNGEON_WAR, battleId, [], count, 0);
|
|
await checkTaskWithWar(roleId, sid, TASK_TYPE.BATTLE_TOWER, battleId, [], count, 0);
|
|
await checkTaskWithWar(roleId, sid, TASK_TYPE.BATTLE_VESTIGE, battleId, [], count, 0);
|
|
await checkTaskWithWar(roleId, sid, TASK_TYPE.BATTLE_EXPEDITION, battleId, [], count, 0);
|
|
//成长任务
|
|
let dicWar = gameData.war.get(battleId);
|
|
if (dicWar) {
|
|
if (dicWar.warType == WAR_TYPE.NORMAL) {
|
|
await checkActivityTask(serverId, sid, roleId, TASK_TYPE.BATTLE_MAIN, count, { warId: battleId })
|
|
} else if (dicWar.warType == WAR_TYPE.EXPEDITION) {
|
|
await checkActivityTask(serverId, sid, roleId, TASK_TYPE.BATTLE_EXPEDITION, count)
|
|
} else if (dicWar.warType == WAR_TYPE.MYSTERY || dicWar.warType == WAR_TYPE.MYSTERY_ELITE) {
|
|
await checkActivityTask(serverId, sid, roleId, TASK_TYPE.BATTLE_DUNGEON, count)
|
|
await checkActivityTask(serverId, sid, roleId, TASK_TYPE.BATTLE_DUNGEON_WAR, count, { warId: battleId })
|
|
} else if (dicWar.warType == WAR_TYPE.TOWER) {
|
|
await checkActivityTask(serverId, sid, roleId, TASK_TYPE.BATTLE_TOWER, count)
|
|
} else if (dicWar.warType == WAR_TYPE.DAILY) {
|
|
await checkActivityTask(serverId, sid, roleId, TASK_TYPE.BATTLE_DAILY, count, { dailyType: dicWar.dailyType })
|
|
}
|
|
}
|
|
}
|
|
|
|
export async function checkTaskInComBattleStart(roleStatus: RoleStatus[], capId: string) {
|
|
// console.log('********', JSON.stringify(roleStatus), capId, quality)
|
|
for (let { roleId, isRobot } of roleStatus) {
|
|
if (!isRobot) {
|
|
|
|
let { serverId } = await RoleModel.findByRoleId(roleId);
|
|
|
|
if (roleId == capId && roleStatus.length > 1) { // 招募队友
|
|
await checkTask(roleId, null, TASK_TYPE.COM_BATTLE_CREATE_TEAM, 1, true, {});
|
|
} else if (roleId !== capId) { // 协助寻宝
|
|
await checkTask(roleId, null, TASK_TYPE.COM_BATTLE_ASSIST_TEAM, 1, true, {});
|
|
//活动任务
|
|
await checkActivityTask(serverId, null, roleId, TASK_TYPE.COM_BATTLE_ASSIST_TEAM, 1);
|
|
}
|
|
await checkTask(roleId, null, TASK_TYPE.COM_BATTLE, 1, true, {});
|
|
//活动任务
|
|
await checkActivityTask(serverId, null, roleId, TASK_TYPE.COM_BATTLE, 1);
|
|
}
|
|
}
|
|
}
|
|
|
|
export async function checkTaskInPvpEnd(roleId: string, sid: string, isSuccess: boolean, heroScores: HeroScores[]) {
|
|
if (isSuccess) {
|
|
await checkTask(roleId, sid, TASK_TYPE.PVP_WIN, 1, true, {});
|
|
await checkTask(roleId, sid, TASK_TYPE.PVP_WIN_SERIES, 1, true, {});
|
|
} else {
|
|
await checkTask(roleId, sid, TASK_TYPE.PVP_WIN_SERIES, 0, false, {});
|
|
}
|
|
|
|
await checkTask(roleId, sid, TASK_TYPE.PVP_HERO_SCORE, 0, false, { heroScores });
|
|
}
|
|
|
|
|
|
// 获取task状态
|
|
export async function getCurTask(roleId: string, session: FrontendOrBackendSession) {
|
|
let userTask = await UserTaskModel.findByRole(roleId);
|
|
|
|
let { dailyTaskRefWeekly, dailyTaskRef } = userTask;
|
|
let curWeekStart = getZeroPointD(SHOP_REFRESH_TYPE.WEEKLY);
|
|
if (dailyTaskRefWeekly < curWeekStart) { // 刷新周宝箱
|
|
dailyTaskRefWeekly = curWeekStart;
|
|
}
|
|
session.set('refWeekly', getSeconds(dailyTaskRefWeekly));
|
|
session.push('refWeekly', () => { });
|
|
|
|
if (shouldRefresh(dailyTaskRef, new Date())) {
|
|
dailyTaskRef = new Date();
|
|
userTask = await UserTaskModel.updateInfo(roleId, { dailyTaskRef });
|
|
await removeHistoryTask(roleId, TASK_FUN_TYPE.DAILY);
|
|
}
|
|
session.set('refDaily', getSeconds(dailyTaskRef));
|
|
session.push('refDaily', () => { });
|
|
|
|
let mainTask = await getMainTask(roleId, userTask);
|
|
let dailyTask = await getDailyTask(roleId, userTask);
|
|
let achievement = await getAchievement(roleId, userTask);
|
|
return { mainTask, dailyTask, achievement };
|
|
}
|
|
|
|
export async function getMainTask(roleId: string, userTask: UserTaskType) {
|
|
let type = TASK_FUN_TYPE.MAIN;
|
|
let { mainTaskStage: stage } = userTask;
|
|
let recMap = await UserTaskRecModel.findByRoleAndType(roleId, type); // group=>userTaskRec
|
|
|
|
let taskList: TaskListReturn[] = [];
|
|
for (let [id, dic] of gameData.mainTask) {
|
|
if (dic.taskStage == stage) {
|
|
let dbRec = recMap.get(dic.taskType)?.get(dic.group);
|
|
if (dbRec) {
|
|
taskList.push({ type, id, count: dbRec.count, received: dbRec.received.includes(id) });
|
|
} else {
|
|
taskList.push({ type, id, count: 0, received: false });
|
|
}
|
|
}
|
|
}
|
|
return { stage, taskList }
|
|
}
|
|
|
|
export async function getDailyTask(roleId: string, userTask: UserTaskType) {
|
|
let type = TASK_FUN_TYPE.DAILY;
|
|
let { dailyTaskPoint: point, dailyTaskRefWeekly, dailyTaskPointWeekly: weeklyPoint, dailyTaskBox: box } = userTask;
|
|
let curWeekStart = getZeroPointD(SHOP_REFRESH_TYPE.WEEKLY);
|
|
if (dailyTaskRefWeekly < curWeekStart) { // 刷新
|
|
dailyTaskRefWeekly = curWeekStart;
|
|
weeklyPoint = 0;
|
|
box = [];
|
|
}
|
|
let recMap = await UserTaskRecModel.findByRoleAndType(roleId, type); // group=>userTaskRec
|
|
|
|
let taskList: TaskListReturn[] = [];
|
|
for (let [id, dic] of gameData.dailyTask) {
|
|
let dbRec = recMap.get(dic.taskType)?.get(dic.group);
|
|
if (dbRec) {
|
|
taskList.push({ type, id, count: dbRec.count, received: dbRec.received.includes(id) });
|
|
} else {
|
|
taskList.push({ type, id, count: 0, received: false });
|
|
}
|
|
}
|
|
return { point, weeklyPoint, taskList, box }
|
|
}
|
|
|
|
export async function getAchievement(roleId: string, userTask: UserTaskType) {
|
|
let type = TASK_FUN_TYPE.ACHIEVEMENT;
|
|
let { achievementBox: box, achievementPoint: point } = userTask;
|
|
let recMap = await UserTaskRecModel.findByRoleAndType(roleId, type); // group=>userTaskRec
|
|
|
|
let taskList: TaskListReturn[] = [];
|
|
for (let [id, dic] of gameData.achievement) {
|
|
let dbRec = recMap.get(dic.taskType)?.get(dic.group);
|
|
if (dbRec) {
|
|
taskList.push({ type, id, count: dbRec.count, received: dbRec.received.includes(id) });
|
|
} else {
|
|
taskList.push({ type, id, count: 0, received: false });
|
|
}
|
|
}
|
|
return { point, taskList, box }
|
|
}
|
|
|
|
export async function getPvpTask(roleId: string) {
|
|
let type = TASK_FUN_TYPE.PVP;
|
|
let recMap = await UserTaskRecModel.findByRoleAndType(roleId, type); // group=>userTaskRec
|
|
|
|
let taskList: TaskListReturn[] = [];
|
|
for (let [id, dic] of gameData.pvpDailyTask) {
|
|
let dbRec = recMap.get(dic.taskType)?.get(dic.group);
|
|
if (dbRec) {
|
|
taskList.push({ type, id, count: dbRec.count, received: dbRec.received.includes(id) });
|
|
} else {
|
|
taskList.push({ type, id, count: 0, received: false });
|
|
}
|
|
}
|
|
return { taskList }
|
|
}
|
|
|
|
// 刷新每日任务
|
|
export async function refDailyTask(roleId: string, sid: string) {
|
|
let userTask = await UserTaskModel.findByRole(roleId);
|
|
let taskList = await getDailyTask(roleId, userTask);
|
|
|
|
// 转移每日任务
|
|
await removeHistoryTask(roleId, TASK_FUN_TYPE.DAILY);
|
|
|
|
let uids = [{ uid: roleId, sid }];
|
|
pinus.app.get('channelService').pushMessageByUids('onDailyTaskRefresh', resResult(STATUS.SUCCESS, { taskList }), uids);
|
|
|
|
}
|
|
|
|
export async function removeHistoryTask(roleId: string, type: number, today?: Date) {
|
|
|
|
// 转移每日任务
|
|
let history = await UserTaskRecModel.getHistoryRec(roleId, type, today);
|
|
if (history.length > 0) {
|
|
await UserTaskHistoryModel.pushUserTask(roleId, history);
|
|
await UserTaskRecModel.deleteHistory(history);
|
|
}
|
|
}
|
|
|
|
// 刷新每日宝箱数量
|
|
export async function refDailyTaskBox(roleId: string, sid: string, debug = false) {
|
|
let userTask = await UserTaskModel.refreshWeekly(roleId, debug);
|
|
if (userTask) {
|
|
let { dailyTaskPoint: point, dailyTaskPointWeekly: weeklyPoint, dailyTaskBox: box } = userTask;
|
|
let uids = [{ uid: roleId, sid }];
|
|
pinus.app.get('channelService').pushMessageByUids('onTaskBoxRefresh', resResult(STATUS.SUCCESS, {
|
|
type: TASK_FUN_TYPE.DAILY,
|
|
point, weeklyPoint, box
|
|
}), uids);
|
|
}
|
|
} |