var jccloud = require('jccloud'); var jcgamelog = require('jcgamelog'); var jclogin = require('jclogin'); var jcshare = require('jcshare'); //var jcAD = require('jcAD'); var jcStat = require('jcstat'); // var jcNotify = require('jcnotify'); //var jcvoodoo = require('jcvoodoo'); var storage = require('storage'); const notification = require('notification'); const _GAME_LOCALUUID = 'jc_game_localuuid'; const _GAME_INFO_HANDLER_BINDING = 'jc_gameinfo_handler_binding'; const _GAME_INFO_HANDLER_UNBINDING = 'jc_gameinfo_handler_unbinding'; module.exports = { cloud: jccloud, gamelog: jcgamelog, login: jclogin, share: jcshare, //ad: jcAD, stat: jcStat, //notify: jcNotify, //voodoo: jcvoodoo, localUUID: '', systemInfo: null, lauchInfo: null, shareInfo: {}, _handlers: [], // /** * * @param {number} channelid : * @param {number} gameid : * @param {boolean} isoffical : * @param {object} response : * .onLogin(logininfo) * .onLoginFail(net_err, logic_err, errmsg) * .onUpdateUser(userinfo) * .onUpdateUserFail(net_err, logic_err, errmsg) * .onShare(***) * .onShareFail(***) */ init(channelid, gameid, isoffical, response, env) { this.rsp = response; this.channelID = parseInt(channelid); this.gameID = parseInt(gameid); this.isOffical = isoffical; this._fixChannelID(); this._checkListener(); this.gamelog.init(this.channelID, this.gameID, isoffical, this); this.cloud.init(this.channelID, this.gameID, isoffical, this); this.login.init(this.channelID, this.gameID, isoffical, this, env); this.share.init(this.channelID, this.gameID, isoffical, this); //this.ad.init(this.channelID, this.gameID, isoffical, this); this.stat.init(this.channelID, this.gameID, isoffical, this); //this.notify.init(this.channelID, this.gameID, isoffical, this); //this.voodoo.init(this.channelID, this.gameID, isoffical, this); this._loadLocalID(); this._loadLaunchInfo(); this._loadSystemInfo(); // this._loadConfig(); this._handleInit(this.channelID, this.gameID, isoffical); }, /** * @param {object} successcb : * @param {object} failcb : * @param {string} ex_param : */ loginUser(successcb, failcb, ex_param) { if (this._logining) { return 1; } this.logining = true; this.login.loginPT( (res) => { this._logining = false; this.setAccountID(res.account_id, res.session_id, res); this.setNickName(res.nickname); this.setServerTime(res.server_time); this.gamelog.logLoginSuccess(res, this.login.login_costtime); this.gamelog.logSystemInfo(this.systemInfo); if ( this.shareInfo && this.shareInfo.id && this.shareInfo.id != '' ) { this.share.acceptDailyInvite( this.shareInfo.param, this.shareInfo.id, () => {}, () => {} ); this.gamelog.logShareInvite( this.shareInfo.inviter, this.shareInfo.type, this.shareInfo.id, this.shareInfo.param ); } this.rsp && this.rsp.onLogin && this.rsp.onLogin(res); successcb && successcb(res); if (this.cacheuser) { this.updateUser( this.cacheuser.info, this.cacheuser.success, this.cacheuser.fail ); } }, (neterr, logicerr, errmsg) => { this._logining = false; this.gamelog.logLoginFailed(neterr, logicerr, errmsg); this.rsp && this.rsp.onLoginFail && this.rsp.onLoginFail(neterr, logicerr, errmsg); failcb && failcb(neterr, logicerr, errmsg); }, ex_param ); return 0; }, // /** * * @param {object} allinfo : * @param {object} successcb : * @param {object} failcb : */ updateUser(allinfo, successcb, failcb) { if (!this.logined) { if (!this.cacheuser) { this.cacheuser = {}; } this.cacheuser.info = allinfo; this.cacheuser.success = successcb; this.cacheuser.fail = failcb; return; } else { this.cacheuser = null; } let userinfo = allinfo.userInfo; if (!userinfo) { this.gamelog.logAuthFail(); return; } else { this.gamelog.logAuthSuccess(userinfo); this.setUserInfo(userinfo); } if (this._userupdating) { return 1; } this._userupdating = true; this.login.updatePTInfo( userinfo, allinfo, (obj) => { this._userupdating = false; this.rsp && this.rsp.onUpdateUser && this.rsp.onUpdateUser(obj); successcb && successcb(obj); }, (neterr, logicerr, errmsg) => { this._userupdating = false; this.rsp && this.rsp.onUpdateUserFail && this.rsp.onUpdateUserFail(neterr, logicerr, errmsg); failcb && failcb(neterr, logicerr, errmsg); } ); return 0; }, initGlobalConfig(successcb, failcb) { this.cloud.getServerConfig( (cfg) => { this.rsp && this.rsp.onServerConfig && this.rsp.onServerConfig(cfg); successcb && successcb(cfg); }, (neterr, logicerr, errmsg) => { this.rsp && this.rsp.onServerConfigFail && this.rsp.onServerConfigFail(neterr, logicerr, errmsg); failcb && failcb(neterr, logicerr, errmsg); } ); }, shareNormal(content, imgurl, sharetype, shareparam, extrainfo) { this.share.shareNormal( content, imgurl, sharetype, shareparam, extrainfo, () => { this.rsp && this.rsp.onShare && this.rsp.onShare( content, imgurl, sharetype, shareparam, extrainfo ); }, () => { this.rsp && this.rsp.onShareFail && this.rsp.onShareFail( content, imgurl, sharetype, shareparam, extrainfo ); } ); }, shareCapture(content, capture_rect, sharetype, shareparam, extrainfo) { this.share.shareCapture( content, capture_rect, sharetype, shareparam, extrainfo, () => { this.rsp && this.rsp.onShare && this.rsp.onShare( content, capture_rect, sharetype, shareparam, extrainfo ); }, () => { this.rsp && this.rsp.onShareFail && this.rsp.onShareFail( content, capture_rect, sharetype, shareparam, extrainfo ); } ); }, makeUUID() { return this.gamelog.generateUUID(); }, makeAccountID(openid) { return this.login.AccountID(openid); }, addHandler(handler) { this._checkListener(); if (!handler) { return false; } let obj = this._handlers.find((element) => { return element == handler; }); if (!obj) { this._handlers.push(handler); this._initHandler(handler); } return true; }, delHandler(handler) { if (!handler || !this._handlers) { return false; } let idx = this._handlers.findIndex((item) => { return item == handler; }); if (idx < 0) { return true; } this._handlers.splice(idx, 1); return true; }, resetLocalID() { this._cleanLocalID(); this._loadLocalID(); }, setResponse(rsp) { this.rsp = rsp; }, gameStart(startmode, extparam) { switch (startmode) { case 0: this.gamelog.logStartGame(extparam); break; case 1: this.gamelog.logRestartGame(extparam); break; case 2: this.gamelog.logTryAgain(extparam); break; } this._handleGameStart(startmode, extparam); }, gameOver(score, bsuccess, extparam) { this.gamelog.logGameover(extparam, score, bsuccess ? 1 : 0); this._handleGameOver(score, bsuccess, extparam); }, gameShow(showinfo) { this.gamelog.logShow(); this._handleGameShow(showinfo); }, gameHide() { this.gamelog.logHide(); this._handGameHide(); }, gameUseItem(itemid, itemcount, reson, extparam) { this.gamelog.logUseItem(itemid, itemcount, reson, extparam); }, gameGetItem(itemid, itemcount, reson, extparam) { this.gamelog.logProductItem(itemid, itemcount, reson, extparam); }, //----------------------------------------------------------------- setAccountID(accountid, sessionid, exobj) { this.accountID = accountid; this.sessionID = sessionid; this.accountobj = exobj; if (exobj && exobj.wid) { this.weakID = exobj.wid; } if (exobj && exobj.sid) { this.strongID = exobj.sid; } if (exobj && exobj.ptid) { this.ptID = exobj.ptID; } this.share.setAccountID(accountid, sessionid, exobj); this.gamelog.setAccountID(accountid, sessionid, exobj); this.login.setAccountID(accountid, sessionid, exobj); this.cloud.setAccountID(accountid, sessionid, exobj); //this.ad.setAccountID(accountid, sessionid, exobj); this.stat.setAccountID(accountid, sessionid, exobj); //this.notify.setAccountID(accountid, sessionid, exobj); //this.voodoo.setAccountID(accountid, sessionid, exobj); this._handleAccountID(accountid, sessionid, exobj); this.logined = this.accountID && this.accountID != ''; }, setLocalUUID(uuid) { this.localUUID = uuid; this.gamelog.setLocalUUID(uuid); this.cloud.setLocalUUID(uuid); this.login.setLocalUUID(uuid); this.share.setLocalUUID(uuid); // this.ad.setLocalUUID(uuid); this.stat.setLocalUUID(uuid); //this.notify.setLocalUUID(uuid); //this.voodoo.setLocalUUID(uuid); this._handleLocalID(uuid); }, setNickName(nickname) { if (nickname && nickname != '' && this.nickName != nickname) { this.nickName = nickname; this.gamelog.setNickName(nickname); this.cloud.setNickName(nickname); this.login.setNickName(nickname); this.share.setNickName(nickname); //this.ad.setNickName(nickname); this.stat.setNickName(nickname); //this.notify.setNickName(nickname); //this.voodoo.setNickName(nickname); this._handleNickName(nickname); } }, setFromAppID(appid, scene, paramobj) { this.scene = scene; if (appid) { this.fromID = appid; this.gamelog.setFromAppID(appid); this.cloud.setFromAppID(appid); this.login.setFromAppID(appid); this.share.setFromAppID(appid); //this.ad.setFromAppID(appid); this.stat.setFromAppID(appid); //this.notify.setFromAppID(appid); // this.voodoo.setFromAppID(appid); // this.voodoo.setLaunchInfo(scene, appid, paramobj); } if (paramobj) { this.lauchparam = paramobj; } this._handleLauchInfo(scene, appid, paramobj); }, setSystemInfo(info) { this.systemInfo = info; this.gamelog.setSystemInfo(info); this.cloud.setSystemInfo(info); this.login.setSystemInfo(info); this.share.setSystemInfo(info); //this.ad.setSystemInfo(info); this.stat.setSystemInfo(info); //this.notify.setSystemInfo(info); //this.voodoo.setSystemInfo(info); }, setUserInfo(info) { this.userInfo = info; if (info.nickName) { this.setNickName(info.nickName); } this._handleUserInfo(info); }, setServerTime(srvtime) { if (!srvtime) { return; } this.loginSrvTime = srvtime; this.loginLocalTime = new Date().getTime(); //this.notify.setServerTime(srvtime, this.loginLocalTime); this._handleServerTime(srvtime, this.loginLocalTime); }, getServerNowTime() { let tm = new Date().getTime(); return this.loginSrvTime + (tm - this.loginLocalTime) / 1000; }, _loadLocalID() { let id = storage.get(_GAME_LOCALUUID); let bneedsave = false; if (!id) { id = this.makeUUID(); bneedsave = true; } this.setLocalUUID(id); if (bneedsave) { this._saveLocalID(); } }, _saveLocalID() { if (!storage.set(_GAME_LOCALUUID, this.localUUID)) { setTimeout(this._saveLocalID.bind(this), 5000); } }, _cleanLocalID() { storage.remove(_GAME_LOCALUUID); }, _loadLaunchInfo() { // if(cc.sys.platform == cc.sys.WECHAT_GAME){ // this.lauchInfo = wx.getLaunchOptionsSync(); // this.gamelog.handlelaunch(this.lauchInfo, (sharetype, shareparam, shareid, inviterid, // fromid, fromscene, launchobj) => { // this.shareInfo.type = sharetype; // this.shareInfo.param = shareparam; // this.shareInfo.id = shareid; // this.shareInfo.inviter = inviterid; // this.setFromAppID(fromid, fromscene, launchobj); // }); // } this.gamelog.logLauch2(); }, _loadSystemInfo() { if (cc.sys.platform == cc.sys.WECHAT_GAME) { let info = wx.getSystemInfoSync(); this.gamelog.logLaunchSystemInfo(info); this.setSystemInfo(info); } }, _loadConfig() { this.initGlobalConfig(); }, _initHandler(obj) { if (this.gameID && obj.onJCInit) { obj.onJCInit(this.channelID, this.gameID, this.isOffical); } if (this.accountID && this.sessionID && obj.onJCAccountID) { obj.onJCAccountID(this.accountID, this.sessionID, this.accountobj); } if (this.localUUID && obj.onJCLocalUUID) { obj.onJCLocalUUID(this.localUUID); } if (this.nickName && obj.onJCNickName) { obj.onJCNickName(this.nickName); } if (this.userInfo && obj.onJCUserInfo) { obj.onJCUserInfo(this.userInfo); } if (this.fromID && obj.onJCLaunchInfo) { obj.onJCLaunchInfo(this.scene, this.fromID, this.lauchparam); } }, _handleInit(channelid, gameid, isoffical) { this._handlers.forEach((element) => { if (element.onJCInit) { element.onJCInit(channelid, gameid, isoffical); } }); }, _handleAccountID(accountid, sessionid, exobj) { this._handlers.forEach((element) => { if (element.onJCAccountID) { element.onJCAccountID(accountid, sessionid, exobj); } }); }, _handleLocalID(localuuid) { this._handlers.forEach((element) => { if (element.onJCLocalUUID) { element.onJCLocalUUID(localuuid); } }); }, _handleNickName(nickname) { this._handlers.forEach((element) => { if (element.onJCNickName) { element.onJCNickName(nickname); } }); }, _handleUserInfo(userinfo) { this._handlers.forEach((element) => { if (element.onJCUserInfo) { element.onJCUserInfo(userinfo); } }); }, _handleLauchInfo(lauchscene, lauchfromid, launchparam) { this._handlers.forEach((element) => { if (element.onJCLaunchInfo) { element.onJCLaunchInfo(lauchscene, lauchfromid, launchparam); } }); }, _handleServerTime(servertime, localtime) { this._handlers.forEach((element) => { if (element.onJCServerTime) { element.onJCServerTime(servertime, localtime); } }); }, _handleGameStart(startmode, extparam) { this._handlers.forEach((element) => { if (element.onJCGameStart) { element.onJCGameStart(startmode, extparam); } }); }, _handleGameOver(score, bsuccess, extparam) { this._handlers.forEach((element) => { if (element.onJCGameOver) { element.onJCGameOver(score, bsuccess, extparam); } }); }, _handleGameShow(showinfo) { this._handlers.forEach((element) => { if (element.onJCGameOver) { element.onJCGameShow(showinfo); } }); }, _handGameHide() { this._handlers.forEach((element) => { if (element.onJCGameOver) { element.onJCGameHide(showinfo); } }); }, _handleNotifyMsgs(lst, notifyobj) { this._handlers.forEach((element) => { if (element.onJCNotifyMsg) { element.onJCNotifyMsg(lst, notifyobj); } }); }, _checkListener() { if (!this._listenerID) { this._listenerID = notification.on( _GAME_INFO_HANDLER_BINDING, (msgid, handler) => { this.addHandler(handler); }, this ); } if (!this._unlistenerID) { this._unlistenerID = notification.on( _GAME_INFO_HANDLER_UNBINDING, (msgid, handler) => { this.delHandler(handler); }, this ); } }, _fixChannelID() { if (cc.sys.platform == cc.sys.WECHAT_GAME) { this.channelID = 6001; } else if (cc.sys.platform == cc.sys.QQ_PLAY) { this.channelID = 6002; } if ( cc.sys.platform == cc.sys.MOBILE_BROWSER || cc.sys.platform == cc.sys.DESKTOP_BROWSER ) { if (this.channelID == 6001 || this.channelID == 6002) { this.channelID = 6000; } } }, };