123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- // 聊天模块
- import Gamecfg from "../../common/gameCfg";
- import { gameMethod } from "../../common/gameMethod";
- import { ChatHistory, ChatHistoryPram, ChatSend, ChatSendPram } from "../../common/Xyc";
- import { ChannelType, ChatInfo, SevBack, SevBackType } from "../../common/Xys";
- import { RedUtil } from "../../frameWork/fgui/RedUtil";
- import EventMng from "../../manager/EventMng";
- import GameMath from "../../utils/GameMath";
- import { ChatEvent } from "../const/EventConst";
- import { OpenType, RedUtilType, TeamChallengeType } from "../const/TypeConst";
- import GameDataCenter from "../GameDataCenter";
- import IDataModel from "../../frameWork/model/IDataModel";
- export default class ChatModel extends IDataModel {
- // 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;
- 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");
- GameDataCenter.setRspModel("chat", this);
- GameDataCenter.setRspModel("zhuli", this);
- }
- doEvent(result: SevBack): void {
- if (result.chat) {
- if (result.chat.club) {
- EventMng.emit(ChatEvent.UP_CHAT_INFO, ChannelType.club, result.chat);
- }
- if (result.chat.kua) {
- EventMng.emit(ChatEvent.UP_CHAT_INFO, ChannelType.kua, result.chat);
- }
- if (result.chat.hefu) {
- EventMng.emit(ChatEvent.UP_CHAT_INFO, ChannelType.hefu, result.chat);
- }
- if (result.chat.zudui) {
- // 红点检测
- RedUtil.updateRed(RedUtilType.TeamChat)
- EventMng.emit(ChatEvent.UP_CHAT_INFO, ChannelType.zudui, result.chat);
- }
- if (result.chat.zhaomu) {
- EventMng.emit(ChatEvent.UP_CHAT_INFO, ChannelType.zhaomu, result.chat, true);
- }
- }
- if (result.zhuli?.u?.list) {
- EventMng.emit(ChatEvent.UP_ZHULI_INFO);
- }
- }
- // checkChatRed() {
- // if (GameDataCenter.page.checkOpen(OpenType.chat)) {
- // let chatList = Object.values(GameDataCenter.sevBack.chat.kua.a);
- // if (gameMethod.isEmpty(chatList)) {
- // return false;
- // }
- // let sevNewId = chatList[chatList.length - 1].id;
- // return sevNewId > this.chatNewId;
- // }
- // return false;
- // }
- /**
- * 获取聊天信息,如果
- * @param hdcid
- * @param cb1
- * @returns
- */
- sendGetChatHistory(hdcid: ChannelType, cb1?: (sev: SevBack) => void) {
- GameDataCenter.chat.refreshCd = GameDataCenter.time.sevTime + this.historyMinCd;
- let param: ChatHistoryPram = {
- hdcid: hdcid,
- lastId: this.lastId,
- };
- this.send(ChatHistory.url, param, cb1);
- }
- checkCanGetHistory(): boolean {
- // 本地没历史信息
- if (this.refreshCd > GameDataCenter.time.sevTime) {
- return false;
- }
- let list = GameDataCenter.sevBack.chat?.[this.curChannel]?.a ?? {};
- for (const key in list) {
- this.lastId = list[key].id;
- break;
- }
- if (this.lastId <= 1 || this.lastId == null) {
- return false;
- }
- return true;
- }
- // 发送聊天信息
- sendChat(hdcid: ChannelType, str: string, type: string = "1", isSys: boolean = false, cb: Function = () => { }) {
- let param: ChatSendPram = {
- hdcid: hdcid,
- type: type,
- str: str,
- isSys: isSys,
- };
- this.chatCd = GameDataCenter.time.sevTime + this.sendChatMinCd;
- this.send(ChatSend.url, param, (result: SevBack) => {
- if (result.type == SevBackType.success) {
- cb();
- }
- this.chatCd = GameDataCenter.time.sevTime + 10;
- });
- }
- checkHistory() {
- let list = Object.keys(GameDataCenter.sevBack.chat?.[this.curChannel]?.a ?? {});
- return list.length < this.getChatNum;
- }
- //检测是否可发送聊天
- checkCanSendChat() {
- let chatCz = this.getChatCz();
- let iscz = this.getIsCz();
- return iscz >= chatCz;
- }
- //获取聊天需要充值金额
- getChatCz(bl: boolean = false) {
- let chatCz = GameDataCenter.login.playerInfo?.switch?.chatCz ?? 0;
- let chatCzBl = GameDataCenter.login.playerInfo?.switch?.chatCzBl ?? 1;
- if (bl) {
- return chatCz * chatCzBl;
- }
- return chatCz;
- }
- //获取用户实付金额
- getIsCz(bl: boolean = false) {
- let iscz = GameDataCenter.sevBack?.userInfo?.a?.chatNum ?? 0;
- let chatCzBl = GameDataCenter.login.playerInfo?.switch?.chatCzBl ?? 1;
- if (bl) {
- return iscz * chatCzBl;
- }
- return iscz;
- }
- /**获得聊天同服 */
- 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;
- }
- getZhuLiInfoById(id: string) {
- let info = GameDataCenter.sevBack?.zhuli?.a?.list?.[id];
- return info;
- }
- getTeamInfoById(id: string) {
- let info = GameDataCenter.sevBack?.zudui?.a?.[id];
- return info;
- }
- 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 };
- }
- checkChatZuduiRed() {
- let chat = GameDataCenter.sevBack.chat?.[ChannelType.zudui]?.a
- if (gameMethod.isEmpty(chat)) {
- return false
- }
- let list: ChatInfo[] = Object.values(chat ?? {});
- let sevNew = list[list.length - 1];
- //如果是自己发的,不显示红点
- if (sevNew.fuuid == GameDataCenter?.sevBack?.userInfo?.a?.uuid) {
- return false
- }
- return sevNew.id > GameDataCenter.chat.chatNewId
- }
- }
|