UserInfo.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. _stars: 0,
  27. stars: {
  28. get: function () {
  29. return this._stars;
  30. },
  31. set: function (value) {
  32. console.log('星星增加数 ' + value);
  33. this._stars = value;
  34. }
  35. },
  36. _gold: 0,
  37. gold: {
  38. get: function () {
  39. return this._gold;
  40. },
  41. set: function (value) {
  42. this._gold = value;
  43. this.grossIncomeRichText.string = `<outline color=#ffffff width=3><color=#009100>${TapTapTool.parseToString(this._gold)}</c></outline>`;
  44. }
  45. },
  46. //建筑每秒自动生成的金币数
  47. _rateGold: 0,
  48. rateGold: {
  49. get: function () {
  50. return this._rateGold;
  51. },
  52. set: function (value) {
  53. this._rateGold = value;
  54. if (this._rateGold.n > 0) {
  55. this.rateRichText.node.active = true;
  56. let rate = TapTapTool.multiple(GameModule.skill.multiple, this.perpetualMt);
  57. let newValue = TapTapTool.multiple(rate, this._rateGold);
  58. this.rateRichText.string = `<outline color=#ffffff width=3><color=#009100>+${TapTapTool.parseToString(newValue)}</c></outline>`;
  59. this.rateCoinRichText.string = `<b>${TapTapTool.parseToString(newValue)}</b>`;
  60. } else {
  61. this.rateRichText.node.active = false;
  62. this.rateCoinRichText.string = "<b>0</b>";
  63. }
  64. }
  65. },
  66. //自动每次点击需要的秒数
  67. _secondClick: 0,
  68. secondClick: {
  69. get: function () {
  70. return this._secondClick;
  71. },
  72. set: function (value) {
  73. this._secondClick = value < 0 ? 0 : parseFloat(value);
  74. this.unschedule(this.autoClickGetGold, this);
  75. if (this._secondClick > 0) {
  76. this.schedule(this.autoClickGetGold, this._secondClick);
  77. }
  78. }
  79. },
  80. _diamond: 0,
  81. diamond: {
  82. get: function () {
  83. return this._diamond;
  84. },
  85. set: function (value) {
  86. this._diamond = value;
  87. this.diamondLabel.string = this._diamond;
  88. }
  89. },
  90. _clickCount: 0,
  91. clickCount: {
  92. get: function () {
  93. return this._clickCount;
  94. },
  95. set: function (value) {
  96. this._clickCount = value;
  97. }
  98. },
  99. _buyStarCount: 0,
  100. buyStarCount: {
  101. get: function () {
  102. return this._buyStarCount;
  103. },
  104. set: function (value) {
  105. this._buyStarCount = value;
  106. }
  107. },
  108. _buildingLevel: 1,
  109. buildingLevel: {
  110. get: function () {
  111. return this._buildingLevel;
  112. },
  113. set: function (value) {
  114. this._buildingLevel = value;
  115. if (CC_WECHATGAME) {
  116. // 把用户的总部等级上报给微信
  117. DWTool.submitWechatScore(this._buildingLevel);
  118. } else if (CC_QQPLAY) {
  119. // 把用户的总部等级上报给QQ玩一玩
  120. DWTool.submitQQScore(this._buildingLevel);
  121. }
  122. }
  123. },
  124. levelHomeItemFullCount: 0
  125. },
  126. onLoad() {
  127. GameModule.userInfo = this;
  128. this._rate = 0;
  129. this._isPlayAnimation = false;
  130. this.seq = 1;
  131. this._rateGold = {'n': 0, 'e': 0};
  132. this._gold = {'n': 0, 'e': 0};
  133. this.coinTap = {'n': 0, 'e': 0};
  134. this.buildingLevel = GameGlobal.userData.buildingLevel;
  135. // let objc = TapTapTool.goldStrToClass('3.265;29');
  136. // let str = TapTapTool.parseToString(objc);
  137. // let sub = TapTapTool.compare({'n': 323, 'e': 25}, objc);
  138. // let n = {'n': 423, 'e': 0};
  139. // let m = {'n': 1.1, 'e': 0};
  140. // console.log(TapTapTool.multiple(n, m));
  141. // console.log(sub);
  142. // console.log(str);
  143. // 初始化用户信息
  144. this.initUserInfo();
  145. // 轮询上报游戏数据
  146. this.reportFunc = () => {
  147. if (this._reportFailDuration > 0) {
  148. this._reportFailDuration -= 3;
  149. return;
  150. }
  151. this.doReport();
  152. };
  153. this.schedule(this.reportFunc, 3.0);
  154. //建筑每秒自动赚的钱
  155. this.addMoneyFunc = () => {
  156. this.buildingAddMoney();
  157. };
  158. this.schedule(this.addMoneyFunc, 1.0);
  159. },
  160. /**
  161. * 每秒增加金币
  162. */
  163. buildingAddMoney() {
  164. if (this.rateGold.n > 0) {
  165. // this.gold = TapTapTool.add(this.rateGold, this.gold);
  166. let add = TapTapTool.multiple(this.rateGold, GameModule.skill.multiple);
  167. this.gold = TapTapTool.add(this.gold, TapTapTool.multiple(add, this.perpetualMt));
  168. // this.gold += (this.rateGold * GameModule.skill.multiple * this.perpetualMt);
  169. this.secondRateTextAnimation();
  170. }
  171. },
  172. secondRateTextAnimation() {
  173. let fadeIn = cc.fadeIn(0.2);
  174. let delay = cc.delayTime(0.3);
  175. let fadeOut = cc.fadeOut(0.3);
  176. let action = cc.sequence(fadeIn, delay, fadeOut);
  177. this.rateRichText.node.runAction(action);
  178. },
  179. refreshSecondText() {
  180. if (this._rateGold.n > 0) {
  181. this.rateRichText.node.active = true;
  182. let rate = TapTapTool.multiple(GameModule.skill.multiple, this.perpetualMt);
  183. let newValue = TapTapTool.multiple(rate, this._rateGold);
  184. this.rateRichText.string = `<outline color=#ffffff width=3><color=#009100>+${TapTapTool.parseToString(newValue)}</c></outline>`;
  185. this.rateCoinRichText.string = `<b>${TapTapTool.parseToString(newValue)}</b>`;
  186. } else {
  187. this.rateRichText.node.active = false;
  188. }
  189. if (GameModule.userInfo.coinTap.n > 0) {
  190. this.clickCoinRichText.string = `<b>${TapTapTool.parseToString(GameModule.userInfo.coinTap)}</b>`;
  191. } else {
  192. this.clickCoinRichText.string = "<b>0</b>";
  193. }
  194. },
  195. /**
  196. * 每秒自动点击次数
  197. */
  198. autoClickGetGold() {
  199. GameEvent.fire(GameNotificationKey.AutoClickGold);
  200. },
  201. /**
  202. * 上报用户数据
  203. */
  204. doReport() {
  205. // return;
  206. let goldStr = this._gold.n + ";" + this._gold.e;
  207. DWTool.reportInfo(this.seq, goldStr, this.stars, this.clickCount, this.recordModify, this.recordUnlockModify, () => {
  208. this._reportFailDuration = 0;
  209. this.recordModify = [];
  210. this.recordUnlockModify = [];
  211. this.seq += 1;
  212. }, (err) => {
  213. if (err.code === -4) {
  214. this._reportFailDuration = 60;
  215. }
  216. console.log('上报失败 ' + err.msg);
  217. })
  218. },
  219. /**
  220. * 初始化用户数据
  221. */
  222. initUserInfo() {
  223. let userInfo = this.userInfo = GameGlobal.userData;
  224. if (userInfo != undefined) {
  225. this.updateUserRes({
  226. gold: userInfo.goldObj,
  227. stars: userInfo.ogStars,
  228. diamond: userInfo.diamond,
  229. ticket: userInfo.ticket,
  230. clickCount: userInfo.clickCount
  231. });
  232. this.perpetualMt = userInfo.mtObj;
  233. this.perpetualClickMt = userInfo.clickMtObj;
  234. this.buyStarCount = userInfo.buyStarCount;
  235. }
  236. },
  237. /**
  238. * 更新用户资源数据
  239. * @param {object} data 更新资源对象
  240. * @param {number} data.grossIncome 增加的金币
  241. * @param {number} data.stars 星星
  242. * @param {number} data.diamond 钻石
  243. * @param {number} data.ticket 艺人券
  244. */
  245. updateUserRes(data) {
  246. console.log("Update userData: ", JSON.stringify(data));
  247. if (data.gold) {
  248. this.gold = TapTapTool.add(data.gold, this.gold);
  249. }
  250. if (data.stars) {
  251. this.stars += parseInt(data.stars);
  252. }
  253. if (data.diamond) {
  254. this.diamond += parseInt(data.diamond);
  255. }
  256. if (data.ticket) {
  257. this.ticket += parseInt(data.ticket);
  258. }
  259. if (data.clickCount) {
  260. this.clickCount += parseInt(data.clickCount);
  261. }
  262. },
  263. updateRecordModify(buildingInfo) {
  264. for (let i = 0; i < this.recordModify.length; i++) {
  265. let temp = this.recordModify[i];
  266. if (buildingInfo.roomId == temp.roomId) {
  267. this.recordModify.splice(i, 1, buildingInfo);
  268. return;
  269. }
  270. }
  271. this.recordModify.push(buildingInfo);
  272. }
  273. });