GameData.ts 40 KB

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