MailData.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import { UserData } from "./UserData";
  2. interface Mail {
  3. id: string;
  4. sys: number;
  5. expire: string;
  6. read: string;//读取状态
  7. awards: any;//奖励附件
  8. }
  9. class Data {
  10. private _mails: { [id: string]: Mail } = {};
  11. private _mailMaxIdTab: { [sys: number]: number } = { 0: 0, 1: 0 };
  12. init(): void {
  13. this.reset();
  14. }
  15. reset(): void {
  16. this._mails = {};
  17. this._mailMaxIdTab = { 0: 0, 1: 0 };
  18. }
  19. purge(): void {
  20. this.reset();
  21. }
  22. setData(data: Mail[]): void {
  23. if (data) {
  24. for (const v of data) {
  25. this._mails[v.id] = v;
  26. }
  27. for (const v of data) {
  28. if (parseInt(v.id, 10) > this._mailMaxIdTab[v.sys]) {
  29. this._mailMaxIdTab[v.sys] = parseInt(v.id, 10);
  30. }
  31. }
  32. }
  33. }
  34. setMailMax(data: number[]): void {
  35. for (const v of data) {
  36. if (v > this._mailMaxIdTab[1]) {
  37. this._mailMaxIdTab[1] = v;
  38. }
  39. }
  40. }
  41. setNewMail(data: Mail[]): void {
  42. for (const v of data) {
  43. this._mails[v.id] = v;
  44. }
  45. for (const v of data) {
  46. if (parseInt(v.id, 10) > this._mailMaxIdTab[v.sys]) {
  47. this._mailMaxIdTab[v.sys] = parseInt(v.id, 10);
  48. }
  49. }
  50. }
  51. getData(): { [id: string]: Mail } {
  52. return this._mails;
  53. }
  54. getMaxMailId(type: number): number {
  55. return this._mailMaxIdTab[type];
  56. }
  57. orderMail(): Mail[] {
  58. const mailList: Mail[] = [];
  59. const nowTime = UserData.getServerTime();
  60. for (const key in this._mails) {
  61. const v = this._mails[key];
  62. // if (parseInt(v.expire, 10) >= nowTime) {
  63. mailList.push(v);
  64. // }
  65. }
  66. mailList.sort((a, b) => {
  67. const x = parseInt(a.read, 10) === 0 ? 1 : 0;
  68. const y = parseInt(b.read, 10) === 0 ? 1 : 0;
  69. if (x > y) {
  70. return -1;
  71. } else if (x < y) {
  72. return 1;
  73. }
  74. return parseInt(a.expire, 10) > parseInt(b.expire, 10) ? -1 : 1;
  75. });
  76. return mailList;
  77. }
  78. getLeftDays(time: number): number {
  79. let leftTime = time - UserData.getServerTime();
  80. leftTime = Math.ceil(leftTime / (24 * 60 * 60));
  81. return leftTime <= 0 ? 0 : leftTime;
  82. }
  83. haveItemToGet(): boolean {
  84. for (const key in this._mails) {
  85. const v = this._mails[key];
  86. if (v.awards && v.awards.length > 0) {
  87. return true;
  88. }
  89. }
  90. return false;
  91. }
  92. setMailReadById(id: string): void {
  93. for (const key in this._mails) {
  94. const v = this._mails[key];
  95. if (parseInt(v.id, 10) === parseInt(id, 10)) {
  96. v.read = '1';
  97. break;
  98. }
  99. }
  100. }
  101. setMailRead(ids: string[]): void {
  102. for (const k1 of ids) {
  103. for (const key in this._mails) {
  104. const v = this._mails[key];
  105. if (parseInt(v.id, 10) === parseInt(k1, 10)) {
  106. v.read = '1';
  107. }
  108. }
  109. }
  110. }
  111. haveMailNotRead(): boolean {
  112. const nowTime = UserData.getServerTime();
  113. for (const key in this._mails) {
  114. const v = this._mails[key];
  115. if (parseInt(v.expire, 10) >= nowTime && parseInt(v.read, 10) === 0) {
  116. return true;
  117. }
  118. }
  119. return false;
  120. }
  121. getShowAward(id: string): any {
  122. for (const key in this._mails) {
  123. const v = this._mails[key];
  124. if (parseInt(v.id, 10) === parseInt(id, 10)) {
  125. if (v.awards && v.awards.length > 0) {
  126. if (this._mails[parseInt(v.id, 10)].read === '1') {
  127. return null;
  128. } else {
  129. return v.awards[0];
  130. }
  131. }
  132. }
  133. }
  134. return null;
  135. }
  136. getMailText(content: any, isTitle: boolean): string {
  137. let jsonData = content;
  138. let text: string;
  139. // if (isTitle) {
  140. // if (typeof jsonData === 'number') {
  141. // text = ConfData.getMapData('maillocaltext.dat')[jsonData.toString()].Title;
  142. // } else {
  143. // text = content;
  144. // }
  145. // return Util.getString(text);
  146. // } else {
  147. // if (Array.isArray(jsonData)) {
  148. // const count = jsonData.length;
  149. // text = ConfData.getMapData('maillocaltext.dat')[jsonData[0].toString()].Desc;
  150. // if (count === 2) {
  151. // const tcConfNew = ConfData.getMapData('manorcity.dat');
  152. // const tcsConfNew = ConfData.getMapData('manorschedule.dat');
  153. // const tcConf = ConfData.getMapData('territorycity.dat');
  154. // const tcsConf = ConfData.getMapData('territoryschedule.dat');
  155. // if (parseInt(jsonData[0], 10) === 14) {
  156. // const str = ' Lv.' + tcConf[jsonData[1]].CityLevel + ' ' + Util.getString(tcConf[jsonData[1]].Name);
  157. // text = Util.getString(text).replace('%s', str);
  158. // } else {
  159. // }
  160. // }
  161. // return text;
  162. // } else {
  163. return content;
  164. // }
  165. // }
  166. }
  167. removeMail(mailList): void {
  168. for (const k1 in mailList) {
  169. for (const key in this._mails) {
  170. const v = this._mails[key];
  171. if (parseInt(v.id, 10) === parseInt(k1, 10)) {
  172. delete this._mails[key];
  173. }
  174. }
  175. }
  176. }
  177. removeMailById(id: string): void {
  178. for (const key in this._mails) {
  179. const v = this._mails[key];
  180. if (parseInt(v.id, 10) === parseInt(id, 10)) {
  181. delete this._mails[key];
  182. }
  183. }
  184. }
  185. }
  186. export let MailData = new Data;