GameData.ts 39 KB

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