GameData.ts 38 KB

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