UserInfo.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. GameModule.audioMng.playGetcoin();
  55. }
  56. },
  57. _diamond: 0,
  58. diamond: {
  59. get: function () {
  60. return this._diamond;
  61. },
  62. set: function (value) {
  63. this._diamond = value < 0 ? 0 : parseInt(value);
  64. this.diamondLabel.string = this._diamond;
  65. }
  66. },
  67. _ticket: 0,
  68. ticket: {
  69. get: function () {
  70. return this._ticket;
  71. },
  72. set: function (value) {
  73. this._ticket = value < 0 ? 0 : parseInt(value);
  74. this.ticketLabel.string = this._ticket;
  75. }
  76. },
  77. levelHomeItemFullCount: 0
  78. },
  79. onLoad() {
  80. GameModule.userInfo = this;
  81. this._rate = 0;
  82. this._isPlayAnimation = false;
  83. this.seq = 1;
  84. // 初始化用户信息
  85. this.initUserInfo()
  86. // 监听用户头像点击事件
  87. this.headSprite.node.on(cc.Node.EventType.TOUCH_END, _.debounce(() => {
  88. GameEvent.fire(NotiKey.ShowUserInfomation, this.userInfo.uid);
  89. }, 1000, true), this);
  90. // 监听用户收取金币事件
  91. GameEvent.on(NotiKey.UserCollectCoin, this, (flag) => {
  92. let animKey = flag ? 'jinbi_huoqu2' : 'jinbi_huoqu'
  93. this.grossCoin.setAnimation(0, animKey, false)
  94. });
  95. // 监听满级事件
  96. GameEvent.on(NotiKey.LevelHomeItemBuildingFull, this, () => {
  97. this.levelHomeItemFullCount += 1;
  98. if (this.levelHomeItemFullCount === 5 && Global.devCityId < cityList.length) {
  99. GameEvent.fire(NotiKey.LevelHomeItemBuildingAllFull);
  100. GameEvent.fire(NotiKey.CurrentCompanyMax);
  101. // 满级立刻上报
  102. this.doReport();
  103. }
  104. });
  105. // 轮询上报
  106. this.reportFunc = () => {
  107. if (this._reportFailDuration > 0) {
  108. this._reportFailDuration -= 3;
  109. return;
  110. }
  111. this.doReport()
  112. }
  113. this.schedule(this.reportFunc, 3.0);
  114. },
  115. /**
  116. * 上报用户数据
  117. */
  118. doReport() {
  119. DWTool.reportInfo(this.seq, this.grossIncome, this.stars, this.recordModify, this.recordUnlockModify)
  120. .then(() => {
  121. this._reportFailDuration = 0;
  122. this.recordModify = [];
  123. this.recordUnlockModify = [];
  124. this.seq += 1;
  125. }).catch(err => {
  126. if (err.code === -4) {
  127. this._reportFailDuration = 60;
  128. }
  129. console.log(err.msg);
  130. })
  131. },
  132. /**
  133. * 初始化用户数据
  134. */
  135. initUserInfo() {
  136. let userInfo = this.userInfo = Global.userData;
  137. Api.createImageFromUrl(userInfo.head, (spriteFrame) => {
  138. this.headSprite.spriteFrame = spriteFrame;
  139. }, null);
  140. this.updateUserRes({
  141. grossIncome: userInfo.grossIncome,
  142. stars: userInfo.ogStars,
  143. diamond: userInfo.diamond,
  144. ticket: userInfo.ticket
  145. })
  146. },
  147. /**
  148. * 更新用户资源数据
  149. * @param {object} data 更新资源对象
  150. * @param {number} data.grossIncome 增加的金币
  151. * @param {number} data.stars 星星
  152. * @param {number} data.diamond 钻石
  153. * @param {number} data.ticket 艺人券
  154. */
  155. updateUserRes(data) {
  156. console.log("Update userData: ", JSON.stringify(data));
  157. if (data.grossIncome) {
  158. this.grossIncome += parseInt(data.grossIncome);
  159. }
  160. if (data.stars) {
  161. this.stars += parseInt(data.stars);
  162. }
  163. if (data.diamond) {
  164. this.diamond += parseInt(data.diamond);
  165. }
  166. if (data.ticket) {
  167. this.ticket += parseInt(data.ticket);
  168. }
  169. },
  170. /**
  171. * 增加星星的动画
  172. */
  173. updateStarAnimation() {
  174. if (this._isPlayAnimation) { return; }
  175. this._isPlayAnimation = true;
  176. let jumpHeight = 30;
  177. let duration = 0.2;
  178. let animationArray = [];
  179. while (jumpHeight > 0.1) {
  180. jumpHeight = jumpHeight - (jumpHeight / 3);
  181. duration = duration - (duration / 3);
  182. let upAction = cc.moveBy(duration, 0, jumpHeight).easing(cc.easeCubicActionOut());
  183. let downAction = cc.moveBy(duration, 0, -jumpHeight).easing(cc.easeCubicActionIn());
  184. animationArray.push(upAction);
  185. animationArray.push(downAction);
  186. }
  187. let callback = cc.callFunc(() => {
  188. this._isPlayAnimation = false;
  189. });
  190. animationArray.push(callback);
  191. this.starsLabel.node.runAction(cc.sequence(animationArray));
  192. },
  193. updateRecordModify(buildingInfo) {
  194. for (let i = 0; i < this.recordModify.length; i++) {
  195. let temp = this.recordModify[i];
  196. if (buildingInfo.buildingId == temp.buildingId) {
  197. this.recordModify.splice(i, 1, buildingInfo)
  198. return;
  199. }
  200. }
  201. this.recordModify.push(buildingInfo);
  202. },
  203. /**
  204. * 星星数到达10个,触发艺人入驻提示
  205. */
  206. stars10() {
  207. GameEvent.fire(NotiKey.LevelHomeItemUnlock);
  208. }
  209. });