pubgv3/assets/scripts/netBattle/battlenetmanage.js
2022-05-31 16:44:54 +08:00

606 lines
15 KiB
JavaScript

var jsMatchvs = require("jcmatchvs");
// var jcmsghandler = require("jcmsghandler");
var SDKManage = require("SDKManage");
var NetManage = require("NetManage");
var wxvoice = require("wxVoice");
var jcfwlogin = require("jcfw").login;
var clientserver = require("clientserver");
var Main = require("Main");
cc.mytimescale = 1;
cc.zscale = 1;
var battlrsp = function () {
this.msgpool = [];
this.connecting = false;
this.netclosecb = null;
this.netlinkcb = null;
this.renetlinkcb = null;
this.functionarr = {};
this.init = function (offical, team_uuid) {
if (!this.m_chat) {
this.m_chat = new jsMatchvs();
this.m_chat.team_uuid = team_uuid;
this.m_chat.init(offical, this);
} else {
this.m_chat.team_uuid = team_uuid;
this.m_chat.connectNet();
}
this.m_chat.reconnectcount = 0;
};
this.disconnect = function () {
this.m_chat.disconnect();
};
this.initResponse = function () {
for (var k in this.fcb) {
var k2 = this.m_chat.s2cMsgfcb["_" + k];
this.functionarr[k2] = this.fcb[k];
}
this.msgfcb = {};
for (var k in this.m_chat.s2cMsgfcb) {
var vk = k.slice(1, k.length);
this.msgfcb[this.m_chat.s2cMsgfcb[k]] = vk;
}
this.pversion = this.m_chat.pversion;
this.m_chat.connectNet();
};
this.netResponse = function (res) {
if (res.eventtype == "connect") {
this.connecting = true;
if (this.netlinkcb) {
this.netlinkcb();
this.netlinkcb = null;
} else {
this.renetlinkcb();
}
} else if (res.eventtype == "close" || res.eventtype == "reconnect") {
this.connecting = false;
if (res.eventtype == "close") {
cc.director.emit("SOCKETCLOSE");
}
this.netclosecb && this.netclosecb();
}
};
this.recvMsg = function (mid, msg) {
if (mid == 101) {
return;
}
if (this.functionarr[mid]) {
this.functionarr[mid](msg);
} else {
cc.Notifier.emit(this.msgfcb[mid], msg);
}
};
this.sendmsg = function (k, p) {
// this.msgpool.push({
// k: "_" + k,
// p: p
// })
this.m_chat.sengMsg("_" + k, p);
};
// this.update = function(dt) {
// if (this.connecting) {
// if (this.msgpool.length > 0) {
// var msg = this.msgpool.shift()
// this.m_chat.sengMsg(msg.k, msg.p)
// }
// }
// }
this.bindrecvMsg = function (v) {
this.fcb = v;
};
this.bindclose = function (v) {
this.netclosecb = v;
};
this.bindconnect = function (v) {
this.netlinkcb = v;
};
this.bindreconnect = function (v) {
this.renetlinkcb = v;
};
};
var NetWorkManage = function () {
this.frames = [];
this.tankarr = [];
this.online = false;
this.engine = null;
this.initIng = false;
this.AliveCount = 0;
this.reclickcount = 0;
this.battlecount = 0;
this.clean = function () {
this.frames.length = 0;
this.tankarr.length = 0;
this.online = false;
this.joined = false;
this.AliveCount = 0;
if (this.engine) {
this.engine.disconnect();
}
if (this.clients) {
this.clients.clean();
this.clients = null;
}
};
this.onJoinRsp = function (res) {
if (res.error_code != 0) {
this.initIng = false;
this.clean();
cc.director.emit("SOCKETCLOSE");
return;
}
SDKManage.logEvent("joinroom", "joinroom");
this.joined = true;
// this.uuid = res.player_id
// this.roomid = res.room_uuid
// this.timeNow = new Date().getTime()
// this.server_info = res.server_info
cc.Notifier.emit("onJoinRsp");
};
this.onGameOverRsp = function (res) {
if (cc.gameMgr && cc.gameMgr.uic) {
cc.gameMgr.uic.netGameOver(res);
}
};
this.onVoiceNotifyRsp = function (res) {
wxvoice.play(res.download_url);
};
this.onUpdateRsp = function (res) {
this.frames.push(res);
};
this.onWatchWar = function (res) {
cc.Notifier.emit("onWatchWar", res);
};
this.onSMRollMsg = function (msg) {
if (cc.gameMgr && cc.gameMgr.uic) {
cc.gameMgr.uic.showbattletip(msg);
}
};
this.onSMLeave = function (msg) {
if (cc.gameMgr && cc.gameMgr.uic) {
cc.gameMgr.uic.gotohome();
}
};
this.onSMUiUpdate = function (msg) {
if (cc.gameMgr && cc.gameMgr.uic) {
cc.gameMgr.uic.refreshAlive(msg);
cc.gameMgr.uic.refreshTank(msg.car_list);
} else {
this.tankarr = msg.car_list;
this.AliveCount = msg.alive_count;
}
};
this.onSMWxVoip = function (msg) {
return;
this.getVoipSign(msg.group_id);
};
this.changescene = function () {
cc.uiHelper.showLoading();
setTimeout(() => {
cc.battleCache.beforebattle();
cc.director.loadScene("GameScene");
}, 1000);
cc.Notifier.emit("changescene");
};
this.onMapInfoRsp = function (msg) {
cc.mapid = msg.map_id;
// this.joined = true
this.uuid = msg.player_id;
this.roomid = msg.room_uuid;
this.timeNow = new Date().getTime();
this.server_info = msg.server_info;
this.changescene();
};
this.onSMGameStart = function (msg) {
if (cc.gameMgr) {
cc.gameMgr.dogamestart();
}
};
this.onDebugMsg = function (msg) {
if (!cc.battledebug) {
return;
}
console.log("[onDebugMsg]: " + msg.debug_msg);
};
this.bindcallback = function () {
this.engine.bindrecvMsg({
SMJoinedNotify: this.onJoinRsp.bind(this),
SMMapInfo: this.onMapInfoRsp.bind(this),
SMUpdate: this.onUpdateRsp.bind(this),
SMVoiceNotify: this.onVoiceNotifyRsp.bind(this),
SMGameOver: this.onGameOverRsp.bind(this),
SMWatchWar: this.onWatchWar.bind(this),
SMDebugMsg: this.onDebugMsg.bind(this),
SMRollMsg: this.onSMRollMsg.bind(this),
SMLeave: this.onSMLeave.bind(this),
SMUiUpdate: this.onSMUiUpdate.bind(this),
SMWxVoip: this.onSMWxVoip.bind(this),
SMGameStart: this.onSMGameStart.bind(this),
SMReconnect: this.onSMReconnect.bind(this),
});
this.engine.bindclose(this.onnetclose.bind(this));
};
this.onSMReconnect = function (res) {
this.recing = true;
console.log(res);
if (res.errcode != 0) {
this.clean();
cc.director.emit("SOCKETCLOSE");
}
};
this.onnetclose = function () {
this.online = false;
};
this.onconnectnormal = function () {
SDKManage.logEvent("scoket_connect", "socket connect");
this.online = true;
this.joinroom();
var self = this;
setTimeout(() => {
if (self.joined) {
return;
}
self.initIng = false;
self.clean();
cc.director.emit("SOCKETCLOSE");
}, 5000);
};
this.onconnectreconnect = function () {
this.online = true;
this.recing = false;
this.engine.sendmsg("CMReconnect", {
server_id: 1,
team_uuid: this.opt.team_uuid,
account_id: this.opt.account_id,
session_id: NetManage.session_id,
room_uuid: this.roomid,
server_info: this.server_info,
});
var self = this;
setTimeout(() => {
if (self.recing) {
return;
}
cc.director.emit("SOCKETCLOSE");
}, 5000);
};
this.setopt = function (opt) {
return {
server_id: 1,
proto_version: this.engine.pversion,
account_id: opt.account_id,
name: cc.playerData.name,
avatar_url: cc.playerData.head_id,
head_frame: Number(cc.playerData.head_frame),
baseskin: opt.baseskin,
energy_shield: opt.energy_shield,
team_uuid: opt.team_uuid,
weapons: opt.weapons,
skins: opt.skins,
prepare_items: opt.prepare_items,
session_id: NetManage.session_id,
atk_add: opt.atk_add,
pre_settlement_info: opt.pre_settlement_info,
auto_fill: opt.auto_fill,
today_enter_times: opt.today_enter_times,
prepare_items2: opt.prepare_items2,
grow_weapons: opt.grow_weapons,
force_entry_newbie_room: opt.force_entry_newbie_room,
team_members: opt.team_members,
room_mode: opt.room_mode,
guild_id: opt.guild_id,
vip_lv: opt.vip_lv,
sex: opt.sex,
mapid: opt.mapid,
user_data: opt.user_data,
skill_list: opt.skill_list,
hero_id: opt.hero_id,
hero_uniid: opt.hero_uniid,
show_team_ui: opt.show_team_ui,
hero_skin: opt.hero_skin,
team_mode: opt.show_team_ui,
// hero_uniid: opt.hero_uniid,
// weapon_uniid: opt.weapon_uniid,
};
};
this.joinroom = function () {
var opt = this.opt;
cc.skinid = opt.baseskin[0];
opt.weapons = opt.weapon_uniid;
console.log("opts:" + JSON.stringify(this.opt));
if (window.team_uuid) {
if (typeof window.team_uuid == "string") {
opt.team_uuid = window.team_uuid;
} else {
opt.team_uuid = window.team_uuid.team_uuid;
}
}
const msg = this.setopt(this.opt);
console.log(msg);
this.engine.sendmsg("CMJoin", msg);
if (opt.team_uuid) {
if (cc.sys.os == cc.sys.OS_ANDROID) {
jsb.reflection.callStaticMethod(
"org/cocos2dx/javascript/AppActivity",
"joinRoom",
"(Ljava/lang/String;Ljava/lang/String;)V",
opt.team_uuid,
opt.account_id
);
} else if (cc.sys.os == cc.sys.OS_IOS) {
jsb.reflection.callStaticMethod(
"AppController",
"joinRoom:who:",
opt.team_uuid,
opt.account_id
);
}
}
};
this.startclient = function (opt) {
if (this.clients) {
return;
}
this.frames.length = 0;
this.tankarr.length = 0;
this.online = false;
this.joined = false;
this.AliveCount = 0;
this.battlecount = opt.battlecount;
var csv = new clientserver(this.battlecount);
csv.init(opt);
this.uuid = csv.player.obj_uniid;
this.timeNow = undefined;
this.onUpdateRsp(csv.sendupdate());
this.onMapInfoRsp({
map_id: opt.mapid,
player_id: csv.player.obj_uniid,
});
this.clients = csv;
};
this.initengine = function (opt) {
cc.hdscale = Main.config.mainConfig.viewscale;
cc.battlepause = false;
this.gamemode = opt.room_mode;
this.firstzombie = opt.firstzombie;
this.emojis = opt.emojis;
this.needbox = opt.needbox;
this.newbiemode = opt.newbiemode;
this.timedelay = opt.timedelay;
if (this.initIng || cc.uiHelper.isloading()) {
return;
}
if (opt.offline) {
this.startclient(opt);
return;
}
var self = this;
setTimeout(() => {
self.initIng = false;
}, 10000);
SDKManage.logEvent("reqbattle", "battlerequest");
this.clean();
if (!this.engine) {
this.engine = new battlrsp();
var offical = SDKManage.isoffical;
if (SDKManage.checking) {
offical = false;
}
this.offical = offical;
this.bindcallback();
}
this.engine.init(this.offical, opt.team_uuid);
this.battlecount = opt.battlecount;
cc.isFirstgame = opt.first_game;
this.engine.bindconnect(this.onconnectnormal.bind(this));
this.engine.bindreconnect(this.onconnectreconnect.bind(this));
this.opt = opt;
this.initIng = true;
};
this.move = function (pack) {
if (this.clients) {
this.clients.move(pack);
var frm = this.clients.sendupdate();
if (frm) {
this.frames.push(frm);
}
} else {
if (this.online) {
var param = {
move_dir: pack.move_dir,
attack_dir: pack.attack_dir,
shot_hold: pack.shot_hold,
interaction_objids: pack.interaction_objids,
interaction: pack.interaction,
select_weapon: pack.select_weapon,
drop_weapon: pack.drop_weapon,
use_scope: pack.use_scope,
fly_distance: pack.fly_distance,
shot_start: pack.shot_start,
reload: pack.reload,
cancel_action: pack.cancel_action,
use_item_idx: pack.use_item_idx,
use_item_id: pack.use_item_id,
spectate: pack.spectate,
emote: pack.emote,
jump: pack.jump,
use_skill: pack.use_skill,
get_down: pack.get_down,
get_on: pack.get_on,
aiming: pack.aiming,
skill_dir: pack.skill_dir,
skill_target_id: pack.skill_target_id,
skill_id: pack.skill_id,
skill_distance: pack.skill_distance,
switch_seat: pack.switch_seat,
follow: pack.follow,
dive: pack.dive,
};
this.engine.sendmsg("CMMove", param);
}
}
};
this.sendVoice = function (url) {
if (this.online) {
var param = {
download_url: url,
};
this.engine.sendmsg("CMVoice", param);
}
};
this.sendGameOver = function () {
if (this.clients) {
this.clients.sendGameOver();
return;
}
if (this.online) {
this.engine.sendmsg("CMGameOver");
}
};
this.sendWatch = function () {
if (this.online) {
this.engine.sendmsg("CMWatchWar");
}
};
this.sendLeave = function () {
if (this.clients) {
cc.gameMgr.uic.gotohome();
return;
}
if (this.online) {
this.engine.sendmsg("CMLeave");
}
};
this.sendRevive = function () {
if (this.clients) {
this.clients.sendRevive();
return;
}
if (this.online) {
this.engine.sendmsg("CMRevive");
}
};
this.sendfqRevive = function () {
if (this.clients) {
this.clients.sendfqRevive();
return;
}
if (this.online) {
this.engine.sendmsg("CMCancelRevive");
}
};
this.sendBuffAdStart = function () {
if (this.online) {
this.engine.sendmsg("CMAdStart");
}
};
this.sendBuffAdCancel = function () {
if (this.online) {
this.engine.sendmsg("CMAdCancel");
}
};
this.sendBuffAdEnd = function (v) {
if (this.online) {
var param = {
param: v,
};
this.engine.sendmsg("CMAdEnd", param);
}
};
this.getVoipSign = function (gid) {
if (cc.sys.platform == cc.sys.WECHAT_GAME) {
var cb = function (res) {
if (cc.gameMgr && cc.gameMgr.uic) {
cc.gameMgr.uic.startTeamTalk(res);
}
};
jcfwlogin.getVoipSign(gid, cb);
}
};
this.dohit = function (gun, uuid, tp, pos, oid) {
if (this.clients) {
this.clients.dohit(gun, uuid, tp, pos, oid);
}
};
this.updateevent = function (dt) {
// if (this.clients) {
// this.clients.updateevent(dt)
// }
};
this.sendBaoxiang = function () {
if (this.clients) {
this.clients.dobaoxiang();
return;
}
if (this.online) {
var param = {
box_id: 1,
};
this.engine.sendmsg("CMOpenBox", param);
}
};
this.sendgm = function (str) {
if (this.online) {
this.engine.sendmsg("CMExecCommand", {
cmd: str,
});
}
};
this.sendbattlemsg = function (k, v) {
if (this.online) {
this.engine.sendmsg(k, v);
}
};
};
var instance = new NetWorkManage();
cc.battleIns = instance;
// instance.init();
module.exports = instance;