242 lines
11 KiB
TypeScript
242 lines
11 KiB
TypeScript
import * as taskUtil from '../pubUtils/taskUtil';
|
|
import { RoleType } from '../db/Role';
|
|
import { pinus, FrontendOrBackendSession } from 'pinus';
|
|
import { resResult, shouldRefresh } from '../pubUtils/util';
|
|
import { STATUS, TASK_TYPE, TASK_FUN_TYPE } from '../consts';
|
|
import { TaskParam, TaskListReturn } from '../domain/roleField/task';
|
|
import { HeroType } from '../db/Hero';
|
|
import { EquipType } from '../db/Equip';
|
|
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 { getCurWeekDate, getSeconds } from '../pubUtils/timeUtil';
|
|
import { RoleStatus } from '../db/ComBattleTeam';
|
|
|
|
export async function checkTaskWithRoles(roleId: string, sid: string, funcs: number[], taskType: number, roles: RoleType[]) {
|
|
for (let role of roles) {
|
|
if (role) {
|
|
await checkTaskWithRole(role.roleId, role.roleId == roleId ? sid : null, role.roleId == roleId ? funcs : null, taskType, role);
|
|
}
|
|
}
|
|
}
|
|
|
|
export async function checkTaskWithRole(roleId: string, sid: string, funcs: number[], taskType: number, role: RoleType) {
|
|
let pushMessage = await taskUtil.checkTaskWithRole(roleId, taskType, role, funcs);
|
|
pushTaskUpdate(roleId, sid, funcs, pushMessage);
|
|
}
|
|
|
|
export async function checkTaskWithHero(roleId: string, sid: string, funcs: number[], taskType: number, hero: HeroType, args?: number[]) {
|
|
let pushMessage = await taskUtil.checkTaskWithHero(roleId, taskType, hero, args, funcs);
|
|
pushTaskUpdate(roleId, sid, funcs, pushMessage);
|
|
}
|
|
|
|
export async function checkTaskWithEquip(roleId: string, sid: string, funcs: number[], taskType: number, equip: EquipType, args?: number[]) {
|
|
let pushMessage = await taskUtil.checkTaskWithEquip(roleId, taskType, equip, args, funcs);
|
|
pushTaskUpdate(roleId, sid, funcs, pushMessage);
|
|
}
|
|
|
|
export async function checkTaskWithArgs(roleId: string, sid: string, funcs: number[], taskType: number, args: number[]) {
|
|
let pushMessage = await taskUtil.checkTaskWithArgs(roleId, taskType, args, funcs);
|
|
pushTaskUpdate(roleId, sid, funcs, pushMessage);
|
|
}
|
|
|
|
export async function checkTaskWithWar(roleId: string, sid: string, funcs: number[], taskType: number, warId: number, heroes: number[], count: number, star: number) {
|
|
let pushMessage = await taskUtil.checkTaskWithWar(roleId, taskType, warId, heroes, count, star, funcs);
|
|
pushTaskUpdate(roleId, sid, funcs, pushMessage);
|
|
}
|
|
|
|
export async function checkTaskWithGoods(roleId: string, sid: string, funcs: number[], taskType: number, goods: ItemInter[]) {
|
|
let pushMessage = await taskUtil.checkTaskWithGoods(roleId, taskType, goods, funcs);
|
|
pushTaskUpdate(roleId, sid, funcs, pushMessage);
|
|
}
|
|
|
|
export async function checkTask(roleId: string, sid: string, funcs: number[], taskType: number, count: number, isInc: boolean, param: TaskParam) {
|
|
let pushMessage = await taskUtil.checkTask(roleId, taskType, count, isInc, param, funcs);
|
|
pushTaskUpdate(roleId, sid, funcs, pushMessage);
|
|
}
|
|
|
|
export async function pushTaskUpdate(roleId: string, sid: string, funcs: number[], pushMessage: TaskListReturn[]) {
|
|
if (pushMessage.length > 0) {
|
|
if (!sid || !funcs) {
|
|
let onlineUser = await getRoleOnlineInfo(roleId);
|
|
sid = onlineUser.sid;
|
|
funcs = onlineUser.funcs || [];
|
|
}
|
|
if (!!sid) {
|
|
let uids = [{ uid: roleId, sid }];
|
|
pinus.app.get('channelService').pushMessageByUids('onTaskUpdate', resResult(STATUS.SUCCESS, pushMessage), uids);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* battle.normalBattleHandler.battleEnd 中会触发的任务,因为有点多提出来了
|
|
*/
|
|
export async function checkTaskInBattleEnd(roleId: string, sid: string, funcs: number[], battleId: number, heroes: number[], star: number) {
|
|
|
|
await checkTaskWithWar(roleId, sid, funcs, TASK_TYPE.BATTLE_WITH_HERO, battleId, heroes, 1, star);
|
|
await checkTaskWithWar(roleId, sid, funcs, TASK_TYPE.BATTLE_MAIN, battleId, heroes, 1, star);
|
|
await checkTaskWithWar(roleId, sid, funcs, TASK_TYPE.BATTLE_DAILY_STAR, battleId, heroes, 1, star);
|
|
await checkTaskWithWar(roleId, sid, funcs, TASK_TYPE.BATTLE_DAILY, battleId, heroes, 1, star);
|
|
await checkTaskWithWar(roleId, sid, funcs, TASK_TYPE.BATTLE_DUNGEON, battleId, heroes, 1, star);
|
|
await checkTaskWithWar(roleId, sid, funcs, TASK_TYPE.BATTLE_DUNGEON_WAR, battleId, heroes, 1, star);
|
|
await checkTaskWithWar(roleId, sid, funcs, TASK_TYPE.BATTLE_TOWER, battleId, heroes, 1, star);
|
|
await checkTaskWithWar(roleId, sid, funcs, TASK_TYPE.BATTLE_VESTIGE, battleId, heroes, 1, star);
|
|
await checkTaskWithWar(roleId, sid, funcs, TASK_TYPE.BATTLE_EXPEDITION, battleId, heroes, 1, star);
|
|
//成长任务
|
|
await taskUtil.accomplishTask(roleId, TASK_TYPE.BATTLE_MAIN, 1, { warId: battleId })
|
|
|
|
}
|
|
|
|
export async function checkTaskInComBattleEnd(roleStatus: RoleStatus[], capId: string, quality: number) {
|
|
console.log('********', JSON.stringify(roleStatus), capId, quality)
|
|
for (let { roleId, isRobot } of roleStatus) {
|
|
if(!isRobot) {
|
|
if (roleId == capId && roleStatus.length > 1) { // 招募队友
|
|
await checkTask(roleId, null, null, TASK_TYPE.COM_BATTLE_CREATE_TEAM, 1, true, {});
|
|
} else if (roleId !== capId) { // 协助寻宝
|
|
await checkTask(roleId, null, null, TASK_TYPE.COM_BATTLE_ASSIST_TEAM, 1, true, {});
|
|
}
|
|
await checkTask(roleId, null, null, TASK_TYPE.COM_BATTLE, 1, true, {});
|
|
await checkTask(roleId, null, null, TASK_TYPE.COM_BATTLE_QUALITY, 1, true, { quality });
|
|
}
|
|
}
|
|
}
|
|
|
|
export async function checkTaskInPvpEnd(roleId: string, sid: string, funcs: number[], isSuccess: boolean, heroScores: HeroScores[]) {
|
|
await checkTask(roleId, sid, funcs, TASK_TYPE.PVP, 1, true, {});
|
|
if (isSuccess) {
|
|
await checkTask(roleId, sid, funcs, TASK_TYPE.PVP_WIN, 1, true, {});
|
|
await checkTask(roleId, sid, funcs, TASK_TYPE.PVP_WIN_SERIES, 1, true, {});
|
|
} else {
|
|
await checkTask(roleId, sid, funcs, TASK_TYPE.PVP_WIN_SERIES, 0, false, {});
|
|
}
|
|
|
|
await checkTask(roleId, sid, funcs, 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 = getCurWeekDate(1, 5);
|
|
if (dailyTaskRefWeekly < curWeekStart) { // 刷新周宝箱
|
|
dailyTaskRefWeekly = curWeekStart;
|
|
}
|
|
session.set('refWeekly', getSeconds(dailyTaskRefWeekly));
|
|
session.push('refWeekly', () => { });
|
|
|
|
if (shouldRefresh(dailyTaskRef, new Date(), 5)) {
|
|
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 = getCurWeekDate(1, 5);
|
|
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 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);
|
|
}
|
|
} |