const QuestApi = require('../net/QuestApi');
const DWTool = require("../utils/DWTool");
const QuestDailyMissionType = require("../utils/GameEnum").QuestDailyMissionType;
cc.Class({
extends: cc.Component,
properties: {
questWrap: {
tooltip: '每日任务容器',
default: null,
type: cc.Node
},
questBtns: [cc.Node],
userDegreeRichText: {
tooltip: '用户当前活跃度',
default: null,
type: cc.RichText
},
_userDegree: 0,
userDegree: {
get () {
return this._userDegree;
},
set (value) {
this._userDegree = value;
// 设置用户当前活跃度
// this.actProgress.progress = data / 100
// this.userDegreeLabel.string = data
}
},
giftFrames: {
tooltip: '奖励图片素材',
default: [],
type: [cc.SpriteFrame]
},
questItem: cc.Prefab,
resetTimeLabel: cc.Label,
isLoadData: {
default: false,
visible: false
}
},
onLoad () {
this.userDegree = 0;
this.dailyDegreeList = [];
this.giftMap = ['diamond', 'coin', 'ticket'];
},
init (quest) {
this.quest = quest;
this.loadNetworkData();
},
loadNetworkData() {
QuestApi.getDailyTask((responseData) => {
this.dailyDegreeList = responseData.dailyDegreeList.sort((a, b) => {
return a.count - b.count
});
this.degree = this.dailyDegreeList[0];
this.dailyTaskList = responseData.dailyTaskList;
this.userDegree = parseInt(responseData.userDegree);
this._setUserDegree();
this._setTaskList();
this.updateTime = responseData.updateTime;
this.isLoadData = true;
} , (error) => {
console.log('dailytask error' + error);
});
},
/**
* 渲染每日任务列表
*/
_setTaskList () {
this.questWrap.removeAllChildren();
this.dailyTaskList.forEach((item, index) => {
let questItem = cc.instantiate(this.questItem);
this.questWrap.addChild(questItem);
questItem.getComponent('QuestDailyItem').init(this, item, index);
})
},
/**
* 设置活跃度奖励进度条
*/
_setUserDegree () {
var canGet = false;
var alreadyGetCount = 0;
for (let i = 0; i < this.dailyTaskList.length; ++i) {
let dailyTask = this.dailyTaskList[i];
if (dailyTask.state == QuestDailyMissionType.AlreadyGet){
alreadyGetCount++;
}
}
if (alreadyGetCount >= this.degree.count) {
canGet = true;
}
this.userDegreeRichText.string = `每日任务 最终奖励 (${alreadyGetCount}/${this.degree.count})`;
this.questBtns.forEach(n => {
n.active = false;
});
let btn;
if (this.degree.state == QuestDailyMissionType.NoFinished) {
btn = this.questBtns[0];
} else if (this.degree.state == QuestDailyMissionType.CanGain) {
if (canGet) {
btn = this.questBtns[1];
} else {
btn = this.questBtns[0];
}
} else {
btn = this.questBtns[2];
}
btn.active = true;
let countNode = cc.find('/awardNode/count_label', btn);
let spriteNode = cc.find('/awardNode/sprite', btn);
let awardLabelNode = cc.find('/awardLabel', btn);
var awardString = '奖励';
if (canGet) {
awardString = '获得';
}
let _count = 0;
let _type = '';
let _sprite = null;
this.giftMap.forEach((value, index) => {
if(this.degree[value] > 0) {
_count = this.degree[value];
_type = value;
_sprite = this.giftFrames[index];
}
});
if (spriteNode) {
spriteNode.getComponent('cc.Sprite').spriteFrame = _sprite;
}
if (countNode) {
countNode.getComponent('cc.Label').string = `${DWTool.coinParseNoFixed(_count)}`;
}
if (awardLabelNode) {
awardLabelNode.getComponent('cc.Label').string = awardString;
}
},
updateUserDegree() {
this._setUserDegree();
},
/**
* 领取每日任务最终奖励
*/
handleGiftBtn () {
// state = 0, 可领取状态
if(this.degree.state == QuestDailyMissionType.CanGain) {
this.questBtns[1].interactable = false;
QuestApi.postGetDailyDegreeReward(this.degree.count, (res) => {
this.questBtns[1].interactable = true;
// 更新全局userInfo
this.quest.updateUserInfo(this.degree.coin, this.degree.diamond, this.degree.ticket);
// state = 2, 已领取状态
this.degree.state = QuestDailyMissionType.AlreadyGet;
this.questBtns.forEach(n => {
n.active = false;
});
this.questBtns[2].active = true;
// 显示动画
setTimeout(() => {
this.quest.showActGift({
ticket: this.degree.ticket,
diamond: this.degree.diamond,
coin: this.degree.coin
})
}, 200);
if (res.isCancel) {
let isCancel = res.isCancel == 1 ? true : false;
GameEvent.fire('quest_daily_notice', isCancel);
}
}, (errCode, errMsg) => {
this.questBtns[1].interactable = true;
// 领取失败
Global.commonAlert.showCommonErrorAlert("领取奖励失败");
});
}
},
update(dt) {
if (this.updateTime == undefined) {
return;
}
if (this.isLoadData == true) {
var curTimestamp = parseInt(new Date().getTime());
if ((this.updateTime - curTimestamp) > 0) {
this.resetTimeLabel.string = `每日任务重置:${DWTool.calculateTimeString((this.updateTime - curTimestamp) / 1000)}`;
} else {
this.isLoadData = false;
this.loadNetworkData();
}
}
}
// start () {
//
// }
});