guoqing.zhu 7d1b9146dd update
2022-05-30 19:46:41 +08:00

294 lines
6.5 KiB
JavaScript

var NetManage = require('NetManage');
var Utils = require('Utils');
var battlenet = require('battlenetmanage');
var SDKManage = require('SDKManage');
cc.Class({
extends: cc.Component,
properties: {
lb_first: {
default: null,
type: cc.Label,
},
lb_second: {
default: null,
type: cc.Label,
},
nd_zhaomu: {
default: null,
type: cc.Node,
},
nd_pipei: {
default: null,
type: cc.Node,
},
nd_players: {
default: [],
type: cc.Node,
},
btn_start: {
default: null,
type: cc.Node,
},
friendList:{
default:null,
type:cc.Node,
},
content_friend:{
default:null,
type:cc.Node,
},
friendScrollview:{
default:null,
type:cc.ScrollView,
},
pb_oneFriend:{
default:null,
type:cc.Prefab,
}
},
// LIFE-CYCLE CALLBACKS:
// onLoad () {},
onLoad() {
this.zhaomutime = 0;
this.m_curTime = 0;
this.m_curSeconds = 0;
this.updateteamtime = 0;
this.step = 0;
cc.Notifier.on(
'SMUpdateMatchInfo',
this,
this.SMUpdateMatchInfo.bind(this)
);
cc.director.on('SOCKETCLOSE', this.socketCloseCallBack, this);
this.btn_start.active = false;
cc.Notifier.on("friendlist", this, this.getFriendList.bind(this));
cc.Notifier.on("inviteplayer", this, this.onCloseFriendList.bind(this));
},
onDestroy() {
cc.playerData.team_list = null;
cc.Notifier.off('SMUpdateMatchInfo', this);
cc.director.off('SOCKETCLOSE', this);
cc.Notifier.off("friendlist", this, this.getFriendList.bind(this));
cc.playerData.teamuuid = null;
},
initdata(teamuuid) {
this.nd_pipei.active = false;
if (teamuuid) {
this.nd_zhaomu.active = true;
this.teamuuid = teamuuid;
cc.playerData.teamuuid = this.teamuuid;
window.team_uuid = this.teamuuid;
} else {
NetManage.createTeam((res) => {
this.nd_zhaomu.active = true;
this.teamuuid = res.team_uuid;
cc.playerData.teamuuid = this.teamuuid;
window.team_uuid = this.teamuuid;
});
}
cc.Notifier.emit('closechat');
},
startpipei() {
this.nd_zhaomu.active = false;
this.nd_pipei.active = true;
this.step = 1;
var teamlist = cc.playerData.team_list;
var team_members = [];
for (var i = 0; i < teamlist.length; i++) {
team_members.push({
account_id: teamlist[i].account_id,
});
}
cc.playerData.changeHero(30100);
var opt = cc.playerData.getbattleopt();
opt.team_uuid = this.teamuuid.team_uuid;
opt.team_mode = 1;
opt.team_members = team_members;
battlenet.initengine(opt);
},
SMUpdateMatchInfo(_data) {
var data = _data.info;
this.lb_first.string = Utils.second3Time(data.predict_time);
this.refreshwait(data.members);
if (data.phase == 2) {
cc.uiMain.callbattlechosehero({
teamuuid: this.teamuuid,
});
this.node.destroy();
return;
}
},
refreshwait(data) {
for (var i = 0; i < 4; i++) {
var name = '';
var headid = '';
var frameid = '';
if (data[i]) {
name = data[i].name;
headid = data[i].head_id;
if (!headid) {
headid = data[i].avatar_url;
}
frameid = data[i].head_frame;
}
this.nd_players[i].getComponent('iconset').initdata({
name: name,
headid: headid,
head_frame: frameid,
});
}
},
onclickpipei() {
if (this.teamuuid) {
NetManage.teamStartGame(this.teamuuid);
}
},
onclickclose() {
if (this.teamuuid) {
NetManage.leaveTeam(this.teamuuid);
this.node.destroy();
}
},
socketCloseCallBack() {
this.node.destroy();
},
btnClose() {
// sendBattlemsg(key,value)
cc.battleIns.sendbattlemsg('CMMatchCancel');
setTimeout(() => {
cc.battleIns.clean();
}, 100);
this.node.destroy();
},
onclickzhaomu() {
var nowtime = new Date().getTime() / 1000;
if (nowtime - this.zhaomutime < 30) {
cc.uiHelper.showTips('Recruiting, please wait');
return;
}
this.zhaomutime = nowtime;
let battleobj = {
tp: 1,
text: 'Hey,come and play~',
title: 'Rank - Multiplayer',
roomid: this.teamuuid,
};
let tmpData = {
chat_channel: 1,
target: '',
msg_type: 1,
msg_body: JSON.stringify(battleobj),
};
cc.chatMgr.sendmsg('CMSendChatMsg', tmpData);
cc.uiHelper.showTips('Send recruitment');
},
onclicktalk() {
cc.uiMain.callChat();
},
onOpenFriendList(){
this.friendList.active = true;
cc.chatMgr.sendmsg("CMFriendList");
},
setToNone() {
this.content_friend.destroyAllChildren();
this.friendScrollview.stopAutoScroll();
this.friendScrollview.scrollToTop();
},
initOne(data, tmpData = {}) {
const node = cc.instantiate(this.pb_oneFriend);
node.getComponent("SingleFriendList").setToInvite();
node.getComponent("SingleFriendList").init(data);
if (tmpData) node.getComponent("SingleFriendList").saveData(tmpData);
this.content_friend.addChild(node);
},
getFriendList(msg) {
this.setToNone();
this.friend_list = [];
for (var m = 0; m < msg.friend_list.length; m++) {
this.friend_list.push(msg.friend_list[m]);
}
for (var i = this.friend_list.length - 1; i >= 0; i--) {
if (
this.friend_list[i].base_data["account_id"] ==
SDKManage.account_id
) {
this.friend_list.splice(i, 1);
break;
}
}
for (let i = 0; i < this.friend_list.length; i += 1) {
var data={
player:cc.playerData.name,
teaminfo:this.team_info
}
this.initOne(this.friend_list[i].base_data, data);
}
},
onCloseFriendList(){
this.friendList.active = false;
},
updateteaminfo(v) {
console.log(v);
if (v.errcode == 0) {
var info = v.team_info;
this.team_info = v.team_info;
this.btn_start.active =
info.member_list[0].account_id == SDKManage.account_id;
this.refreshwait(info.member_list);
cc.playerData.team_list = info.member_list;
if (info.state == 1) {
this.startpipei();
}
}
},
update(dt) {
if (this.step == 0) {
if (this.teamuuid) {
this.updateteamtime -= dt;
if (this.updateteamtime <= 0) {
NetManage.getTeamInfo(
this.teamuuid,
this.updateteaminfo.bind(this)
);
this.updateteamtime = 1;
}
}
return;
}
this.m_curTime += dt;
if (this.m_curTime > 1) {
this.m_curTime = 0;
this.m_curSeconds++;
this.lb_second.string = Utils.second3Time(this.m_curSeconds);
}
},
});