import Comm_Log from "./Comm_Log";
import Comm_Main from "../Comm_Main";
const CCGlobal = require('CCGlobal');
const CCConst = require('CCConst');
const Stat = require("Statistics");
const Ads = require('AdsFunc');
const Share = require('Share');
const QQRank = require("QQrank");
const Examin = require('Examin');
interface VideoCallBack {
pos?: string,
successShare: Function,
sucessVedio: Function,
failShare: Function,
failVedio: Function,
};
/**
* 通用平臺組件
*/
class Comm_Platform {
private constructor() { };
private static instance: Comm_Platform = null;
public static getInstance(): Comm_Platform {
this.instance = this.instance || new Comm_Platform();
return this.instance;
};
private _gameKey: string = ''; // 玩法key值
private _statName: string = ''; // 統計時使用的遊戲名
private _api: any = null; // sdk
private _isInit: boolean = false; // 初始化
private _networkState: boolean = false; // 網絡狀態
private _isUseVideo: boolean = false; // 是否在用激勵視頻
private _isShowVideoPoint: boolean = true; // 是否顯示視頻點
// private _creatInsert: Function = null; // 創建插屏
private _isShowBanner: boolean = false; // 是否顯示banner
/**
* 獲取網絡狀況
*/
public get networkStatus(): boolean {
return this._networkState;
};
/**
* 是否正在使用視頻
*/
public get isUseVideo(): boolean {
return this._isUseVideo;
};
/**
* 是否顯示視頻點
*/
public get isShowVideoPoint(): boolean {
return this._isShowVideoPoint;
};
/**
* 配置遊戲key值
*/
public setGameKey(key: string) {
if (key) {
this._gameKey = key;
}
};
/**
* 配置統計用的名字
*/
public setStatName(name: string) {
if (name) {
this._statName = name;
}
};
/**
* 初始化
* @param key 玩法key
* @param statName 玩法統計名
*/
public init(key: string, statName: string) {
if (this._isInit) return;
this._isInit = true;
this._gameKey = key;
this._statName = statName;
this._isShowVideoPoint = (window.GameConfig && window.GameConfig.info && window.GameConfig.info.showAd == "true") ? true : false;
let needInsert = false;
//根據不同平臺,處理相應的邏輯
if (CCGlobal.platform == CCConst.PLATFORM.WEIXIN) {
this._api = wx;
needInsert = true;
} else if (CCGlobal.platform == CCConst.PLATFORM.TT) {
this._api = tt;
// Ads.createVideoAd();
} else if (CCGlobal.platform == CCConst.PLATFORM.OPPO) {
this._api = qg;
} else if (CCGlobal.platform == CCConst.PLATFORM.QQ) {
this._api = qq;
} else if (CCGlobal.platform == CCConst.PLATFORM.XIAOMI) {
this._api = qg;
needInsert = true;
} else if (CCGlobal.platform == CCConst.PLATFORM.UC) {
this._api = uc;
} else if (CCGlobal.platform == CCConst.PLATFORM.MEIZU) {
this._api = mz;
needInsert = true;
}
if (!this._api) return;
// 獲取網絡狀態
this._api.getNetworkType({
success: (res) => {
Comm_Log.log('network:', JSON.stringify(res));
if (res.networkType == 'none') {
this._networkState = false;
} else {
this._networkState = true;
}
}
});
// 監聽網絡狀態
this._api.onNetworkStatusChange((res) => {
Comm_Log.log('network:', JSON.stringify(res));
this._networkState = !!res.isConnected;
});
if (needInsert) {
// 創建插屏
if (CCGlobal.platform == CCConst.PLATFORM.MEIZU) {
mz.offShow();
mz.onShow(() => {
this._creatInsert();
});
} else {
cc.game.off(cc.game.EVENT_SHOW, this._creatInsert, this);
cc.game.on(cc.game.EVENT_SHOW, this._creatInsert, this);
}
}
};
private _creatInsert() {
Comm_Log.log('切前後臺,顯示插屏');
Ads.setInsertAdVisible(true);
this._statInsertAdAll();
let obj = {
loadCallback: this._statInsertAdSelect.bind(this),
showCallback: this._statInsertAdShow.bind(this),
};
Ads.createInsertAd(obj);
}
/**
* 服務器更新分數(QQ專用)
* @param score
*/
public updateScore(score: number, key: string = this._gameKey) {
if (key) {
if (CCGlobal.platform == CCConst.PLATFORM.QQ) {
QQRank.updateRecord(key, score);
}
}
};
/* *************banner相關*************** */
public creatBanner(isHide = true, adNode: cc.Node = null, key: string = this._gameKey, pos: string = '') {
Ads.initBannerNode(adNode);
if (CCGlobal.platform == CCConst.PLATFORM.WEIXIN) {
let model = {
key: key,
pos: pos,
};
if (!this._canShowBanner(adNode)) {
this.hideBanner();
return;
}
Ads.createBanner({ model: model, flag: 0.5, isHide: isHide });
} else if (CCGlobal.platform == CCConst.PLATFORM.OPPO) {
Ads.createBanner({ isHide: isHide });
} else if (CCGlobal.platform == CCConst.PLATFORM.TT) {
if (!this._canShowBanner(adNode)) {
this.hideBanner();
return;
}
Ads.createBanner({ isHide: isHide });
} else if (CCGlobal.platform == CCConst.PLATFORM.QQ) {
if (!this._canShowBanner(adNode)) {
this.hideBanner();
return;
}
Ads.createBanner({ isHide: isHide });
}
else if (CCGlobal.platform == CCConst.PLATFORM.MEIZU) {
if (!this._canShowBanner(adNode)) {
this.hideBanner();
return;
}
Ads.createBanner({ isHide: isHide });
}
};
// 顯示banner
public showBanner(adNode?: cc.Node) {
if (!this._canShowBanner(adNode)) {
this.hideBanner();
return;
}
console.log("show banner");
Ads.showBanner();
this._isShowBanner = true;
};
// 隱藏banner
public hideBanner() {
console.log("hide banner");
Ads.hideBanner();
this._isShowBanner = false;
};
// 判斷是否能顯示banner
private _canShowBanner(adNode: cc.Node) {
if (adNode == null) return true;
let size = cc.view.getFrameSize();
let y = adNode.getPosition().y;
let realHeight = size.height * cc.view.getDesignResolutionSize().width / size.width;
let h = realHeight / 2 + y;
let adH = cc.view.getDesignResolutionSize().width / size.width * 104;
console.log('h: ', h, 'adH: ', adH);
if (h < adH - 100) {
return false;
} else {
return true;
}
};
/* *************插屏相關*************** */
/**
* 顯示插屏
* @param obj
*/
public showInsertAd(obj?: any) {
Ads.setInsertAdVisible(true);
Stat.reportEvent("insertads", this._gameKey + "_all", "count");
let temp = {
loadCallback: () => {
if (obj && obj.loadCallback) {
obj.loadCallback();
}
Stat.reportEvent("insertads", this._gameKey + "_select", "count");
},
showCallback: () => {
if (obj && obj.showCallback) {
obj.showCallback();
}
Stat.reportEvent("insertads", this._gameKey + "_show", "count");
},
}
Ads.createInsertAd(temp);
};
/* *************視頻相關*************** */
/**
* 顯示視頻廣告
* @param {Object} options
*/
public showVideoAds(options: VideoCallBack) {
cc.audioEngine.pauseMusic();
if (this._isUseVideo) {
console.log('正在使用視頻播放');
return;
}
this._isUseVideo = true;
console.log('網絡狀態: ', this._networkState);
// 網絡狀態
if (this._networkState) {
let model = {
key: this._gameKey,
pos: options.pos
};
if (CCGlobal.platform == CCConst.PLATFORM.WEIXIN) {
Ads.createVideoAd({ model: model });
} else if (CCGlobal.platform == CCConst.PLATFORM.MEIZU) {
Ads.createVideoAd();
} else if (CCGlobal.platform == CCConst.PLATFORM.UC) {
Ads.createVideoAd();
}
this.hideBanner();
Ads.loadVideoAd({
suCallback: () => {
this._isUseVideo = false;
if (typeof options.sucessVedio === 'function') {
options.sucessVedio();
Ads.setInsertAdVisible(false);
cc.audioEngine.resumeMusic();
}
if (this._isShowBanner) {
this.showBanner();
}
},
failCallback: () => {
this._isUseVideo = false;
if (typeof options.failVedio === 'function') {
options.failVedio();
cc.audioEngine.resumeMusic();
}
// 視頻未播放完成
Comm_Main.showToast('觀看完整視頻才能獲得獎勵哦!');
console.log('觀看完整視頻才能獲得獎勵哦!');
if (this._isShowBanner) {
this.showBanner();
}
},
errCallback: (err) => {
this._isUseVideo = false;
if (Examin.switchState) {
Comm_Main.showToast('您的網絡不穩定,請稍後再試!');
cc.audioEngine.resumeMusic();
// wx.showToast({
// title: "",
// icon: "none",
// duration: 2000,
// });
} else if (typeof options.successShare === 'function' && this._networkState) {
Share.shareGameMsg({
key: this._gameKey,
suCallback: () => {
options.successShare();
cc.audioEngine.resumeMusic();
},
failCallback: () => {
options.failShare();
cc.audioEngine.resumeMusic();
}
})
} else {
Comm_Main.showToast('暫時無法獲取視頻,請稍後再試!');
cc.audioEngine.resumeMusic();
}
if (this._isShowBanner) {
this.showBanner();
}
}
})
} else {
// 無網絡
Comm_Main.showToast('您的網絡不穩定,請稍後再試!');
console.log('您的網絡不穩定,請稍後再試!');
this._isUseVideo = false;
}
};
/* *************統計相關*************** */
/**
* 統計玩家數量
* @param storageStr 用於存儲的字符串
* @param statName 本玩法的名字
*/
public statPlayerCount(storageStr: string, statName: string = this._statName) {
if (storageStr && statName) {
let isFirst = cc.sys.localStorage.getItem(storageStr);
if (!isFirst) {
cc.sys.localStorage.setItem(storageStr, '1');
Stat.reportEvent("playpeople", statName, "count");
}
}
};
/**
* 統計遊戲開始
* @param event 統計的事件
* @param statName 本玩法的名字
*/
public statEventCount(event: string, statName: string = this._statName) {
if (statName) {
Stat.reportEvent(event, statName, "count");
}
};
/**
* 統計遊戲開始
* @param statName 本玩法的名字
*/
public statGameStart(statName: string = this._statName) {
if (statName) {
Stat.reportEvent("playtime", statName, "time");
Stat.reportEvent("starttimes", statName, "count");
}
};
/**
* 統計遊戲結束
* @param statName 本玩法的名字
*/
public statGameEnd(statName: string = this._statName) {
if (statName) {
Stat.reportEvent("playtime", statName, "time");
Stat.reportEvent("overtimes", statName, "count");
}
};
/**
* 統計視頻點
* @param videoName 視頻點類型的名字
* @param statName 玩法的名字
*/
public statVideoAd(videoName: string, statName: string = this._statName) {
if (videoName && statName) {
Stat.reportEvent("video_" + statName, videoName, "ad");
Stat.reportEvent("video", statName, "count");
}
};
/**
* 統計插屏廣告-總數
*/
private _statInsertAdAll() {
Stat.reportEvent("insertads", "home_all", "count");
};
/**
* 統計插屏廣告被點擊
*/
private _statInsertAdSelect() {
Stat.reportEvent("insertads", "home_select", "count");
};
/**
* 統計插屏廣告展示
*/
private _statInsertAdShow() {
Stat.reportEvent("insertads", "home_show", "count");
};
}
export default Comm_Platform.getInstance();