| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- import { _decorator, Component, Node, Sprite, Label, ImageAsset, SpriteFrame, Texture2D, find } from 'cc';
- import { JSB } from 'cc/env';
- import { Http, HttpResponseCode } from '../../core/net/Http';
- import { ResourcesUtils } from '../../core/resourceManager/ResourcesUtils';
- import { OpenWindow } from '../../core/ui/window/OpenWindow';
- import { WindowManager } from '../../core/ui/window/WindowManager';
- import { ConfigData } from '../../Data/ConfigData';
- import { g } from '../../Data/g';
- import { UIEffect } from '../UIEffect';
- import { RankItemData } from './RankData';
- const { ccclass, property } = _decorator;
- @ccclass('RankItem')
- export class RankItem extends Component {
- @property({ type: Http, tooltip: "Http组件" }) http: Http = null;
- @property({ type: Node, tooltip: "背景框" }) bg: Node = null;
- @property({ type: Sprite, tooltip: "头像" }) imgHead: Sprite = null;
- @property({ type: Label, tooltip: "昵称文本" }) txtNickName: Label = null;
- @property({ type: Label, tooltip: "排名文本" }) txtRank: Label = null;
- @property({ type: Label, tooltip: "金额文本" }) txtMoney: Label = null;
- @property({ type: Label, tooltip: "积分文本" }) txtScore: Label = null;
- @property({ type: Node, tooltip: "红包" }) rbNode: Node = null;
- @property({ type: Node, tooltip: "领取按钮" }) btnGet: Node = null;
- private data: RankItemData;
- private money: number = 0;
- private isPlayerSelf: boolean = false;
- private rank: number = 1;
- public async onDataChange(data: RankItemData, index: number, isShowBg: boolean, isPlayerSelf: boolean = false) {
- this.data = data;
- this.isPlayerSelf = isPlayerSelf;
- this.rank = index + 1;
- let rankCfg = ConfigData.configMap.get("rank");
- if (index + 1 <= 100) {
- if (index + 1 <= 1) {
- this.money = rankCfg.reward1;
- } else if (index + 1 <= 2) {
- this.money = rankCfg.reward2;
- } else if (index + 1 <= 3) {
- this.money = rankCfg.reward3;
- } else if (index + 1 <= 10) {
- this.money = rankCfg.reward10;
- } else if (index + 1 <= 30) {
- this.money = rankCfg.reward30;
- } else if (index + 1 <= 100) {
- this.money = rankCfg.reward100;
- } else {
- this.money = 0;
- }
- }
- this.bg.active = isShowBg;
- this.txtNickName.string = `${data.nick.length > 6 ? data.nick.slice(0, 5) + "..." : data.nick}`;
- this.txtRank.string = `${(index + 1) > 100 ? "100+" : index + 1}`;
- this.txtScore.string = `积分:${data.score}`;
- this.txtMoney.string = `${this.money}元`;
- this.rbNode.active = isPlayerSelf ? this.money > 0 && g.gameData.rankData && g.gameData.rankData.received == false : this.money > 0;
- this.btnGet.active = isPlayerSelf && g.gameData.rankData && g.gameData.rankData.received && this.rank <= 100;
- if (isPlayerSelf && g.gameData.rankData && g.gameData.rankData.received) { this.btnGet.getComponent(Sprite).grayscale = true; }
- if (JSB && data.avator != "") {
- let img = await ResourcesUtils.loadRemote<ImageAsset>(data.avator, { ext: ".png" }, this);
- const spriteFrame = new SpriteFrame();
- const texture = new Texture2D();
- texture.image = img;
- spriteFrame.texture = texture;
- this.imgHead.spriteFrame = spriteFrame;
- }
- }
- update() {
- if (this.isPlayerSelf) {
- if (!this.btnGet.active && g.gameData.isCanGetRankReward && this.rank <= 100) {
- this.rbNode.active = false;
- this.btnGet.active = true;
- this.btnGet.getComponent(Sprite).grayscale = false;
- }
- }
- }
- private async onClickRb() {
- if (this.isPlayerSelf) {
- if (g.gameData.isCanGetRankReward) {
- let result = await this.http.send("/api/rank/ReceiveRank");
- console.log("RecieveRank: ", result);
- if (result && result.code == HttpResponseCode.Success) {
- g.userData.contribution += result.data;
- g.gameData.rankData.received = true;
- g.gameData.isCanGetRankReward = false;
- this.btnGet.getComponent(Sprite).grayscale = true;
- //find("Canvas/UI").getComponent(UIEffect).showBezierRedBagEffect(2, null);
- WindowManager.showTips(`恭喜获得${result.data}贡献`);
- if (this.node.getComponent(OpenWindow)) {
- this.node.getComponent(OpenWindow).open();
- }
- }
- else if (result && result.code == 109) {
- WindowManager.showTips("你已领取该奖励");
- } else {
- WindowManager.showTips("请求失败");
- }
- } else {
- if (g.gameData.rankData.received) {
- WindowManager.showTips("你已领取该奖励");
- } else {
- WindowManager.showTips("还不能领取哦");
- }
- }
- } else {
- WindowManager.showTips("这不是你的红包哦");
- }
- }
- }
|