/** * 邮件管理器 */ import GameConf from "../../conf/GameConf"; import GameUtil from "../core/GameUtil"; import Launch from "../core/Launch"; import SKDataUtil from "../gear/SKDataUtil"; import Player from "../object/Player"; import { EMailState, MsgCode } from "../role/EEnum"; import DB from "../utils/DB"; import MailData from "./MailData"; export default class MailMgr { static shared: MailMgr = new MailMgr(); timer: any; system: MailData[] = []; launch() { if(!GameConf.mailEnabled){ return; } let self = this; DB.getSystemMail(GameUtil.serverId, (error: any, rows: any[]) => { if (error) { throw new Error(`系统邮件:读档异常!`); } self.system = []; for (let row of rows) { let mail = new MailData(); mail.parseDB(row); self.system.push(mail); } let className: string = self.constructor.name; Launch.shared.complete(className); }); } // 加入系统邮件 addSystemMail(mailData: MailData) { if(!GameConf.mailEnabled){ return; } mailData.server_id = GameUtil.serverId; mailData.mail_id = GameUtil.nextId(); mailData.date = new Date(); let self = this; this.saveMail(mailData, (code: MsgCode, msg: string) => { if (code == MsgCode.SUCCESS) { self.system.push(mailData); } }); } // 获得指定邮件 getMailBy(mailId: number): MailData { let result: MailData; for (let mail of this.system) { if (mail.mail_id == mailId) { result = mail.clone(); break; } } return result; } // 存档 saveMail(mailData: MailData, callback: (code: MsgCode, msg: string) => void) { DB.saveSystemMail(mailData, callback); } // 登录玩家检查拉取邮件 check(player: Player, list: MailData[]): MailData[] { let result: MailData[] = []; let mailIds: number[] = []; for (let mail of list) { mailIds.push(mail.mail_id); } for (let target of this.system) { // 系统邮件已删除或已领取跳过 if (target.state != EMailState.UN_GET) { continue; } // 已拉取跳过 if (SKDataUtil.atRange(target.mail_id, mailIds)) { continue; } // 如果是指定邀请码的玩家则领取 if (target.invite.length > 0) { if (target.invite == player.invite) { result.push(target.clone()); } continue; } // 如果是指定玩家邮件则领取 if (target.role_id != 0) { if (target.role_id == player.roleid) { result.push(target.clone()); target.state = EMailState.GET; } continue; } // 全部玩家可领取 result.push(target.clone()); continue; } return result; } }