UserInfo.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. const GameModule = require("../utils/GameModule");
  2. const DWTool = require("../utils/DWTool");
  3. const Api = require('../net/Api');
  4. const GameNotificationKey = require('../utils/GameEnum').GameNotificationKey;
  5. const TapTapTool = require("../utils/TapTapTool");
  6. // var cityList = require('../data/city');
  7. // const BuildingModel = require('./utils/BuildingModel');
  8. // const AlertManager = require('./utils/AlertManager');
  9. // const ReportType = require("./utils/GameEnum").ReportType;
  10. cc.Class({
  11. extends: cc.Component,
  12. properties: {
  13. _reportFailDuration: 0,
  14. grossIncomeLabel: cc.Label,
  15. grossCoin: sp.Skeleton,
  16. grossIncomeRichText: cc.RichText,
  17. rateRichText: cc.RichText,
  18. clickCoinRichText: cc.RichText,
  19. rateCoinRichText: cc.RichText,
  20. headSprite: cc.Sprite,
  21. starsLabel: cc.Label,
  22. starsProgress: cc.ProgressBar,
  23. diamondLabel: cc.Label,
  24. recordModify: [],
  25. recordUnlockModify: [],
  26. buildingData: [],
  27. _stars: 0,
  28. stars: {
  29. get: function () {
  30. return this._stars;
  31. },
  32. set: function (value) {
  33. console.log('星星增加数 ' + value);
  34. this._stars = value;
  35. }
  36. },
  37. _gold: 0,
  38. gold: {
  39. get: function () {
  40. return this._gold;
  41. },
  42. set: function (value) {
  43. cc.sys.localStorage.setItem(`localGold_${Global.user.uid}`, JSON.stringify(this._gold));
  44. this._gold = value;
  45. this.grossIncomeRichText.string = `<outline color=#ffffff width=3><color=#009100>${TapTapTool.parseToString(this._gold)}</c></outline>`;
  46. }
  47. },
  48. //建筑每秒自动生成的金币数
  49. _rateGold: 0,
  50. rateGold: {
  51. get: function () {
  52. return this._rateGold;
  53. },
  54. set: function (value) {
  55. this._rateGold = value;
  56. if (this._rateGold.n > 0) {
  57. this.rateRichText.node.active = true;
  58. let rate = TapTapTool.multiple(GameModule.skill.multiple, this.perpetualMt);
  59. rate = TapTapTool.multiple(rate, GameModule.shop.multiple);
  60. let newValue = TapTapTool.multiple(rate, this._rateGold);
  61. this.rateRichText.string = `<outline color=#ffffff width=3><color=#009100>+${TapTapTool.parseToString(newValue)}</c></outline>`;
  62. this.rateCoinRichText.string = `<b>${TapTapTool.parseToString(newValue)}</b>`;
  63. } else {
  64. this.rateRichText.node.active = false;
  65. this.rateCoinRichText.string = "<b>0</b>";
  66. }
  67. }
  68. },
  69. //自动每次点击需要的秒数
  70. _secondClick: 0,
  71. secondClick: {
  72. get: function () {
  73. return this._secondClick;
  74. },
  75. set: function (value) {
  76. this._secondClick = value < 0 ? 0 : parseFloat(value);
  77. this.unschedule(this.autoClickGetGold, this);
  78. if (this._secondClick > 0) {
  79. this.schedule(this.autoClickGetGold, this._secondClick);
  80. }
  81. }
  82. },
  83. _diamond: 0,
  84. diamond: {
  85. get: function () {
  86. return this._diamond;
  87. },
  88. set: function (value) {
  89. this._diamond = value;
  90. this.diamondLabel.string = this._diamond;
  91. }
  92. },
  93. _clickCount: 0,
  94. clickCount: {
  95. get: function () {
  96. return this._clickCount;
  97. },
  98. set: function (value) {
  99. this._clickCount = value;
  100. }
  101. },
  102. _buyStarCount: 0,
  103. buyStarCount: {
  104. get: function () {
  105. return this._buyStarCount;
  106. },
  107. set: function (value) {
  108. this._buyStarCount = value;
  109. }
  110. },
  111. _buildingLevel: 1,
  112. buildingLevel: {
  113. get: function () {
  114. return this._buildingLevel;
  115. },
  116. set: function (value) {
  117. this._buildingLevel = value;
  118. // 把用户的总部等级上报给微信
  119. DWTool.submitWechatScore(this._buildingLevel);
  120. }
  121. },
  122. // //获得奖励的永久产出倍数
  123. // perpetualMt: {
  124. // default: 1,
  125. // visible: false,
  126. // type: cc.Float
  127. // },
  128. // //获得奖励的永久点击倍数
  129. // perpetualClickMt: {
  130. // default: 1,
  131. // visible: false,
  132. // type: cc.Float
  133. // },
  134. //入驻在建筑的明星加成倍数
  135. // starMt: {
  136. // default: 1,
  137. // visible: false,
  138. // type: cc.Float
  139. // },
  140. levelHomeItemFullCount: 0
  141. },
  142. onLoad() {
  143. GameModule.userInfo = this;
  144. this._rate = 0;
  145. this._isPlayAnimation = false;
  146. this.seq = 1;
  147. this._rateGold = {'n': 0, 'e': 0};
  148. this._gold = {'n': 0, 'e': 0};
  149. this.coinTap = {'n': 0, 'e': 0};
  150. this.buildingLevel = Global.userData.buildingLevel;
  151. // let objc = TapTapTool.goldStrToClass('3.265;29');
  152. // let str = TapTapTool.parseToString(objc);
  153. // let sub = TapTapTool.compare({'n': 323, 'e': 25}, objc);
  154. // let n = {'n': 423, 'e': 0};
  155. // let m = {'n': 1.1, 'e': 0};
  156. // console.log(TapTapTool.multiple(n, m));
  157. // console.log(sub);
  158. // console.log(str);
  159. // 初始化用户信息
  160. this.initUserInfo();
  161. // 轮询上报游戏数据
  162. this.reportFunc = () => {
  163. if (this._reportFailDuration > 0) {
  164. this._reportFailDuration -= 3;
  165. return;
  166. }
  167. this.doReport();
  168. };
  169. this.schedule(this.reportFunc, 3.0);
  170. //建筑每秒自动赚的钱
  171. this.addMoneyFunc = () => {
  172. this.buildingAddMoney();
  173. };
  174. this.schedule(this.addMoneyFunc, 1.0);
  175. this.reportFirst();
  176. },
  177. /**
  178. * 每秒增加金币
  179. */
  180. buildingAddMoney() {
  181. if (this.rateGold.n > 0) {
  182. // this.gold = TapTapTool.add(this.rateGold, this.gold);
  183. let add = TapTapTool.multiple(this.rateGold, GameModule.skill.multiple);
  184. add = TapTapTool.multiple(add, GameModule.shop.multiple);
  185. this.gold = TapTapTool.add(this.gold, TapTapTool.multiple(add, this.perpetualMt));
  186. // this.gold += (this.rateGold * GameModule.skill.multiple * this.perpetualMt);
  187. this.secondRateTextAnimation();
  188. }
  189. },
  190. secondRateTextAnimation() {
  191. let fadeIn = cc.fadeIn(0.2);
  192. let delay = cc.delayTime(0.3);
  193. let fadeOut = cc.fadeOut(0.3);
  194. let action = cc.sequence(fadeIn, delay, fadeOut);
  195. this.rateRichText.node.runAction(action);
  196. },
  197. refreshSecondText() {
  198. if (this._rateGold.n > 0) {
  199. this.rateRichText.node.active = true;
  200. let rate = TapTapTool.multiple(GameModule.skill.multiple, this.perpetualMt);
  201. rate = TapTapTool.multiple(rate, GameModule.shop.multiple);
  202. let newValue = TapTapTool.multiple(rate, this._rateGold);
  203. this.rateRichText.string = `<outline color=#ffffff width=3><color=#009100>+${TapTapTool.parseToString(newValue)}</c></outline>`;
  204. this.rateCoinRichText.string = `<b>${TapTapTool.parseToString(newValue)}</b>`;
  205. } else {
  206. this.rateRichText.node.active = false;
  207. }
  208. if (GameModule.userInfo.coinTap.n > 0) {
  209. this.clickCoinRichText.string = `<b>${TapTapTool.parseToString(GameModule.userInfo.coinTap)}</b>`;
  210. } else {
  211. this.clickCoinRichText.string = "<b>0</b>";
  212. }
  213. },
  214. /**
  215. * 每秒自动点击次数
  216. */
  217. autoClickGetGold() {
  218. GameEvent.fire(GameNotificationKey.AutoClickGold);
  219. },
  220. /**
  221. * 上报用户数据
  222. */
  223. doReport() {
  224. // return;
  225. let goldStr = this._gold.n + ";" + this._gold.e;
  226. DWTool.reportInfo(this.seq, goldStr, this.stars, this.clickCount, this.recordModify, this.recordUnlockModify)
  227. .then(() => {
  228. this._reportFailDuration = 0;
  229. this.recordModify = [];
  230. this.recordUnlockModify = [];
  231. this.seq += 1;
  232. }).catch(err => {
  233. if (err.code === -4) {
  234. this._reportFailDuration = 60;
  235. }
  236. console.log('上报失败 ' + err.msg);
  237. })
  238. },
  239. reportFirst() {
  240. //如果为true则以网络数据为主不用覆盖
  241. if (Global.localData) {
  242. return;
  243. }
  244. this.compareRoomsLocalData();
  245. let goldStr = this._gold.n + ";" + this._gold.e;
  246. DWTool.reportRoomsInfo(goldStr, this.buildingData);
  247. },
  248. /**
  249. * 初始化用户数据
  250. */
  251. initUserInfo() {
  252. let userInfo = this.userInfo = Global.userData;
  253. if (userInfo != undefined) {
  254. let goldObj = this.compareGoldLocalData();
  255. this.updateUserRes({
  256. gold: goldObj,
  257. stars: userInfo.ogStars,
  258. diamond: userInfo.diamond,
  259. ticket: userInfo.ticket,
  260. clickCount: userInfo.clickCount
  261. });
  262. this.perpetualMt = userInfo.mtObj;
  263. this.perpetualClickMt = userInfo.clickMtObj;
  264. this.buyStarCount = userInfo.buyStarCount;
  265. }
  266. },
  267. compareGoldLocalData() {
  268. let localGold = cc.sys.localStorage.getItem(`localGold_${Global.user.uid}`);
  269. if (this.userInfo != undefined && localGold!= undefined && localGold && !Global.localData) {
  270. let goldObj = JSON.parse(localGold);
  271. if (goldObj != undefined) {
  272. return goldObj;
  273. } else {
  274. return this.userInfo.goldObj;
  275. }
  276. } else {
  277. return this.userInfo.goldObj;
  278. }
  279. },
  280. compareRoomsLocalData() {
  281. let localData = cc.sys.localStorage.getItem(`localRooms_${Global.user.uid}`);
  282. this.buildingData = Global.BuildingManager.networkRooms;
  283. if (this.userInfo != undefined && localData!= undefined && localData && !Global.localData) {
  284. let userRooms = JSON.parse(localData);
  285. if (Global.BuildingManager.networkRooms && Global.BuildingManager.networkRooms.length > 0 && userRooms.length > 0) {
  286. userRooms.forEach(room => {
  287. this.buildingData.forEach(n => {
  288. if (room.roomId == n.roomId) {
  289. n.roomLevel = Math.max(room.level, n.roomLevel);
  290. }
  291. });
  292. });
  293. }
  294. }
  295. },
  296. /**
  297. * 更新用户资源数据
  298. * @param {object} data 更新资源对象
  299. * @param {number} data.grossIncome 增加的金币
  300. * @param {number} data.stars 星星
  301. * @param {number} data.diamond 钻石
  302. * @param {number} data.ticket 艺人券
  303. */
  304. updateUserRes(data) {
  305. // console.log("Update userData: ", JSON.stringify(data));
  306. if (data.gold) {
  307. this.gold = TapTapTool.add(data.gold, this.gold);
  308. }
  309. if (data.stars) {
  310. this.stars += parseInt(data.stars);
  311. }
  312. if (data.diamond) {
  313. this.diamond += parseInt(data.diamond);
  314. }
  315. if (data.ticket) {
  316. this.ticket += parseInt(data.ticket);
  317. }
  318. if (data.clickCount) {
  319. this.clickCount += parseInt(data.clickCount);
  320. }
  321. },
  322. updateRecordModify(buildingInfo) {
  323. for (let i = 0; i < this.recordModify.length; i++) {
  324. let temp = this.recordModify[i];
  325. if (buildingInfo.roomId == temp.roomId) {
  326. this.recordModify.splice(i, 1, buildingInfo);
  327. return;
  328. }
  329. }
  330. this.recordModify.push(buildingInfo);
  331. }
  332. });