GameData.ts 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  1. import Util from "../../before/util/Util";
  2. import FunBtns from "../game/FunBtns";
  3. import Game from "../game/Game";
  4. /**
  5. * @description 游戏核心玩法数据
  6. * @author 邹勇
  7. */
  8. export class GameData {
  9. public dataFinish: boolean = false;
  10. public savePropFinish: boolean = false;
  11. public savePropsFinish: boolean = false;
  12. public getPropsFnish: boolean = false;
  13. public configs: any = {};
  14. public adShowConfig: ADShowCfg;
  15. /** 功能开启 */
  16. public funOpenData: string[] = [];
  17. public gameData: PlayerProp = null;
  18. /** 标志位:刷新主界面货币 */
  19. public init_coin: boolean = false;
  20. public updatePiggyBankValue: boolean = false;
  21. /** 标志位:刷新主界面头像 */
  22. public init_head: boolean = false;
  23. /** 标志位:刷新主界面红点 */
  24. public init_red_point: boolean = true;
  25. /** 属性存储字典 */
  26. public props: Map<number, any> = new Map<number, any>();
  27. public pools = [
  28. { url: "game/prefab/tips", num: 20 },
  29. { url: "game/prefab/coin", num: 20 }
  30. ];
  31. /** 主页样式脚本 */
  32. public gameStyle: Game = null;
  33. /** 入口按钮 */
  34. public funBtns: FunBtns = null;
  35. /** 剩余可种植次数 */
  36. public leftTimes: number = -1;
  37. /** 最大可种植次数 */
  38. public maxTimes: number = -1;
  39. /** 恢复种植次数间隔 */
  40. public ProductionRecovery: number = 15;
  41. public init_leftTimes: boolean = false;
  42. /** 可生产商品配置 */
  43. public nextCanProduct = null;
  44. /** 按钮生产选中的农田、牧场或工厂 */
  45. public nextMake = null;
  46. /** 类型标识 */
  47. private nextType = 0;
  48. /** 刷新生产按钮信息 */
  49. public init_makeInfo = false;
  50. /** 正在生产 */
  51. public isProducting = false;
  52. /** 标志位 刷新任务描述 */
  53. public init_task = false;
  54. /**
  55. * 初始化游戏数据:网络配置信息,用户信息
  56. * @returns
  57. */
  58. public async init() {
  59. let data: any = { "versionCode": gData.appData.version };
  60. let response = await mk.http.sendData('getAllConfigInfo', data);
  61. if (response.errcode != 0) {
  62. if (response.errcode == -10003 || response.errcode == -20003) {
  63. //清除缓存
  64. gData.loginData.reload();
  65. }
  66. return;
  67. }
  68. mk.console.logSingle("getAllConfigInfo", response.data);
  69. this.initConfigs(response.data);
  70. //初始化topon
  71. mk.ad.init();
  72. data = {};
  73. response = await mk.http.sendData('getInfoCrypt', data);
  74. if (response.errcode != 0) {
  75. return;
  76. }
  77. mk.console.logSingle("getInfoCrypt", response.data);
  78. this.initPlayerProp(response.data)
  79. this.initProps(response.data.gameUserData);
  80. data = {};
  81. response = await mk.http.sendData('user/adShowConfig', data);
  82. if (response.errcode != 0) {
  83. return;
  84. }
  85. this.adShowConfig = response.data;
  86. mk.console.logSingle("user/adShowConfig", response.data);
  87. gData.adData.initEcpmData();
  88. // data = {};
  89. // response = await mk.http.sendData('welFare/getUserWelFareTaskInfo', data);
  90. // if (response.errcode != 0) {
  91. // return;
  92. // }
  93. // this.gameData.userTuCaoInfo = response.data.userWelFareTaskInfo;
  94. // mk.console.logSingle("welFare/getUserWelFareTaskInfo", response.data);
  95. gData.guideData.init();
  96. gData.guideWeakData.init();
  97. gData.moduleData.forEach(element => {
  98. element.init();
  99. });
  100. this.dataFinish = true;
  101. }
  102. private initConfigs(data) {
  103. // console.log('config ', data);
  104. this.configs = data;
  105. this.funOpenData = this.configs.ServerConfig.Functionswitch.split(",");
  106. //从服务器解析配置
  107. // this.setProductMapFromJson();
  108. }
  109. /**
  110. * 初始化玩家数据
  111. */
  112. private initPlayerProp(res_data) {
  113. this.gameData = new PlayerProp();
  114. this.gameData.cashIndex = res_data.cashIndex;
  115. this.gameData.gameUserData = res_data.gameUserData;
  116. this.gameData.isSignInToday = res_data.isSignInToday;
  117. this.gameData.isWithdrawable = res_data.isWithdrawable;
  118. this.gameData.lastTime = res_data.lastTime;
  119. this.gameData.loginDays = res_data.loginDays;
  120. this.gameData.newPlayer = res_data.newPlayer;
  121. this.gameData.initPiggyBank(res_data.piggyBank);
  122. this.gameData.piggyBankCashTimes = res_data.piggyBankCashTimes;
  123. this.gameData.redMoney = res_data.redMoney == null ? 0 : res_data.redMoney;
  124. this.gameData.signInDay = res_data.signInDay;
  125. this.gameData.totalPiggyBankCashTimes = res_data.totalPiggyBankCashTimes;
  126. this.gameData.turntableTimes = res_data.turntableTimes;
  127. this.gameData.versioncfg = res_data.versioncfg;
  128. this.gameData.isFirstRedMoney = res_data.isFirstRedMoney;
  129. gData.adData.watchNumToday = res_data.dayVideoTimesForRedMoney == null ? 0 : res_data.dayVideoTimesForRedMoney;
  130. this.gameData.userTuCaoInfo = res_data.userWelFareTaskInfo;
  131. this.gameData.farmExpValue = res_data.userExp;
  132. this.gameData.farmGradeData = res_data.userFarmGrageRewardInfo;
  133. mk.data.setTAUserID(gData.loginData.uin);
  134. if (gData.loginData.isFirstIn) {
  135. gData.loginData.isFirstIn = false;
  136. mk.data.setTAEventRegister();
  137. mk.data.sendDataEvent('ABTest', `触发${this.gameData.groupCode}方案`);
  138. //注册时版本号
  139. mk.data.setTAEventUserStr(0, 'regtime_app_id', gData.appData.appVersion);
  140. }
  141. //当前版本号
  142. mk.data.setTAEventUserStr(0, 'now_app_id', gData.appData.appVersion);
  143. //渠道号
  144. mk.data.setTAEventUserStr(0, 'channel_id', gData.appData.umengChannel);
  145. }
  146. /**
  147. * 保存单个数据
  148. */
  149. public async setProp(key: GameProp, value: any) {
  150. let data = {
  151. key: key + '',
  152. value: value
  153. };
  154. this.props.set(key, value);
  155. await mk.http.sendData('savePlayerProp', data);
  156. this.savePropFinish = true;
  157. }
  158. /**
  159. * 保存多个数据
  160. */
  161. public async setProps(arr: { key: GameProp, value: any }[]) {
  162. let needProp = {};
  163. for (let i = 0; i < arr.length; i++) {
  164. needProp[arr[i].key + ''] = arr[i].value;
  165. }
  166. let data = {
  167. needProp: needProp
  168. };
  169. for (let i = 0; i < arr.length; i++) {
  170. this.props.set(arr[i].key, arr[i].value);
  171. }
  172. await mk.http.sendData('saveAllPlayerProp', data);
  173. this.savePropsFinish = true;
  174. }
  175. /** 获取属性 */
  176. public getProp(key: GameProp): any {
  177. if (this.props == null) {
  178. return 0;
  179. }
  180. let value = this.props.get(key);
  181. return value;
  182. }
  183. /**
  184. * 向服务器请求所有属性后更新
  185. */
  186. public async getAllProps() {
  187. let response = await mk.http.sendData('getAllPlayerProp', {});
  188. if (response.errcode != 0) {
  189. return;
  190. }
  191. this.initProps(response.data);
  192. this.getPropsFnish = true;
  193. }
  194. private initProps(data) {
  195. if (data == null) {
  196. return;
  197. }
  198. for (let key in data) {
  199. this.props.set(parseInt(key), data[key]);
  200. }
  201. this.setFramDataMapByServer();
  202. this.setPastureDataMapByServer();
  203. this.setFactoryDataMapByServer();
  204. this.initSetMaxProduct();
  205. this.initLeftTimes();
  206. this.initFarmMapGetRewardData();
  207. }
  208. private initLeftTimes() {
  209. let lefttimes = this.getProp(GameProp.leftTimes);
  210. if (lefttimes == undefined) {
  211. this.leftTimes = parseInt(this.configs.ServerConfig.FreeProduction);
  212. this.setProp(GameProp.leftTimes, this.leftTimes);
  213. }
  214. else {
  215. this.leftTimes = lefttimes;
  216. }
  217. this.maxTimes = parseInt(this.configs.ServerConfig.ProductionMax);
  218. this.ProductionRecovery = parseInt(this.configs.ServerConfig.ProductionRecovery);
  219. //test
  220. this.leftTimes = 100;
  221. this.maxTimes = 100;
  222. this.ProductionRecovery = 1;
  223. }
  224. //图鉴领取数据
  225. private _farmMapRewardData = [];
  226. public getFarmMapRewardState(index) {
  227. if (index.i < this._farmMapRewardData.length) {
  228. if (index.j < this._farmMapRewardData[index.i].length) {
  229. return this._farmMapRewardData[index.i][index.j];
  230. }
  231. }
  232. return 1;
  233. }
  234. public setFarmMapRewardState(index, state) {
  235. if (index.i < this._farmMapRewardData.length) {
  236. if (index.j < this._farmMapRewardData[index.i].length) {
  237. this._farmMapRewardData[index.i][index.j] = state;
  238. this.setProp(GameProp.farmMapGetRewardData, this._farmMapRewardData);
  239. }
  240. }
  241. }
  242. private initFarmMapGetRewardData() {
  243. this._farmMapRewardData = this.getProp(GameProp.farmMapGetRewardData);
  244. if (!this._farmMapRewardData) {
  245. this._farmMapRewardData = [];
  246. }
  247. let needSync = false;
  248. let index = 0;
  249. this._productTypeMap.forEach((value, key) => {
  250. if (this._farmMapRewardData.length <= index) {
  251. let len = value.length;
  252. let arr = [];
  253. for (let i = 0; i != len; ++i) {
  254. arr.push(0);
  255. }
  256. this._farmMapRewardData.push(arr);
  257. needSync = true;
  258. console.log("-----add 数组数据")
  259. } else {
  260. let data = this._farmMapRewardData[index];
  261. let len = value.length;
  262. for (let i = 0; i != len; ++i) {
  263. if (i >= data.length) {
  264. data.push(0);
  265. needSync = true;
  266. console.log("-----add 单个数据")
  267. }
  268. }
  269. }
  270. ++index;
  271. });
  272. if (needSync) {
  273. this.setProp(GameProp.farmMapGetRewardData, this._farmMapRewardData);
  274. }
  275. }
  276. /**
  277. * 第二天需要重置的数据
  278. */
  279. public resetProps() {
  280. let arr = [];
  281. arr.push({ key: GameProp.sign_can, value: 1 });
  282. arr.push({ key: GameProp.isAutoOpenPanel, value: 1 });
  283. arr.push({ key: GameProp.isOpenBankOnCloseCash, value: 0 });
  284. this.setProps(arr);
  285. }
  286. /** 是否是新用户 */
  287. public isNewPlayer(): boolean {
  288. let v = this.getProp(GameProp.newPlayer);
  289. return v == 0;
  290. }
  291. //*********** ================= 农场数据 ================== */
  292. /** 农田数据 服务器数据 */
  293. private _farmData = [];
  294. /** 农田字典 本地数据,方便操作*/
  295. private _farmDataMap: Map<number, any> = new Map();
  296. //刷新农田
  297. public needFreshArr = [];
  298. /** 随机获取可种植物品 */
  299. getRandomPlantConfig() {
  300. let id = Util.rnd(10001, this.getMaxProduct(ProductType.nzw));
  301. return this.getProductMap(id);
  302. }
  303. /** 随机获取可生产产品 */
  304. getRandomFactoryConfig(configID) {
  305. let tab = gData.gameData.getTabByConfigID(configID);
  306. let arr = this.getProductArrByType(tab);
  307. let max = this.getMaxProduct(tab);
  308. let canArr = [];
  309. let len = arr.length;
  310. for (var i = 0; i < len; i++) {
  311. if (arr[i].picture <= max) {
  312. canArr.push(arr[i].picture);
  313. if (arr[i].picture == max) {
  314. break;
  315. }
  316. }
  317. }
  318. let index = Util.rnd(0, canArr.length - 1);
  319. let id = canArr[index];
  320. return this.getProductMap(id);
  321. }
  322. /** 自动生产选中下一个 */
  323. setNextProduct(sel = true) {
  324. if (this.nextMake) {
  325. return;
  326. }
  327. let next = gData.farmSystem.selectNextFarm(sel);
  328. if (!next) {
  329. let next1 = gData.pastureSystem.nextPasture();
  330. if (!next1) {
  331. let next2 = gData.factorySystem.nextFactory();
  332. if (!next2) {
  333. this.nextType = 0;
  334. }
  335. else {
  336. this.nextType = 3;
  337. }
  338. }
  339. else {
  340. this.nextType = 2;
  341. }
  342. }
  343. else {
  344. this.nextType = 1;
  345. }
  346. this.init_makeInfo = true;
  347. }
  348. hasFirstCheck = false;
  349. firstCheckNext() {
  350. if (!this.hasFirstCheck && gData.pastureSystem.pastureIcons.length >= 3 && gData.factorySystem.factoryIcons.length >= 6) {
  351. this.hasFirstCheck = true;
  352. gData.gameData.setNextProduct(false);
  353. }
  354. }
  355. makeProduct() {
  356. switch (this.nextType) {
  357. case 0:
  358. mk.tip.pop('农场已满,无法继续生产');
  359. break;
  360. case 1:
  361. gData.farmSystem.btnMake();
  362. gData.gameData.nextMake = null;
  363. this.nextMake = null;
  364. this.setNextProduct(false);
  365. break;
  366. case 2:
  367. gData.pastureSystem.btnMake();
  368. gData.gameData.nextMake = null;
  369. this.nextMake = null;
  370. this.setNextProduct();
  371. gData.adData.checkPopRed();
  372. break;
  373. case 3:
  374. gData.factorySystem.btnMake();
  375. gData.gameData.nextMake = null;
  376. this.nextMake = null;
  377. this.setNextProduct();
  378. break;
  379. }
  380. }
  381. public setFarmDataMap(id, data, sendToServer = true) {
  382. this._farmDataMap.set(id, data);
  383. let len = this._farmData.length;
  384. for (var i = 0; i < len; i++) {
  385. if (this._farmData[i].id == id) {
  386. this._farmData[i].state = data.state;
  387. this._farmData[i].productID = data.productID;
  388. this._farmData[i].growSpan = data.growSpan;
  389. break;
  390. }
  391. }
  392. this.needFreshArr.push(id);
  393. //更新到服务器
  394. if (sendToServer) {
  395. this.setProp(GameProp.farmData, this._farmData);
  396. }
  397. }
  398. public getFarmDataMap(id) {
  399. return this._farmDataMap.get(id);
  400. }
  401. /** 服务器数据设置到本地 */
  402. public setFramDataMapByServer() {
  403. this._farmData = this.getProp(GameProp.farmData);
  404. if (!this._farmData) {
  405. this._farmData = [];
  406. }
  407. let len = this._farmData.length;
  408. //新玩家初始化农田数据
  409. if (len < 18) {
  410. let id = 31000;
  411. let data = null;
  412. for (var i = 0; i < 18; i++) {
  413. if (i < 9) {
  414. data = { id: id, state: FarmState.Empty, productID: 0, growSpan: 0 };
  415. }
  416. else if (i == 9) {
  417. data = { id: id, state: FarmState.CanUnlock, productID: 0, growSpan: 0 };
  418. }
  419. else {
  420. data = { id: id, state: FarmState.Lock, productID: 0, growSpan: 0 };
  421. }
  422. this._farmData.push(data);
  423. this._farmDataMap.set(id, data);
  424. id++;
  425. }
  426. //第一次初始数据
  427. this.setMaxProduct(ProductType.nzw, 10003);
  428. //更新到服务器
  429. this.setProp(GameProp.farmData, this._farmData);
  430. }
  431. else {
  432. for (var i = 0; i < len; i++) {
  433. this._farmDataMap.set(this._farmData[i].id, this._farmData[i]);
  434. }
  435. }
  436. }
  437. /** puduct配置数据 <picture, config> */
  438. private _productMap: Map<number, any> = new Map();
  439. /** puduct类型配置数据 <tab, 类型数组> */
  440. private _productTypeMap: Map<string, any> = new Map();
  441. /** 设置puduct配置数据 */
  442. public setProductMapFromJson() {
  443. // let productJson = this.configs.product;
  444. //先用本地数据
  445. let productJson = gData.configData.configMap.get('product');
  446. let len = productJson.length;
  447. let product = null;
  448. let tab = '';
  449. let arr = [];
  450. for (var i = 0; i < len; i++) {
  451. product = productJson[i];
  452. this._productMap.set(product.picture, product);
  453. if (tab != product.tab) {
  454. arr = [];
  455. this._productTypeMap.set(product.tab, arr);
  456. tab = product.tab;
  457. }
  458. arr.push(product);
  459. }
  460. }
  461. /** 根据id获取单个数据 */
  462. public getProductMap(id) {
  463. return this._productMap.get(id);
  464. }
  465. /** 根据tab获取数组 */
  466. public getProductArrByType(type: ProductType) {
  467. return this._productTypeMap.get(type);
  468. }
  469. /** 产品生产/种植次数 服务器数据 */
  470. private _productMakeTimesData = [];
  471. /** 产品生产/种植次数 */
  472. private _productMakeTimesMap: Map<number, number> = new Map();
  473. addProductMakeTimesById(id: number) {
  474. let times = this._productMakeTimesMap.get(id);
  475. if (!times) {
  476. times = 0;
  477. }
  478. times++;
  479. this._productMakeTimesMap.set(id, times);
  480. //更新到服务器
  481. let has = false;
  482. let len = this._productMakeTimesData.length;
  483. for (var i = 0; i < len; i++) {
  484. if (this._productMakeTimesData[i].id == id) {
  485. this._productMakeTimesData[i].times = times;
  486. has = true;
  487. break;
  488. }
  489. }
  490. if (!has) {
  491. let data = { 'id': id, 'times': times };
  492. this._productMakeTimesData.push(data);
  493. }
  494. //设置可种植/生产最大id
  495. let nextID = id + 1;
  496. let curConfig = this.getProductMap(id);
  497. let nextConfig = this.getProductMap(nextID);
  498. if (nextConfig) {
  499. if (nextConfig.tab == curConfig.tab && nextID > this.getMaxProduct(curConfig.tab)) {
  500. if (nextConfig.unlock == 1 && nextConfig.value <= times) {
  501. this.setMaxProduct(curConfig.tab, nextID);
  502. }
  503. }
  504. }
  505. this.setProp(GameProp.productMakeTimes, this._productMakeTimesData);
  506. }
  507. getProductMakeTimesById(id: number) {
  508. let times = this._productMakeTimesMap.get(id);
  509. if (!times) {
  510. times = 0
  511. }
  512. return times;
  513. }
  514. //------------- 养殖场数据-----------
  515. /** 养殖场数据 服务器数据 */
  516. private _pastureData = [];
  517. /** 养殖场字典 本地数据,方便操作*/
  518. private _pastureDataMap: Map<number, any> = new Map();
  519. public setPastureDataMap(id, data, sendToServer = true) {
  520. this._pastureDataMap.set(id, data);
  521. let len = this._pastureData.length;
  522. for (var i = 0; i < len; i++) {
  523. if (this._pastureData[i].id == id) {
  524. this._pastureData[i].state = data.state;
  525. this._pastureData[i].productID = data.productID;
  526. this._pastureData[i].growSpan = data.growSpan;
  527. break;
  528. }
  529. }
  530. this.needFreshArr.push(id);
  531. //更新到服务器
  532. if (sendToServer) {
  533. this.setProp(GameProp.pastureData, this._pastureData);
  534. }
  535. }
  536. public getPastureDataMap(id) {
  537. return this._pastureDataMap.get(id);
  538. }
  539. /** 服务器数据设置到本地 */
  540. public setPastureDataMapByServer() {
  541. this._pastureData = this.getProp(GameProp.pastureData);
  542. if (!this._pastureData) {
  543. this._pastureData = [];
  544. }
  545. let len = this._pastureData.length;
  546. //新玩家初始化养殖场数据
  547. if (len < 3) {
  548. let arr = this.getProductArrByType(ProductType.dw);
  549. let len1 = arr.length;
  550. let id = 30001;
  551. let data = null;
  552. for (var i = 0; i < len1; i++) {
  553. if (arr[i].value < this.gameData.gradeLevel) {
  554. data = { id: id, state: PastureState.Lock, productID: arr[i].picture, growSpan: 0 };
  555. }
  556. else {
  557. data = { id: id, state: PastureState.Empty, productID: arr[i].picture, growSpan: 0 };
  558. }
  559. this._pastureData.push(data);
  560. this._pastureDataMap.set(id, data);
  561. id++;
  562. }
  563. //更新到服务器
  564. this.setProp(GameProp.pastureData, this._pastureData);
  565. }
  566. else {
  567. for (var i = 0; i < len; i++) {
  568. this._pastureDataMap.set(this._pastureData[i].id, this._pastureData[i]);
  569. }
  570. }
  571. }
  572. //------------- 工厂数据-----------
  573. /** 工厂数据 服务器数据 */
  574. private _factoryData = [];
  575. /** 工厂字典 本地数据,方便操作*/
  576. private _factoryDataMap: Map<number, any> = new Map();
  577. public setFactoryDataMap(id, data, sendToServer = true) {
  578. this._factoryDataMap.set(id, data);
  579. let len = this._factoryData.length;
  580. for (var i = 0; i < len; i++) {
  581. if (this._factoryData[i].id == id) {
  582. this._factoryData[i].state = data.state;
  583. this._factoryData[i].productID = data.productID;
  584. this._factoryData[i].growSpan = data.growSpan;
  585. break;
  586. }
  587. }
  588. this.needFreshArr.push(id);
  589. //更新到服务器
  590. if (sendToServer) {
  591. this.setProp(GameProp.factoryData, this._factoryData);
  592. }
  593. }
  594. public getFactoryDataMap(id) {
  595. return this._factoryDataMap.get(id);
  596. }
  597. /** 服务器数据设置到本地 */
  598. public setFactoryDataMapByServer() {
  599. this._factoryData = this.getProp(GameProp.factoryData);
  600. if (!this._factoryData) {
  601. this._factoryData = [];
  602. }
  603. let len = this._factoryData.length;
  604. //新玩家初始化工厂数据
  605. if (len < 6) {
  606. let id = 30004;
  607. let data = null;
  608. let tab = null;
  609. for (var i = 0; i < 6; i++) {
  610. tab = this.getTabByConfigID(id);
  611. let arr = this.getProductArrByType(tab);
  612. if (arr[0].value < this.gameData.gradeLevel) {
  613. data = { id: id, state: FactroyState.Lock, productID: 0, growSpan: 0 };
  614. }
  615. else {
  616. data = { id: id, state: FactroyState.Empty, productID: 0, growSpan: 0 };
  617. //更新最大可购买商品
  618. this.setMaxProduct(tab, arr[0].picture, false);
  619. }
  620. this._factoryData.push(data);
  621. this._factoryDataMap.set(id, data);
  622. id++;
  623. }
  624. //更新到服务器
  625. this.setProp(GameProp.maxProduct, this._maxProduct);
  626. this.setProp(GameProp.factoryData, this._factoryData);
  627. }
  628. else {
  629. for (var i = 0; i < len; i++) {
  630. this._factoryDataMap.set(this._factoryData[i].id, this._factoryData[i]);
  631. }
  632. }
  633. }
  634. /** 通过工厂建筑物ID获取标签 */
  635. getTabByConfigID(configID: number): ProductType {
  636. let tab = ProductType.bmhc;
  637. switch (configID) {
  638. case 30004:
  639. tab = ProductType.bmhc;
  640. break;
  641. case 30005:
  642. tab = ProductType.gdp;
  643. break;
  644. case 30006:
  645. tab = ProductType.ztc;
  646. break;
  647. case 30007:
  648. tab = ProductType.lrc;
  649. break;
  650. case 30008:
  651. tab = ProductType.gfmg;
  652. break;
  653. case 30009:
  654. tab = ProductType.kcd;
  655. break;
  656. }
  657. return tab;
  658. }
  659. /** 通过标签获取工厂建筑物ID */
  660. getConfigIDByTab(tab: ProductType): number {
  661. let configID = 0;
  662. switch (tab) {
  663. case ProductType.bmhc:
  664. configID = 30004;
  665. break;
  666. case ProductType.gdp:
  667. configID = 30005;
  668. break;
  669. case ProductType.ztc:
  670. configID = 30006;
  671. break;
  672. case ProductType.lrc:
  673. configID = 30007;
  674. break;
  675. case ProductType.gfmg:
  676. configID = 30008;
  677. break;
  678. case ProductType.kcd:
  679. configID = 30009;
  680. break;
  681. }
  682. return configID;
  683. }
  684. /** 最大可种植/生产商品数组 */
  685. private _maxProduct = [];
  686. public getMaxProduct(tab) {
  687. if (this._maxProduct) {
  688. let len = this._maxProduct.length;
  689. let data = null;
  690. for (var i = 0; i < len; i++) {
  691. data = this._maxProduct[i];
  692. if (data.tab == tab) {
  693. return data.productID;
  694. }
  695. }
  696. }
  697. return null;
  698. }
  699. public setMaxProduct(tab, id, sendToServer = true) {
  700. if (!this._maxProduct) {
  701. this._maxProduct = [];
  702. }
  703. let len = this._maxProduct.length;
  704. let data = null;
  705. let has = false;
  706. for (var i = 0; i < len; i++) {
  707. data = this._maxProduct[i];
  708. if (data.tab == tab) {
  709. data.productID = id;
  710. has = true;
  711. break;
  712. }
  713. }
  714. if (!has) {
  715. data = { tab: tab, productID: id };
  716. this._maxProduct.push(data);
  717. }
  718. if (sendToServer) {
  719. this.setProp(GameProp.maxProduct, this._maxProduct);
  720. }
  721. }
  722. public initSetMaxProduct() {
  723. if (this._maxProduct.length <= 0) {
  724. this._maxProduct = this.getProp(GameProp.maxProduct);
  725. }
  726. }
  727. /** 农场升级时解锁工厂,检查最大可种植/生产商品 */
  728. public checkGradeUpUnLock() {
  729. for (var key in ProductType) {
  730. let tab = ProductType[key];
  731. let configArr = this.getProductArrByType(tab);
  732. let len = configArr.length;
  733. let data = null;
  734. let needUpdate = false;
  735. switch (tab) {
  736. case ProductType.nzw:
  737. //不做处理,游戏最开始已解锁
  738. break;
  739. case ProductType.dw:
  740. let id = 30001;
  741. needUpdate = false;
  742. for (var i = 0; i < len; i++) {
  743. data = this.getPastureDataMap(id);
  744. if (data.state == PastureState.Lock && configArr[i].value <= this.gameData.gradeLevel) {
  745. data = { id: id, state: PastureState.Empty, productID: configArr[i].picture, growSpan: 0 };
  746. this.setPastureDataMap(id, data, false);
  747. needUpdate = true;
  748. }
  749. id++;
  750. }
  751. //更新到服务器
  752. if (needUpdate) {
  753. this.setProp(GameProp.pastureData, this._pastureData);
  754. this.setNextProduct();
  755. }
  756. break;
  757. default:
  758. //工厂解锁根据第一个商品
  759. needUpdate = false;
  760. let configID = this.getConfigIDByTab(tab);
  761. data = this.getFactoryDataMap(configID);
  762. if (data.state == FactroyState.Lock && configArr[0].value <= this.gameData.gradeLevel) {
  763. data = { id: id, state: FactroyState.Empty, productID: 0, growSpan: 0 };
  764. this.setFactoryDataMap(configID, data, false);
  765. needUpdate = true;
  766. //更新最大可购买商品
  767. this.setMaxProduct(tab, configArr[0].picture);
  768. }
  769. if (needUpdate) {
  770. this.setProp(GameProp.factoryData, this._factoryData);
  771. this.setNextProduct();
  772. }
  773. break;
  774. }
  775. }
  776. }
  777. }
  778. /**
  779. * 所有模块的非校验数据
  780. */
  781. export enum GameProp {
  782. /** -------------------- 通用玩家数据 ------------------------- */
  783. /** 是否新手 0新手 1老手 */
  784. newPlayer = 1,
  785. /** 当前新手引导步骤 */
  786. guideID = 2,
  787. /** 需要自动弹界面 每次登录都弹 除首次 表示今日是否要自动开 1自动开 2不自动开 其他不处理*/
  788. isAutoOpenPanel = 3,
  789. /** 关闭常规提现时需要自动开存钱罐 每天一次 表示今日有没有开过 */
  790. isOpenBankOnCloseCash = 4,
  791. //看视频次数
  792. videoTimes = 5,
  793. //提现次数
  794. cashTimes = 6,
  795. /** ------------------ 游戏核心数据 --------------------------- */
  796. /** 农田数据 */
  797. farmData = 11,
  798. /** 产品生产次数 */
  799. productMakeTimes = 12,
  800. /** 可生产最大产品字典 */
  801. maxProduct = 13,
  802. /** 养殖场数据 */
  803. pastureData = 14,
  804. /** 工厂数据 */
  805. factoryData = 15,
  806. /** 剩余可种植次数 */
  807. leftTimes = 16,
  808. /**
  809. 图鉴领取数据*/
  810. farmMapGetRewardData = 17,
  811. /** ------------------ 转盘数据 ------------------------------ */
  812. turnable_leftTimes = 20,
  813. /** ------------------ 存钱罐数据 ----------------------------- */
  814. /** ------------------ 签到数据 ----------------------------- */
  815. /** 上次签到进度 */
  816. sign_last_bar = 120,
  817. /** 当前能否签到 */
  818. sign_can = 121,
  819. /** ------------------ 红包提现 ----------------------------- */
  820. /** 提现进度 */
  821. redBag_cash_bar = 220,
  822. }
  823. /**
  824. * 玩家数据
  825. */
  826. class PlayerProp {
  827. /** 自定义不需要校验的数据 */
  828. gameUserData = 0;
  829. _isSignInToday = 0;
  830. /**
  831. * 今日是否有签到
  832. * - 0 未签到,表示可以签到
  833. * - 1 有签到,表示不可签到
  834. */
  835. set isSignInToday(value: number) {
  836. this._isSignInToday = value;
  837. gData.sign.init_data = true;
  838. gData.gameData.init_red_point = true;
  839. }
  840. get isSignInToday(): number {
  841. return this._isSignInToday;
  842. }
  843. private _piggyBank: number = 0;
  844. /** 存钱罐存款 */
  845. set piggyBank(value: number) {
  846. if (this._piggyBank === value) return;
  847. if (gData.gameData.gameData.isWithdrawable && value > 0) return;// 存钱罐可提现状态,不能增加存钱罐金额
  848. this._piggyBank = value;
  849. gData.gameData.init_coin = true;
  850. gData.pigbank.init_data = true;
  851. gData.gameData.init_red_point = true;
  852. }
  853. get piggyBank(): number {
  854. return this._piggyBank;
  855. }
  856. /** 初始化 */
  857. initPiggyBank(v) {
  858. this._piggyBank = v;
  859. }
  860. _isWithdrawable = 0;
  861. /**
  862. * 存钱罐能否提现
  863. * - 0 不能提现
  864. * - 1 能提现
  865. */
  866. set isWithdrawable(value: number) {
  867. if (this._isWithdrawable != value) {
  868. this._isWithdrawable = value;
  869. gData.pigbank.init_data = true;
  870. gData.gameData.init_red_point = true;
  871. }
  872. }
  873. get isWithdrawable(): number {
  874. return this._isWithdrawable;
  875. }
  876. /** 存钱罐每日提现次数 */
  877. piggyBankCashTimes = 0;
  878. /** 累计存钱罐每日提现次数 */
  879. totalPiggyBankCashTimes = 0;
  880. /** 提现进度 */
  881. cashIndex = 0;
  882. /** 上次登录时间 */
  883. lastTime = 0;
  884. /** 登录天数 */
  885. loginDays = 0;
  886. newPlayer = 0;
  887. private _redMoney: number = 0;
  888. /** 红包币数量 */
  889. set redMoney(value: number) {
  890. this._redMoney = value;
  891. gData.gameData.init_coin = true;
  892. gData.gameData.init_red_point = true;
  893. }
  894. get redMoney(): number {
  895. return this._redMoney;
  896. }
  897. _signInDay = 0;
  898. /** 累计签到次数 客户端理解 上次签到天数 */
  899. set signInDay(value: number) {
  900. if (this._signInDay === value) return;
  901. this._signInDay = value;
  902. gData.sign.init_data = true;
  903. }
  904. get signInDay(): number {
  905. return this._signInDay;
  906. }
  907. /** 转盘次数 */
  908. turntableTimes = 0;
  909. /** 版本号 */
  910. versioncfg = 0;
  911. /** 福袋数据 */
  912. userTuCaoInfo = 0;
  913. /** 能免费领红包 1 表示是新人第一次产出红包币,不用看视频*/
  914. isFirstRedMoney = 0;
  915. //农场等级
  916. gradeLevel = 6;
  917. //农场等级奖励数据
  918. farmGradeData:any = {};
  919. //农场经验
  920. private farmExp = 0;
  921. set farmExpValue(value: number) {
  922. this.farmExp = value;
  923. }
  924. get farmExpValue(): number {
  925. return this.farmExp;
  926. }
  927. /** ABTest分组id */
  928. groupCode = 0;
  929. }
  930. /**
  931. * 奖励状态
  932. */
  933. export enum RewardState {
  934. /** 已领取 */
  935. none = 1,
  936. /** 未解锁 */
  937. lock = 2,
  938. /** 可领取 */
  939. unlock = 3
  940. }
  941. /**
  942. * 奖励类型
  943. */
  944. export enum RewardType {
  945. /** 红包币 */
  946. redBag = 1,
  947. /** 毛币 */
  948. rmb = 2,
  949. /** 金猪币 */
  950. pigRmb = 3,
  951. }
  952. /**
  953. * 音频路径
  954. */
  955. export enum AUDIO_TYPE {
  956. /**主界面背景音乐*/
  957. bgm = 'bgm',
  958. /**打开存钱罐界面播放一次*/
  959. pigBank = 'pigBank',
  960. /**关卡玩法背景音乐*/
  961. missionBg = 'missionBg',
  962. /**红包币领取界面关闭音效*/
  963. rewardClose = 'rewardClose',
  964. /**红包币领取界面开启音效*/
  965. rewardOpen = 'rewardOpen',
  966. /**红包界面打开音效*/
  967. reward = 'reward',
  968. /**提现到账界面打开播放一次*/
  969. receiptNotice = 'receiptNotice',
  970. /**通用按钮点击音效 */
  971. button = 'button',
  972. /**转盘转动音效*/
  973. turnableplay = 'turnableplay',
  974. /**转盘转动音效*/
  975. turnplateDrawEnd = 'turnplateDrawEnd',
  976. /**主页背景音乐 */
  977. music_startBg = "music_startBg",
  978. /**游戏背景音乐 */
  979. music_gameBg = "music_gameBg",
  980. /**按钮点击 */
  981. ef_button_click = "ef_button_click",
  982. /**红包打开 */
  983. ef_redPacket_open = "ef_redPacket_open",
  984. /**红包来了音效 */
  985. ef_redPacket_come = "ef_redPacket_come",
  986. /**消除4个 */
  987. ef_bonus4 = "ef_bonus4",
  988. /**消除5个 */
  989. ef_bonus5 = "ef_bonus5",
  990. /**消除6个 */
  991. ef_bonus6 = "ef_bonus6",
  992. /**消除7个 */
  993. ef_bonus7 = "ef_bonus7",
  994. /**消除8个 */
  995. ef_bonus8 = "ef_bonus8",
  996. /**普通消除音效 */
  997. ef_eliminate = "ef_eliminate",
  998. /**bouns消除音效 */
  999. ef_eliminate_bonus = "ef_eliminate_bonus",
  1000. /**胜利音效 */
  1001. ef_win = "ef_win",
  1002. /**锤子 */
  1003. ef_change = "ef_change",
  1004. /**刷新 */
  1005. ef_refresh = "ef_refresh",
  1006. /**飞翔 */
  1007. ef_fly = "ef_fly",
  1008. /**关闭按钮音效*/
  1009. closeButton = "closeButton",
  1010. }
  1011. /** ECPM */
  1012. type ADShowCfg = {
  1013. average_ecpm: number,
  1014. insert_probability: number,
  1015. is_show_banner: number,
  1016. is_show_inter: number,
  1017. }
  1018. type PlayerInfoType = {
  1019. /** 自定义不需要校验的数据 */
  1020. gameUserData: object,
  1021. /** 今日是否有签到 */
  1022. isSignInToday: number,
  1023. /** 存钱罐存款 */
  1024. piggyBank: number,
  1025. /** 存钱罐能否提现 */
  1026. isWithdrawable: boolean,
  1027. /** 存钱罐每日提现次数 */
  1028. piggyBankCashTimes: number,
  1029. /** 累计存钱罐每日提现次数 */
  1030. totalPiggyBankCashTimes: number,
  1031. /** 提现进度 */
  1032. cashIndex: number,
  1033. /** 上次登录时间 */
  1034. lastTime: number,
  1035. /** 登录天数 */
  1036. loginDays: number,
  1037. newPlayer: Boolean,
  1038. /** 红包币数量 */
  1039. redMoney: number,
  1040. /** 累计签到次数 客户端理解 上次签到天数 */
  1041. signInDay: number,
  1042. /** 转盘次数 */
  1043. turntableTimes: number,
  1044. /** 版本号 */
  1045. versioncfg: string,
  1046. /** 福袋数据 */
  1047. userTuCaoInfo: object,
  1048. }
  1049. /**事件类型 */
  1050. export enum EVENT_TYPE {
  1051. BACK_WxAuth = "BACK_WxAuth",
  1052. UPDATE_CashNum = "UPDATE_CashNum",
  1053. }
  1054. /** 数据事件Id */
  1055. export enum DataEventId {
  1056. /** 点击 */
  1057. guide = "guide",
  1058. /** 按钮点击 */
  1059. button_click = "button_click",
  1060. /** 看视频 */
  1061. video_init = "video_init",
  1062. /** 看视频完成 */
  1063. video_end = "video_end",
  1064. /** 福袋功能 */
  1065. fudaiFunction = "fudaiFunction",
  1066. /** 互推功能 */
  1067. hutuiFunction = "hutuiFunction",
  1068. /** 关卡 */
  1069. level = "level",
  1070. /** 常规提现-红包币提现 */
  1071. commonWithDrawal = "commonWithDrawl",
  1072. /** 存钱罐提现 */
  1073. jarWithDrawal = "jarWithDrawal",
  1074. /** 签到提现 */
  1075. signWithDrawal = "signWithDrawal",
  1076. /** 激励视频ecpm */
  1077. ad_videoEcpm = 'ad_video',
  1078. /** 开屏ecpm */
  1079. ad_openEcpm = 'ad_open'
  1080. }
  1081. /** 视频广告类型 */
  1082. export enum VideoAdType {
  1083. Null = "",
  1084. /** 关卡积分红包 */
  1085. LevelScoreRedBag = "关卡积分红包",
  1086. /** 主界面气泡红包 */
  1087. StartQiPaoRedBag = "主界面气泡红包",
  1088. /** 关卡气泡红包 */
  1089. LevelQiPaoRedBag = "关卡气泡红包",
  1090. /** 转盘 */
  1091. Turntable = "转盘视频",
  1092. /** 福袋视频" */
  1093. Fudai = "福袋视频",
  1094. /** 签到视频 */
  1095. Sign = "签到视频",
  1096. //钱包提现新手视频
  1097. CashOutNoviceWelfare = "钱包提现新手视频",
  1098. //钱包提现加现金视频
  1099. CashOutAddCash = "钱包提现加现金视频",
  1100. //农场等级加经验视频
  1101. farmGradeAddExp = "农场等级加经验视频",
  1102. //农场等级提现视频
  1103. farmGradeCashOut = "农场等级提现视频",
  1104. }
  1105. export enum ExpAddType{
  1106. EAT_video = 1,
  1107. EAT_harvest = 2,
  1108. }
  1109. export enum FarmState {
  1110. Lock,
  1111. CanUnlock,
  1112. Empty,
  1113. Growing,
  1114. Ripe,
  1115. Sick
  1116. }
  1117. export enum PastureState {
  1118. Lock,
  1119. Empty,
  1120. Growing,
  1121. Ripe,
  1122. Sick
  1123. }
  1124. export enum ProductType {
  1125. nzw = '农作物',
  1126. dw = '动物',
  1127. bmhc = '爆米花厂',
  1128. gdp = '糕点铺',
  1129. ztc = '制糖厂',
  1130. lrc = '炼乳厂',
  1131. gfmg = '功夫面馆',
  1132. kcd = '快餐店'
  1133. }
  1134. /** 动物状态 */
  1135. export enum AnimalState {
  1136. Hanger,
  1137. Eat,
  1138. Wait
  1139. }
  1140. export enum FactroyState {
  1141. Lock,
  1142. Empty,
  1143. Producting,
  1144. Ripe,
  1145. Sick
  1146. }