GameData.ts 41 KB

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