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++); if (c == 0xfe0f) { continue; } if (p) { var value = 0x10000 + ((p - 0xd800) << 10) + (c - 0xdc00); r.push({ v: value, pos: pos, }); p = 0; } else if (0xd800 <= c && c <= 0xdbff) { p = c; } else { r.push({ v: c, pos: pos, }); } } 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; // // 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; }; 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', 'loginginish'); 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', 'loginsuccess'); 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', 'getuserinfo'); self.initFriendInfo(); self.doLogin(); 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', 'initplayer'); setTimeout(() => { self.doLogin(); }, 10); }; this.getGameConfig = function () { var self = this; var cb = function (res2) { self.logEvent('getGameConfig', 'getconfig'); 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) { 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; 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;