pubgv3/assets/scripts/manages/chatManage.js
guoqing.zhu f5075e3450 update
2022-05-23 16:05:01 +08:00

268 lines
8.6 KiB
JavaScript

// Learn cc.Class:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
// - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
// - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
// - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
var charserver = require("chatrsp");
var playerData = require("playerData");
var NetManage = require("NetManage");
cc.Class({
extends: cc.Component,
properties: {
// foo: {
// // ATTRIBUTES:
// default: null, // The default value will be used only when the component attaching
// // to a node for the first time
// type: cc.SpriteFrame, // optional, default is typeof default
// serializable: true, // optional, default is true
// },
// bar: {
// get () {
// return this._bar;
// },
// set (value) {
// this._bar = value;
// }
// },
},
// LIFE-CYCLE CALLBACKS:
onLoad() {
cc.chatMgr = this;
},
initengine(offical, opt) {
this.chat = new charserver();
this.chat.initengine(offical, true);
this.bindcallback();
this.chat.loginparma = opt;
this.haslongin = false;
this.tmpmsg = [];
// this.sendmsg("CMLogin",{
// account_id:opt.account_id,
// session_id:opt.session_id,
// nickname:opt.nickname,
// avatar_url:opt.avatar_url,
// sex:opt.sex
// })
},
sendmsg(msg, parma) {
var flag = true;
//console.log("ChatMsg=====222",msg,parma);
if (this[msg]) {
flag = this[msg](parma);
}
if (flag) {
if (this.haslongin) {
this.chat.sendmsg(msg, parma);
} else {
this.tmpmsg.push({ m: msg, p: parma });
}
}
},
CMFriendApply(parma) {
var date = new Date();
this.time =
Number(playerData.invitecd / 1000) -
(date.getTime() / 1000 - playerData.friendtime);
let uid = parma.friend_id;
let tmpState = playerData.checkAtFriendApplyArr(uid);
if (tmpState == 1) {
cc.uiHelper.showTips(
cc.language.stringformat("nottimeaddsameplayer")
);
return false;
} else {
cc.uiHelper.showTips(
cc.language.stringformat("friend request send!")
);
playerData.AddfriendApplyArr(uid);
}
// if (playerData.inviteaccountid == parma.friend_id && this.time > 0) {
// return false
// } else {
// playerData.inviteaccountid = parma.friend_id
// playerData.friendtime = date.getTime() / 1000
// }
return true;
},
onSMFriendList(msg) {
playerData.friend_list = msg.friend_list;
cc.Notifier.emit("friendlist", msg);
},
SMFriendIdList(msg) {
playerData.friends = msg.friends;
playerData.blacklist = msg.blacklist;
},
onSMFriendAgree(msg) {},
SMCustomMsgNotify(msg) {
var noti = JSON.parse(msg.msg);
let tmpState = 0;
if (noti["type"] == 1 && !playerData.inteam && playerData.inviteflag) {
playerData.teamuuid = noti["msg"];
playerData.invite_name = noti["name"];
playerData.invite_ava = noti["avatar_url"];
playerData.invite_sex = noti["sex"];
tmpState = 1;
cc.Notifier.emit("friendinvite");
}
if (noti["type"] == 2 && playerData.guild_id == "0") {
if (
(playerData.invite_guild_id == "" &&
playerData.invite_guild_sign == "") ||
this.compareInviteid(playerData.invite_guild_id, noti["msg"])
) {
var msgbox = {
id: noti["msg"],
name: noti["name"],
sign: noti["sign"],
};
playerData.invite_guild_id = noti["msg"];
playerData.invite_guild_name = noti["name"];
playerData.invite_guild_sign = noti["sign"];
tmpState = 1;
cc.Notifier.emit("guildinvite", msgbox);
}
}
console.log("自定义数据======02FromChatManager==", tmpState, noti);
if (tmpState == 0) {
cc.Notifier.emit("SMCustomMsgNotify", msg);
}
},
compareInviteid(v1, v2) {
if (v1.high != v2.high && v1.low != v2.low) {
return true;
} else {
return false;
}
},
SMUpdateRedPointNotify(msg) {
console.log(msg.red_point_flags);
if (this.istrue(0, msg.red_point_flags)) {
playerData.friend_red = 1;
} else {
playerData.friend_red = 0;
}
cc.Notifier.emit("showFriendRedPoint");
if (this.istrue(1, msg.red_point_flags)) {
playerData.guild_red = 1;
} else {
playerData.guild_red = 0;
}
cc.Notifier.emit("showGuildRedPoint");
if (this.istrue(2, msg.red_point_flags)) {
playerData.chatred = true;
} else {
playerData.chatred = false;
}
cc.Notifier.emit("refreshchatred");
},
istrue(num, flag) {
if ((flag & (1 << num)) != 0) {
return true;
} else {
return false;
}
},
SMLogin(msg) {
cc.openGuild = true;
if (msg.errcode == 0) {
this.haslongin = true;
this.account_info = msg.account_info;
playerData.guild_id =
this.account_info.user_info.base_data["guild_id"];
console.log("公会ID=0222========", playerData.guild_id);
if (playerData.guild_id == "0") {
playerData.isguild = false;
} else {
playerData.isguild = true;
playerData.guild_job =
this.account_info.user_info.base_data["guild_job"];
playerData.guild_sign =
this.account_info.user_info.base_data["user_sign"];
}
}
},
SMFriendBlackList(msg) {
playerData.black_list = msg.black_list;
cc.Notifier.emit("blacklist", msg);
},
SMShowErrorMsg(msg) {
cc.uiHelper.showTips(msg.msg);
},
onnetclose() {
this.haslongin = false;
},
SMGuildCreate(msg) {
if (!msg.errcode) {
playerData.guild_id = msg.guild_id;
playerData.isguild = true;
cc.uiHelper.showTips(
cc.language.stringformat("createguildsuccess")
);
NetManage.updateDiamond();
cc.Notifier.emit("SMGuildCreate", msg);
} else {
cc.uiHelper.showTips(msg.errmsg);
}
},
bindcallback() {
this.chat.bindrecvMsg({
SMFriendList: this.onSMFriendList.bind(this),
SMCustomMsgNotify: this.SMCustomMsgNotify.bind(this),
SMUpdateRedPointNotify: this.SMUpdateRedPointNotify.bind(this),
SMLogin: this.SMLogin.bind(this),
SMFriendBlackList: this.SMFriendBlackList.bind(this),
SMShowErrorMsg: this.SMShowErrorMsg.bind(this),
SMFriendIdList: this.SMFriendIdList.bind(this),
SMGuildCreate: this.SMGuildCreate.bind(this),
});
this.chat.bindclose(this.onnetclose.bind(this));
},
update(dt) {
if (this.chat) {
this.chat.update(dt);
}
if (this.haslongin && this.tmpmsg.length > 0) {
for (var i = 0; i < this.tmpmsg.length; i++) {
this.chat.sendmsg(this.tmpmsg[i].m, this.tmpmsg[i].p);
}
this.tmpmsg.length = 0;
}
},
checkreconnectNet() {
if (this.chat) {
this.chat.m_chat.checkreconnectNet();
}
},
close() {
if (this.chat) {
this.chat.close();
}
},
});