GameData.ts 40 KB

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