123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- import { UserData } from "./UserData";
- interface Mail {
- id: string;
- sys: number;
- expire: string;
- read: string;//读取状态
- awards: any;//奖励附件
- }
- class Data {
- private _mails: { [id: string]: Mail } = {};
- private _mailMaxIdTab: { [sys: number]: number } = { 0: 0, 1: 0 };
- init(): void {
- this.reset();
- }
- reset(): void {
- this._mails = {};
- this._mailMaxIdTab = { 0: 0, 1: 0 };
- }
- purge(): void {
- this.reset();
- }
- setData(data: Mail[]): void {
- if (data) {
- for (const v of data) {
- this._mails[v.id] = v;
- }
- for (const v of data) {
- if (parseInt(v.id, 10) > this._mailMaxIdTab[v.sys]) {
- this._mailMaxIdTab[v.sys] = parseInt(v.id, 10);
- }
- }
- }
- }
- setMailMax(data: number[]): void {
- for (const v of data) {
- if (v > this._mailMaxIdTab[1]) {
- this._mailMaxIdTab[1] = v;
- }
- }
- }
- setNewMail(data: Mail[]): void {
- for (const v of data) {
- this._mails[v.id] = v;
- }
- for (const v of data) {
- if (parseInt(v.id, 10) > this._mailMaxIdTab[v.sys]) {
- this._mailMaxIdTab[v.sys] = parseInt(v.id, 10);
- }
- }
- }
- getData(): { [id: string]: Mail } {
- return this._mails;
- }
- getMaxMailId(type: number): number {
- return this._mailMaxIdTab[type];
- }
- orderMail(): Mail[] {
- const mailList: Mail[] = [];
- const nowTime = UserData.getServerTime();
- for (const key in this._mails) {
- const v = this._mails[key];
- // if (parseInt(v.expire, 10) >= nowTime) {
- mailList.push(v);
- // }
- }
- mailList.sort((a, b) => {
- const x = parseInt(a.read, 10) === 0 ? 1 : 0;
- const y = parseInt(b.read, 10) === 0 ? 1 : 0;
- if (x > y) {
- return -1;
- } else if (x < y) {
- return 1;
- }
- return parseInt(a.expire, 10) > parseInt(b.expire, 10) ? -1 : 1;
- });
- return mailList;
- }
- getLeftDays(time: number): number {
- let leftTime = time - UserData.getServerTime();
- leftTime = Math.ceil(leftTime / (24 * 60 * 60));
- return leftTime <= 0 ? 0 : leftTime;
- }
- haveItemToGet(): boolean {
- for (const key in this._mails) {
- const v = this._mails[key];
- if (v.awards && v.awards.length > 0) {
- return true;
- }
- }
- return false;
- }
- setMailReadById(id: string): void {
- for (const key in this._mails) {
- const v = this._mails[key];
- if (parseInt(v.id, 10) === parseInt(id, 10)) {
- v.read = '1';
- break;
- }
- }
- }
- setMailRead(ids: string[]): void {
- for (const k1 of ids) {
- for (const key in this._mails) {
- const v = this._mails[key];
- if (parseInt(v.id, 10) === parseInt(k1, 10)) {
- v.read = '1';
- }
- }
- }
- }
- haveMailNotRead(): boolean {
- const nowTime = UserData.getServerTime();
- for (const key in this._mails) {
- const v = this._mails[key];
- if (parseInt(v.expire, 10) >= nowTime && parseInt(v.read, 10) === 0) {
- return true;
- }
- }
- return false;
- }
- getShowAward(id: string): any {
- for (const key in this._mails) {
- const v = this._mails[key];
- if (parseInt(v.id, 10) === parseInt(id, 10)) {
- if (v.awards && v.awards.length > 0) {
- if (this._mails[parseInt(v.id, 10)].read === '1') {
- return null;
- } else {
- return v.awards[0];
- }
- }
- }
- }
- return null;
- }
- getMailText(content: any, isTitle: boolean): string {
- let jsonData = content;
- let text: string;
- // if (isTitle) {
- // if (typeof jsonData === 'number') {
- // text = ConfData.getMapData('maillocaltext.dat')[jsonData.toString()].Title;
- // } else {
- // text = content;
- // }
- // return Util.getString(text);
- // } else {
- // if (Array.isArray(jsonData)) {
- // const count = jsonData.length;
- // text = ConfData.getMapData('maillocaltext.dat')[jsonData[0].toString()].Desc;
- // if (count === 2) {
- // const tcConfNew = ConfData.getMapData('manorcity.dat');
- // const tcsConfNew = ConfData.getMapData('manorschedule.dat');
- // const tcConf = ConfData.getMapData('territorycity.dat');
- // const tcsConf = ConfData.getMapData('territoryschedule.dat');
- // if (parseInt(jsonData[0], 10) === 14) {
- // const str = ' Lv.' + tcConf[jsonData[1]].CityLevel + ' ' + Util.getString(tcConf[jsonData[1]].Name);
- // text = Util.getString(text).replace('%s', str);
- // } else {
- // }
- // }
- // return text;
- // } else {
- return content;
- // }
- // }
- }
- removeMail(mailList: string[]): void {
- for (const k1 of mailList) {
- for (const key in this._mails) {
- const v = this._mails[key];
- if (parseInt(v.id, 10) === parseInt(k1, 10)) {
- delete this._mails[key];
- }
- }
- }
- }
- removeMailById(id: string): void {
- for (const key in this._mails) {
- const v = this._mails[key];
- if (parseInt(v.id, 10) === parseInt(id, 10)) {
- delete this._mails[key];
- }
- }
- }
- }
- export let MailData = new Data;
|