GameData.ts 38 KB

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