GameData.ts 42 KB

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