GameData.ts 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319
  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.pastureSystem.pastureIcons.length >= 3 && gData.factorySystem.factoryIcons.length >= 6) {
  354. this.hasFirstCheck = true;
  355. Util.objSortByKey(gData.pastureSystem.pastureIcons, 'sortID');
  356. gData.gameData.setNextProduct(false);
  357. }
  358. }
  359. makeProduct() {
  360. switch (this.nextType) {
  361. case 0:
  362. mk.tip.pop('农场已满,无法继续生产');
  363. break;
  364. case 1:
  365. gData.farmSystem.btnMake();
  366. gData.gameData.nextMake = null;
  367. this.nextMake = null;
  368. this.setNextProduct(false);
  369. break;
  370. case 2:
  371. gData.pastureSystem.btnMake();
  372. gData.gameData.nextMake = null;
  373. this.nextMake = null;
  374. this.setNextProduct();
  375. gData.adData.checkPopRed();
  376. break;
  377. case 3:
  378. gData.factorySystem.btnMake();
  379. gData.gameData.nextMake = null;
  380. this.nextMake = null;
  381. this.setNextProduct();
  382. break;
  383. }
  384. }
  385. public setFarmDataMap(id, data, sendToServer = true) {
  386. this._farmDataMap.set(id, data);
  387. let len = this._farmData.length;
  388. for (var i = 0; i < len; i++) {
  389. if (this._farmData[i].id == id) {
  390. this._farmData[i].state = data.state;
  391. this._farmData[i].productID = data.productID;
  392. this._farmData[i].growSpan = data.growSpan;
  393. break;
  394. }
  395. }
  396. this.needFreshArr.push(id);
  397. //更新到服务器
  398. if (sendToServer) {
  399. this.setProp(GameProp.farmData, this._farmData);
  400. }
  401. }
  402. public getFarmDataMap(id) {
  403. return this._farmDataMap.get(id);
  404. }
  405. /** 服务器数据设置到本地 */
  406. public setFramDataMapByServer() {
  407. this._farmData = this.getProp(GameProp.farmData);
  408. if (!this._farmData) {
  409. this._farmData = [];
  410. }
  411. let len = this._farmData.length;
  412. //新玩家初始化农田数据
  413. if (len < 18) {
  414. let id = 31000;
  415. let data = null;
  416. for (var i = 0; i < 18; i++) {
  417. if (i < 9) {
  418. data = { id: id, state: FarmState.Empty, productID: 0, growSpan: 0 };
  419. }
  420. else if (i == 9) {
  421. data = { id: id, state: FarmState.CanUnlock, productID: 0, growSpan: 0 };
  422. }
  423. else {
  424. data = { id: id, state: FarmState.Lock, productID: 0, growSpan: 0 };
  425. }
  426. this._farmData.push(data);
  427. this._farmDataMap.set(id, data);
  428. id++;
  429. }
  430. //第一次初始数据
  431. this.setMaxProduct(ProductType.nzw, 10003);
  432. //更新到服务器
  433. this.setProp(GameProp.farmData, this._farmData);
  434. }
  435. else {
  436. for (var i = 0; i < len; i++) {
  437. this._farmDataMap.set(this._farmData[i].id, this._farmData[i]);
  438. }
  439. }
  440. //初始化产品最大次数
  441. }
  442. /** puduct配置数据 <picture, config> */
  443. private _productMap: Map<number, any> = new Map();
  444. /** puduct类型配置数据 <tab, 类型数组> */
  445. private _productTypeMap: Map<string, any> = new Map();
  446. /** 设置puduct配置数据 */
  447. public setProductMapFromJson() {
  448. // let productJson = this.configs.product;
  449. //先用本地数据
  450. let productJson = gData.configData.configMap.get('product');
  451. let len = productJson.length;
  452. let product = null;
  453. let tab = '';
  454. let arr = [];
  455. for (var i = 0; i < len; i++) {
  456. product = productJson[i];
  457. this._productMap.set(product.picture, product);
  458. if (tab != product.tab) {
  459. arr = [];
  460. this._productTypeMap.set(product.tab, arr);
  461. tab = product.tab;
  462. }
  463. arr.push(product);
  464. }
  465. }
  466. /** 根据id获取单个数据 */
  467. public getProductMap(id) {
  468. return this._productMap.get(id);
  469. }
  470. /** 根据tab获取数组 */
  471. public getProductArrByType(type: ProductType) {
  472. return this._productTypeMap.get(type);
  473. }
  474. /** 产品生产/种植次数 服务器数据 */
  475. private _productMakeTimesData = [];
  476. /** 产品生产/种植次数 */
  477. private _productMakeTimesMap: Map<number, number> = new Map();
  478. addProductMakeTimesById(id: number) {
  479. let times = this._productMakeTimesMap.get(id);
  480. if (!times) {
  481. times = 0;
  482. }
  483. times++;
  484. this._productMakeTimesMap.set(id, times);
  485. //更新到服务器
  486. let has = false;
  487. let len = this._productMakeTimesData.length;
  488. for (var i = 0; i < len; i++) {
  489. if (this._productMakeTimesData[i].id == id) {
  490. this._productMakeTimesData[i].times = times;
  491. has = true;
  492. break;
  493. }
  494. }
  495. if (!has) {
  496. let data = { 'id': id, 'times': times };
  497. this._productMakeTimesData.push(data);
  498. }
  499. //设置可种植/生产最大id
  500. let nextID = id + 1;
  501. let curConfig = this.getProductMap(id);
  502. let nextConfig = this.getProductMap(nextID);
  503. if (id == this.getMaxProduct(curConfig.tab)) {
  504. if (nextConfig) {
  505. gData.plantData.init_itemIndex = nextID;
  506. }
  507. }
  508. if (nextConfig) {
  509. if (nextConfig.tab == curConfig.tab && nextID > this.getMaxProduct(curConfig.tab)) {
  510. if (nextConfig.unlock == 1 && nextConfig.value <= times) {
  511. this.setMaxProduct(curConfig.tab, nextID);
  512. let nConfig = this.getProductMap(nextID + 1);
  513. if (nConfig) {
  514. gData.plantData.init_lock = nextID + 1;
  515. }
  516. }
  517. }
  518. }
  519. this.setProp(GameProp.productMakeTimes, this._productMakeTimesData);
  520. }
  521. getProductMakeTimesById(id: number) {
  522. let times = this._productMakeTimesMap.get(id);
  523. if (!times) {
  524. times = 0
  525. }
  526. return times;
  527. }
  528. //------------- 养殖场数据-----------
  529. /** 养殖场数据 服务器数据 */
  530. private _pastureData = [];
  531. /** 养殖场字典 本地数据,方便操作*/
  532. private _pastureDataMap: Map<number, any> = new Map();
  533. public setPastureDataMap(id, data, sendToServer = true) {
  534. this._pastureDataMap.set(id, data);
  535. let len = this._pastureData.length;
  536. for (var i = 0; i < len; i++) {
  537. if (this._pastureData[i].id == id) {
  538. this._pastureData[i].state = data.state;
  539. this._pastureData[i].productID = data.productID;
  540. this._pastureData[i].growSpan = data.growSpan;
  541. break;
  542. }
  543. }
  544. this.needFreshArr.push(id);
  545. //更新到服务器
  546. if (sendToServer) {
  547. this.setProp(GameProp.pastureData, this._pastureData);
  548. }
  549. }
  550. public getPastureDataMap(id) {
  551. return this._pastureDataMap.get(id);
  552. }
  553. /** 服务器数据设置到本地 */
  554. public setPastureDataMapByServer() {
  555. this._pastureData = this.getProp(GameProp.pastureData);
  556. if (!this._pastureData) {
  557. this._pastureData = [];
  558. }
  559. let len = this._pastureData.length;
  560. //新玩家初始化养殖场数据
  561. if (len < 3) {
  562. let arr = this.getProductArrByType(ProductType.dw);
  563. let len1 = arr.length;
  564. let id = 30001;
  565. let data = null;
  566. for (var i = 0; i < len1; i++) {
  567. if (arr[i].value < this.gameData.gradeLevel) {
  568. data = { id: id, state: PastureState.Lock, productID: arr[i].picture, growSpan: 0 };
  569. }
  570. else {
  571. data = { id: id, state: PastureState.Empty, productID: arr[i].picture, growSpan: 0 };
  572. }
  573. this._pastureData.push(data);
  574. this._pastureDataMap.set(id, data);
  575. id++;
  576. }
  577. //更新到服务器
  578. this.setProp(GameProp.pastureData, this._pastureData);
  579. }
  580. else {
  581. for (var i = 0; i < len; i++) {
  582. this._pastureDataMap.set(this._pastureData[i].id, this._pastureData[i]);
  583. }
  584. }
  585. }
  586. //------------- 工厂数据-----------
  587. /** 工厂数据 服务器数据 */
  588. private _factoryData = [];
  589. /** 工厂字典 本地数据,方便操作*/
  590. private _factoryDataMap: Map<number, any> = new Map();
  591. public setFactoryDataMap(id, data, sendToServer = true) {
  592. this._factoryDataMap.set(id, data);
  593. let len = this._factoryData.length;
  594. for (var i = 0; i < len; i++) {
  595. if (this._factoryData[i].id == id) {
  596. this._factoryData[i].state = data.state;
  597. this._factoryData[i].productID = data.productID;
  598. this._factoryData[i].growSpan = data.growSpan;
  599. break;
  600. }
  601. }
  602. this.needFreshArr.push(id);
  603. //更新到服务器
  604. if (sendToServer) {
  605. this.setProp(GameProp.factoryData, this._factoryData);
  606. }
  607. }
  608. public getFactoryDataMap(id) {
  609. return this._factoryDataMap.get(id);
  610. }
  611. /** 服务器数据设置到本地 */
  612. public setFactoryDataMapByServer() {
  613. this._factoryData = this.getProp(GameProp.factoryData);
  614. if (!this._factoryData) {
  615. this._factoryData = [];
  616. }
  617. let len = this._factoryData.length;
  618. //新玩家初始化工厂数据
  619. if (len < 6) {
  620. let id = 30004;
  621. let data = null;
  622. let tab = null;
  623. for (var i = 0; i < 6; i++) {
  624. tab = this.getTabByConfigID(id);
  625. let arr = this.getProductArrByType(tab);
  626. if (arr[0].value < this.gameData.gradeLevel) {
  627. data = { id: id, state: FactroyState.Lock, productID: 0, growSpan: 0 };
  628. }
  629. else {
  630. data = { id: id, state: FactroyState.Empty, productID: 0, growSpan: 0 };
  631. //更新最大可购买商品
  632. this.setMaxProduct(tab, arr[0].picture, false);
  633. }
  634. this._factoryData.push(data);
  635. this._factoryDataMap.set(id, data);
  636. id++;
  637. }
  638. //更新到服务器
  639. this.setProp(GameProp.maxProduct, this._maxProduct);
  640. this.setProp(GameProp.factoryData, this._factoryData);
  641. }
  642. else {
  643. for (var i = 0; i < len; i++) {
  644. this._factoryDataMap.set(this._factoryData[i].id, this._factoryData[i]);
  645. }
  646. }
  647. }
  648. /** 通过工厂建筑物ID获取标签 */
  649. getTabByConfigID(configID: number): ProductType {
  650. let tab = ProductType.bmhc;
  651. switch (configID) {
  652. case 30004:
  653. tab = ProductType.bmhc;
  654. break;
  655. case 30005:
  656. tab = ProductType.gdp;
  657. break;
  658. case 30006:
  659. tab = ProductType.ztc;
  660. break;
  661. case 30007:
  662. tab = ProductType.lrc;
  663. break;
  664. case 30008:
  665. tab = ProductType.gfmg;
  666. break;
  667. case 30009:
  668. tab = ProductType.kcd;
  669. break;
  670. }
  671. return tab;
  672. }
  673. /** 通过标签获取工厂建筑物ID */
  674. getConfigIDByTab(tab: ProductType): number {
  675. let configID = 0;
  676. switch (tab) {
  677. case ProductType.bmhc:
  678. configID = 30004;
  679. break;
  680. case ProductType.gdp:
  681. configID = 30005;
  682. break;
  683. case ProductType.ztc:
  684. configID = 30006;
  685. break;
  686. case ProductType.lrc:
  687. configID = 30007;
  688. break;
  689. case ProductType.gfmg:
  690. configID = 30008;
  691. break;
  692. case ProductType.kcd:
  693. configID = 30009;
  694. break;
  695. }
  696. return configID;
  697. }
  698. /** 最大可种植/生产商品数组 */
  699. private _maxProduct = [];
  700. public getMaxProduct(tab) {
  701. if (this._maxProduct) {
  702. let len = this._maxProduct.length;
  703. let data = null;
  704. for (var i = 0; i < len; i++) {
  705. data = this._maxProduct[i];
  706. if (data.tab == tab) {
  707. return data.productID;
  708. }
  709. }
  710. }
  711. return null;
  712. }
  713. public setMaxProduct(tab, id, sendToServer = true) {
  714. if (!this._maxProduct) {
  715. this._maxProduct = [];
  716. }
  717. let len = this._maxProduct.length;
  718. let data = null;
  719. let has = false;
  720. for (var i = 0; i < len; i++) {
  721. data = this._maxProduct[i];
  722. if (data.tab == tab) {
  723. data.productID = id;
  724. has = true;
  725. break;
  726. }
  727. }
  728. if (!has) {
  729. data = { tab: tab, productID: id };
  730. this._maxProduct.push(data);
  731. }
  732. if (sendToServer) {
  733. this.setProp(GameProp.maxProduct, this._maxProduct);
  734. }
  735. }
  736. public initSetMaxProduct() {
  737. if (this._maxProduct.length <= 0) {
  738. this._maxProduct = this.getProp(GameProp.maxProduct);
  739. }
  740. }
  741. private initProductTimes() {
  742. this._productMakeTimesData = this.getProp(GameProp.productMakeTimes);
  743. let len = this._productMakeTimesData.length;
  744. for (var i = 0; i < len; i++) {
  745. this._productMakeTimesMap.set(this._productMakeTimesData[i].id, this._productMakeTimesData[i].times);
  746. }
  747. }
  748. /** 农场升级时解锁工厂,检查最大可种植/生产商品 */
  749. public checkGradeUpUnLock() {
  750. for (var key in ProductType) {
  751. let tab = ProductType[key];
  752. let configArr = this.getProductArrByType(tab);
  753. let len = configArr.length;
  754. let data = null;
  755. let needUpdate = false;
  756. switch (tab) {
  757. case ProductType.nzw:
  758. //不做处理,游戏最开始已解锁
  759. break;
  760. case ProductType.dw:
  761. let id = 30001;
  762. needUpdate = false;
  763. for (var i = 0; i < len; i++) {
  764. data = this.getPastureDataMap(id);
  765. if (data.state == PastureState.Lock && configArr[i].value <= this.gameData.gradeLevel) {
  766. data = { id: id, state: PastureState.Empty, productID: configArr[i].picture, growSpan: 0 };
  767. this.setPastureDataMap(id, data, false);
  768. needUpdate = true;
  769. }
  770. id++;
  771. }
  772. //更新到服务器
  773. if (needUpdate) {
  774. this.setProp(GameProp.pastureData, this._pastureData);
  775. this.setNextProduct();
  776. }
  777. break;
  778. default:
  779. //工厂解锁根据第一个商品
  780. needUpdate = false;
  781. let configID = this.getConfigIDByTab(tab);
  782. data = this.getFactoryDataMap(configID);
  783. if (data.state == FactroyState.Lock && configArr[0].value <= this.gameData.gradeLevel) {
  784. data = { id: id, state: FactroyState.Empty, productID: 0, growSpan: 0 };
  785. this.setFactoryDataMap(configID, data, false);
  786. needUpdate = true;
  787. //更新最大可购买商品
  788. this.setMaxProduct(tab, configArr[0].picture);
  789. }
  790. if (needUpdate) {
  791. this.setProp(GameProp.factoryData, this._factoryData);
  792. this.setNextProduct();
  793. }
  794. break;
  795. }
  796. }
  797. }
  798. }
  799. /**
  800. * 所有模块的非校验数据
  801. */
  802. export enum GameProp {
  803. /** -------------------- 通用玩家数据 ------------------------- */
  804. /** 是否新手 0新手 1老手 */
  805. newPlayer = 1,
  806. /** 当前新手引导步骤 */
  807. guideID = 2,
  808. /** 需要自动弹界面 每次登录都弹 除首次 表示今日是否要自动开 1自动开 2不自动开 其他不处理*/
  809. isAutoOpenPanel = 3,
  810. /** 关闭常规提现时需要自动开存钱罐 每天一次 表示今日有没有开过 */
  811. isOpenBankOnCloseCash = 4,
  812. //看视频次数
  813. videoTimes = 5,
  814. //提现次数
  815. cashTimes = 6,
  816. /** ------------------ 游戏核心数据 --------------------------- */
  817. /** 农田数据 */
  818. farmData = 11,
  819. /** 产品生产次数 */
  820. productMakeTimes = 12,
  821. /** 可生产最大产品字典 */
  822. maxProduct = 13,
  823. /** 养殖场数据 */
  824. pastureData = 14,
  825. /** 工厂数据 */
  826. factoryData = 15,
  827. /** 剩余可种植次数 */
  828. leftTimes = 16,
  829. /**
  830. 图鉴领取数据*/
  831. farmMapGetRewardData = 17,
  832. /** ------------------ 转盘数据 ------------------------------ */
  833. turnable_leftTimes = 20,
  834. /** ------------------ 存钱罐数据 ----------------------------- */
  835. /** ------------------ 签到数据 ----------------------------- */
  836. /** 上次签到进度 */
  837. sign_last_bar = 120,
  838. /** 当前能否签到 */
  839. sign_can = 121,
  840. /** ------------------ 红包提现 ----------------------------- */
  841. /** 提现进度 */
  842. redBag_cash_bar = 220,
  843. }
  844. /**
  845. * 玩家数据
  846. */
  847. class PlayerProp {
  848. /** 自定义不需要校验的数据 */
  849. gameUserData = 0;
  850. _isSignInToday = 0;
  851. /**
  852. * 今日是否有签到
  853. * - 0 未签到,表示可以签到
  854. * - 1 有签到,表示不可签到
  855. */
  856. set isSignInToday(value: number) {
  857. this._isSignInToday = value;
  858. gData.sign.init_data = true;
  859. gData.gameData.init_red_point = true;
  860. }
  861. get isSignInToday(): number {
  862. return this._isSignInToday;
  863. }
  864. private _piggyBank: number = 0;
  865. /** 存钱罐存款 */
  866. set piggyBank(value: number) {
  867. if (this._piggyBank === value) return;
  868. if (gData.gameData.gameData.isWithdrawable && value > 0) return;// 存钱罐可提现状态,不能增加存钱罐金额
  869. this._piggyBank = value;
  870. gData.gameData.init_coin = true;
  871. gData.pigbank.init_data = true;
  872. gData.gameData.init_red_point = true;
  873. }
  874. get piggyBank(): number {
  875. return this._piggyBank;
  876. }
  877. /** 初始化 */
  878. initPiggyBank(v) {
  879. this._piggyBank = v;
  880. }
  881. _isWithdrawable = 0;
  882. /**
  883. * 存钱罐能否提现
  884. * - 0 不能提现
  885. * - 1 能提现
  886. */
  887. set isWithdrawable(value: number) {
  888. if (this._isWithdrawable != value) {
  889. this._isWithdrawable = value;
  890. gData.pigbank.init_data = true;
  891. gData.gameData.init_red_point = true;
  892. }
  893. }
  894. get isWithdrawable(): number {
  895. return this._isWithdrawable;
  896. }
  897. /** 存钱罐每日提现次数 */
  898. piggyBankCashTimes = 0;
  899. /** 累计存钱罐每日提现次数 */
  900. totalPiggyBankCashTimes = 0;
  901. /** 提现进度 */
  902. cashIndex = 0;
  903. /** 上次登录时间 */
  904. lastTime = 0;
  905. /** 登录天数 */
  906. loginDays = 0;
  907. newPlayer = 0;
  908. private _redMoney: number = 0;
  909. /** 红包币数量 */
  910. set redMoney(value: number) {
  911. this._redMoney = value;
  912. gData.gameData.init_coin = true;
  913. gData.gameData.init_red_point = true;
  914. }
  915. get redMoney(): number {
  916. return this._redMoney;
  917. }
  918. _signInDay = 0;
  919. /** 累计签到次数 客户端理解 上次签到天数 */
  920. set signInDay(value: number) {
  921. if (this._signInDay === value) return;
  922. this._signInDay = value;
  923. gData.sign.init_data = true;
  924. }
  925. get signInDay(): number {
  926. return this._signInDay;
  927. }
  928. /** 转盘次数 */
  929. turntableTimes = 0;
  930. /** 版本号 */
  931. versioncfg = 0;
  932. /** 福袋数据 */
  933. userTuCaoInfo = 0;
  934. /** 能免费领红包 1 表示是新人第一次产出红包币,不用看视频*/
  935. isFirstRedMoney = 0;
  936. //农场等级
  937. gradeLevel = 6;
  938. //农场等级奖励数据
  939. farmGradeData: any = {};
  940. //农场经验
  941. private farmExp = 0;
  942. set farmExpValue(value: number) {
  943. this.farmExp = value;
  944. gData.gameData.init_expLevel = true;
  945. }
  946. get farmExpValue(): number {
  947. return this.farmExp;
  948. }
  949. /** ABTest分组id */
  950. groupCode = 0;
  951. }
  952. /**
  953. * 奖励状态
  954. */
  955. export enum RewardState {
  956. /** 已领取 */
  957. none = 1,
  958. /** 未解锁 */
  959. lock = 2,
  960. /** 可领取 */
  961. unlock = 3
  962. }
  963. /**
  964. * 奖励类型
  965. */
  966. export enum RewardType {
  967. /** 红包币 */
  968. redBag = 1,
  969. /** 毛币 */
  970. rmb = 2,
  971. /** 金猪币 */
  972. pigRmb = 3,
  973. }
  974. /**
  975. * 音频路径
  976. */
  977. export enum AUDIO_TYPE {
  978. /**主界面背景音乐*/
  979. bgm = 'bgm',
  980. /**打开存钱罐界面播放一次*/
  981. pigBank = 'pigBank',
  982. /**关卡玩法背景音乐*/
  983. missionBg = 'missionBg',
  984. /**红包币领取界面关闭音效*/
  985. rewardClose = 'rewardClose',
  986. /**红包币领取界面开启音效*/
  987. rewardOpen = 'rewardOpen',
  988. /**红包界面打开音效*/
  989. reward = 'reward',
  990. /**提现到账界面打开播放一次*/
  991. receiptNotice = 'receiptNotice',
  992. /**通用按钮点击音效 */
  993. button = 'button',
  994. /**转盘转动音效*/
  995. turnableplay = 'turnableplay',
  996. /**转盘转动音效*/
  997. turnplateDrawEnd = 'turnplateDrawEnd',
  998. /**主页背景音乐 */
  999. music_startBg = "music_startBg",
  1000. /**游戏背景音乐 */
  1001. music_gameBg = "music_gameBg",
  1002. /**按钮点击 */
  1003. ef_button_click = "ef_button_click",
  1004. /**红包打开 */
  1005. ef_redPacket_open = "ef_redPacket_open",
  1006. /**红包来了音效 */
  1007. ef_redPacket_come = "ef_redPacket_come",
  1008. /**消除4个 */
  1009. ef_bonus4 = "ef_bonus4",
  1010. /**消除5个 */
  1011. ef_bonus5 = "ef_bonus5",
  1012. /**消除6个 */
  1013. ef_bonus6 = "ef_bonus6",
  1014. /**消除7个 */
  1015. ef_bonus7 = "ef_bonus7",
  1016. /**消除8个 */
  1017. ef_bonus8 = "ef_bonus8",
  1018. /**普通消除音效 */
  1019. ef_eliminate = "ef_eliminate",
  1020. /**bouns消除音效 */
  1021. ef_eliminate_bonus = "ef_eliminate_bonus",
  1022. /**胜利音效 */
  1023. ef_win = "ef_win",
  1024. /**锤子 */
  1025. ef_change = "ef_change",
  1026. /**刷新 */
  1027. ef_refresh = "ef_refresh",
  1028. /**飞翔 */
  1029. ef_fly = "ef_fly",
  1030. /**关闭按钮音效*/
  1031. closeButton = "closeButton",
  1032. }
  1033. /** ECPM */
  1034. type ADShowCfg = {
  1035. average_ecpm: number,
  1036. insert_probability: number,
  1037. is_show_banner: number,
  1038. is_show_inter: number,
  1039. }
  1040. type PlayerInfoType = {
  1041. /** 自定义不需要校验的数据 */
  1042. gameUserData: object,
  1043. /** 今日是否有签到 */
  1044. isSignInToday: number,
  1045. /** 存钱罐存款 */
  1046. piggyBank: number,
  1047. /** 存钱罐能否提现 */
  1048. isWithdrawable: boolean,
  1049. /** 存钱罐每日提现次数 */
  1050. piggyBankCashTimes: number,
  1051. /** 累计存钱罐每日提现次数 */
  1052. totalPiggyBankCashTimes: number,
  1053. /** 提现进度 */
  1054. cashIndex: number,
  1055. /** 上次登录时间 */
  1056. lastTime: number,
  1057. /** 登录天数 */
  1058. loginDays: number,
  1059. newPlayer: Boolean,
  1060. /** 红包币数量 */
  1061. redMoney: number,
  1062. /** 累计签到次数 客户端理解 上次签到天数 */
  1063. signInDay: number,
  1064. /** 转盘次数 */
  1065. turntableTimes: number,
  1066. /** 版本号 */
  1067. versioncfg: string,
  1068. /** 福袋数据 */
  1069. userTuCaoInfo: object,
  1070. }
  1071. /**事件类型 */
  1072. export enum EVENT_TYPE {
  1073. BACK_WxAuth = "BACK_WxAuth",
  1074. UPDATE_CashNum = "UPDATE_CashNum",
  1075. }
  1076. /** 数据事件Id */
  1077. export enum DataEventId {
  1078. /** 点击 */
  1079. guide = "guide",
  1080. /** 按钮点击 */
  1081. button_click = "button_click",
  1082. /** 看视频 */
  1083. video_init = "video_init",
  1084. /** 看视频完成 */
  1085. video_end = "video_end",
  1086. /** 福袋功能 */
  1087. fudaiFunction = "fudaiFunction",
  1088. /** 互推功能 */
  1089. hutuiFunction = "hutuiFunction",
  1090. /** 关卡 */
  1091. level = "level",
  1092. /** 常规提现-红包币提现 */
  1093. commonWithDrawal = "commonWithDrawl",
  1094. /** 存钱罐提现 */
  1095. jarWithDrawal = "jarWithDrawal",
  1096. /** 签到提现 */
  1097. signWithDrawal = "signWithDrawal",
  1098. /** 激励视频ecpm */
  1099. ad_videoEcpm = 'ad_video',
  1100. /** 开屏ecpm */
  1101. ad_openEcpm = 'ad_open'
  1102. }
  1103. /** 视频广告类型 */
  1104. export enum VideoAdType {
  1105. Null = "",
  1106. /** 关卡积分红包 */
  1107. LevelScoreRedBag = "关卡积分红包",
  1108. /** 主界面气泡红包 */
  1109. StartQiPaoRedBag = "主界面气泡红包",
  1110. /** 关卡气泡红包 */
  1111. LevelQiPaoRedBag = "关卡气泡红包",
  1112. /** 转盘 */
  1113. Turntable = "转盘视频",
  1114. /** 福袋视频" */
  1115. Fudai = "福袋视频",
  1116. /** 签到视频 */
  1117. Sign = "签到视频",
  1118. //钱包提现新手视频
  1119. CashOutNoviceWelfare = "钱包提现新手视频",
  1120. //钱包提现加现金视频
  1121. CashOutAddCash = "钱包提现加现金视频",
  1122. //农场等级加经验视频
  1123. farmGradeAddExp = "农场等级加经验视频",
  1124. //农场等级提现视频
  1125. farmGradeCashOut = "农场等级提现视频",
  1126. }
  1127. export enum ExpAddType {
  1128. EAT_video = 1,
  1129. EAT_harvest = 2,
  1130. }
  1131. export enum FarmState {
  1132. Lock,
  1133. CanUnlock,
  1134. Empty,
  1135. Growing,
  1136. Ripe,
  1137. Sick
  1138. }
  1139. export enum PastureState {
  1140. Lock,
  1141. Empty,
  1142. Growing,
  1143. Ripe,
  1144. Sick
  1145. }
  1146. export enum ProductType {
  1147. nzw = '农作物',
  1148. dw = '动物',
  1149. bmhc = '爆米花厂',
  1150. gdp = '糕点铺',
  1151. ztc = '制糖厂',
  1152. lrc = '炼乳厂',
  1153. gfmg = '功夫面馆',
  1154. kcd = '快餐店'
  1155. }
  1156. /** 动物状态 */
  1157. export enum AnimalState {
  1158. Hanger,
  1159. Eat,
  1160. Wait
  1161. }
  1162. export enum FactroyState {
  1163. Lock,
  1164. Empty,
  1165. Producting,
  1166. Ripe,
  1167. Sick
  1168. }