RankItem.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { _decorator, Component, Node, Sprite, Label, ImageAsset, SpriteFrame, Texture2D, find } from 'cc';
  2. import { JSB } from 'cc/env';
  3. import { Http, HttpResponseCode } from '../../core/net/Http';
  4. import { ResourcesUtils } from '../../core/resourceManager/ResourcesUtils';
  5. import { OpenWindow } from '../../core/ui/window/OpenWindow';
  6. import { WindowManager } from '../../core/ui/window/WindowManager';
  7. import { ConfigData } from '../../Data/ConfigData';
  8. import { g } from '../../Data/g';
  9. import { UIEffect } from '../UIEffect';
  10. import { RankItemData } from './RankData';
  11. const { ccclass, property } = _decorator;
  12. @ccclass('RankItem')
  13. export class RankItem extends Component {
  14. @property({ type: Http, tooltip: "Http组件" }) http: Http = null;
  15. @property({ type: Node, tooltip: "背景框" }) bg: Node = null;
  16. @property({ type: Sprite, tooltip: "头像" }) imgHead: Sprite = null;
  17. @property({ type: Label, tooltip: "昵称文本" }) txtNickName: Label = null;
  18. @property({ type: Label, tooltip: "排名文本" }) txtRank: Label = null;
  19. @property({ type: Label, tooltip: "金额文本" }) txtMoney: Label = null;
  20. @property({ type: Label, tooltip: "积分文本" }) txtScore: Label = null;
  21. @property({ type: Node, tooltip: "红包" }) rbNode: Node = null;
  22. @property({ type: Node, tooltip: "领取按钮" }) btnGet: Node = null;
  23. private data: RankItemData;
  24. private money: number = 0;
  25. private isPlayerSelf: boolean = false;
  26. private rank: number = 1;
  27. public async onDataChange(data: RankItemData, index: number, isShowBg: boolean, isPlayerSelf: boolean = false) {
  28. this.data = data;
  29. this.isPlayerSelf = isPlayerSelf;
  30. this.rank = index + 1;
  31. let rankCfg = ConfigData.configMap.get("rank");
  32. if (index + 1 <= 100) {
  33. if (index + 1 <= 1) {
  34. this.money = rankCfg.reward1;
  35. } else if (index + 1 <= 2) {
  36. this.money = rankCfg.reward2;
  37. } else if (index + 1 <= 3) {
  38. this.money = rankCfg.reward3;
  39. } else if (index + 1 <= 10) {
  40. this.money = rankCfg.reward10;
  41. } else if (index + 1 <= 30) {
  42. this.money = rankCfg.reward30;
  43. } else if (index + 1 <= 100) {
  44. this.money = rankCfg.reward100;
  45. } else {
  46. this.money = 0;
  47. }
  48. }
  49. this.bg.active = isShowBg;
  50. this.txtNickName.string = `${data.nick.length > 6 ? data.nick.slice(0, 5) + "..." : data.nick}`;
  51. this.txtRank.string = `${(index + 1) > 100 ? "100+" : index + 1}`;
  52. this.txtScore.string = `积分:${data.score}`;
  53. this.txtMoney.string = `${this.money}元`;
  54. this.rbNode.active = isPlayerSelf ? this.money > 0 && g.gameData.rankData && g.gameData.rankData.received == false : this.money > 0;
  55. this.btnGet.active = isPlayerSelf && g.gameData.rankData && g.gameData.rankData.received && this.rank <= 100;
  56. if (isPlayerSelf && g.gameData.rankData && g.gameData.rankData.received) { this.btnGet.getComponent(Sprite).grayscale = true; }
  57. if (JSB && data.avator != "") {
  58. let img = await ResourcesUtils.loadRemote<ImageAsset>(data.avator, { ext: ".png" }, this);
  59. const spriteFrame = new SpriteFrame();
  60. const texture = new Texture2D();
  61. texture.image = img;
  62. spriteFrame.texture = texture;
  63. this.imgHead.spriteFrame = spriteFrame;
  64. }
  65. }
  66. update() {
  67. if (this.isPlayerSelf) {
  68. if (!this.btnGet.active && g.gameData.isCanGetRankReward && this.rank <= 100) {
  69. this.rbNode.active = false;
  70. this.btnGet.active = true;
  71. this.btnGet.getComponent(Sprite).grayscale = false;
  72. }
  73. }
  74. }
  75. private async onClickRb() {
  76. if (this.isPlayerSelf) {
  77. if (g.gameData.isCanGetRankReward) {
  78. let result = await this.http.send("/api/rank/ReceiveRank");
  79. console.log("RecieveRank: ", result);
  80. if (result && result.code == HttpResponseCode.Success) {
  81. g.userData.contribution += result.data;
  82. g.gameData.rankData.received = true;
  83. g.gameData.isCanGetRankReward = false;
  84. this.btnGet.getComponent(Sprite).grayscale = true;
  85. //find("Canvas/UI").getComponent(UIEffect).showBezierRedBagEffect(2, null);
  86. WindowManager.showTips(`恭喜获得${result.data}贡献`);
  87. if (this.node.getComponent(OpenWindow)) {
  88. this.node.getComponent(OpenWindow).open();
  89. }
  90. }
  91. else if (result && result.code == 109) {
  92. WindowManager.showTips("你已领取该奖励");
  93. } else {
  94. WindowManager.showTips("请求失败");
  95. }
  96. } else {
  97. if (g.gameData.rankData.received) {
  98. WindowManager.showTips("你已领取该奖励");
  99. } else {
  100. WindowManager.showTips("还不能领取哦");
  101. }
  102. }
  103. } else {
  104. WindowManager.showTips("这不是你的红包哦");
  105. }
  106. }
  107. }