UserInfo.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. const GameModule = require("./utils/GameModule");
  2. const DWTool = require("./utils/DWTool");
  3. const Api = require('./net/Api');
  4. const NotiKey = require('./utils/GameEnum').GameNotificationKey;
  5. var cityList = require('./data/city');
  6. // const BuildingModel = require('./utils/BuildingModel');
  7. // const AlertManager = require('./utils/AlertManager');
  8. // const ReportType = require("./utils/GameEnum").ReportType;
  9. cc.Class({
  10. extends: cc.Component,
  11. properties: {
  12. _reportFailDuration: 0,
  13. grossIncomeLabel: cc.Label,
  14. grossCoin: sp.Skeleton,
  15. // rateLabel: cc.Label,
  16. headSprite: cc.Sprite,
  17. starsLabel: cc.Label,
  18. starsProgress: cc.ProgressBar,
  19. diamondLabel: cc.Label,
  20. ticketLabel: cc.Label,
  21. recordModify: [],
  22. recordUnlockModify: [],
  23. _stars: 0,
  24. stars: {
  25. get: function () {
  26. return this._stars;
  27. },
  28. set: function (value) {
  29. this._stars = value;
  30. // 每5级跳动一次
  31. if (value % 5 == 0) {
  32. this.updateStarAnimation();
  33. }
  34. let cStar = Math.floor(this._stars / 5)
  35. this.starsLabel.string = cStar;
  36. // 把用户的星星数上报给微信
  37. DWTool.submitWechatStars(cStar);
  38. // 更新星星数的进度条
  39. this.starsProgress.progress = value % 5 == 0 ? 1 : value % 5 / 5
  40. if (cStar >= 10) {
  41. GameModule.homeGuide.getComponent('HomeGuide').handleGuideState6();
  42. // GameEvent.fire(NotiKey.ResetLevelHomePaddingBottom);
  43. }
  44. }
  45. },
  46. _grossIncome: 0,
  47. grossIncome: {
  48. get: function () {
  49. return this._grossIncome;
  50. },
  51. set: function (value) {
  52. this._grossIncome = value < 0 ? 0 : parseInt(value);
  53. this.grossIncomeLabel.string = DWTool.coinParse(this._grossIncome);
  54. }
  55. },
  56. _diamond: 0,
  57. diamond: {
  58. get: function () {
  59. return this._diamond;
  60. },
  61. set: function (value) {
  62. this._diamond = value < 0 ? 0 : parseInt(value);
  63. this.diamondLabel.string = this._diamond;
  64. }
  65. },
  66. _ticket: 0,
  67. ticket: {
  68. get: function () {
  69. return this._ticket;
  70. },
  71. set: function (value) {
  72. this._ticket = value < 0 ? 0 : parseInt(value);
  73. this.ticketLabel.string = this._ticket;
  74. }
  75. },
  76. levelHomeItemFullCount: 0
  77. },
  78. onLoad() {
  79. GameModule.userInfo = this;
  80. this._rate = 0;
  81. this._isPlayAnimation = false;
  82. this.seq = 1;
  83. // 初始化用户信息
  84. this.initUserInfo()
  85. // 监听用户头像点击事件
  86. this.headSprite.node.on(cc.Node.EventType.TOUCH_END, _.debounce(() => {
  87. GameEvent.fire(NotiKey.ShowUserInfomation, this.userInfo.uid);
  88. }, 1000, true), this);
  89. // 监听用户收取金币事件
  90. GameEvent.on(NotiKey.UserCollectCoin, this, (flag) => {
  91. let animKey = flag ? 'jinbi_huoqu2' : 'jinbi_huoqu'
  92. this.grossCoin.setAnimation(0, animKey, false)
  93. });
  94. // 监听满级事件
  95. GameEvent.on(NotiKey.LevelHomeItemBuildingFull, this, () => {
  96. this.levelHomeItemFullCount += 1;
  97. if (this.levelHomeItemFullCount === 5 && Global.devCityId < cityList.length) {
  98. GameEvent.fire(NotiKey.LevelHomeItemBuildingAllFull);
  99. GameEvent.fire(NotiKey.CurrentCompanyMax);
  100. // 满级立刻上报
  101. this.doReport();
  102. }
  103. });
  104. // 轮询上报
  105. this.reportFunc = () => {
  106. if (this._reportFailDuration > 0) {
  107. this._reportFailDuration -= 3;
  108. return;
  109. }
  110. this.doReport()
  111. }
  112. this.schedule(this.reportFunc, 3.0);
  113. },
  114. /**
  115. * 上报用户数据
  116. */
  117. doReport() {
  118. DWTool.reportInfo(this.seq, this.grossIncome, this.stars, this.recordModify, this.recordUnlockModify)
  119. .then(() => {
  120. this._reportFailDuration = 0;
  121. this.recordModify = [];
  122. this.recordUnlockModify = [];
  123. this.seq += 1;
  124. }).catch(err => {
  125. if (err.code === -4) {
  126. this._reportFailDuration = 60;
  127. }
  128. console.log(err.msg);
  129. })
  130. },
  131. /**
  132. * 初始化用户数据
  133. */
  134. initUserInfo() {
  135. let userInfo = this.userInfo = Global.userData;
  136. Api.createImageFromUrl(userInfo.head, (spriteFrame) => {
  137. this.headSprite.spriteFrame = spriteFrame;
  138. }, null);
  139. this.updateUserRes({
  140. grossIncome: userInfo.grossIncome,
  141. stars: userInfo.ogStars,
  142. diamond: userInfo.diamond,
  143. ticket: userInfo.ticket
  144. })
  145. },
  146. /**
  147. * 更新用户资源数据
  148. * @param {object} data 更新资源对象
  149. * @param {number} data.grossIncome 增加的金币
  150. * @param {number} data.stars 星星
  151. * @param {number} data.diamond 钻石
  152. * @param {number} data.ticket 艺人券
  153. */
  154. updateUserRes(data) {
  155. console.log("Update userData: ", JSON.stringify(data));
  156. if (data.grossIncome) {
  157. this.grossIncome += parseInt(data.grossIncome);
  158. }
  159. if (data.stars) {
  160. this.stars += parseInt(data.stars);
  161. }
  162. if (data.diamond) {
  163. this.diamond += parseInt(data.diamond);
  164. }
  165. if (data.ticket) {
  166. this.ticket += parseInt(data.ticket);
  167. }
  168. },
  169. /**
  170. * 增加星星的动画
  171. */
  172. updateStarAnimation() {
  173. if (this._isPlayAnimation) { return; }
  174. this._isPlayAnimation = true;
  175. let jumpHeight = 30;
  176. let duration = 0.2;
  177. let animationArray = [];
  178. while (jumpHeight > 0.1) {
  179. jumpHeight = jumpHeight - (jumpHeight / 3);
  180. duration = duration - (duration / 3);
  181. let upAction = cc.moveBy(duration, 0, jumpHeight).easing(cc.easeCubicActionOut());
  182. let downAction = cc.moveBy(duration, 0, -jumpHeight).easing(cc.easeCubicActionIn());
  183. animationArray.push(upAction);
  184. animationArray.push(downAction);
  185. }
  186. let callback = cc.callFunc(() => {
  187. this._isPlayAnimation = false;
  188. });
  189. animationArray.push(callback);
  190. this.starsLabel.node.runAction(cc.sequence(animationArray));
  191. },
  192. updateRecordModify(buildingInfo) {
  193. for (let i = 0; i < this.recordModify.length; i++) {
  194. let temp = this.recordModify[i];
  195. if (buildingInfo.buildingId == temp.buildingId) {
  196. this.recordModify.splice(i, 1, buildingInfo)
  197. return;
  198. }
  199. }
  200. this.recordModify.push(buildingInfo);
  201. }
  202. });