import { _decorator, Component, Node, sys } from 'cc'; import { Singleton } from '../../mx/tools/Singleton'; import { ItemNumType, OtherDataType, StatisticsType, StatusType } from '../game/GameData'; import { TableNames } from '../game/ConfigTableData'; import { DEBUG, DEV } from 'cc/env'; import { sdkConfig } from '../sdk/sdkConfig'; import { ItemData } from '../game/ConfigProjectData'; const { ccclass, property } = _decorator; /**内购模块 */ @ccclass('Purchase') export class Purchase extends Singleton { /**购买商品 */ buy(payId: string, callback: Function) { if (DEV || DEBUG || sys.isBrowser) { callback(true); return; } if (!sdkConfig.isIAPVersion) { gg.sdk.showVideoAd(callback); return; } //验证时间 gg.sdk.checkTime((curentTime) => { if (curentTime) { console.error("时间异常"); gg.ui.showToast("时间异常,请检查时间设置"); } else { //在这后面调内购的API并返回callback回调.... callback(true); gg.data.saveToServer(null, true); } }); } /**每天重置 */ everyDayInit() { gg.data.setStatus(StatusType.MonCardDailyReward1, 0); gg.data.setStatus(StatusType.MonCardDailyReward2, 0); gg.data.setStatus(StatusType.MonCardDailyReward3, 0); this.resetCurentFinishBuyContinuousBag(); } /**根据商品key获取支付配置信息 */ getPayConfig(payId: string) { let payConfig = gg.data.table.getTableList(TableNames.Pay).find(x => x.payId == payId); if (!payConfig) { console.error("未找到支付配置", payId); return null; } return payConfig; } //#region 月卡相关 /** * 判断是否激活月卡(1:小月卡,2:大月卡,3:终身卡) */ isActiveMonCard(type: MonthCardType = MonthCardType.Small) { let t = this.getMonCardStatisticsType(type); let expireTime = gg.data.getStatistics(t); let now = gg.sdk.getServerTime(); return expireTime > now; } /**获取月卡剩余时间 */ getMonCardRemainTime(type: MonthCardType = MonthCardType.Small) { let t = this.getMonCardStatisticsType(type); let expireTime = gg.data.getStatistics(t); let now = gg.sdk.getServerTime(); return Math.max(0, expireTime - now); } /**激活月卡 */ activeMonCard(type: MonthCardType = MonthCardType.Small) { //剩余时间 let now = gg.sdk.getServerTime(); let remainTime = this.getMonCardRemainTime(type); let expireTime = remainTime + now + 1000 * 60 * 60 * 24 * 30; if (type == MonthCardType.Forever)//终身卡 expireTime = 999999999999999999; let t = this.getMonCardStatisticsType(type); gg.data.setStatistics(t, expireTime); } /**领取月卡每日奖励 */ getDailyReward(type: MonthCardType = MonthCardType.Small) { let t = this.getMonCardStatusType(type); gg.data.setStatus(t, 1); } /**今日月卡奖励是否已经领取 */ isGetDailyReward(type: MonthCardType = MonthCardType.Small) { let isActiv = this.isActiveMonCard(type); if (!isActiv) return true; let t = this.getMonCardStatusType(type); return gg.data.getStatus(t) == 1; } /**根据类型获取状态类型 */ getMonCardStatusType(type: MonthCardType = MonthCardType.Small) { let t = StatusType.MonCardDailyReward1; if (type == MonthCardType.Big) t = StatusType.MonCardDailyReward2; if (type == MonthCardType.Forever) t = StatusType.MonCardDailyReward3; return t; } /**根据类型获取月卡统计类型 */ getMonCardStatisticsType(type: MonthCardType = MonthCardType.Small) { let t = StatisticsType.MonCardExpireTime1; if (type == MonthCardType.Big) t = StatisticsType.MonCardExpireTime2; if (type == MonthCardType.Forever) t = StatisticsType.MonCardExpireTime3; return t; } /**获取出怪倍数 */ getMonCardSpawerTimeScale() { let count = 0; for (let key in MonthCardType) { let typeNum = MonthCardType[key as keyof typeof MonthCardType] let isActiv = this.isActiveMonCard(typeNum); let data = this.getMonCardFunctionData(typeNum, 1); if (isActiv && data && data.function_num > count) count = data.function_num; } return count; } /**获取免费刷新次数 */ getFreeRefreshTimes() { let count = 0; for (let key in MonthCardType) { let typeNum = MonthCardType[key as keyof typeof MonthCardType] let isActiv = this.isActiveMonCard(typeNum); let data = this.getMonCardFunctionData(typeNum, 2); if (isActiv && data) count += data.function_num; } return count; } /**获取每日关卡及副本免费扫荡次数 */ getFreeSweepTimes(type: MonthCardType = null) { let count = 0; for (let key in MonthCardType) { let typeNum = MonthCardType[key as keyof typeof MonthCardType] if (type && typeNum != type) continue; let isActiv = this.isActiveMonCard(typeNum); let data = this.getMonCardFunctionData(typeNum, 3); if (isActiv && data) count += data.function_num; } return count; } /**是否可以卸载彩虹飞弹 */ isCanUninstallRainbowBullet() { for (let key in MonthCardType) { let typeNum = MonthCardType[key as keyof typeof MonthCardType] let isActiv = this.isActiveMonCard(typeNum); let data = this.getMonCardFunctionData(typeNum, 4); if (isActiv && data) return true; } return false; } /**获取主线奖励加成 */ getMainLineRewardAdd() { let count = 0; // for (let key in MonthCardType) { // let typeNum = MonthCardType[key as keyof typeof MonthCardType] // let isActiv = this.isActiveMonCard(typeNum); // let data = this.getMonCardFunctionData(typeNum, 5); // if (isActiv && data) count += data.function_num; // } return count / 10000; } /**根据月卡类型和功能类型查询月卡功能数据 */ getMonCardFunctionData(type: MonthCardType, functionType: number) { return gg.data.table.getTableList(TableNames.MonthCard).find(x => x.type == type && x.function_type == functionType); } /**检查是否有可领取的月卡奖励 */ checkHaveCanGetMonthReward() { for (let key in MonthCardType) { let typeNum = MonthCardType[key as keyof typeof MonthCardType] let isActiv = this.isActiveMonCard(typeNum); let isGet = this.isGetDailyReward(MonthCardType.Small); if (isActiv && !isGet) return true; } return false; } //#endregion //#region 首充礼包 /**根据天数获取首充礼包数据 */ getFirstRechargeData() { let dataList = gg.data.table.getTableList(TableNames.FirstBag); return dataList; } /**是否已经购买 */ isBuyFirstRecharge() { return gg.data.getStatus(StatusType.FirstRecharge) > 0; } /**激活首充礼包 */ activeFirstRecharge() { gg.data.setStatus(StatusType.FirstRecharge, gg.sdk.getServerTime()); } /**根据天数设置领取首充礼包状态 */ setGetFirstRecharge(day: number) { gg.data.setkeyKeyValue(OtherDataType.FirstRechargeGetRewardStatus, day, "1"); } /**根据天数检查是否已经领取 */ isGetFirstRecharge(day: number) { let status = gg.data.getkeyKeyValue(OtherDataType.FirstRechargeGetRewardStatus, day); return status == "1"; } /**根据天数判断是否可以领取 */ canGetFirstRecharge(day: number) { if (!this.isBuyFirstRecharge()) return false; let activeTime = gg.data.getStatus(StatusType.FirstRecharge); let now = gg.sdk.getServerTime(); let diff = now - activeTime; let dayDiff = Math.floor(diff / (1000 * 60 * 60 * 24)); return dayDiff >= day - 1; } /**检查是否有可领取的首充礼包 */ checkFirstRechargeCanGet() { if (!this.isBuyFirstRecharge()) return false; for (let i = 1; i <= 3; i++) { let canGet = this.canGetFirstRecharge(i); let isfinish = this.isGetFirstRecharge(i); if (canGet && !isfinish) return true; } return false; } //#endregion //#region 连续礼包 /**获取当前可以购买的连续礼包列表 */ getCurentCanGetContinuousBagList(day: number) { let list = this.getContinuousList(day); if (list.length == 0) return []; let curentBuyIndex = this.getCurentFinishBuyContinuousBag(); let resultList: ContinuousBagData[] = list.filter(x => x.index > curentBuyIndex); return resultList; } /**获取当前连续礼包列表 */ getContinuousList(day: number) { let dataList: ContinuousBagData[] = []; let config = this.getContinuousBagData(day); if (config) { dataList = gg.purchase.parseContinuousBagData(config.reward); } return dataList; } /**根据天数获取连续礼包配置 */ getContinuousBagData(day: number) { let dataList = gg.data.table.getTableList(TableNames.ContinueBag); let data: ITableContinueBag = null; for (let i = day; i > 1; i--) { data = dataList.find(x => x.id == i); if (data) break; } return data; } /**解析连续礼包配置 */ parseContinuousBagData(configStr: string) { // 支付id;奖励1|奖励2#支付id;奖励1|奖励2 */ // 字符串解析规则:#分割档位,;分割类型和奖励,|分割奖励 let result: ContinuousBagData[] = []; let arr = configStr.split("#"); for (let i = 0; i < arr.length; i++) { let item = arr[i]; let data = new ContinuousBagData(); let arr2 = item.split(";"); data.index = i + 1; data.payID = arr2[0]; let rewardStr = arr2[1]; let rewardArr = rewardStr.split("|"); for (let j = 0; j < rewardArr.length; j++) { let [id, num] = rewardArr[j].split(","); let itemData = new ItemData(Number(id), parseInt(num)); itemData.staticData = gg.data.table.getItemData(Number(id)); data.rewards.push(itemData); } result.push(data); } return result; } /**获取当前购买的连续礼包索引 */ getCurentFinishBuyContinuousBag() { return gg.data.getStatus(StatusType.CurrentBuyContinuousBagIndex); } /**设置当前购买的连续礼包索引 */ setCurentFinishBuyContinuousBag(index: number) { gg.data.setStatus(StatusType.CurrentBuyContinuousBagIndex, index); } /**重置当前购买的连续礼包索引 */ resetCurentFinishBuyContinuousBag() { this.setCurentFinishBuyContinuousBag(0); } /**根据天数验证是否有连续礼包可显示 */ canShowContinuousBag(day: number) { let data = this.getCurrentUnlockedContinuousBag(day); return data != null; } /**检查是否有可领取的免费连续礼包 */ checkCanGetFreeContinuousBag(day: number) { let daily = this.getCurrentUnlockedContinuousBag(day); return daily && daily.payID == "0"; } /**获取当前解锁的连续礼包 */ getCurrentUnlockedContinuousBag(day: number) { let list = this.getCurentCanGetContinuousBagList(day); if (list.length == 0) return null; return list[0]; } //#endregion //#region 基金 /**获取基金配置列表 */ getFundList() { let dataList = gg.data.table.getTableList(TableNames.Fund); return dataList; } /**检查基金是否开放 */ checkIsOpen() { return gg.data.doc.chapter > 3; } /**检查基金是否已购买 */ checkIsBuyFund() { return gg.data.getStatus(StatusType.CurrentBuyFundIndex) > 0; } /**设置已经购买基金索引 */ setCurentFinishBuyFundIndex(index: number) { gg.data.setStatus(StatusType.CurrentBuyFundIndex, index); } /**根据等级查询是否已经领取等级基金奖励 * @param lv 等级 * @param isPay 是否付费 * @returns 是否已领取 */ checkIsGetFundReward(lv: number, isPay = false) { let t = isPay ? OtherDataType.PayFundRewardLv : OtherDataType.FreeFundRewardLv; let arr = gg.data.getArrayData(t); let index = arr.indexOf(lv + ""); return index >= 0; } /**设置已领取基金奖励状态 */ setGetFundRewardStatus(lv: number, isPay) { if (this.checkIsGetFundReward(lv, isPay)) return; let t = isPay ? OtherDataType.PayFundRewardLv : OtherDataType.FreeFundRewardLv; let arr = gg.data.getArrayData(t); arr.push(lv + ""); gg.data.setArrayData(t, arr); } /**获取所有基金付费奖励的钻石数量 */ getAllFundPayRewardNum() { let dataList = this.getFundList(); let num = 0; for (let i = 0; i < dataList.length; i++) { let data = dataList[i]; let rewardStrArr = data.buy_reward.split("|"); let [id, itemNum] = rewardStrArr[0].split(","); if (Number(id) == ItemNumType.diamond) num += parseInt(itemNum); } return num; } /**检查是否有可领取的基金奖励 */ checkCanGetFundReward() { let isPay = this.checkIsBuyFund(); let lv = gg.game.getRoleLv(); let list = this.getFundList(); for (let i = 0; i < list.length; i++) { let data = list[i]; if (data.player_level > lv) break; if (!this.checkIsGetFundReward(data.player_level, false)) return true; if (isPay && !this.checkIsGetFundReward(data.player_level, true)) return true; } return false; } //#endregion //#region 章节礼包 /**获取可显示的章节礼包 */ getCanShowChapterGiftList() { let chapter = gg.data.doc.chapter; let list = gg.data.table.getTableList(TableNames.Chapter_chest); let result: ITableChapter_chest[] = []; for (let i = 0; i < list.length; i++) { let data = list[i]; if (data.chapter > chapter) { if (result.length == 0) { result.push(data); } break; } if (!this.checkIsBuyChapterGift(data.chapter)) { result.push(data); } } return result; } /**检查章节礼包是否已购买 */ checkIsBuyChapterGift(chapterid: number) { let arr = gg.data.getArrayData(OtherDataType.ChapterGiftBuy); let index = arr.indexOf(chapterid + ""); return index >= 0; } /**设置章节礼包已购买 */ setChapterGiftBuy(chapterid: number) { if (this.checkIsBuyChapterGift(chapterid)) return; let arr = gg.data.getArrayData(OtherDataType.ChapterGiftBuy); arr.push(chapterid + ""); gg.data.setArrayData(OtherDataType.ChapterGiftBuy, arr); } //#endregion //#region 钻石商店 /**获取钻石商店配置列表 */ getDiamondShopList() { let dataList = gg.data.table.getTableList(TableNames.ShopBuy); return dataList; } /**检查是否已购买过钻石商店商品 */ checkIsBuyDiamondShop(payID: string) { let arr = gg.data.getArrayData(OtherDataType.BuyDiamondShop); let index = arr.indexOf(payID); return index >= 0; } /**设置已购买钻石商店商品 */ setBuyDiamondShop(payID: string) { if (this.checkIsBuyDiamondShop(payID)) return; let arr = gg.data.getArrayData(OtherDataType.BuyDiamondShop); arr.push(payID); gg.data.setArrayData(OtherDataType.BuyDiamondShop, arr); } //#endregion } /**月卡类型 */ export enum MonthCardType { /**小月卡 */ Small = 1, /**大月卡 */ Big = 2, /**终身卡 */ Forever = 3, } export class ContinuousBagData { /**索引 */ index: number = 1; /**天数 */ payID: string = ""; /**奖励 */ rewards: ItemData[] = []; }