GameData.ts 36 KB

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