var Config = require('config'); var Util = require('util'); var cloudData = require('cloudData'); var Res = require('res'); // let stringUtil = require('stringUtil'); let isTrue = require('stringUtil').isTrue; const COOKIE_ARCHIVE_DATA = 'cookie_archive_data'; const TRADER_INFO_KEY = 'trader_info_key'; var global = { NetEnum: { STARTGAME: 1, ENTERGAME: 2, MOVE: 3, USE_ITEM: 4, UP_SCORE: 5, CREATE_ROW: 8, GAME_OVER: 9, UPDATE_PLAYER: 10 }, EVENTS: { TRADER_BEGIN: 'trader_begin', // 神秘商人开始 TRADER_END: 'trader_end', // 神秘商人结束 USER_MONEY_UPDATE: 'user_money_update', //用户金币更新事件 CAKE_MAKE_END: 'cake_make_end', // 蛋糕制作结束 CAKE_MAKE_TIME_UPDATE: 'cake_make_time_update', // 蛋糕制作时间更新 }, gameStartState: { NONE: 0,//正常开始 RESTART: 1,//重新开始 REVIVE: 2//复活 }, //本地数据 saveData: { score: 0,//当前分数 level: 1, grids: [], }, levelData: { cookie: {}, bricks: {}, lines: 0, maxCombo: 1 }, /** * 用户存档数据 * cookie 结构 * { * cookId: count * } * cake 结构 * { * cakeId: { * count: 0, 已制作的数量 * lastMake: 121212112 最后一个蛋糕开始制作的时间, 为0的话,表示没有蛋糕在制作中, * active: 0 // 是否已经解锁 * } * } * * */ archiveData: { cookie: {}, cake: {}, gold: 0, alevel: 0, ascore: 0 }, isPlayMusic: true, isPlaySound: true, isVS: false,//是否是对战模式 isCreater: false,//是否是包厢主人 startState: 0, isFirstDead: true,//是否是第一次死亡 lastScore: 0, lastWeek: 0,//最高分对应的周 isShowMaxScoreTips: false,//是否显示过最高纪录 get_item_num_today: 0, item: [0, 0, 0], // 贪吃娃, 锤子, 巧克力 isCanGetItem: true, isAddItem: false,//分享得道具 isGaming: false,//是否游戏中 roomid: 0, creatRoomTime: 0,//创建包厢的时间 毫秒 startTime: 0,//开始游戏时间 毫秒 openid: '', account_id: '', nickName: '', avatarUrl: '', ChannelId: '6502', GameId: '1004', session_id: '', launch_options: '', showLoadingTime: 0,//显示loading界面的时间戳, redpackTodayCount: 0, redpackCount: 0, redpackMoney: 0, // 新手引导状态 guideStatusNum: 0, itemImgs: ['mouth', 'hammer'], LocalStorageSetItem: function (key, value) { try { cc.sys.localStorage.setItem(key, value); } catch (err) { console.log(err); //this.scheduleOnce(this.LocalStorageSetItem(key,value),1); } }, initLocalData: function () { this.isPlayMusic = true; if (this.hasItem('isPlayMusic')) { let val = cc.sys.localStorage.getItem('isPlayMusic'); this.isPlayMusic = isTrue(val); } this.isPlaySound = true; if (this.hasItem('isPlaySound')) { let val = cc.sys.localStorage.getItem('isPlaySound'); this.isPlaySound = isTrue(val); } let day = cc.sys.localStorage.getItem('today'); if (day != null && day != '') { day = Number(day); } let curDay = (new Date()).getDate(); if (curDay != day) { this.get_item_num_today = 0; this.LocalStorageSetItem('todayItemCount', 0); this.LocalStorageSetItem('today', curDay); } else { let count = cc.sys.localStorage.getItem('todayItemCount'); if (count != null && count != '') { this.get_item_num_today = Number(count); } } let isCanGetItem = cc.sys.localStorage.getItem('getItem'); if (isCanGetItem != null && isCanGetItem != '') { this.isCanGetItem = false; } this.getLastScore(); this.loadItem(); this.getSaveData(); this.loadHelpStatus(); let self = this; cloudData.redpackTodayCount(function (count) { self.redpackTodayCount = count; }) cloudData.redpackTotal(function (info) { self.redpackCount = info ? info.count : 0; self.redpackMoney = info ? info.money : 0; }) }, resetVSData: function () { this.roomid = 0; this.isVS = false; this.isCreater = false; this.creatRoomTime = 0; this.startTime = 0; }, hasItem: function(key){ var result = cc.sys.localStorage.getItem(key); if (cc.sys.platform == cc.sys.WECHAT_GAME) { if(result == '' && typeof(result) != 'number'){ return false; } return true; }else{ if(result == null || result == undefined){ return false; } return true; } }, setOnOffMusic: function () { this.isPlayMusic = !this.isPlayMusic; this.LocalStorageSetItem('isPlayMusic', this.isPlayMusic); }, setOnOffSound: function () { this.isPlaySound = !this.isPlaySound; this.LocalStorageSetItem('isPlaySound', this.isPlaySound); }, getFirstDay: function () { var dt = new Date(); var day = dt.getDay() || 7; var newdt = new Date(dt.getFullYear(), dt.getMonth(), dt.getDate() + 1 - day); newdt.setHours(0); newdt.setMinutes(0); newdt.setSeconds(0); newdt.setMilliseconds(0); return newdt; }, formatDateStr: function (dt) { return dt.getFullYear() + '-' + (dt.getMonth() + 1) + '-' + dt.getDate(); }, getLastScore: function () { let dt = this.getFirstDay(); let curData = this.formatDateStr(dt); let lastData = cc.sys.localStorage.getItem('week'); if (lastData == null || lastData == '') { lastData = curData; this.LocalStorageSetItem('week', curData); } if (lastData == curData) { let lastScore = cc.sys.localStorage.getItem('score'); if (lastScore != null && lastScore != '') { lastScore = Number(lastScore); } else { lastScore = 0; } this.lastScore = lastScore; } else { this.LocalStorageSetItem('week', curData); this.lastScore = 0; } }, saveScore: function (score) { if (score > this.lastScore) { this.lastScore = score; let dt = this.getFirstDay(); let curData = this.formatDateStr(dt); this.LocalStorageSetItem('score', score); this.LocalStorageSetItem('week', curData); } }, saveItem: function () { for (let i = 1; i <= 3; i++) { this.LocalStorageSetItem('item' + i, this.item[i - 1] < 0 ? 0 : this.item[i - 1]); } }, // 获得一个食材, 写入当前关卡和总记录 addOneCookie: function(itemId, count) { if (this.archiveData.cookie[itemId]) { this.archiveData.cookie[itemId] += count; } else { this.archiveData.cookie[itemId] = count; } }, saveArchiveData: function() { cc.rw.setStorage(COOKIE_ARCHIVE_DATA, JSON.stringify(this.archiveData), function () { console.log('update user archive data success'); }, function () { console.log('update user archive data error'); }) }, loadArchiveData: function(cb, fcb) { let self = this; cc.rw.getStorage(COOKIE_ARCHIVE_DATA, function (res) { if (res) { try { self.archiveData = JSON.parse(res); } catch (err) { console.log('error load user archive data with JSON ERROR'); } } else { self.archiveData = { cookie: {}, cake: {}, gold: 0, alevel: 0, ascore: 0 }; } cb && cb(); }, function () { console.log('error load user archive data'); fcb && fcb(); }) }, loadItem: function () { // this.item = [10,10,10]; for (let i = 1; i <= 3; i++) { let count = cc.sys.localStorage.getItem('item' + i); if (count != null && count != '') { this.item[i - 1] = Number(count); } } }, // 0: 贪吃娃, 1: 锤子 addItem: function (index, num, nolimit) { if (!nolimit && this.get_item_num_today >= 10) { return; } this.item[index] += num; if (!nolimit) { this.get_item_num_today += num; this.LocalStorageSetItem('todayItemCount', this.get_item_num_today); } }, resetLevelData: function() { this.levelData = { bricks: [0, 0, 0, 0], cookie: {}, maxCombo: 1, lines: 0, } }, updateCombo: function(combo) { this.levelData.maxCombo = this.levelData.maxCombo < combo ? combo : this.levelData.maxCombo; }, updateLevelData: function(obj, lineCount) { for(let i = 1; i <= 4; i ++) { this.levelData.bricks[i - 1] += (obj[i] || 0); } for (let i = 0; i < Config.cookies.length; i ++) { let id = Config.cookies[i].id; if (obj[id]) { if (this.levelData.cookie[id]) { this.levelData.cookie[id] += obj[id]; } else { this.levelData.cookie[id] = obj[id]; } } } this.saveData.cookie = this.levelData.cookie; this.save(); (lineCount) && (this.levelData.lines += lineCount); // this.saveArchiveData(); }, resetSaveData: function () { this.saveData.score = 0; this.saveData.level = 1; this.saveData.grids.length = 0; this.saveData.cookie = {}; this.LocalStorageSetItem('save', JSON.stringify(this.saveData)); }, getSaveData: function () { let curData = cc.sys.localStorage.getItem('save'); if (curData != null && curData !== '') { curData = JSON.parse(curData); this.saveData = curData; this.saveData.level = curData.level; this.saveData.grids = curData.grids; this.saveData.cake = curData.cake || {}; this.saveData.cookie = curData.cookie || {}; } }, save: function () { this.LocalStorageSetItem('save', JSON.stringify(this.saveData)); }, // 获取一个红包 receiveOneRedpack(money) { }, redpackProbability() { if (!this.redpackCount && !this.redpackMoney && !this.redpackTodayCount) { return 0.25; } else if (this.redpackCount >= Config.redpack_max_count || this.redpackMoney >= Config.redpack_max_money || this.redpackTodayCount >= Config.redpack_day_maxcount ) { return 0; } else { return 0.025 * (Config.redpack_day_maxcount - this.redpackTodayCount) / Config.redpack_day_maxcount * (Config.redpack_max_count - this.redpackCount) / Config.redpack_max_count } }, loadHelpStatus() { // android上的qq小游戏在显示新手引导时,有些android机型会出问题 // if (cc.sys.OS_ANDROID === cc.sys.os && window.qq) { //if (window.qq) { // this.guideStatusNum = 2040; //} else { const _val = cc.sys.localStorage.getItem('guideStateNum1'); this.guideStatusNum = parseInt(_val || 0); //} }, /** * 新手引导状态 * @param {number} type 1: 游戏初始化,显示游戏介绍,并引导进蛋糕房, * 2: 蛋糕房引导点击图鉴 * 3: 图鉴解锁提示 * 4:蛋糕房提示材料不足, * 5:消除方块收集材料引导, * 6:游戏界面引导点击蛋糕房 * 7: 当用户第一次点击制作蛋糕时, 引导点击加速按钮 * 8: 当玩家第一次进入第三关,引导玩家点击神秘商人按钮 * 9: 送货成功提示 * 10: 签到提示 * 11: 商店提示 * 12: 获取金币 * 13: 达到预警线时, 显示锤子帮助 * 14: 达到预警线时, 显示贪吃娃帮助 * @return {boolean} result true: 已显示 false: 未显示 * */ getHelpStatus(type) { if (cc.jc.isVerify) { return true; } else { return (this.guideStatusNum & (2 << (type + 1))) === (2 << (type + 1)); } }, getHelpAllStatus(max) { let result = true; for (let i = 1; i < max; i++) { if (!((this.guideStatusNum & (2 << i)) === (2 << i))) { result = false; break; } } return result; }, updateHelpStatus(type) { this.guideStatusNum = this.guideStatusNum | 2 << (type + 1); this.LocalStorageSetItem('guideStateNum1', this.guideStatusNum); }, getVideoAdID() { return cc.jc.adshare.getRandomVideoAdId(); }, ttLogin: false, hasLogin: false, updateDayGiftStatus(cb) { cloudData.updateDayGiftStatus(cb) }, // 获取每日道具奖励状态 getDayGiftStatus(cb) { cloudData.getDayGiftStatus(cb) }, // 根据id获取食材的名字和icon等信息 getCookieInfo(cookId) { let cookies = Config.cookies; cookId = parseInt(cookId); let result; for (let cookie of cookies) { if (cookie.id === cookId) { result = cookie; break; } } return result; }, saveTraderData() { cc.rw.setStorage(TRADER_INFO_KEY, JSON.stringify(this.traderData), function () { console.log('update user archive data success'); }, function () { console.log('update user archive data error'); }) }, resetTraderData() { if (this.traderData) { this.traderData.yuyueKey = 0; this.traderData.step = 0; this.traderData.money = 0; this.traderData.taskId = ''; } else { this.traderData = { money: 0, step: 0, yuyue: 0, yuyueCount: 0, yuyueKey: 0 } } this.saveTraderData(); }, loadTraderData(cb) { let self = this; cc.rw.getStorage(TRADER_INFO_KEY, function (res) { if (res) { try { self.traderData = JSON.parse(res); } catch (err) { console.log('error load user archive data with JSON ERROR'); self.traderData = { money: 0, step: 0, yuyue: 0, yuyueCount: 0, }; } } else { self.traderData = { money: 0, step: 0, yuyue: 0, yuyueCount: 0, }; } cb && cb(self.traderData); }, function () { console.log('error load user archive data'); }) }, getLevelTarget(level) { let score = Config.levels[level - 1]; if (!score) { score = Config.levels[Config.levels.length - 1]; score += score * (level - Config.levels.length) / Config.levels.length } return score; // if (Config.level_target.length >= level) { // return Config.level_target[level - 1]; // } else { // let idx0 = Util.getRandom(Config.cookies.length, 0); // let idx1 = Util.getRandom(Config.cookies.length, 0); // while (idx0 === idx1) { // idx1 = Util.getRandom(Config.cookies.length, 0); // } // let id0 = Config.cookies[idx0].id; // let id1 = Config.cookies[idx1].id; // let obj = {} // obj[id0] = this.getItemCount(level); // obj[id1] = this.getItemCount(level); // return obj; // } }, getItemCount(level) { let s = Math.floor(level / Config.level_target_step); let count0 = Util.getRandom(Config.level_target_max + 1, Config.level_target_min); return Math.floor(count0 * (1 + s * Config.level_target_plus)); }, // 获取用户当前最高的有奖励的成就对象 getMaxAchieve() { let currentLevel = this.archiveData.alevel || 0; let result; for (let obj of Config.achieveCfgs){ if (obj.level <= currentLevel && obj.reward.type === 1) { result = obj; } } return result; }, // 获取当前配置中最大的成就等级 getMaxAchieveLevel() { let level = 0; for (let obj of Config.achieveCfgs) { level = level < obj.level ? obj.level : level; } return level; }, reachMaxLevel() { let maxLevel = this.getMaxAchieveLevel(); let myLevel = this.archiveData.alevel || 0; return myLevel >= maxLevel; }, }; module.exports = global;