game/src/jcfw/jcfw.js
2019-03-21 10:29:45 +08:00

641 lines
18 KiB
JavaScript
Executable File
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 jccloud = require('./service/jccloud');
var jcgamelog = require('./service/jcgamelog');
var jclogin = require('./service/jclogin');
var jcshare = require('./service/jcshare');
var jcAD = require('./service/jcAD');
var jcStat = require('./service/jcstat');
var jcNotify = require('./service/jcnotify');
var storage = require('./common/storage');
const notification = require('./common/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';
const JC_CLOUD_URL = 'cloud';
const JC_GAMELOG_URL = 'gamelog';
const JC_LOGIN_URL = 'login';
const JC_NOTIFY_URL = 'notify';
const JC_SVC_URL = 'service';
const JC_STAT_URL = 'stat';
const JC_GAMELIST_URL = 'service';
var JCFW = {
cloud: jccloud,
gamelog: jcgamelog,
login: jclogin,
share: jcshare,
ad: jcAD,
stat: jcStat,
notify: jcNotify,
localUUID: '',
systemInfo: null,
lauchInfo: null,
shareInfo: {},
_handlers: [],
_inited: false,
__getUrl(isoffical, key) {
let pre = key;
if (!isoffical) {
pre += '-test';
}
let str = 'https://' + pre + '.kingsome.cn/webapp/index.php';
// let str = 'https://www.xmjl123.cn/' + pre + '/webapp/index.php';
return str;
},
// 初始化
/**
*
* @param {number} channelid 频道ID
* @param {number} gameid 游戏ID
* @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) {
if (this._inited) {
if (this.rsp != response) {
this.rsp = response;
}
return;
}
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.__getUrl(isoffical, JC_GAMELOG_URL));
this.cloud.init(this.channelID, this.gameID, isoffical, this, this.__getUrl(isoffical, JC_CLOUD_URL));
this.login.init(this.channelID, this.gameID, isoffical, this, env, this.__getUrl(isoffical, JC_LOGIN_URL));
this.share.init(this.channelID, this.gameID, isoffical, this, this.__getUrl(isoffical, JC_SVC_URL));
this.ad.init(this.channelID, this.gameID, isoffical, this);
this.stat.init(this.channelID, this.gameID, isoffical, this, this.__getUrl(isoffical, JC_STAT_URL));
this.notify.init(this.channelID, this.gameID, isoffical, this, this.__getUrl(isoffical, JC_NOTIFY_URL));
this._loadLocalID();
this._loadLaunchInfo();
this._loadSystemInfo();
//this._loadConfig();
this._handleInit(this.channelID, this.gameID, isoffical);
this._inited = true;
},
/**
* 登陆函数
* @param {object} successcb :成功回调(初始化时设置了回调对象的话,此处可不填)
* @param {object} failcb :失败回调(初始化时设置了回调对象的话,此处可不填)
* @param {string} ex_param :额外参数(预留字段,某些平台可能会使用此字段)
*/
loginUser(successcb, failcb, ex_param) {
//console.log('loginUser');
if (this._logining) {
return 1;
}
this.logining = true;
this.login.loginPT((res) => {
this._logining = false;
this.logined = true;
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.logShareInvite(this.shareInfo.inviter, this.shareInfo.type, this.shareInfo.id, this.shareInfo.param);
this.gamelog.logSystemInfo(this.systemInfo);
if (this.shareInfo && this.shareInfo.id && this.shareInfo.id != '') {
this.share.acceptDailyInvite(this.shareInfo.param, this.shareInfo.id, () => {
}, () => {
});
}
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;
},
loginUserByToken(token, successcb, failcb) {
//console.log('loginUserByToken:' + token);
return this.loginUser(successcb, failcb, token);
},
// 激活
/**
*
* @param {object} allinfo :包含用户信息的对象: 注意allinfo.userInfo为用户实际信息
* @param {object} successcb :成功回调(初始化时设置了回调对象的话,此处可不填)
* @param {object} failcb :失败回调(初始化时设置了回调对象的话,此处可不填)
*/
updateUser(allinfo, successcb, failcb) {
//console.log('updateUser');
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 && allinfo.userInfo;
if (!userinfo) {
this.gamelog.logAuthFail();
this.setUserInfo(null);
} else {
this.gamelog.logAuthSuccess(userinfo);
this.setUserInfo(userinfo);
}
if (!allinfo || !userinfo) {
this.rsp && this.rsp.onUpdateUserFail && this.rsp.onUpdateUserFail(0, -10000, 'info is NULL');
failcb && failcb(0, -10000, 'info is NULL');
return 0;
}
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);
});
},
// 生成唯一ID【无需关注此函数】
makeUUID() {
return this.gamelog.generateUUID();
},
// 根据OpenID生成账号ID【通常情况不使用此函数】
makeAccountID(openid) {
return this.login.AccountID(openid);
},
// 添加监听处理器
// handler是回调对象按实际需要可实现以下一个或多个回调
// .onJCInit(channelid, gameid, isoffical){}
// .onJCAccountID(accountid, sessionid){}
// .onJCLocalUUID(localuuid){}
// .onJCUserInfo(userinfo){}
// .onJCLaunchInfo(lauchscene, lauchfromid, launchparam){}
// .onJCGameStart(startmode, extparam){}
// .onJCGameOver(score, bsuccess, extparam){}
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;
},
// 重置本地ID【通常情况不使用此函数】
resetLocalID() {
this._cleanLocalID();
this._loadLocalID();
},
// 设置sdk回调
setResponse(rsp) {
this.rsp = rsp;
},
// 获取服务器当前时间【登陆完成后才能调用】
getServerNowTime() {
let tm = new Date().getTime();
return this.loginSrvTime + (tm - this.loginLocalTime) / 1000;
},
//游戏相关-------------------------------------------------------
// 游戏开始
// startmode: 0:正常开始游戏 1:重新开始 2:再来一次
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._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._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._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);
}
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);
},
setUserInfo(info) {
if (!this._userInfo) {
this._userinfo = {};
}
this.login.appendUserInfo(this._userinfo, info);
// this.userInfo = info;
if (this._userinfo.nickName) {
this.setNickName(this._userinfo.nickName);
}
this.share.setUserInfo(this._userinfo);
this._handleUserInfo(this._userinfo);
},
setServerTime(srvtime) {
if (!srvtime) {
return;
}
this.loginSrvTime = srvtime;
this.loginLocalTime = new Date().getTime();
this.notify.setServerTime(srvtime, this.loginLocalTime);
this._handleServerTime(srvtime, this.loginLocalTime);
},
_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() {
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);
});
},
_loadSystemInfo() {
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.onJCGameShow) {
element.onJCGameShow(showinfo);
}
});
},
_handGameHide(hideinfo) {
this._handlers.forEach(element => {
if (element.onJCGameHide) {
element.onJCGameHide(hideinfo);
}
});
},
_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() {
}
};
module.exports = JCFW;