emulator/fc/js/jcmatchvs.js
2019-03-05 17:48:36 +08:00

1542 lines
48 KiB
JavaScript

JCMsgHandler = function(){
this.init=function(handler){
this._handobj = handler;
this.msglst = [];
this.deltatime = 0;
this.delaytime = 500;
this.rate = 60;
}
this.run=function (dt){
var self = this;
this.msglst.forEach(element => {
element.delaycount--;
if(element.delaycount <= 0){
self._handobj && self._handobj.handleData && self._handobj.handleData(element);
element.handled = true;
}
});
var i = this.msglst.length;
while(i--){
if(this.msglst[i].handled){
this.msglst.splice(i,1);
}
}
}
this.pushMsg =function(obj){
var msg = this.msgclone(obj);
var subobj = JSON.parse(msg.content);
var tm = subobj.timestamp + this.deltatime;
var d = new Date();
msg.delaycount = Math.ceil((d.getTime() - tm) / this.rate);
msg.handled = false;
this.msglst.push(msg);
}
this.msgclone=function(obj) {
// Handle the 3 simple types, and null or undefined
if (null == obj || "object" != typeof obj) return obj;
// Handle Date
if (obj instanceof Date) {
var copy = new Date();
copy.setTime(obj.getTime());
return copy;
}
// Handle Array
if (obj instanceof Array) {
var copy = [];
var len = obj.length;
for (var i = 0; i < len; ++i) {
copy[i] = this.msgclone(obj[i]);
}
return copy;
}
// Handle Object
if (obj instanceof Object) {
var copy = {};
for (var attr in obj) {
if (obj.hasOwnProperty(attr)) copy[attr] = this.msgclone(obj[attr]);
}
return copy;
}
throw new Error("Unable to copy obj! Its type isn't supported.");
}
this.setDeltaTime=function(dt){
this.deltatime = dt;
}
this.setFramerate=function(rt){
this.rate = rt;
}
};
MsgEnum = function(){
this.c2sMsg = function(){
return this.msg.CMMessageId_e;
}
this.s2cMsg = function(){
return this.msg.SMMessageId_e;
}
this.downloadFile=function(fileName, registrationHandler) {
var ajax = new XMLHttpRequest();
ajax.onload = registrationHandler;
ajax.open("GET", "./" + fileName, true);
ajax.responseType = "text";
ajax.overrideMimeType("text/plain; charset=x-user-defined");
ajax.send(null);
}
this.init=function(cb){
this.msg = null;
var protoFile = "./resources/messages.proto";
this.downloadFile(protoFile, function ( res){
if(res.target.status===200){
var Builder = ProtoBuf.protoFromString(res.target.response);
this.msg = Builder.build("kingsomevs");
cb && cb();
}
}.bind(this));
}
};
MsgObj =function(arrbuf, len){
var handbuf = new Uint8Array(arrbuf);
this._id = handbuf[2] | ((handbuf[3] << 8) & 0xFF);
var msgbuf = handbuf.subarray(8, 8+len);
this.u8buf = new Uint8Array(len);
this.u8buf.set(msgbuf);
this.id=function(){
return this._id;
}
this.data=function(){
return this.u8buf.buffer;
}
};
const BUFF_SIZE = 1024;
MsgBuffer =function(){
this.init=function(){
this.BUFF_SZ = BUFF_SIZE;
this.buffer = new ArrayBuffer(BUFF_SIZE);
this.handrecvbuf = new Uint8Array(this.buffer);
this.usedlen = 0;
}
this.pushData=function(buf){
if(!buf || !buf.byteLength || buf.byteLength <= 0){
return;
}
var len = buf.byteLength;
while (this.usedlen + len >= this.BUFF_SZ){
console.log("msgbuffer enlarge *2!" + this.BUFF_SZ + "|" + this.usedlen);
this.BUFF_SZ *= 2;
var newBuffer = new ArrayBuffer(this.BUFF_SZ);
if(this.usedlen>0){
var oldbuf = new Uint8Array(this.buffer);
var newbuf = new Uint8Array(newBuffer);
var subold = oldbuf.subarray(this.usedlen);
newbuf.set(subold);
}
this.buffer = newBuffer;
this.handrecvbuf = new Uint8Array(this.buffer);
}
var subbuf = this.handrecvbuf.subarray(this.usedlen);
var hbuf = new Uint8Array(buf);
subbuf.set(hbuf);
this.usedlen += len;
}
this.hasPackMsg=function(){
if(this.usedlen < 8){//len(2byte) + id(2byte) + code(4byte)
return false;
}
var handbuf = this.handrecvbuf;
var num = handbuf[0] | ((handbuf[1] << 8));
if(this.usedlen < num + 8){
return false;
}
return true;
}
this.popPackMsg=function(){
var handbuf = this.handrecvbuf;
var num = handbuf[0] | ((handbuf[1] << 8));
var id = handbuf[2] | ((handbuf[3] << 8));
var msg = new MsgObj(handbuf.buffer, num);
// console.log("[popPackMsg]:"+num+","+id);
//start update param
var n = num + 8;
if(this.usedlen > n){
this.buffer.copyWithin(0, n, this.usedlen);
}
this.usedlen -= n;
return msg;
}
this.buildPackMsg=function(msgid, arrbuf){
var len = arrbuf? arrbuf.byteLength: 0;
var num = len + 8;
var bytebuf = new Uint8Array(num);
bytebuf[0] = len > 0? len & 0xFF: 0;
bytebuf[1] = len > 0? (len >> 8) & 0xFF: 0;
bytebuf[2] = msgid & 0xFF;
bytebuf[3] = (msgid >> 8) & 0xFF;
bytebuf[4] = 0xAA;
bytebuf[5] = 0xCC;
bytebuf[6] = 0xBB;
bytebuf[7] = 0xAA;
//var handbuf = new Uint16Array(bytebuf.buffer);
//handbuf[0] = num;
//handbuf[1] = msgid;
//var dwordbuf = new Uint32Array(handbuf.buffer);
//dwordbuf[1] = 0xFFCCAABB;
if(arrbuf){
var oldbuf = new Uint8Array(arrbuf);
bytebuf.set(oldbuf, 8);
}
return bytebuf.buffer;
}
}
MessageVo = function(){
this.update= function (mId, proto, cb) {
this.msgId = mId;
this.protoData = proto;
this.callBack = cb;
return this;
}
};
Socket = function(callback, hosturl){
//this._super();
this._sock = null;
this._cb = callback;
this._host = hosturl;
this._sendqueue = [];
this._isclosed = false;
var self = this;
if(typeof(WebSocket) != 'undefined'){
//cocos creator websocket
this._sock = new WebSocket(hosturl);
this._sock.binaryType = 'arraybuffer';
this._sock.onmessage = function (event) {
//console.log("socket on message");
self._cb.onMessage && self._cb.onMessage(event.data);
};
this._sock.onopen = function (event) {
console.log("Create the socket is success :"+self._host );
while (self._sendqueue.length > 0) {
self._sock.send(self._sendqueue.pop());
}
self._cb.onOpen && self._cb.onOpen(self._host);
};
this._sock.onclose = function (e) {
self._isclosed = true;
self._cb.onClose && self._cb.onClose(self._host,e);
console.log("socket on closed ,code:"+e.code+"(1000:NORMAL,1005:CLOSE_NO_STATUS,1006:RESET,1009:CLOSE_TOO_LARGE) msg:"+e.reason);
};
this._sock.onerror = function (event) {
console.log("socket on error ,event:"+JSON.stringify(event));
self._cb.onError && self._cb.onError(self._host,event);
if(!self._isclosed){
self.close();
}
};
}
this.send=function(msg){
var n = this._sock.readyState;
//console.log("sock readyState:" + n);
if(n == WebSocket.CONNECTING){
this._sendqueue.push(msg);
}else if(n == WebSocket.OPEN){
this._sock.send(msg);
}else{
this._sock.onclose({code:-n});
}
}
this.close=function(){
//todo:
this._sock && this._sock.close();
}
}
WS = function(){
this.url =""
this.sock = null;
this.setCallback=function(cb){
this.cb = cb;
}
// LIFE-CYCLE CALLBACKS:
// onLoad () {},
this.start=function () {
}
// update (dt) {},
this.setHost=function(url){
this.url = url;
}
this.connect=function(){
if(this.sock){
this.sock = null;
}
this.sock = new Socket(this, this.url);
}
this.send=function(msg){
this.sock && this.sock.send(msg);
}
this.disconnect=function(){
this.sock && this.sock.close();
}
this.onOpen=function(url){
this.cb && this.cb.onConnect();
}
this.onClose=function(url, errevent){
this.cb && this.cb.onClose(errevent);
}
this.onError=function(url, errevent){
this.cb && this.cb.onError(errevent);
}
this.onMessage=function(msg){
this.cb && this.cb.onRecv(msg);
}
}
// var MessageBuffer = require('msgbuffer');
// var MessageVo = require('messageVo');
// var WS = require('webclient');
//var Base64 = require('base64');
MsgManager = function(){
this.init=function() {
this._sendMsgQueue = {};
this._recvMsgQueue = {};
this._ws = new WS();
this._ws.setCallback(this);
this._buf = new MsgBuffer();
this._buf.init();
this._isconnected = false;
this._netcb = null;
}
this.initSendMsg=function(msgId, proto) {
var vo = new MessageVo();
vo.update(msgId, proto, null);
this._sendMsgQueue[msgId] = vo;
}
this.initRecvMsg=function(msgId, proto, callBack) {
var vo = new MessageVo();
vo.update(msgId, proto, callBack);
this._recvMsgQueue[msgId] = vo;
}
this.sendMsg=function(msgId, msg) {
var msgVo = this._sendMsgQueue[msgId];
//console.log(JSON.stringify(msgVo));
var data = msg? msgVo.protoData.encode(msg).toArrayBuffer():null;
var pkgdata = this._buf.buildPackMsg(msgId, data);
if(pkgdata && this._ws){
//console.log(Base64.arrayBufferToBase64(pkgdata));
//console.log("[sendmsg]:"+msgId + ":" + msg);
this._ws.send(pkgdata);
}
}
this.parseMsg=function(msgId, msgByte) {
// console.log("信息回调id:" + msgId+ ":"+msgByte);
var msgVo = this._recvMsgQueue[msgId];
if(msgVo == undefined){
console.log("[undefined msg]"+msgId);
return;
}
var msg = msgVo.protoData.decode(msgByte);
if(msgVo.callBack) {
//统一处理errorcode
if(msg && msg.errorcode){
//TODO 统一处理errorcode
//1:根据errorcode获取对应的文本信息
//2:提示给用户
}
msgVo.callBack(msg);
}
}
this.isConnected=function(){
return this._isconnected;
}
this.connectNet= function(host, cb){
console.log("[connectNet]");
this._netcb = cb;
//this._ws.setHost(host);
this._ws.url = host;
this._ws.connect();
}
this.closeNet= function(){
this._ws.disconnect();
}
this.onConnect=function(){
this._isconnected = true;
if(this._netcb){
this._netcb("connect", 0);
}
}
this.onClose=function(err){
this._isconnected = false;
if(this._netcb){
var n = err.code? err.code: 0;
this._netcb("close", n);
}
}
this.onError=function(err){
this._isconnected = false;
if(this._netcb){
var n = err.code? err.code: 0;
this._netcb("error", n);
}
}
this.onRecv=function(msg){
// console.log("onrecv");
this._buf.pushData(msg);
while(this._buf.hasPackMsg()){
var msg = this._buf.popPackMsg();
this.parseMsg(msg.id(), msg.data());
}
}
}
// var MsgMan = require('./net/msgmanager');
// var MsgIDEnum = require('./net/msgenum');
// var ProtoBuf = require("./net/proto/protobuf");
// var MsgHandler = require("./jcmsghandler");
const state_enum ={
none: 0,
initing: 1,
inited: 2,
logining: 3,
logined: 4,
loginouting: 5,
loginouted: 6,
joining: 7,
joined: 8,
leaving: 9,
leaved: 10,
};
UserInfo = function(){
this.channelID=""
this.platForm=""
this.gameID=""
this.deviceID=""
this.userID=""
this.token=""
this.roomID=""
this.init_state=state_enum.none
this.login_state= state_enum.none
this.room_state= state_enum.none
};
const url_offical = "wss://matchvs.kingsome.cn/websocket";
const url_test = "wss://matchvs-test.kingsome.cn/websocket";//"ws://118.31.73.76:81/websocket";
JCMatchVS = function(){
// LIFE-CYCLE CALLBACKS:
this.onLoad =function() {
this.userinfo = {};
this.rsp = null;
this.userinfo = new UserInfo();
this.reconnectcount = 0;
this.reconnectmax = 5;
this.msgctrl = new JCMsgHandler();
}
this.run=function(dt){
this.msgctrl.run(dt);
}
// update (dt) {},
this.__classCreate=function(){
this.onLoad();
if(!this.msgManager){
this.msgManager = new MsgManager();
this.msgManager.init();
}
if(!this.msgIDs){
this.msgIDs = new MsgEnum();
}
}
this.init=function(response, channel, platform, gameid, deviceid) {
this.rsp = response;
this.userinfo.channelID = channel;
this.userinfo.platForm = platform;
this.userinfo.gameID = gameid;
this.userinfo.deviceID = deviceid;
this.userinfo.init_state = state_enum.initing;
this.msgIDs.init(function(err){
this.initMsgHandler(this.msgIDs.c2sMsg(), this.msgIDs.s2cMsg());
}.bind(this));
this.msgctrl.init(response);
}
this.downloadFile=function(fileName, registrationHandler) {
var ajax = new XMLHttpRequest();
ajax.onload = registrationHandler;
ajax.open("GET", "./" + fileName, true);
ajax.responseType = "text";
ajax.overrideMimeType("text/plain; charset=x-user-defined");
ajax.send(null);
}
this.initMsgHandler=function(c2sMsg, s2cMsg){
var protoFile = "./resources/kingsomevs.proto";
this.downloadFile(protoFile, function ( res){
var Builder = ProtoBuf.protoFromString(res.target.response);
var msg = Builder.build("kingsomevs");
this.msgManager.initSendMsg(c2sMsg._CMLogin, msg.CMLogin);
this.msgManager.initRecvMsg(s2cMsg._SMLogin, msg.SMLogin, this.onLoginRsp.bind(this));
this.msgManager.initSendMsg(c2sMsg._CMCreateRoom, msg.CMCreateRoom);
this.msgManager.initRecvMsg(s2cMsg._SMCreateRoom, msg.SMCreateRoom, this.onCreateRoomRsp.bind(this));
this.msgManager.initSendMsg(c2sMsg._CMJoinRandomRoom, msg.CMJoinRandomRoom);
this.msgManager.initRecvMsg(s2cMsg._SMJoinRandomRoom, msg.SMJoinRandomRoom, this.onJoinRoomRsp.bind(this));
this.msgManager.initSendMsg(c2sMsg._CMJoinRoom, msg.CMJoinRoom);
this.msgManager.initRecvMsg(s2cMsg._SMJoinRoom, msg.SMJoinRoom, this.onJoinRoomRsp.bind(this));
this.msgManager.initSendMsg(c2sMsg._CMKickPlayer, msg.CMKickPlayer);
this.msgManager.initRecvMsg(s2cMsg._SMKickPlayer, msg.SMKickPlayer, this.onKickPlayerRsp.bind(this));
this.msgManager.initSendMsg(c2sMsg._CMJoinOver, msg.CMJoinOver);
this.msgManager.initRecvMsg(s2cMsg._SMJoinOver, msg.SMJoinOver, this.onJoinOverRsp.bind(this));
this.msgManager.initSendMsg(c2sMsg._CMJoinOpen, msg.CMJoinOpen);
this.msgManager.initRecvMsg(s2cMsg._SMJoinOpen, msg.SMJoinOpen, this.onJoinOpenRsp.bind(this));
this.msgManager.initSendMsg(c2sMsg._CMGameStart, msg.CMGameStart);
this.msgManager.initRecvMsg(s2cMsg._SMGameStart, msg.SMGameStart, this.onGameStartRsp.bind(this));
this.msgManager.initSendMsg(c2sMsg._CMSendRoomEvent, msg.CMSendRoomEvent);
this.msgManager.initRecvMsg(s2cMsg._SMSendRoomEvent, msg.SMSendRoomEvent, this.onSendRoomEvtRsp.bind(this));
this.msgManager.initSendMsg(c2sMsg._CMLeaveRoom, msg.CMLeaveRoom);
this.msgManager.initRecvMsg(s2cMsg._SMLeaveRoom, msg.SMLeaveRoom, this.onLeaveRoomRsp.bind(this));
this.msgManager.initSendMsg(c2sMsg._CMSetFrameSync, msg.CMSetFrameSync);
this.msgManager.initRecvMsg(s2cMsg.SMSetFrameSync, msg.SMSetFrameSync, this.onSetFrameSyncRsp.bind(this));
this.msgManager.initSendMsg(c2sMsg._CMPing, msg.CMPing);
this.msgManager.initRecvMsg(s2cMsg._SMPing, msg.SMPing, this.onPing.bind(this));
this.msgManager.initSendMsg(c2sMsg._CMSendFrameEvent, msg.CMSendFrameEvent);
this.msgManager.initRecvMsg(s2cMsg._SMSendFrameEvent, msg.SMSendFrameEvent, this.onSendFrameEvtRsp.bind(this));
this.msgManager.initSendMsg(c2sMsg._CMReConnect, msg.CMReConnect);
this.msgManager.initRecvMsg(s2cMsg._SMReConnect, msg.SMReConnect, this.onReConnectRsp.bind(this));
this.msgManager.initRecvMsg(s2cMsg._SMRoomPeerJoinNotify, msg.SMRoomPeerJoinNotify, this.onJoinRoomNotify.bind(this));
this.msgManager.initRecvMsg(s2cMsg._SMRoomKickPlayerNotify, msg.SMRoomKickPlayerNotify, this.onKickNotify.bind(this));
this.msgManager.initRecvMsg(s2cMsg._SMRoomEventNotify, msg.SMRoomEventNotify, this.onRoomEvtNotify.bind(this));
this.msgManager.initRecvMsg(s2cMsg._SMRoomPeerLeaveNotify, msg.SMRoomPeerLeaveNotify, this.onLeaveRoomNotify.bind(this));
this.msgManager.initRecvMsg(s2cMsg._SMFrameEventUpdateNotify, msg.SMFrameEventUpdateNotify, this.onFrameUpdateNotify.bind(this));
this.msgManager.initRecvMsg(s2cMsg._SMRoomDisbandNotify, msg.SMRoomDisbandNotify, this.onRoomDestoryNotify.bind(this));
this.msgManager.initRecvMsg(s2cMsg._SMJoinOverNotify, msg.SMJoinOverNotify, this.onJoinOverNotify.bind(this));
this.msgManager.initRecvMsg(s2cMsg._SMJoinOpenNotify, msg.SMJoinOpenNotify, this.onJoinOpenNotify.bind(this));
this.msgManager.initRecvMsg(s2cMsg._SMNetWorkStateNotify, msg.SMNetWorkStateNotify, this.onNetWorkStateNotify.bind(this));
this.msgManager.initSendMsg(c2sMsg._CMSetRoomParam, msg.CMSetRoomParam);
this.msgManager.initRecvMsg(s2cMsg._SMSetRoomParam, msg.SMSetRoomParam, this.onSMSetRoomParam.bind(this));
this.msgManager.initRecvMsg(s2cMsg._SMRoomMergeNotify, msg.SMRoomMergeNotify, this.onSMRoomMergeNotify.bind(this));
this.msgManager.initSendMsg(c2sMsg._CMResetRoom, msg.CMResetRoom);
this.msgManager.initRecvMsg(s2cMsg._SMResetRoom, msg.SMResetRoom, this.onSMResetRoom.bind(this));
this.userinfo.init_state = state_enum.inited;
this.rsp && this.rsp.initResponse({
status: 0
});
}.bind(this));
}
this.setUserId=function(userid){
this.userinfo.userID = userid;
}
this.setToken=function(token){
this.userinfo.token = token;
}
this. _checkInitState=function(){
if(this.userinfo.init_state != state_enum.inited){
if(this.userinfo.init_state == state_enum.none)
return -2;
if(this.userinfo.init_state == state_enum.initing)
return -3;
}
return 0;
}
this._checkLoginState=function(currstate){
if(this.userinfo.login_state == state_enum.none && currstate != state_enum.logining){
return -4;
}
if(this.userinfo.login_state == state_enum.logined && currstate == state_enum.logining){
return -5;
}
if(this.userinfo.login_state == state_enum.logining && this.userinfo.login_state != currstate){
return -6;
}
if(this.userinfo.login_state == state_enum.loginouting && this.userinfo.login_state != currstate){
return -6;
}
return 0;
}
this._checkRoomState=function(currstate){
if(this.userinfo.room_state == state_enum.none && currstate != state_enum.joining){
return -9;
}
if(this.userinfo.room_state == state_enum.joined && currstate == state_enum.joining){
return -8;
}
if(this.userinfo.room_state == state_enum.joining && this.userinfo.room_state != currstate){
return -7;
}
if(this.userinfo.room_state != state_enum.joined && currstate == state_enum.leaving){
return -6;
}
return 0;
}
this._netCallback=function(msg, err){
if(msg == "connect"){
this.reconnectcount = 0;
var res = {
status: 0,
eventtype: msg
};
this.rsp && this.rsp.netResponse(res);
if(this.userinfo.roomID != ""){
this.reconnect();
}else{
this.login();
}
//this.msgManager.sendMsg(this.msgIDs.c2sMsg()._CMLogin, param);
}else{
if(msg == "close"){
if(this.reconnectcount > this.reconnectmax){
var res = {
status: err,
eventtype: msg
};
this.rsp && this.rsp.netResponse(res);
this._cleanState();
}else{
var self = this;
this.scheduleOnce(function(dt){
self.reconnectcount++;
self.reconnectNet();
var res = {
status: -1,
eventtype: "reconnect"
};
this.rsp && this.rsp.netResponse(res);
}, 2);
}
}
}
}
this._cleanState=function(){
this.userinfo.login_state = state_enum.none;
this.userinfo.room_state = state_enum.none;
this.userinfo.roomID = "";
}
this._handleDeltaTime=function(createtime, passtime){
var ts = createtime * 1000 + passtime;
var d = new Date();
var ct = d.getTime() - ts;
this.msgctrl.setDeltaTime(ct);
}
this.isInited=function() {
return this.userinfo.init_state == state_enum.inited;
},
this.isLogined=function() {
return this.userinfo.login_state == state_enum.logined;
}
this.isInRoom=function (){
return this.userinfo.room_state == state_enum.joined;
}
this.isConnected=function (){
return this.msgManager.isConnected();
}
this.reconnectNet=function(){
//this._cleanState();
var url = this.userinfo.platForm == "alpha"? url_test: url_offical;
this.msgManager.connectNet(url, this._netCallback.bind(this));
}
this.disconnect=function(){
// if(this.msgManager.isConnected()){
// this.msgManager.closeNet();
// }
this.msgManager.closeNet();
}
this.login=function(){
var st = state_enum.logining;
var rs = this._checkInitState();
if(rs != 0){
return rs;
}
rs = this._checkLoginState(st);
if(rs != 0){
return rs;
}
this.userinfo.login_state = st;
var param = {
account_id: this.userinfo.userID,
session_id: this.userinfo.token,
game_id: this.userinfo.gameID,
device_id: this.userinfo.deviceID
};
if(!this.isConnected()){
this.reconnectNet();
}else{
this.msgManager.sendMsg(this.msgIDs.c2sMsg()._CMLogin, param);
}
return 0;
}
this.loginOut=function(){
this.userinfo.login_state = state_enum.none;
var param = {
status: 0
};
this.rsp && this.rsp.logoutResponse(param);
}
this.createRoom=function(roomname, maxplayer, userdesc){
var rs = this._checkInitState();
if(rs != 0){
return rs;
}
rs = this._checkLoginState(this.userinfo.login_state);
if(rs != 0){
return rs;
}
var st = state_enum.joining;
rs = this._checkRoomState(st);
if(rs != 0){
return rs;
}
this.userinfo.room_state = st;
var param = {
create_room_info:{
room_name: roomname,
max_player: maxplayer,
allow_merge: 0,
auto_exit_room : 1,
},
user_profile: userdesc
};
this.msgManager.sendMsg(this.msgIDs.c2sMsg()._CMCreateRoom, param);
return 0;
}
this.joinRoom=function(roomid, userdesc){
var rs = this._checkInitState();
if(rs != 0){
return rs;
}
rs = this._checkLoginState(this.userinfo.login_state);
if(rs != 0){
return rs;
}
var st = state_enum.joining;
rs = this._checkRoomState(st);
if(rs != 0){
return rs;
}
this.userinfo.room_state = st;
var param = {
room_id: roomid,
user_profile: userdesc
};
this.msgManager.sendMsg(this.msgIDs.c2sMsg()._CMJoinRoom, param);
return 0;
}
this.joinRandomRoom=function(maxplayer, userdesc){
var rs = this._checkInitState();
if(rs != 0){
return rs;
}
rs = this._checkLoginState(this.userinfo.login_state);
if(rs != 0){
return rs;
}
var st = state_enum.joining;
rs = this._checkRoomState(st);
if(rs != 0){
return rs;
}
this.userinfo.room_state = st;
var param = {
max_player: maxplayer,
user_profile: userdesc
};
this.msgManager.sendMsg(this.msgIDs.c2sMsg()._CMJoinRandomRoom, param);
return 0;
}
this.leaveRoom=function(content){
var rs = this._checkInitState();
if(rs != 0){
return rs;
}
rs = this._checkLoginState(this.userinfo.login_state);
if(rs != 0){
return rs;
}
var st = state_enum.leaving;
rs = this._checkRoomState(st);
if(rs != 0){
return rs;
}
this.userinfo.room_state = st;
var param = {
custom_data: content
};
this.msgManager.sendMsg(this.msgIDs.c2sMsg()._CMLeaveRoom, param);
return 0;
}
this.joinOver=function(content){
var rs = this._checkInitState();
if(rs != 0){
return rs;
}
rs = this._checkLoginState(this.userinfo.login_state);
if(rs != 0){
return rs;
}
var st = state_enum.none;
rs = this._checkRoomState(st);
if(rs != 0){
return rs;
}
var param = {
custom_data: content
};
this.msgManager.sendMsg(this.msgIDs.c2sMsg()._CMJoinOver, param);
return 0;
}
this.joinOpen=function(content){
var rs = this._checkInitState();
if(rs != 0){
return rs;
}
rs = this._checkLoginState(this.userinfo.login_state);
if(rs != 0){
return rs;
}
var st = state_enum.none;
rs = this._checkRoomState(st);
if(rs != 0){
return rs;
}
var param = {
custom_data: content
};
this.msgManager.sendMsg(this.msgIDs.c2sMsg()._CMJoinOpen, param);
return 0;
}
this.gameStart=function(content){
var rs = this._checkInitState();
if(rs != 0){
return rs;
}
rs = this._checkLoginState(this.userinfo.login_state);
if(rs != 0){
return rs;
}
var st = state_enum.none;
rs = this._checkRoomState(st);
if(rs != 0){
return rs;
}
var param = {
custom_data: content
};
this.msgManager.sendMsg(this.msgIDs.c2sMsg()._CMGameStart, param);
return 0;
}
this.kickPlayer=function(userid, content){
var rs = this._checkInitState();
if(rs != 0){
return rs;
}
rs = this._checkLoginState(this.userinfo.login_state);
if(rs != 0){
return rs;
}
var st = state_enum.none;
rs = this._checkRoomState(st);
if(rs != 0){
return rs;
}
var param = {
account_id: userid,
custom_data: content
};
this.msgManager.sendMsg(this.msgIDs.c2sMsg()._CMKickPlayer, param);
return 0;
}
this.sendRoomMsg=function(msg){
var d = new Date();
msg.timestamp = d.getTime() - this.msgctrl.deltatime + this.msgctrl.delaytime;
this.sendRoomEvent(JSON.stringify(msg));
}
this.sendRoomEvent=function(msg){
console.log("[sendRoomEvent]"+msg);
var rs = this._checkInitState();
console.log("rs"+rs);
if(rs != 0){
return rs;
}
rs = this._checkLoginState(this.userinfo.login_state);
if(rs != 0){
return rs;
}
var st = state_enum.none;
rs = this._checkRoomState(st);
if(rs != 0){
return rs;
}
var param = {
custom_data: msg
};
this.msgManager.sendMsg(this.msgIDs.c2sMsg()._CMSendRoomEvent, param);
return 0;
}
this.sendFrameEvent=function(msg){
var rs = this._checkInitState();
if(rs != 0){
return rs;
}
rs = this._checkLoginState(this.userinfo.login_state);
if(rs != 0){
return rs;
}
var st = state_enum.none;
rs = this._checkRoomState(st);
if(rs != 0){
return rs;
}
var param = {
custom_data: msg
};
this.msgManager.sendMsg(this.msgIDs.c2sMsg()._CMSendFrameEvent, param);
return 0;
}
this.setFrameSync=function(framerate){
var rs = this._checkInitState();
if(rs != 0){
return rs;
}
rs = this._checkLoginState(this.userinfo.login_state);
if(rs != 0){
return rs;
}
var st = state_enum.none;
rs = this._checkRoomState(st);
if(rs != 0){
return rs;
}
var param = {
frame_rate: framerate
};
this.msgManager.sendMsg(this.msgIDs.c2sMsg()._CMSetFrameSync, param);
return 0;
}
this.heart=function(){
this.msgManager.sendMsg(this.msgIDs.c2sMsg()._CMPing, null);
return 0;
}
this.reconnect=function(){
var rs = this._checkInitState();
if(rs != 0){
return rs;
}
var param = {
room_id: this.userinfo.roomID,
account_id: this.userinfo.userID,
session_id: this.userinfo.token
};
this.msgManager.sendMsg(this.msgIDs.c2sMsg()._CMReConnect, param);
return 0;
}
this.onLoginRsp=function(msg){
console.log("[onLoginRsp]"+JSON.stringify(msg));
var err = 0;
var errstr = "";
if(msg.result == null || msg.result.error_code == 0){
this.userinfo.login_state = state_enum.logined;
this.userinfo.roomID = msg.room_id? msg.room_id: "";
}else{
err = msg.result.error_code;
errstr = msg.result.error_msg;
this.userinfo.login_state = state_enum.none;
this.userinfo.roomID = "";
}
var param = {
status: err,
statusmsg: errstr,
roomID: this.userinfo.roomID
};
this.rsp && this.rsp.loginResponse(param);
}
this.onLoginOutRsp=function(msg){
console.log("[onLoginOutRsp]"+JSON.stringify(msg));
var err = 0;
var errstr = "";
if(msg.result == null || msg.result.error_code == 0){
this.userinfo.login_state = state_enum.none;
}else{
err = msg.result.error_code;
errstr = msg.result.error_msg;
this.userinfo.login_state = state_enum.logined;
}
var param = {
status: err,
statusmsg: errstr,
};
this.rsp && this.rsp.logoutResponse(param);
}
this.onCreateRoomRsp=function(msg){
console.log("[onCreateRoomRsp]"+JSON.stringify(msg));
var err = 0;
var errstr = "";
if(msg.result == null || msg.result.error_code == 0){
this.userinfo.room_state = state_enum.joined;
this.userinfo.roomID = msg.room_id? msg.room_id: "";
}else{
err = msg.result.error_code;
errstr = msg.result.error_msg;
this.userinfo.room_state = state_enum.none;
}
var param = {
status: err,
statusmsg: errstr,
roomID: msg.room_id? msg.room_id: "",
owner: "",
create_time: msg.create_time,
room_info:msg.room_info,
};
if(param.status == 0){
this._handleDeltaTime(param.create_time, 0);
}
this.rsp && this.rsp.createRoomResponse(param);
}
this.onJoinRoomRsp=function(msg){
console.log("[onJoinRoomRsp]"+JSON.stringify(msg));
var err = 0;
var errstr = "";
if(msg.result == null ||msg.result.error_code == 0||msg.result.error_code == -3){
this.userinfo.room_state = state_enum.joined;
this.userinfo.roomID = msg.room_info? msg.room_info.room_id: "";
}else{
err = msg.result.error_code;
errstr = msg.result.error_msg;
this.userinfo.room_state = state_enum.none;
}
var lst = [];
if(msg.room_user_info_list){
msg.room_user_info_list.forEach(element => {
var obj = {
userId: element.account_id,
userProfile: element.user_profile
};
lst.push(obj);
});
}
var param = {
status: err,
statusmsg: errstr,
roomInfo:{
roomID: msg.room_info? msg.room_info.room_id: "",
roomProperty: msg.room_info? msg.room_info.room_property: "",
ownerId: msg.room_info? msg.room_info.owner_id: "",
create_time: msg.room_info? msg.room_info.create_time: 0,
_timestamp: msg.room_info? msg.room_info._timestamp: 0,
robot_list:msg.room_info? msg.room_info.robot_list: 0
},
roomUserInfoList: lst
};
if(param.status == 0||param.status == -3){
this._handleDeltaTime(param.roomInfo.create_time, param.roomInfo._timestamp);
}
this.rsp && this.rsp.joinRoomResponse(param);
}
this.onJoinOverRsp=function(msg){
console.log("[onJoinOverRsp]"+JSON.stringify(msg));
var err = 0;
var errstr = "";
if(msg.result != null && msg.result.error_code != 0){
err = msg.result.error_code;
errstr = msg.result.error_msg;
}
var param = {
status: err,
statusmsg: errstr,
content: msg.custom_data? msg.custom_data: ""
};
this.rsp && this.rsp.joinOverResponse(param);
}
this.onJoinOpenRsp=function(msg){
console.log("[onJoinOpenRsp]"+JSON.stringify(msg));
var err = 0;
var errstr = "";
if(msg.result != null && msg.result.error_code != 0){
err = msg.result.error_code;
errstr = msg.result.error_msg;
}
var param = {
status: err,
statusmsg: errstr,
content: msg.custom_data? msg.custom_data: ""
};
this.rsp && this.rsp.joinOpenResponse(param);
}
this.onGameStartRsp=function(msg){
console.log("[onGameStartRsp]"+JSON.stringify(msg));
var err = 0;
var errstr = "";
if(msg.result != null && msg.result.error_code != 0){
err = msg.result.error_code;
errstr = msg.result.error_msg;
}
var param = {
status: err,
statusmsg: errstr,
content: msg.custom_data? msg.custom_data: ""
};
this.rsp && this.rsp.gameStartResponse(param);
}
this.onLeaveRoomRsp=function(msg){
console.log("[onLeaveRoomRsp]"+JSON.stringify(msg));
var err = 0;
var errstr = "";
if(msg.result == null || msg.result.error_code == 0 || msg.result.error_code == -1){
this.userinfo.room_state = state_enum.none;
this.userinfo.roomID = "";
}else{
err = msg.result.error_code;
errstr = msg.result.error_msg;
this.userinfo.room_state = state_enum.joined;
}
var param = {
status: err,
statusmsg: errstr,
roomID: msg.room_id? msg.room_id: "",
userId: msg.account_id? msg.account_id: "",
content: msg.custom_data? msg.custom_data: ""
};
this.rsp && this.rsp.leaveRoomResponse(param);
}
this.onKickPlayerRsp=function(msg){
console.log("[onKickPlayerRsp]"+JSON.stringify(msg));
var err = 0;
var errstr = "";
if(msg.result != null && msg.result.error_code != 0){
err = msg.result.error_code;
errstr = msg.result.error_msg;
}
var param = {
status: err,
statusmsg: errstr,
owner: "",
userId: msg.account_id? msg.account_id: "",
content: msg.custom_data? msg.custom_data: ""
};
this.rsp && this.rsp.kickPlayerResponse(param);
}
this.onSendRoomEvtRsp=function(msg){
console.log("[onSendRoomEvtRsp]"+JSON.stringify(msg));
var err = 0;
var errstr = "";
if(msg.result != null && msg.result.error_code != 0){
err = msg.result.error_code;
errstr = msg.result.error_msg;
}
var param = {
status: err,
statusmsg: errstr,
eventid: msg.event_id? msg.event_id: "",
content: msg.custom_data? msg.custom_data: "",
_timestamp: msg._timestamp? msg._timestamp: 0
};
this.rsp && this.rsp.sendEventResponse(param);
}
this.onSetFrameSyncRsp=function(msg){
console.log("[onSetFrameSyncRsp]"+JSON.stringify(msg));
var err = 0;
var errstr = "";
if(msg.result != null && msg.result.error_code != 0){
err = msg.result.error_code;
errstr = msg.result.error_msg;
}
var param = {
status: err,
statusmsg: errstr,
framerate: msg.frame_rate? msg.frame_rate: 0
};
this.rsp && this.rsp.setFrameSyncResponse(param);
}
this.onSendFrameEvtRsp=function(msg){
// console.log("[onSendFrameEvtRsp]"+JSON.stringify(msg));
var err = 0;
var errstr = "";
if(msg.result != null && msg.result.error_code != 0){
err = msg.result.error_code;
errstr = msg.result.error_msg;
}
var param = {
status: err,
statusmsg: errstr,
eventid: msg.event_id? msg.event_id: ""
};
this.rsp && this.rsp.sendFrameEventResponse(param);
}
this.onReConnectRsp=function(msg){
console.log("[onReConnectRsp]"+JSON.stringify(msg));
var err = 0;
var errstr = "";
if(msg.result != null){
if(msg.result.error_code != 0){
err = msg.result.error_code;
errstr = msg.result.error_msg;
this._cleanState();
}else{
this.userinfo.roomID = msg.room_info? msg.room_info.room_id: "";
}
}
var lst = [];
if(msg.room_user_info_list){
msg.room_user_info_list.forEach(element => {
var obj = {
userId: element.account_id,
userProfile: element.user_profile
};
lst.push(obj);
});
}
var param = {
status: err,
statusmsg: errstr,
roomInfo:{
roomID: msg.room_info? msg.room_info.room_id: "",
roomProperty: msg.room_info? msg.room_info.room_property: "",
ownerId: msg.room_info? msg.room_info.owner_id: "",
create_time: msg.room_info? msg.room_info.create_time: 0,
_timestamp: msg.room_info? msg.room_info._timestamp: 0
},
roomUserInfoList: lst
};
if(param.status == 0){
this._handleDeltaTime(param.roomInfo.create_time, param.roomInfo._timestamp);
}
this.rsp && this.rsp.reconnectResponse(param);
}
this.onRoomEvtNotify=function(msg){
console.log("[onRoomEvtNotify]"+JSON.stringify(msg));
var param = {
srcUserId: msg.src_account_id? msg.src_account_id: "",
eventid: msg.event_id? msg.event_id: "",
content: msg.custom_data? msg.custom_data: ""
};
this.msgctrl.pushMsg(param);
this.rsp && this.rsp.sendRoomEvtNotify(param);
}
this.onKickNotify=function(msg){
console.log("[onKickNotify]"+JSON.stringify(msg));
var param = {
srcUserId: "",
userId: msg.account_id? msg.account_id: "",
owner: "",
content: msg.custom_data? msg.custom_data: ""
};
this.rsp && this.rsp.kickPlayerNotify(param);
}
this.onJoinRoomNotify=function(msg){
console.log("[onJoinRoomNotify]"+JSON.stringify(msg));
var param = {
userId: msg.room_user_info? msg.room_user_info.account_id: "",
userProfile: msg.room_user_info? msg.room_user_info.user_profile: ""
};
this.rsp && this.rsp.joinRoomNotify(param);
}
this.onLeaveRoomNotify=function(msg){
console.log("[onLeaveRoomNotify]"+JSON.stringify(msg));
var param = {
userId: msg.room_user_info? msg.room_user_info.account_id: "",
userProfile: msg.room_user_info? msg.room_user_info.user_profile: "",
roomID: msg.room_id? msg.room_id: "",
owner: "",
content: ""
};
this.rsp && this.rsp.leaveRoomNotify(param);
}
this.onFrameUpdateNotify=function(msg){
// console.log("[onFrameUpdateNotify]"+JSON.stringify(msg));
var lst = [];
if(msg.frame_info && msg.frame_info.frame_items){
msg.frame_info.frame_items.forEach(element =>{
var obj = {
srcUserID: element.src_account_id,
content: element.custom_data,
timestamp: element.timestamp
};
lst.push(obj);
});
}
var param = {
frameIndex: msg.frame_info? msg.frame_info.frame_index: 0,
frameItems: lst,
frameWaitCount: msg.frame_info? msg.frame_info.frame_wait_count: 0
};
this.rsp && this.rsp.frameUpdate(param);
}
this.onRoomDestoryNotify=function(msg){
console.log("[onFrameUpdateNotify]"+JSON.stringify(msg));
var param = {
roomID: msg.room_id? msg.room_id: ""
};
this.userinfo.room_state = state_enum.none;
this.userinfo.roomID = "";
this.rsp && this.rsp.destroyRoomNotify(param);
}
this.onJoinOverNotify=function(msg){
console.log("[onJoinOverNotify]"+JSON.stringify(msg));
var param = {
content: msg.custom_data? msg.custom_data: ""
};
this.rsp && this.rsp.joinOverRoomNotify(param);
}
this.onJoinOpenNotify=function(msg){
console.log("[onJoinOpenNotify]"+JSON.stringify(msg));
var param = {
content: msg.custom_data? msg.custom_data: ""
};
this.rsp && this.rsp.joinOpenRoomNotify(param);
}
this.onNetWorkStateNotify=function(msg){
console.log("[onNetWorkStateNotify]"+JSON.stringify(msg));
var param = {
roomID: msg.room_id? msg.room_id: "",
userID: msg.account_id? msg.account_id: "",
state: msg.state? msg.state: -100,
owner: msg.owner? msg.owner: ""
};
this.rsp && this.rsp.netWorkStateNotify(param);
}
this.resetRoom=function(msg){
var rs = this._checkInitState();
if(rs != 0){
return rs;
}
rs = this._checkLoginState(this.userinfo.login_state);
if(rs != 0){
return rs;
}
var st = state_enum.none;
rs = this._checkRoomState(st);
if(rs != 0){
return rs;
}
var param = {
join_over: msg,
};
this.msgManager.sendMsg(this.msgIDs.c2sMsg()._CMResetRoom, param);
}
this.setRoomParam=function(msg){
var rs = this._checkInitState();
if(rs != 0){
return rs;
}
rs = this._checkLoginState(this.userinfo.login_state);
if(rs != 0){
return rs;
}
var st = state_enum.none;
rs = this._checkRoomState(st);
if(rs != 0){
return rs;
}
var param = {
allow_merge: msg,
};
this.msgManager.sendMsg(this.msgIDs.c2sMsg()._CMSetRoomParam, param);
}
this.onSMSetRoomParam=function(msg){
console.log("[onSMSetRoomParam]");
this.rsp && this.rsp.onSMSetRoomParam(msg);
}
this.onSMResetRoom=function(msg){
console.log("[onSMResetRoom]");
this.rsp && this.rsp.onSMResetRoom(msg);
}
this.onSMRoomMergeNotify=function(msg){
console.log("[onSMRoomMergeNotify]");
this.rsp && this.rsp.onSMRoomMergeNotify(msg);
}
this.onPing=function(msg){
}
}