GameData.ts 41 KB

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