import { _decorator, Component, Node, Sprite, Label, Animation } from 'cc'; import { DataSystem } from '../../core/data/DataSystem'; import { Http, HttpResponseCode } from '../../core/net/Http'; import List from '../../core/ui/virtualList/List'; import { WindowOpenMode } from '../../core/ui/window/WindowOpenMode'; import { WindowSystem } from '../../core/ui/window/WindowSystem'; import { ConfigData } from '../../data/ConfigData'; import { UserData } from '../../data/UserData'; import { RedPointData } from '../../main/RedPointData'; import { ReportThinking } from '../../ReportThinking'; import { FightData } from '../FightData'; import { FriendFightItem } from '../item/FriendFightItem'; const { ccclass, property } = _decorator; @ccclass('FriendFightUI') export class FriendFightUI extends Component { @property({ type: Http, displayName: "Http组件", tooltip: "Http组件" }) http: Http = null; @property({ type: List, displayName: "List列表", tooltip: "List列表" }) list: List = null; @property({ type: Sprite, displayName: "图进度", tooltip: "图进度" }) imgProgress: Sprite = null; @property({ type: Label, displayName: "文本进度", tooltip: "文本进度" }) txtProgress: Label = null; @property({ type: Animation, displayName: "宝箱动画", tooltip: "宝箱动画" }) aniBox: Animation = null; private isCanClick = true; private listData: { id: number, avator: string, nickName: string, attack: number, isChallengeWin: boolean }[]; async start() { await this.getFriend(); this.updateInfo(); } update() { DataSystem.watch(FightData, "pvpData") && this.updateInfo(); this.watchChallenge(); } private watchChallenge() { if (DataSystem.watch(FightData, "challengeSuccessFriendId")) { if (DataSystem.getData(FightData).challengeSuccessFriendId > 0) { let fightData = DataSystem.getData(FightData); if (fightData.pvpData.challengeWinUserIDList.indexOf(fightData.challengeSuccessFriendId) == -1) { fightData.pvpData.challengeWinUserIDList.push(fightData.challengeSuccessFriendId); fightData.challengeSuccessFriendId = -1; fightData.pvpData = fightData.pvpData; this.updateFriendData(fightData); if (fightData.pvpData.challengeWinUserIDList.length - DataSystem.getData(ConfigData).get("battle").challengeWinTimes >= 0 && !fightData.pvpData.receivedChallengeReward) { DataSystem.getData(RedPointData).canReceiveFightBox = true; this.aniBox.play(); } } } } } /**获取好友数据*/ private async getFriend() { let fightData = DataSystem.getData(FightData); if (fightData.friendDataList.length == 0) { let friendData = await this.http.send("/api/pvp/GetFriend"); if (friendData && friendData.code == HttpResponseCode.Success) { fightData.friendDataList = friendData.data.friendDatas; } } //if (!fightData.pvpData) { let pvpData = await this.http.send("/api/pvp/GetPVPData"); if (pvpData && pvpData.code == HttpResponseCode.Success) { fightData.pvpData = pvpData.data; } //} this.updateFriendData(fightData); } /**更新好友列表数据*/ private updateFriendData(fightData: FightData) { let cfg = DataSystem.getData(ConfigData).get("battle"); let tempFriendData = fightData.friendDataList.concat(); let tempListData = []; for (let i = 0; i < tempFriendData.length; i++) { if (tempFriendData[i].campLv >= cfg.unlockLv) { let isChallengeWin = false; for (let j = 0; j < fightData.pvpData.challengeWinUserIDList.length; j++) { if (fightData.pvpData.challengeWinUserIDList[j] == tempFriendData[i].id) { isChallengeWin = true; break; } } let tempData: any = { id: tempFriendData[i].id, avator: tempFriendData[i].avator, nickName: tempFriendData[i].nickName, attack: tempFriendData[i].attack, isChallengeWin: isChallengeWin }; tempListData.push(tempData); } } this.listData = tempListData.concat(); this.list.numItems = this.listData.length; console.log("FriendNum: " + this.listData.length); } private updateInfo() { console.log("updateInfo"); let num = DataSystem.getData(FightData).pvpData.challengeWinUserIDList ? DataSystem.getData(FightData).pvpData.challengeWinUserIDList.length : 0; this.imgProgress.fillRange = num / DataSystem.getData(ConfigData).get("battle").challengeWinTimes; this.txtProgress.string = `${num} /${DataSystem.getData(ConfigData).get("battle").challengeWinTimes}`; if (DataSystem.getData(FightData).pvpData.challengeWinUserIDList.length - DataSystem.getData(ConfigData).get("battle").challengeWinTimes >= 0 && !DataSystem.getData(FightData).pvpData.receivedChallengeReward) { DataSystem.getData(RedPointData).canReceiveFightBox = true; this.aniBox.play(); } } /**当列表渲染时*/ public onListRender(item: Node, index: number) { if (this.listData && this.listData.length > 0) { item.getComponent(FriendFightItem).onDataChange(this.listData[index], index); } } /**获取奖励 * @param rewardType 奖励类型 除了 money diamond bonus prestige 之外为武将碎片id * @param numData 数量 */ private getReward(rewardType: string, numData: number) { let type = 1; switch (rewardType) { case "money": type = 1; break; case "diamond": type = 2; break; case "bonus": type = 3; break; case "prestige": type = 4; break; //WindowSystem.open("prefabs/ui/turntable/prizeFly", WindowOpenMode.NotCloseAndCover, [{ type: type, num: numData }]); //break; } let userData = DataSystem.getData(UserData); ReportThinking.currency_increase(rewardType, userData[rewardType], numData, userData[rewardType] + numData, 'challengeReward'); WindowSystem.open("prefabs/ui/fight/fightRewardUI", WindowOpenMode.NotCloseAndCover, [[{ rewardType: type, num: numData }]]); } private async onClickBox() { if (!this.isCanClick) return; if (!DataSystem.getData(FightData).pvpData.receivedChallengeReward) { let deltaNum = DataSystem.getData(FightData).pvpData.challengeWinUserIDList.length - DataSystem.getData(ConfigData).get("battle").challengeWinTimes; if (deltaNum >= 0) { this.isCanClick = false; let result = await this.http.send("/api/pvp/ReceiveChallengeReward"); if (result && result.code == HttpResponseCode.Success) { this.getReward(result.data.rewardType, result.data.rewardNum); DataSystem.getData(FightData).pvpData.receivedChallengeReward = true; DataSystem.getData(RedPointData).canReceiveFightBox = false; this.aniBox.stop(); } else { WindowSystem.showTips(`请求失败`); } this.isCanClick = true; } else { WindowSystem.showTips(`还需${Math.abs(deltaNum)}场胜利即可领取宝箱`); } } else { WindowSystem.showTips(`你已领取该奖励`); } } }