ChatModel.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // 聊天模块
  2. import Gamecfg from "../../common/gameCfg";
  3. import { gameMethod } from "../../common/gameMethod";
  4. import { ChatHistory, ChatHistoryPram, ChatSend, ChatSendPram } from "../../common/Xyc";
  5. import { ChannelType, ChatInfo, SevBack, SevBackType } from "../../common/Xys";
  6. import { RedUtil } from "../../frameWork/fgui/RedUtil";
  7. import EventMng from "../../manager/EventMng";
  8. import GameMath from "../../utils/GameMath";
  9. import { ChatEvent } from "../const/EventConst";
  10. import { OpenType, RedUtilType, TeamChallengeType } from "../const/TypeConst";
  11. import GameDataCenter from "../GameDataCenter";
  12. import IDataModel from "../../frameWork/model/IDataModel";
  13. export default class ChatModel extends IDataModel {
  14. // chatCd: number = 0 // 下一次可聊天的时间戳
  15. private _lastId: Map<ChannelType, number> = new Map();
  16. private _chatNewId: Map<ChannelType, number> = new Map(); // 最新的一条消息ID
  17. chatCd: number = 0;
  18. refreshCd: number = 0;
  19. readonly getChatNum: number = 10; //每次获取的,聊天信息的数量
  20. readonly historyMinCd: number = 3;
  21. readonly sendChatMinCd: number = 3;
  22. curChannel: ChannelType;
  23. get lastId() {
  24. return this._lastId.get(this.curChannel);
  25. }
  26. set lastId(id: number) {
  27. this._lastId.set(this.curChannel, id);
  28. }
  29. get chatNewId() {
  30. return this._chatNewId.get(this.curChannel);
  31. }
  32. set chatNewId(id: number) {
  33. this._chatNewId.set(this.curChannel, id);
  34. }
  35. constructor() {
  36. super("chat");
  37. GameDataCenter.setRspModel("chat", this);
  38. GameDataCenter.setRspModel("zhuli", this);
  39. }
  40. doEvent(result: SevBack): void {
  41. if (result.chat) {
  42. if (result.chat.club) {
  43. EventMng.emit(ChatEvent.UP_CHAT_INFO, ChannelType.club, result.chat);
  44. }
  45. if (result.chat.kua) {
  46. EventMng.emit(ChatEvent.UP_CHAT_INFO, ChannelType.kua, result.chat);
  47. }
  48. if (result.chat.hefu) {
  49. EventMng.emit(ChatEvent.UP_CHAT_INFO, ChannelType.hefu, result.chat);
  50. }
  51. if (result.chat.zudui) {
  52. // 红点检测
  53. RedUtil.updateRed(RedUtilType.TeamChat)
  54. EventMng.emit(ChatEvent.UP_CHAT_INFO, ChannelType.zudui, result.chat);
  55. }
  56. if (result.chat.zhaomu) {
  57. EventMng.emit(ChatEvent.UP_CHAT_INFO, ChannelType.zhaomu, result.chat, true);
  58. }
  59. }
  60. if (result.zhuli?.u?.list) {
  61. EventMng.emit(ChatEvent.UP_ZHULI_INFO);
  62. }
  63. }
  64. // checkChatRed() {
  65. // if (GameDataCenter.page.checkOpen(OpenType.chat)) {
  66. // let chatList = Object.values(GameDataCenter.sevBack.chat.kua.a);
  67. // if (gameMethod.isEmpty(chatList)) {
  68. // return false;
  69. // }
  70. // let sevNewId = chatList[chatList.length - 1].id;
  71. // return sevNewId > this.chatNewId;
  72. // }
  73. // return false;
  74. // }
  75. /**
  76. * 获取聊天信息,如果
  77. * @param hdcid
  78. * @param cb1
  79. * @returns
  80. */
  81. sendGetChatHistory(hdcid: ChannelType, cb1?: (sev: SevBack) => void) {
  82. GameDataCenter.chat.refreshCd = GameDataCenter.time.sevTime + this.historyMinCd;
  83. let param: ChatHistoryPram = {
  84. hdcid: hdcid,
  85. lastId: this.lastId,
  86. };
  87. this.send(ChatHistory.url, param, cb1);
  88. }
  89. checkCanGetHistory(): boolean {
  90. // 本地没历史信息
  91. if (this.refreshCd > GameDataCenter.time.sevTime) {
  92. return false;
  93. }
  94. let list = GameDataCenter.sevBack.chat?.[this.curChannel]?.a ?? {};
  95. for (const key in list) {
  96. this.lastId = list[key].id;
  97. break;
  98. }
  99. if (this.lastId <= 1 || this.lastId == null) {
  100. return false;
  101. }
  102. return true;
  103. }
  104. // 发送聊天信息
  105. sendChat(hdcid: ChannelType, str: string, type: string = "1", isSys: boolean = false, cb: Function = () => { }) {
  106. let param: ChatSendPram = {
  107. hdcid: hdcid,
  108. type: type,
  109. str: str,
  110. isSys: isSys,
  111. };
  112. this.chatCd = GameDataCenter.time.sevTime + this.sendChatMinCd;
  113. this.send(ChatSend.url, param, (result: SevBack) => {
  114. if (result.type == SevBackType.success) {
  115. cb();
  116. }
  117. this.chatCd = GameDataCenter.time.sevTime + 10;
  118. });
  119. }
  120. checkHistory() {
  121. let list = Object.keys(GameDataCenter.sevBack.chat?.[this.curChannel]?.a ?? {});
  122. return list.length < this.getChatNum;
  123. }
  124. //检测是否可发送聊天
  125. checkCanSendChat() {
  126. let chatCz = this.getChatCz();
  127. let iscz = this.getIsCz();
  128. return iscz >= chatCz;
  129. }
  130. //获取聊天需要充值金额
  131. getChatCz(bl: boolean = false) {
  132. let chatCz = GameDataCenter.login.playerInfo?.switch?.chatCz ?? 0;
  133. let chatCzBl = GameDataCenter.login.playerInfo?.switch?.chatCzBl ?? 1;
  134. if (bl) {
  135. return chatCz * chatCzBl;
  136. }
  137. return chatCz;
  138. }
  139. //获取用户实付金额
  140. getIsCz(bl: boolean = false) {
  141. let iscz = GameDataCenter.sevBack?.userInfo?.a?.chatNum ?? 0;
  142. let chatCzBl = GameDataCenter.login.playerInfo?.switch?.chatCzBl ?? 1;
  143. if (bl) {
  144. return iscz * chatCzBl;
  145. }
  146. return iscz;
  147. }
  148. /**获得聊天同服 */
  149. getServerList() {
  150. let sid = GameDataCenter.user?.userInfo?.sid;
  151. let serverList = GameDataCenter.login.playerInfo?.qufuList;
  152. let heid = serverList?.[sid]?.heid;
  153. let list: string[] = []
  154. for (let key in serverList) {
  155. if (serverList[key].heid == heid) {
  156. list.push(serverList[key].sid);
  157. }
  158. }
  159. return list;
  160. }
  161. getZhuLiInfoById(id: string) {
  162. let info = GameDataCenter.sevBack?.zhuli?.a?.list?.[id];
  163. return info;
  164. }
  165. getTeamInfoById(id: string) {
  166. let info = GameDataCenter.sevBack?.zudui?.a?.[id];
  167. return info;
  168. }
  169. showTime(time: number): string {
  170. let dhms = this.getHM(time);
  171. let tTime: string = `${GameMath.addZero(dhms.h)}:${GameMath.addZero(dhms.m)}`;
  172. return tTime;
  173. }
  174. getHM(time: number) {
  175. if (time.toString().length == 10) {
  176. time = time * 1000
  177. }
  178. var date = new Date(time);
  179. var hour = date.getHours()
  180. var minute = date.getMinutes()
  181. return { h: hour, m: minute };
  182. }
  183. checkChatZuduiRed() {
  184. let chat = GameDataCenter.sevBack.chat?.[ChannelType.zudui]?.a
  185. if (gameMethod.isEmpty(chat)) {
  186. return false
  187. }
  188. let list: ChatInfo[] = Object.values(chat ?? {});
  189. let sevNew = list[list.length - 1];
  190. //如果是自己发的,不显示红点
  191. if (sevNew.fuuid == GameDataCenter?.sevBack?.userInfo?.a?.uuid) {
  192. return false
  193. }
  194. return sevNew.id > GameDataCenter.chat.chatNewId
  195. }
  196. }