UserInfo.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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_${GameGlobal.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. if (CC_WECHATGAME) {
  119. // 把用户的总部等级上报给微信
  120. DWTool.submitWechatScore(this._buildingLevel);
  121. } else if (CC_QQPLAY) {
  122. // 把用户的总部等级上报给QQ玩一玩
  123. DWTool.submitQQScore(this._buildingLevel);
  124. }
  125. }
  126. },
  127. levelHomeItemFullCount: 0
  128. },
  129. onLoad() {
  130. GameModule.userInfo = this;
  131. this._rate = 0;
  132. this._isPlayAnimation = false;
  133. this.seq = 1;
  134. this._rateGold = {'n': 0, 'e': 0};
  135. this._gold = {'n': 0, 'e': 0};
  136. this.coinTap = {'n': 0, 'e': 0};
  137. this.buildingLevel = GameGlobal.userData.buildingLevel;
  138. // let objc = TapTapTool.goldStrToClass('3.265;29');
  139. // let str = TapTapTool.parseToString(objc);
  140. // let sub = TapTapTool.compare({'n': 323, 'e': 25}, objc);
  141. // let n = {'n': 423, 'e': 0};
  142. // let m = {'n': 1.1, 'e': 0};
  143. // console.log(TapTapTool.multiple(n, m));
  144. // console.log(sub);
  145. // console.log(str);
  146. // 初始化用户信息
  147. this.initUserInfo();
  148. // 轮询上报游戏数据
  149. this.reportFunc = () => {
  150. if (this._reportFailDuration > 0) {
  151. this._reportFailDuration -= 3;
  152. return;
  153. }
  154. this.doReport();
  155. };
  156. this.schedule(this.reportFunc, 3.0);
  157. //建筑每秒自动赚的钱
  158. this.addMoneyFunc = () => {
  159. this.buildingAddMoney();
  160. };
  161. this.schedule(this.addMoneyFunc, 1.0);
  162. this.reportFirst();
  163. },
  164. /**
  165. * 每秒增加金币
  166. */
  167. buildingAddMoney() {
  168. if (this.rateGold.n > 0) {
  169. // this.gold = TapTapTool.add(this.rateGold, this.gold);
  170. let add = TapTapTool.multiple(this.rateGold, GameModule.skill.multiple);
  171. add = TapTapTool.multiple(add, GameModule.shop.multiple);
  172. this.gold = TapTapTool.add(this.gold, TapTapTool.multiple(add, this.perpetualMt));
  173. // this.gold += (this.rateGold * GameModule.skill.multiple * this.perpetualMt);
  174. this.secondRateTextAnimation();
  175. }
  176. },
  177. secondRateTextAnimation() {
  178. let fadeIn = cc.fadeIn(0.2);
  179. let delay = cc.delayTime(0.3);
  180. let fadeOut = cc.fadeOut(0.3);
  181. let action = cc.sequence(fadeIn, delay, fadeOut);
  182. this.rateRichText.node.runAction(action);
  183. },
  184. refreshSecondText() {
  185. if (this._rateGold.n > 0) {
  186. this.rateRichText.node.active = true;
  187. let rate = TapTapTool.multiple(GameModule.skill.multiple, this.perpetualMt);
  188. rate = TapTapTool.multiple(rate, GameModule.shop.multiple);
  189. let newValue = TapTapTool.multiple(rate, this._rateGold);
  190. this.rateRichText.string = `<outline color=#ffffff width=3><color=#009100>+${TapTapTool.parseToString(newValue)}</c></outline>`;
  191. this.rateCoinRichText.string = `<b>${TapTapTool.parseToString(newValue)}</b>`;
  192. } else {
  193. this.rateRichText.node.active = false;
  194. }
  195. if (GameModule.userInfo.coinTap.n > 0) {
  196. this.clickCoinRichText.string = `<b>${TapTapTool.parseToString(GameModule.userInfo.coinTap)}</b>`;
  197. } else {
  198. this.clickCoinRichText.string = "<b>0</b>";
  199. }
  200. },
  201. /**
  202. * 每秒自动点击次数
  203. */
  204. autoClickGetGold() {
  205. GameEvent.fire(GameNotificationKey.AutoClickGold);
  206. },
  207. /**
  208. * 上报用户数据
  209. */
  210. doReport() {
  211. // return;
  212. let goldStr = this._gold.n + ";" + this._gold.e;
  213. DWTool.reportInfo(this.seq, goldStr, this.stars, this.clickCount, this.recordModify, this.recordUnlockModify, () => {
  214. this._reportFailDuration = 0;
  215. this.recordModify = [];
  216. this.recordUnlockModify = [];
  217. this.seq += 1;
  218. }, (err) => {
  219. if (err.code === -4) {
  220. this._reportFailDuration = 60;
  221. }
  222. console.log('上报失败 ' + err.msg);
  223. })
  224. },
  225. reportFirst() {
  226. //如果为true则以网络数据为主不用覆盖
  227. if (GameGlobal.localData) {
  228. return;
  229. }
  230. this.compareRoomsLocalData();
  231. let goldStr = this._gold.n + ";" + this._gold.e;
  232. DWTool.reportRoomsInfo(goldStr, this.buildingData);
  233. },
  234. /**
  235. * 初始化用户数据
  236. */
  237. initUserInfo() {
  238. let userInfo = this.userInfo = GameGlobal.userData;
  239. if (userInfo != undefined) {
  240. let goldObj = this.compareGoldLocalData();
  241. this.updateUserRes({
  242. gold: goldObj,
  243. stars: userInfo.ogStars,
  244. diamond: userInfo.diamond,
  245. ticket: userInfo.ticket,
  246. clickCount: userInfo.clickCount
  247. });
  248. this.perpetualMt = userInfo.mtObj;
  249. this.perpetualClickMt = userInfo.clickMtObj;
  250. this.buyStarCount = userInfo.buyStarCount;
  251. }
  252. },
  253. compareGoldLocalData() {
  254. let localGold = cc.sys.localStorage.getItem(`localGold_${GameGlobal.user.uid}`);
  255. if (this.userInfo != undefined && localGold!= undefined && localGold && !GameGlobal.localData) {
  256. let goldObj = JSON.parse(localGold);
  257. if (goldObj != undefined) {
  258. return goldObj;
  259. } else {
  260. return this.userInfo.goldObj;
  261. }
  262. } else {
  263. return this.userInfo.goldObj;
  264. }
  265. },
  266. compareRoomsLocalData() {
  267. let localData = cc.sys.localStorage.getItem(`localRooms_${GameGlobal.user.uid}`);
  268. this.buildingData = GameGlobal.BuildingManager.networkRooms;
  269. if (this.userInfo != undefined && localData!= undefined && localData && !GameGlobal.localData) {
  270. let userRooms = JSON.parse(localData);
  271. if (GameGlobal.BuildingManager.networkRooms && GameGlobal.BuildingManager.networkRooms.length > 0 && userRooms.length > 0) {
  272. userRooms.forEach(room => {
  273. this.buildingData.forEach(n => {
  274. if (room.roomId == n.roomId) {
  275. n.roomLevel = Math.max(room.level, n.roomLevel);
  276. }
  277. });
  278. });
  279. }
  280. }
  281. },
  282. /**
  283. * 更新用户资源数据
  284. * @param {object} data 更新资源对象
  285. * @param {number} data.grossIncome 增加的金币
  286. * @param {number} data.stars 星星
  287. * @param {number} data.diamond 钻石
  288. * @param {number} data.ticket 艺人券
  289. */
  290. updateUserRes(data) {
  291. // console.log("Update userData: ", JSON.stringify(data));
  292. if (data.gold) {
  293. this.gold = TapTapTool.add(data.gold, this.gold);
  294. }
  295. if (data.stars) {
  296. this.stars += parseInt(data.stars);
  297. }
  298. if (data.diamond) {
  299. this.diamond += parseInt(data.diamond);
  300. }
  301. if (data.ticket) {
  302. this.ticket += parseInt(data.ticket);
  303. }
  304. if (data.clickCount) {
  305. this.clickCount += parseInt(data.clickCount);
  306. }
  307. },
  308. updateRecordModify(buildingInfo) {
  309. for (let i = 0; i < this.recordModify.length; i++) {
  310. let temp = this.recordModify[i];
  311. if (buildingInfo.roomId == temp.roomId) {
  312. this.recordModify.splice(i, 1, buildingInfo);
  313. return;
  314. }
  315. }
  316. this.recordModify.push(buildingInfo);
  317. }
  318. });