ChatModel.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // 聊天模块
  2. import { gameMethod } from "../../../common/gameMethod";
  3. import EventMng from "../../../manager/EventMng";
  4. import { ChannelType, ResChatSend } from "../../../shared/chat/PtlChatSend";
  5. import GameMath from "../../../utils/GameMath";
  6. import { ChatEvent } from "../../const/EventConst";
  7. import GameDataCenter from "../../GameDataCenter";
  8. import { ChatModelData } from "./ChatModelData";
  9. export default class ChatModel extends ChatModelData {
  10. // chatCd: number = 0 // 下一次可聊天的时间戳
  11. private _lastId: Map<ChannelType, number> = new Map();
  12. private _chatNewId: Map<ChannelType, number> = new Map(); // 最新的一条消息ID
  13. chatCd: number = 0;
  14. refreshCd: number = 0;
  15. readonly getChatNum: number = 10; //每次获取的,聊天信息的数量
  16. readonly historyMinCd: number = 3;
  17. readonly sendChatMinCd: number = 3;
  18. curChannel: ChannelType = ChannelType.hefu;
  19. get lastId() {
  20. return this._lastId.get(this.curChannel);
  21. }
  22. set lastId(id: number) {
  23. this._lastId.set(this.curChannel, id);
  24. }
  25. get chatNewId() {
  26. return this._chatNewId.get(this.curChannel);
  27. }
  28. set chatNewId(id: number) {
  29. this._chatNewId.set(this.curChannel, id);
  30. }
  31. constructor() {
  32. super("chat");
  33. }
  34. onRegister(): void {
  35. GameDataCenter.gameServer.ListenMsg("chat/server/ChatNew_s", this.onChatChange, this);
  36. }
  37. onChatChange(res: ResChatSend) {
  38. if (gameMethod.isEmpty(this.chatInfo)) {
  39. this.chatInfo = {};
  40. }
  41. for (const channelType in res) {
  42. if (gameMethod.isEmpty(this.chatInfo[channelType])) {
  43. this.chatInfo[channelType] = {};
  44. }
  45. for (const xbid in res[channelType]) {
  46. this.chatInfo[channelType][xbid] = res[channelType][xbid];
  47. }
  48. }
  49. if (CC_PREVIEW) console.log("onChatChange:", this.chatInfo);
  50. EventMng.emit(ChatEvent.UP_CHAT_INFO)
  51. }
  52. checkCanGetHistory(): boolean {
  53. // 本地没历史信息
  54. if (this.refreshCd > GameDataCenter.time.sevTime) {
  55. return false;
  56. }
  57. let list = this.chatInfo ?? {};
  58. for (const key in list) {
  59. this.lastId = list?.[this.curChannel]?.[key]?.id;
  60. break;
  61. }
  62. if (this.lastId <= 1 || this.lastId == null) {
  63. return false;
  64. }
  65. return true;
  66. }
  67. // 获取聊天信息
  68. async sendChatHistory(channelType: ChannelType, xbid: number = 0, cb: Function = () => { }) {
  69. let ret = await GameDataCenter.gameServer.ReqApi("chat/ChatHistory", {
  70. channelType: channelType,
  71. xbid: xbid
  72. });
  73. if (ret?.res) {
  74. this.onChatChange(ret?.res);
  75. cb(ret.res);
  76. }
  77. }
  78. // 发送聊天信息
  79. async sendChat(channelType: ChannelType, str: string, cb: Function = () => { }) {
  80. let ret = await GameDataCenter.gameServer.ReqApi("chat/ChatSend", {
  81. channelType: channelType,
  82. str: str
  83. });
  84. if (ret?.res) {
  85. cb(ret.res);
  86. }
  87. }
  88. checkHistory() {
  89. let list = Object.keys(GameDataCenter.sevBack.chat?.[this.curChannel]?.a ?? {});
  90. return list.length < this.getChatNum;
  91. }
  92. /**获得聊天同服 */
  93. getServerList() {
  94. let sid = GameDataCenter.user?.userInfo?.sid;
  95. let serverList = GameDataCenter.login.playerInfo?.qufuList;
  96. let heid = serverList?.[sid]?.heid;
  97. let list: string[] = []
  98. for (let key in serverList) {
  99. if (serverList[key].heid == heid) {
  100. list.push(serverList[key].sid);
  101. }
  102. }
  103. return list;
  104. }
  105. showTime(time: number): string {
  106. let dhms = this.getHM(time);
  107. let tTime: string = `${GameMath.addZero(dhms.h)}:${GameMath.addZero(dhms.m)}`;
  108. return tTime;
  109. }
  110. getHM(time: number) {
  111. if (time.toString().length == 10) {
  112. time = time * 1000
  113. }
  114. var date = new Date(time);
  115. var hour = date.getHours()
  116. var minute = date.getMinutes()
  117. return { h: hour, m: minute };
  118. }
  119. }