GameData.ts 39 KB

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