GameData.ts 39 KB

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