UserInfo.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. // 把用户的总部等级上报给微信
  116. DWTool.submitWechatScore(this._buildingLevel);
  117. }
  118. },
  119. // //获得奖励的永久产出倍数
  120. // perpetualMt: {
  121. // default: 1,
  122. // visible: false,
  123. // type: cc.Float
  124. // },
  125. // //获得奖励的永久点击倍数
  126. // perpetualClickMt: {
  127. // default: 1,
  128. // visible: false,
  129. // type: cc.Float
  130. // },
  131. //入驻在建筑的明星加成倍数
  132. // starMt: {
  133. // default: 1,
  134. // visible: false,
  135. // type: cc.Float
  136. // },
  137. levelHomeItemFullCount: 0
  138. },
  139. onLoad() {
  140. GameModule.userInfo = this;
  141. this._rate = 0;
  142. this._isPlayAnimation = false;
  143. this.seq = 1;
  144. this._rateGold = {'n': 0, 'e': 0};
  145. this._gold = {'n': 0, 'e': 0};
  146. this.coinTap = {'n': 0, 'e': 0};
  147. this.buildingLevel = Global.userData.buildingLevel;
  148. // let objc = TapTapTool.goldStrToClass('3.265;29');
  149. // let str = TapTapTool.parseToString(objc);
  150. // let sub = TapTapTool.compare({'n': 323, 'e': 25}, objc);
  151. // let n = {'n': 423, 'e': 0};
  152. // let m = {'n': 1.1, 'e': 0};
  153. // console.log(TapTapTool.multiple(n, m));
  154. // console.log(sub);
  155. // console.log(str);
  156. // 初始化用户信息
  157. this.initUserInfo();
  158. // 轮询上报游戏数据
  159. this.reportFunc = () => {
  160. if (this._reportFailDuration > 0) {
  161. this._reportFailDuration -= 3;
  162. return;
  163. }
  164. this.doReport();
  165. };
  166. this.schedule(this.reportFunc, 3.0);
  167. //建筑每秒自动赚的钱
  168. this.addMoneyFunc = () => {
  169. this.buildingAddMoney();
  170. };
  171. this.schedule(this.addMoneyFunc, 1.0);
  172. },
  173. /**
  174. * 每秒增加金币
  175. */
  176. buildingAddMoney() {
  177. if (this.rateGold.n > 0) {
  178. // this.gold = TapTapTool.add(this.rateGold, this.gold);
  179. let add = TapTapTool.multiple(this.rateGold, GameModule.skill.multiple);
  180. this.gold = TapTapTool.add(this.gold, TapTapTool.multiple(add, this.perpetualMt));
  181. // this.gold += (this.rateGold * GameModule.skill.multiple * this.perpetualMt);
  182. this.secondRateTextAnimation();
  183. }
  184. },
  185. secondRateTextAnimation() {
  186. let fadeIn = cc.fadeIn(0.2);
  187. let delay = cc.delayTime(0.3);
  188. let fadeOut = cc.fadeOut(0.3);
  189. let action = cc.sequence(fadeIn, delay, fadeOut);
  190. this.rateRichText.node.runAction(action);
  191. },
  192. refreshSecondText() {
  193. if (this._rateGold.n > 0) {
  194. this.rateRichText.node.active = true;
  195. let rate = TapTapTool.multiple(GameModule.skill.multiple, this.perpetualMt);
  196. let newValue = TapTapTool.multiple(rate, this._rateGold);
  197. this.rateRichText.string = `<outline color=#ffffff width=3><color=#009100>+${TapTapTool.parseToString(newValue)}</c></outline>`;
  198. this.rateCoinRichText.string = `<b>${TapTapTool.parseToString(newValue)}</b>`;
  199. } else {
  200. this.rateRichText.node.active = false;
  201. }
  202. if (GameModule.userInfo.coinTap.n > 0) {
  203. this.clickCoinRichText.string = `<b>${TapTapTool.parseToString(GameModule.userInfo.coinTap)}</b>`;
  204. } else {
  205. this.clickCoinRichText.string = "<b>0</b>";
  206. }
  207. },
  208. /**
  209. * 每秒自动点击次数
  210. */
  211. autoClickGetGold() {
  212. GameEvent.fire(GameNotificationKey.AutoClickGold);
  213. },
  214. /**
  215. * 上报用户数据
  216. */
  217. doReport() {
  218. // return;
  219. let goldStr = this._gold.n + ";" + this._gold.e;
  220. DWTool.reportInfo(this.seq, goldStr, this.stars, this.clickCount, this.recordModify, this.recordUnlockModify)
  221. .then(() => {
  222. this._reportFailDuration = 0;
  223. this.recordModify = [];
  224. this.recordUnlockModify = [];
  225. this.seq += 1;
  226. }).catch(err => {
  227. if (err.code === -4) {
  228. this._reportFailDuration = 60;
  229. }
  230. console.log('上报失败 ' + err.msg);
  231. })
  232. },
  233. /**
  234. * 初始化用户数据
  235. */
  236. initUserInfo() {
  237. let userInfo = this.userInfo = Global.userData;
  238. if (userInfo != undefined) {
  239. this.updateUserRes({
  240. gold: userInfo.goldObj,
  241. stars: userInfo.ogStars,
  242. diamond: userInfo.diamond,
  243. ticket: userInfo.ticket,
  244. clickCount: userInfo.clickCount
  245. });
  246. this.perpetualMt = userInfo.mtObj;
  247. this.perpetualClickMt = userInfo.clickMtObj;
  248. this.buyStarCount = userInfo.buyStarCount;
  249. }
  250. },
  251. /**
  252. * 更新用户资源数据
  253. * @param {object} data 更新资源对象
  254. * @param {number} data.grossIncome 增加的金币
  255. * @param {number} data.stars 星星
  256. * @param {number} data.diamond 钻石
  257. * @param {number} data.ticket 艺人券
  258. */
  259. updateUserRes(data) {
  260. console.log("Update userData: ", JSON.stringify(data));
  261. if (data.gold) {
  262. this.gold = TapTapTool.add(data.gold, this.gold);
  263. }
  264. if (data.stars) {
  265. this.stars += parseInt(data.stars);
  266. }
  267. if (data.diamond) {
  268. this.diamond += parseInt(data.diamond);
  269. }
  270. if (data.ticket) {
  271. this.ticket += parseInt(data.ticket);
  272. }
  273. if (data.clickCount) {
  274. this.clickCount += parseInt(data.clickCount);
  275. }
  276. },
  277. updateRecordModify(buildingInfo) {
  278. for (let i = 0; i < this.recordModify.length; i++) {
  279. let temp = this.recordModify[i];
  280. if (buildingInfo.roomId == temp.roomId) {
  281. this.recordModify.splice(i, 1, buildingInfo);
  282. return;
  283. }
  284. }
  285. this.recordModify.push(buildingInfo);
  286. }
  287. });