pubgv3/assets/scripts/manages/SDKManage.js
zhuguoqing ff550d5d6a init
2022-05-22 10:32:02 +08:00

370 lines
8.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

var jcfw = require('jcfw');
var Main = require('Main');
var Utils = require('Utils');
//var playerData = require("playerData")
var jcgamelog = jcfw.gamelog;
var jcshare = jcfw.share;
var NetManage = require('NetManage');
var urlbuilder = require('urlbuilder');
var httpclient = require('httpclient');
var wxVoice = require('wxVoice');
cc.interstitialAdTime = 0;
cc.notreward = 0;
var toCodePoint = function (unicodeSurrogates) {
var r = [],
c = 0,
p = 0,
i = 0;
while (i < unicodeSurrogates.length) {
var pos = i;
c = unicodeSurrogates.charCodeAt(i++); //返回位置的字符的 Unicode 编码
if (c == 0xfe0f) {
continue;
}
if (p) {
var value = 0x10000 + ((p - 0xd800) << 10) + (c - 0xdc00);
r.push({
v: value,
pos: pos,
}); //计算4字节的unicode
p = 0;
} else if (0xd800 <= c && c <= 0xdbff) {
p = c; //如果unicode编码在oxD800-0xDBff之间则需要与后一个字符放在一起
} else {
r.push({
v: c,
pos: pos,
}); //如果是2字节直接将码点转为对应的十六进制形式
}
}
return r;
};
var SDKManage = function () {
this.isoffical = Main.config.mainConfig.isoffical;
this.checking = false;
this.gameId = 2006;
this.nickName = 'test';
this.avatarUrl = '';
this.NodeId = 1;
this.session_id = '';
this.m_hiID = 18001;
this.account_id = 0; //玩家账号ID
// playerData.SDKManage = this
this.getplayerdata = function () {
if (!this.playerData) {
this.playerData = require('playerData');
}
return this.playerData;
};
this.sixstr = function (str, maxChars, suffix) {
if (str == '' || str == null) {
return ' ';
}
maxChars = maxChars == null ? 6 : maxChars;
suffix = suffix == null ? '...' : suffix;
maxChars *= 2;
var codeArr = toCodePoint(str);
var numChar = 0;
var index = 0;
for (var i = 0; i < codeArr.length; ++i) {
var code = codeArr[i].v;
var add = 1;
if (code >= 128) {
add = 2;
}
//如果超过了限制,则按上一个为准
if (numChar + add > maxChars) {
break;
}
index = i;
//累加
numChar += add;
}
if (codeArr.length - 1 == index) {
return str;
}
var more = suffix ? 1 : 0;
return str.substring(0, codeArr[index - more].pos + 1) + suffix;
};
// 6513 游客 6516 钱包
this.init = function (channelid = 6513) {
// this.nickName = this.sixstr(this.nickName)
if (cc.sys.platform == cc.sys.WECHAT_GAME) {
this.ChannelId = 6001;
} else if (cc.sys.platform == cc.sys.QQ_PLAY) {
this.ChannelId = 6002;
} else if (window.qg == cc.sys.QQ_PLAY) {
this.ChannelId = 6003;
} else {
this.ChannelId = channelid; //for test
}
// this.getRecommendGames()
cc.SDKManage = this;
jcfw.init(this.ChannelId, this.gameId, this.isoffical);
};
this.compareVersion = function (v1, v2) {
v1 = v1.split('.');
v2 = v2.split('.');
const len = Math.max(v1.length, v2.length);
while (v1.length < len) {
v1.push('0');
}
while (v2.length < len) {
v2.push('0');
}
for (let i = 0; i < len; i++) {
const num1 = parseInt(v1[i]);
const num2 = parseInt(v2[i]);
if (num1 > num2) {
return 1;
} else if (num1 < num2) {
return -1;
}
}
return 0;
};
this.doLogin = function (res) {
if (this.loginStep < this.logincbarr.length) {
this.logincbarr[this.loginStep](res);
this.loginStep++;
} else {
this.logEvent('loginend', '登录完成');
var lgtime = Number(new Date().getTime() - this.gameloginstartime);
jcgamelog.logEnterMainScene(lgtime);
this.loginOver();
}
};
this.gamelogin = function () {
// this.time1 = new Date().getTime()
var token = cc.sys.localStorage.getItem('token');
var self = this;
if (!token || token == '') {
token = jcfw.makeUUID();
try {
cc.sys.localStorage.setItem('token', token);
} catch (e) {}
}
jcfw.loginUser(
(res) => {
cc.channelid = res.channel;
self.hasLogin = true;
self.logEvent('gamelogin', '游戏登录成功');
self.account_id = res.account_id;
self.session_id = res.session_id;
self.openid = res.openid;
localStorage.setItem('loginaccount', res.account_id);
localStorage.setItem('loginsession', res.session_id);
self.doLogin();
},
(neterr, logicerr, errmsg) => {
setTimeout(() => {
self.gamelogin();
}, 1000);
}
);
};
this.Login = function (cb) {
this.loginStep = 0;
if (cb) {
this.loginOver = cb;
}
this.logincbarr = [];
this.gameloginstartime = new Date().getTime();
this.logincbarr = [
this.gamelogin.bind(this),
this.initNetManage.bind(this),
this.getUserInfo.bind(this),
this.getothers.bind(this),
];
this.doLogin();
};
this.getothers = function () {
var self = this;
this.allcount = 3;
var cb = function () {
self.allcount--;
if (self.allcount <= 0) {
self.gamelogin();
}
};
NetManage.getHeroList(cb);
NetManage.getSkinList(cb);
NetManage.getItemList(cb);
};
this.initFriendInfo = function () {
var opt = {
account_id: this.account_id,
session_id: this.session_id,
nickname: cc.playerData.name,
avatar_url: cc.playerData.head_id,
sex: 0,
// "proto_version":this.friend_pversion,
server_id: 1,
vip_lv: 0, //性别
head: Number(cc.playerData.head_frame), //头像框
};
cc.chatMgr.initengine(cc.SDKManage.isoffical, opt);
};
this.getUserInfo = function () {
var self = this;
var cb = function (obj) {
cc.playerData.refreshInfo(obj.info);
self.logEvent('getUserInfo', '获取用户信息成功');
self.initFriendInfo();
self.doLogin();
console.log('改名次数:' + obj.info.rename_count);
if (obj.info.rename_count == 0) {
window.canrename = true;
} else {
window.canrename = false;
}
};
NetManage.logingame(cb);
};
this.initNetManage = function () {
var self = this;
NetManage.init(
self.account_id,
self.session_id,
self.nickName,
self.avatarUrl
);
NetManage.getNodeID(() => {});
// NetManage.getTime(() => {})
console.log('start init 6516');
self.logEvent('initNetManage', '初始化人物信息');
setTimeout(() => {
self.doLogin();
}, 10);
};
this.getGameConfig = function () {
var self = this;
var cb = function (res2) {
self.logEvent('getGameConfig', '获取配置信息成功');
var res = {};
for (var i = 0; i < res2.length; i++) {
res[res2[i].key] = res2[i].value;
}
setTimeout(() => {
self.doLogin();
}, 10);
};
var cbf = function (res) {
self.getGameConfig();
};
//cb(this.config)
jcfw.cloud.getConfig('', cb, cbf);
};
this.checkRate = function (nubmer) {
//判断正整数/[19]+[09]]/
var re = /^[0-9]+.?[0-9]*/; //
if (!re.test(nubmer)) {
return false;
}
return true;
};
this.logEvent = function (k, v) {
jcgamelog.logButtonClick(k, v);
if (CC_JSB) {
if (!v) {
v = '';
}
if (cc.sys.os == cc.sys.OS_ANDROID) {
//反射调用原生的隐藏方法
jsb.reflection.callStaticMethod(
'org/cocos2dx/javascript/AppActivity',
'logEvent',
'(Ljava/lang/String;Ljava/lang/String;)V',
k + '',
v
);
}
}
};
this.getBattleCounts = function () {
var count = 0;
var scount = cc.sys.localStorage.getItem('battleCount');
var today = new Date();
var datakey = today.getMonth() + '-' + today.getDate();
var savedata = cc.sys.localStorage.getItem('datakey');
if (savedata != datakey) {
try {
cc.sys.localStorage.setItem('datakey', datakey);
} catch (err) {}
try {
cc.sys.localStorage.setItem('battleCount', 0);
} catch (err) {}
scount = null;
}
if (scount != null && scount != '') {
count = parseInt(scount);
}
return count;
};
this.saveBattleCount = function () {
var count = this.getBattleCounts() + 1;
try {
cc.sys.localStorage.setItem('battleCount', count);
} catch (err) {}
};
this.dirtyWordCheck = function (
_str //检查敏感词汇
) {
NetManage.dirtyWordCheck(_str);
};
this.initGameRes = function () //初始化进入游戏前数据
{
wxVoice.playbgm('bgm');
cc.battleCache.loadwave();
cc.highmode = true;
//SDKManage.nickName = "战斗测试"
cc.game.setFrameRate(60);
cc.macro.CLEANUP_IMAGE_CACHE = false;
cc.dynamicAtlasManager.enabled = true;
cc.mygamedelta = 0.0167;
};
};
var instance = new SDKManage();
module.exports = instance;