123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- // 聊天模块
- import { gameMethod } from "../../../common/gameMethod";
- import EventMng from "../../../manager/EventMng";
- import { ChannelType, ResChatSend } from "../../../shared/chat/PtlChatSend";
- import GameMath from "../../../utils/GameMath";
- import { ChatEvent } from "../../const/EventConst";
- import GameDataCenter from "../../GameDataCenter";
- import { ChatModelData } from "./ChatModelData";
- export default class ChatModel extends ChatModelData {
- // chatCd: number = 0 // 下一次可聊天的时间戳
- private _lastId: Map<ChannelType, number> = new Map();
- private _chatNewId: Map<ChannelType, number> = new Map(); // 最新的一条消息ID
- chatCd: number = 0;
- refreshCd: number = 0;
- readonly getChatNum: number = 10; //每次获取的,聊天信息的数量
- readonly historyMinCd: number = 3;
- readonly sendChatMinCd: number = 3;
- curChannel: ChannelType = ChannelType.hefu;
- get lastId() {
- return this._lastId.get(this.curChannel);
- }
- set lastId(id: number) {
- this._lastId.set(this.curChannel, id);
- }
- get chatNewId() {
- return this._chatNewId.get(this.curChannel);
- }
- set chatNewId(id: number) {
- this._chatNewId.set(this.curChannel, id);
- }
- constructor() {
- super("chat");
- }
- onRegister(): void {
- GameDataCenter.gameServer.ListenMsg("chat/server/ChatNew_s", this.onChatChange, this);
- }
- onChatChange(res: ResChatSend) {
- if (gameMethod.isEmpty(this.chatInfo)) {
- this.chatInfo = {};
- }
- for (const channelType in res) {
- if (gameMethod.isEmpty(this.chatInfo[channelType])) {
- this.chatInfo[channelType] = {};
- }
- for (const xbid in res[channelType]) {
- this.chatInfo[channelType][xbid] = res[channelType][xbid];
- }
- }
- if (CC_PREVIEW) console.log("onChatChange:", this.chatInfo);
- EventMng.emit(ChatEvent.UP_CHAT_INFO)
- }
- checkCanGetHistory(): boolean {
- // 本地没历史信息
- if (this.refreshCd > GameDataCenter.time.sevTime) {
- return false;
- }
- let list = this.chatInfo ?? {};
- for (const key in list) {
- this.lastId = list?.[this.curChannel]?.[key]?.id;
- break;
- }
- if (this.lastId <= 1 || this.lastId == null) {
- return false;
- }
- return true;
- }
- // 获取聊天信息
- async sendChatHistory(channelType: ChannelType, xbid: number = 0, cb: Function = () => { }) {
- let ret = await GameDataCenter.gameServer.ReqApi("chat/ChatHistory", {
- channelType: channelType,
- xbid: xbid
- });
- if (ret?.res) {
- this.onChatChange(ret?.res);
- cb(ret.res);
- }
- }
- // 发送聊天信息
- async sendChat(channelType: ChannelType, str: string, cb: Function = () => { }) {
- let ret = await GameDataCenter.gameServer.ReqApi("chat/ChatSend", {
- channelType: channelType,
- str: str
- });
- if (ret?.res) {
- cb(ret.res);
- }
- }
- checkHistory() {
- let list = Object.keys(GameDataCenter.sevBack.chat?.[this.curChannel]?.a ?? {});
- return list.length < this.getChatNum;
- }
- /**获得聊天同服 */
- getServerList() {
- let sid = GameDataCenter.user?.userInfo?.sid;
- let serverList = GameDataCenter.login.playerInfo?.qufuList;
- let heid = serverList?.[sid]?.heid;
- let list: string[] = []
- for (let key in serverList) {
- if (serverList[key].heid == heid) {
- list.push(serverList[key].sid);
- }
- }
- return list;
- }
- showTime(time: number): string {
- let dhms = this.getHM(time);
- let tTime: string = `${GameMath.addZero(dhms.h)}:${GameMath.addZero(dhms.m)}`;
- return tTime;
- }
- getHM(time: number) {
- if (time.toString().length == 10) {
- time = time * 1000
- }
- var date = new Date(time);
- var hour = date.getHours()
- var minute = date.getMinutes()
- return { h: hour, m: minute };
- }
- }
|