GameData.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. import FunBtns from "../game/FunBtns";
  2. import Game from "../game/Game";
  3. /**
  4. * @description 游戏核心玩法数据
  5. * @author 邹勇
  6. */
  7. export class GameData {
  8. public dataFinish: boolean = false;
  9. public savePropFinish: boolean = false;
  10. public savePropsFinish: boolean = false;
  11. public getPropsFnish: boolean = false;
  12. public configs: any = {};
  13. public adShowConfig: ADShowCfg;
  14. /** 功能开启 */
  15. public funOpenData: string[] = [];
  16. public gameData: PlayerProp = null;
  17. /** 标志位:刷新主界面货币 */
  18. public init_coin: boolean = false;
  19. public updatePiggyBankValue: boolean = false;
  20. /** 标志位:刷新主界面头像 */
  21. public init_head: boolean = false;
  22. /** 标志位:刷新主界面红点 */
  23. public init_red_point: boolean = true;
  24. public props: Map<number, number> = new Map<number, number>();
  25. public pools = [
  26. { url: "game/prefab/tips", num: 20 },
  27. { url: "game/prefab/coin", num: 20 }
  28. ];
  29. /** 主页样式脚本 */
  30. public gameStyle: Game = null;
  31. /** 入口按钮 */
  32. public funBtns: FunBtns = null;
  33. /**
  34. * 初始化游戏数据:网络配置信息,用户信息
  35. * @returns
  36. */
  37. public async init() {
  38. let data: any = { "versionCode": gData.appData.version };
  39. let response = await mk.http.sendData('getAllConfigInfo', data);
  40. if (response.errcode != 0) {
  41. if (response.errcode == -10003 || response.errcode == -20003) {
  42. //清除缓存
  43. gData.loginData.reload();
  44. }
  45. return;
  46. }
  47. mk.console.logSingle("getAllConfigInfo", response.data);
  48. this.initConfigs(response.data);
  49. data = {};
  50. response = await mk.http.sendData('getInfoCrypt', data);
  51. if (response.errcode != 0) {
  52. return;
  53. }
  54. mk.console.logSingle("getInfoCrypt", response.data);
  55. this.initPlayerProp(response.data)
  56. this.initProps(response.data.gameUserData);
  57. gData.guideData.init();
  58. gData.guideWeakData.init();
  59. gData.moduleData.forEach(element => {
  60. element.init();
  61. });
  62. data = {};
  63. response = await mk.http.sendData('user/adShowConfig', data);
  64. if (response.errcode != 0) {
  65. return;
  66. }
  67. this.adShowConfig = response.data;
  68. mk.console.logSingle("user/adShowConfig", response.data);
  69. this.dataFinish = true;
  70. }
  71. private initConfigs(data) {
  72. // console.log('config ', data);
  73. this.configs = data;
  74. this.funOpenData = this.configs.ServerConfig.Functionswitch.split(",");
  75. }
  76. /**
  77. * 初始化玩家数据
  78. */
  79. private initPlayerProp(res_data) {
  80. this.gameData = new PlayerProp();
  81. this.gameData.cashIndex = res_data.cashIndex;
  82. this.gameData.gameUserData = res_data.gameUserData;
  83. this.gameData.isSignInToday = res_data.isSignInToday;
  84. this.gameData.isWithdrawable = res_data.isWithdrawable;
  85. this.gameData.lastTime = res_data.lastTime;
  86. this.gameData.loginDays = res_data.loginDays;
  87. this.gameData.newPlayer = res_data.newPlayer;
  88. this.gameData.initPiggyBank(res_data.piggyBank);
  89. this.gameData.piggyBankCashTimes = res_data.piggyBankCashTimes;
  90. this.gameData.redMoney = res_data.redMoney == null ? 0 : res_data.redMoney;
  91. this.gameData.signInDay = res_data.signInDay;
  92. this.gameData.totalPiggyBankCashTimes = res_data.totalPiggyBankCashTimes;
  93. this.gameData.turntableTimes = res_data.turntableTimes;
  94. this.gameData.userTuCaoInfo = res_data.userTuCaoInfo;
  95. this.gameData.versioncfg = res_data.versioncfg;
  96. this.gameData.isFirstRedMoney = res_data.isFirstRedMoney;
  97. gData.adData.watchNumToday = res_data.dayVideoTimesForRedMoney == null ? 0 : res_data.dayVideoTimesForRedMoney;
  98. }
  99. /**
  100. * 保存单个数据
  101. */
  102. public async setProp(key: GameProp, value: number) {
  103. let data = {
  104. key: key + '',
  105. value: value + ''
  106. };
  107. this.props.set(key, value);
  108. await mk.http.sendData('savePlayerProp', data);
  109. this.savePropFinish = true;
  110. }
  111. /**
  112. * 保存多个数据
  113. */
  114. public async setProps(arr: { key: GameProp, value: number }[]) {
  115. let needProp = {};
  116. for (let i = 0; i < arr.length; i++) {
  117. needProp[arr[i].key + ''] = arr[i].value + "";
  118. }
  119. let data = {
  120. needProp: needProp
  121. };
  122. for (let i = 0; i < arr.length; i++) {
  123. this.props.set(arr[i].key, arr[i].value);
  124. }
  125. await mk.http.sendData('saveAllPlayerProp', data);
  126. this.savePropsFinish = true;
  127. }
  128. /** 获取属性 */
  129. public getProp(key: GameProp): number {
  130. if (this.props == null) {
  131. return 0;
  132. }
  133. let value = this.props.get(key);
  134. return value == null ? 0 : value;
  135. }
  136. /**
  137. * 向服务器请求所有属性后更新
  138. */
  139. public async getAllProps() {
  140. let response = await mk.http.sendData('getAllPlayerProp', {});
  141. if (response.errcode != 0) {
  142. return;
  143. }
  144. this.initProps(response.data);
  145. this.getPropsFnish = true;
  146. }
  147. private initProps(data) {
  148. if (data == null) {
  149. return;
  150. }
  151. for (let key in data) {
  152. this.props.set(parseInt(key), JSON.parse(data[key]));
  153. }
  154. }
  155. /**
  156. * 第二天需要重置的数据
  157. */
  158. public resetProps() {
  159. let arr = [];
  160. arr.push({ key: GameProp.sign_can, value: 1 });
  161. arr.push({ key: GameProp.isAutoOpenPanel, value: 1 });
  162. arr.push({ key: GameProp.isOpenBankOnCloseCash, value: 0 });
  163. this.setProps(arr);
  164. }
  165. /** 是否是新用户 */
  166. public isNewPlayer(): boolean {
  167. let v = this.getProp(GameProp.newPlayer);
  168. return v == 0;
  169. }
  170. //*********** ================= 农场数据 ================== */
  171. public needReloadData = false;
  172. private _productingListMap: Map<number, Array<{ productID: number, ripeDate: number }>> = new Map();
  173. /**建筑生产队列列表 */
  174. public getProductingList(id: number): Array<{ productID: number, ripeDate: number }> {
  175. if (!this._productingListMap.has(id)) {
  176. this._productingListMap.set(id, []);
  177. }
  178. return this._productingListMap.get(id);
  179. }
  180. private _unlockMap: Map<number, number> = new Map();
  181. /**建筑解锁流水线数量列表 */
  182. public getUnlock(id: number): number {
  183. if (!this._unlockMap.has(id)) {
  184. if (id <= 30003) {
  185. this._unlockMap.set(id, 5);
  186. } else {
  187. this._unlockMap.set(id, 1);
  188. }
  189. }
  190. return this._unlockMap.get(id);
  191. }
  192. public setUnlock(id: number, count: number): void {
  193. this._unlockMap.set(id, count);
  194. }
  195. /**已激活的建筑列表 */
  196. private buildDataMap: Map<number, number> = new Map();
  197. public hasBuildData(id: number): boolean {
  198. return this.buildDataMap.has(id);
  199. }
  200. public getBuildData(id: number): number {
  201. return this.buildDataMap.get(id);
  202. }
  203. public setBuildData(id: number, userBuildID: number): void {
  204. this.buildDataMap.set(id, userBuildID);
  205. }
  206. // /**设置建筑数据(生产队列,流水线数量,激活情况等) */
  207. // public async setGameData(data: any) {
  208. // this._productingListMap = new Map();
  209. // this._unlockMap = new Map();
  210. // this.buildDataMap = new Map();
  211. // let product = ConfigData.configMap.get("product");
  212. // for (let i = 0; i < data.length; i++) {
  213. // const build = data[i];
  214. // if (!this.buildDataMap.has(build.buildID)) {
  215. // this.buildDataMap.set(build.buildID, build.userBuildID);
  216. // this.setUnlock(build.buildID, build.addValue + 1);
  217. // }
  218. // if (build.productID != 0) {
  219. // let result = product[build.productID];
  220. // this.getProductingList(build.buildID).push({ productID: build.productID, ripeDate: build.plantTime * 1000 + result["time"] * 1000 });
  221. // }
  222. // }
  223. // }
  224. /**是否有任务可领取 */
  225. public taskReceive = false;
  226. /**是否有3日活动任务可领取 */
  227. public threeDayTaskReceive = 0;
  228. /**3日活动还需要邀请人数 */
  229. public needChildCount: number = 0;
  230. /**3日活动激活后结束时间 */
  231. public threedayTaskTime: number = 0
  232. /**种植奖励数据 */
  233. public productPrize = null;
  234. // /**排行榜数据*/
  235. // public rankData: RankData;
  236. // /**排行榜积分数据*/
  237. // public rankScore: number = 0;
  238. // /**是否刷新排行榜活动倒计时*/
  239. // public isRefreshRankTime: boolean;
  240. // /**是否可以领取排行榜奖励*/
  241. // public isCanGetRankReward: boolean;
  242. }
  243. /**
  244. * 所有模块的非校验数据
  245. */
  246. export enum GameProp {
  247. /** -------------------- 通用玩家数据 ------------------------- */
  248. /** 是否新手 0新手 1老手 */
  249. newPlayer = 1,
  250. /** 当前新手引导步骤 */
  251. guideID = 2,
  252. /** 需要自动弹界面 每次登录都弹 除首次 表示今日是否要自动开 1自动开 2不自动开 其他不处理*/
  253. isAutoOpenPanel = 3,
  254. /** 关闭常规提现时需要自动开存钱罐 每天一次 表示今日有没有开过 */
  255. isOpenBankOnCloseCash = 4,
  256. //看视频次数
  257. videoTimes = 5,
  258. //提现次数
  259. cashTimes = 6,
  260. /** ------------------ 游戏核心数据 --------------------------- */
  261. /** 关卡数 */
  262. levelNum = 10,
  263. /** 锤子道具数目 */
  264. hammerPropNum = 1001,
  265. /** 重设道具数目 */
  266. resetPropNum = 1002,
  267. /** 变色道具数目 */
  268. changePropNum = 1003,
  269. /** 当前总分 */
  270. curTotalScore = 11,
  271. /** ------------------ 转盘数据 ------------------------------ */
  272. turnable_leftTimes = 20,
  273. /** ------------------ 存钱罐数据 ----------------------------- */
  274. /** ------------------ 签到数据 ----------------------------- */
  275. /** 上次签到进度 */
  276. sign_last_bar = 120,
  277. /** 当前能否签到 */
  278. sign_can = 121,
  279. /** ------------------ 红包提现 ----------------------------- */
  280. /** 提现进度 */
  281. redBag_cash_bar = 220,
  282. }
  283. /**
  284. * 玩家数据
  285. */
  286. class PlayerProp {
  287. /** 自定义不需要校验的数据 */
  288. gameUserData = 0;
  289. _isSignInToday = 0;
  290. /**
  291. * 今日是否有签到
  292. * - 0 未签到,表示可以签到
  293. * - 1 有签到,表示不可签到
  294. */
  295. set isSignInToday(value: number) {
  296. this._isSignInToday = value;
  297. gData.sign.init_data = true;
  298. gData.gameData.init_red_point = true;
  299. }
  300. get isSignInToday(): number {
  301. return this._isSignInToday;
  302. }
  303. private _piggyBank: number = 0;
  304. /** 存钱罐存款 */
  305. set piggyBank(value: number) {
  306. if (this._piggyBank === value) return;
  307. if (gData.gameData.gameData.isWithdrawable && value > 0) return;// 存钱罐可提现状态,不能增加存钱罐金额
  308. this._piggyBank = value;
  309. gData.gameData.init_coin = true;
  310. gData.pigbank.init_data = true;
  311. gData.gameData.init_red_point = true;
  312. }
  313. get piggyBank(): number {
  314. return this._piggyBank;
  315. }
  316. /** 初始化 */
  317. initPiggyBank(v) {
  318. this._piggyBank = v;
  319. }
  320. _isWithdrawable = 0;
  321. /**
  322. * 存钱罐能否提现
  323. * - 0 不能提现
  324. * - 1 能提现
  325. */
  326. set isWithdrawable(value: number) {
  327. if (this._isWithdrawable != value) {
  328. this._isWithdrawable = value;
  329. gData.pigbank.init_data = true;
  330. gData.gameData.init_red_point = true;
  331. }
  332. }
  333. get isWithdrawable(): number {
  334. return this._isWithdrawable;
  335. }
  336. /** 存钱罐每日提现次数 */
  337. piggyBankCashTimes = 0;
  338. /** 累计存钱罐每日提现次数 */
  339. totalPiggyBankCashTimes = 0;
  340. /** 提现进度 */
  341. cashIndex = 0;
  342. /** 上次登录时间 */
  343. lastTime = 0;
  344. /** 登录天数 */
  345. loginDays = 0;
  346. newPlayer = 0;
  347. private _redMoney: number = 0;
  348. /** 红包币数量 */
  349. set redMoney(value: number) {
  350. this._redMoney = value;
  351. gData.gameData.init_coin = true;
  352. gData.gameData.init_red_point = true;
  353. }
  354. get redMoney(): number {
  355. return this._redMoney;
  356. }
  357. _signInDay = 0;
  358. /** 累计签到次数 客户端理解 上次签到天数 */
  359. set signInDay(value: number) {
  360. if (this._signInDay === value) return;
  361. this._signInDay = value;
  362. gData.sign.init_data = true;
  363. }
  364. get signInDay(): number {
  365. return this._signInDay;
  366. }
  367. /** 转盘次数 */
  368. turntableTimes = 0;
  369. /** 版本号 */
  370. versioncfg = 0;
  371. /** 福袋数据 */
  372. userTuCaoInfo = 0;
  373. /** 关卡数 */
  374. levelNum = 0;
  375. /** 锤子道具数目 */
  376. hammerPropNum = 0;
  377. /** 重设道具数目 */
  378. resetPropNum = 0;
  379. /** 变色道具数目 */
  380. changePropNum = 0;
  381. /** 能免费领红包 1 表示是新人第一次产出红包币,不用看视频*/
  382. isFirstRedMoney = 0;
  383. //玩家等级
  384. gradeLevel = 0;
  385. }
  386. /**
  387. * 奖励状态
  388. */
  389. export enum RewardState {
  390. /** 已领取 */
  391. none = 1,
  392. /** 未解锁 */
  393. lock = 2,
  394. /** 可领取 */
  395. unlock = 3
  396. }
  397. /**
  398. * 奖励类型
  399. */
  400. export enum RewardType {
  401. /** 红包币 */
  402. redBag = 1,
  403. /** 毛币 */
  404. rmb = 2,
  405. /** 金猪币 */
  406. pigRmb = 3,
  407. }
  408. /**
  409. * 音频路径
  410. */
  411. export enum AUDIO_TYPE {
  412. /**主界面背景音乐*/
  413. bgm = 'bgm',
  414. /**打开存钱罐界面播放一次*/
  415. pigBank = 'pigBank',
  416. /**关卡玩法背景音乐*/
  417. missionBg = 'missionBg',
  418. /**红包币领取界面关闭音效*/
  419. rewardClose = 'rewardClose',
  420. /**红包币领取界面开启音效*/
  421. rewardOpen = 'rewardOpen',
  422. /**红包界面打开音效*/
  423. reward = 'reward',
  424. /**提现到账界面打开播放一次*/
  425. receiptNotice = 'receiptNotice',
  426. /**通用按钮点击音效 */
  427. button = 'button',
  428. /**转盘转动音效*/
  429. turnableplay = 'turnableplay',
  430. /**转盘转动音效*/
  431. turnplateDrawEnd = 'turnplateDrawEnd',
  432. /**主页背景音乐 */
  433. music_startBg = "music_startBg",
  434. /**游戏背景音乐 */
  435. music_gameBg = "music_gameBg",
  436. /**按钮点击 */
  437. ef_button_click = "ef_button_click",
  438. /**红包打开 */
  439. ef_redPacket_open = "ef_redPacket_open",
  440. /**红包来了音效 */
  441. ef_redPacket_come = "ef_redPacket_come",
  442. /**消除4个 */
  443. ef_bonus4 = "ef_bonus4",
  444. /**消除5个 */
  445. ef_bonus5 = "ef_bonus5",
  446. /**消除6个 */
  447. ef_bonus6 = "ef_bonus6",
  448. /**消除7个 */
  449. ef_bonus7 = "ef_bonus7",
  450. /**消除8个 */
  451. ef_bonus8 = "ef_bonus8",
  452. /**普通消除音效 */
  453. ef_eliminate = "ef_eliminate",
  454. /**bouns消除音效 */
  455. ef_eliminate_bonus = "ef_eliminate_bonus",
  456. /**胜利音效 */
  457. ef_win = "ef_win",
  458. /**锤子 */
  459. ef_change = "ef_change",
  460. /**刷新 */
  461. ef_refresh = "ef_refresh",
  462. /**飞翔 */
  463. ef_fly = "ef_fly",
  464. /**关闭按钮音效*/
  465. closeButton = "closeButton",
  466. }
  467. /** ECPM */
  468. type ADShowCfg = {
  469. average_ecpm: number,
  470. insert_probability: number,
  471. is_show_banner: number,
  472. is_show_inter: number,
  473. }
  474. type PlayerInfoType = {
  475. /** 自定义不需要校验的数据 */
  476. gameUserData: object,
  477. /** 今日是否有签到 */
  478. isSignInToday: number,
  479. /** 存钱罐存款 */
  480. piggyBank: number,
  481. /** 存钱罐能否提现 */
  482. isWithdrawable: boolean,
  483. /** 存钱罐每日提现次数 */
  484. piggyBankCashTimes: number,
  485. /** 累计存钱罐每日提现次数 */
  486. totalPiggyBankCashTimes: number,
  487. /** 提现进度 */
  488. cashIndex: number,
  489. /** 上次登录时间 */
  490. lastTime: number,
  491. /** 登录天数 */
  492. loginDays: number,
  493. newPlayer: Boolean,
  494. /** 红包币数量 */
  495. redMoney: number,
  496. /** 累计签到次数 客户端理解 上次签到天数 */
  497. signInDay: number,
  498. /** 转盘次数 */
  499. turntableTimes: number,
  500. /** 版本号 */
  501. versioncfg: string,
  502. /** 福袋数据 */
  503. userTuCaoInfo: object,
  504. }
  505. /**事件类型 */
  506. export enum EVENT_TYPE {
  507. BACK_WxAuth = "BACK_WxAuth",
  508. UPDATE_CashNum = "UPDATE_CashNum",
  509. }
  510. /** 数据事件Id */
  511. export enum DataEventId {
  512. /** 点击 */
  513. guide = "guide",
  514. /** 按钮点击 */
  515. button_click = "button_click",
  516. /** 看视频 */
  517. video_init = "video_init",
  518. /** 看视频完成 */
  519. video_end = "video_end",
  520. /** 福袋功能 */
  521. fudaiFunction = "fudaiFunction",
  522. /** 互推功能 */
  523. hutuiFunction = "hutuiFunction",
  524. /** 关卡 */
  525. level = "level",
  526. /** 常规提现-红包币提现 */
  527. commonWithDrawal = "commonWithDrawl",
  528. /** 存钱罐提现 */
  529. jarWithDrawal = "jarWithDrawal",
  530. /** 签到提现 */
  531. signWithDrawal = "signWithDrawal",
  532. }
  533. /** 视频广告类型 */
  534. export enum VideoAdType {
  535. Null = "",
  536. /** 关卡积分红包 */
  537. LevelScoreRedBag = "关卡积分红包",
  538. /** 主界面气泡红包 */
  539. StartQiPaoRedBag = "主界面气泡红包",
  540. /** 关卡气泡红包 */
  541. LevelQiPaoRedBag = "关卡气泡红包",
  542. /** 转盘 */
  543. Turntable = "转盘视频",
  544. /** 福袋视频" */
  545. Fudai = "福袋视频",
  546. /** 签到视频 */
  547. Sign = "签到视频"
  548. }