| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493 |
- import { MONEY_TYPE } from "../datas/CommonData";
- import TreasureData from "../datas/TreasureData";
- import AdM from "../manager/AdM";
- // Learn TypeScript:
- // - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
- // Learn Attribute:
- // - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
- // Learn life-cycle callbacks:
- // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
- import GameM, { AUDIO_TYPE, VIDEO_TYPE } from "../manager/GameM";
- import UiM from "../manager/UiM";
- import EffectNode from "../ui/EffectNode";
- import Treasure from "../ui/Treasure";
- import LogUtil from "../utils/LogUtil";
- const { ccclass, property } = cc._decorator;
- @ccclass
- export default class TreasureItem extends cc.Component {
- @property(cc.Sprite)
- lyHeadArr: cc.Sprite[] = [];
- @property(cc.Sprite)
- spPro: cc.Sprite = null;
- @property(cc.Label)
- labJoin: cc.Label = null;
- @property(cc.Node)
- upReward: cc.Node = null;
- @property(cc.Label)
- labGailv: cc.Label = null;
- @property(cc.Label)
- labAwardNum: cc.Label = null;
- @property(cc.Label)
- labJoinNum: cc.Label = null;
- @property(cc.Label)
- labIssue: cc.Label = null;
- @property(cc.Label)
- labMoney: cc.Label = null;
- @property(cc.Label)
- labPro: cc.Label = null;
- @property(cc.Node)
- iconVideo: cc.Node = null;
- @property(cc.Label)
- labAdd: cc.Label = null;
- @property(cc.Animation)
- aniAdd: cc.Animation = null;
- @property(sp.Skeleton)
- openDraw: sp.Skeleton = null;
- /**概率增加 */
- @property(cc.Node)
- node_chanceUp: cc.Node = null;
- @property([cc.SpriteFrame])
- spriteFrame: cc.SpriteFrame[] = [];
- @property(cc.Sprite)
- clickBtn: cc.Sprite = null
- @property(cc.Node)
- changeUpParent: cc.Node = null
- private addTotal = 0
- private addNum = 0
- private addState = true
- // LIFE-CYCLE CALLBACKS:
- // onLoad () {}
- private curCfg = null
- private data = null
- private hasOpen = false
- private headInfoArr = []
- private lastPhase = 0
- private lastPro = 0
- start() {
- }
- init(cfg) {
- this.curCfg = cfg
- LogUtil.logV('this.curCfg ', this.curCfg)
- this.data = TreasureData.Instance.getTreasureDataById(this.curCfg.drawid)
- let joinTimes = this.data.joinTimes == 0 ? 1 : this.data.joinTimes;
- let gailv = ((1 / this.curCfg.mannum) * 100 * joinTimes).toFixed(2)
- this.labGailv.string = `${gailv}%`
- this.labAwardNum.string = `中奖人数:${1}`
- this.labMoney.string = this.curCfg.name
- this.headInfoArr = this.data.joinManInfoQueue
- this.addTotal = this.headInfoArr.length
- this.addNum = 0
- this.addState = false
- this.refresh()
- let time = 0
- if (this.curCfg.requestfrequency > TreasureData.Instance.treasureOpenTime) {
- time = this.curCfg.requestfrequency - TreasureData.Instance.treasureOpenTime
- }
- else {
- time = this.curCfg.requestfrequency - (TreasureData.Instance.treasureOpenTime % this.curCfg.requestfrequency)
- }
- this.freshVideo()
- this.scheduleOnce(() => {
- TreasureData.Instance.freshTreasureData(this.curCfg.drawid)
- this.schedule(this.freshServerData, this.curCfg.requestfrequency)
- }, time)
- }
- freshVideo() {
- if (this.data.joinTimes == 0 || this.data.joinTimes >= this.curCfg.limitman) {
- this.iconVideo.active = false
- }
- else {
- this.iconVideo.active = true
- }
- }
- onDestroy() {
- this.unschedule(this.freshServerData)
- }
- repeatSchedule() {
- this.index = 0
- this.schedule(this.startAction, 1, cc.macro.REPEAT_FOREVER)
- }
- index = 0
- startAction() {
- if (cc.isValid(this.labJoin)) {
- if (this.index == 0) {
- this.labJoin.string = "等待开奖。"
- this.index = 1
- } else if (this.index == 1) {
- this.labJoin.string = "等待开奖。。"
- this.index = 2
- } else if (this.index == 2) {
- this.labJoin.string = "等待开奖。。。"
- this.index = 0
- }
- }
- }
- refresh(server: boolean = false, ifUpdateJoinTime: boolean = false) {
- this.labPro.string = `${this.data.joinTotal}/${this.curCfg.mannum}`
- this.labIssue.string = `第${this.data.phase}期`
- this.labJoinNum.string = `已有${this.data.joinTotal}人参与`
- // this.labJoin.string = `参与夺宝(${this.data.joinTimes}/${this.curCfg.limitman})`
- this.labJoin.node.stopAllActions()
- let layout = this.labJoin.node.parent.getComponent(cc.Layout)
- layout.enabled = true
- if (this.data.joinTimes == 0) {
- this.clickBtn.spriteFrame = this.spriteFrame[0]
- this.labJoin.string = '参与夺宝'
- this.upReward.active = true
- this.iconVideo.active = false
- } else if (this.data.joinTimes > 0 && this.data.joinTimes < this.curCfg.limitman) {
- this.clickBtn.spriteFrame = this.spriteFrame[1]
- this.labJoin.string = '提升概率(' + (this.data.joinTimes - 1) + "/" + (this.curCfg.limitman - 1) + ")"
- this.upReward.active = false
- this.iconVideo.active = true
- } else if (this.data.joinTimes >= this.curCfg.limitman) {
- this.clickBtn.spriteFrame = this.spriteFrame[2]
- this.labJoin.string = '等待开奖'
- this.upReward.active = false
- this.iconVideo.active = false
- this.index == 0
- layout.enabled = false
- this.labJoin.node.x = -90
- this.labJoin.node.runAction(cc.repeatForever(cc.sequence(cc.callFunc(() => {
- if (cc.isValid(this.labJoin)) {
- if (this.index == 0) {
- this.labJoin.string = "等待开奖。"
- this.index = 1
- } else if (this.index == 1) {
- this.labJoin.string = "等待开奖。。"
- this.index = 2
- } else if (this.index == 2) {
- this.labJoin.string = "等待开奖。。。"
- this.index = 0
- }
- }
- }), cc.delayTime(1))))
- }
- let pro = this.data.joinTotal / this.curCfg.mannum
- this.spPro.fillRange = pro
- if (server) {
- if (this.lastPhase != this.data.phase) {
- //清空头像
- for (var i = 0; i < 5; i++) {
- this.lyHeadArr[i].spriteFrame = null
- }
- if (TreasureData.Instance.getDrawLocalData(this.lastPhase)) {
- this.scheduleOnce(() => {
- TreasureData.Instance.getLotteryresults()
- }, 1)
- }
- this.lastPhase = this.data.phase
- this.lastPro = 0
- this.openDraw.setAnimation(0, 'animation', false)
- }
- if (this.data.joinTotal > this.lastPro) {
- this.aniAdd.play('addUp', 0)
- this.labAdd.string = `+${this.data.joinTotal - this.lastPro}`
- this.lastPro = this.data.joinTotal
- }
- this.addTotal = this.headInfoArr.length
- this.addNum = 0
- this.addState = false
- //开奖特效
- if (this.hasOpen) {
- this.hasOpen = false
- }
- }
- else {
- this.lastPhase = this.data.phase
- this.lastPro = this.data.joinTotal
- }
- //FC:是否更新
- if (ifUpdateJoinTime) {
- let timeOut = setTimeout(() => {
- this.showTip();
- clearTimeout(timeOut);
- }, 500)
- }
- }
- /** 检查头像是否更新 */
- checkNeedQueue(newData) {
- let len = newData.joinManInfoQueue.length
- let len1 = this.data.joinManInfoQueue.length
- let diff = false
- if (len != len1) {
- diff = true
- }
- else {
- for (var i = 0; i < len; i++) {
- for (var j = 0; j < len1; j++) {
- if (newData.joinManInfoQueue[i].headimgurl != this.data.joinManInfoQueue.headimgurl) {
- diff = true
- break
- }
- }
- }
- }
- if (diff) {
- this.headInfoArr = newData.joinManInfoQueue
- }
- }
- freshServerData() {
- TreasureData.Instance.freshTreasureData(this.curCfg.drawid)
- }
- onFreshServerData(hasOpen, data) {
- this.checkNeedQueue(data)
- this.data = data
- this.hasOpen = hasOpen
- this.refresh(true)
- }
- update(dt) {
- if (!this.addState) {
- if (this.addNum < this.addTotal) {
- this.addState = true
- if (this.headInfoArr[this.addNum].headimgurl == '') {
- this.lyHeadArr[this.addNum].spriteFrame = null
- this.addNum++
- this.addState = false
- }
- else {
- cc.loader.load(this.headInfoArr[this.addNum].headimgurl + "?aaa=aa.jpg", (err, res) => {
- if (err) {
- console.log('err:', err);
- }
- else {
- let tex: cc.Texture2D = res as cc.Texture2D;
- if (this.lyHeadArr) {
- this.lyHeadArr[this.addNum].spriteFrame = new cc.SpriteFrame(tex);
- }
- this.addState = false
- }
- this.addNum++
- })
- }
- }
- else {
- this.addState = true
- }
- }
- }
- /** 点击抽奖 */
- clickDrawTreasure() {
- GameM.audioM.playEffect(AUDIO_TYPE.button)
- if (this.data.joinTimes >= this.curCfg.limitman) {
- EffectNode.instance.PlayTip('开奖进度满即可开奖')
- return
- }
- if (this.data.joinTimes == 0 && TreasureData.Instance.treasureDailyTimes >= GameM.commonData.globalCfg.treasureDailylimit) {
- EffectNode.instance.PlayTip('今日夺宝次数已达上限,请明日再来')
- return
- }
- if (TreasureData.Instance.joinCool) {
- EffectNode.instance.PlayTip('你点击太快了,请稍后重试')
- return
- }
- TreasureData.Instance.curTreasureItem = this.node
- //第一次报名,有夺宝券
- if (GameM.commonData.roleData.ticket > 0 && this.data.joinTimes == 0) {
- TreasureData.Instance.drawTreasureType = 0
- let data = {
- "isVideo": false,
- "phase": this.data.phase,
- "recordId": this.data.recordId
- // "isNoCumulative":0
- }
- TreasureData.Instance.updateJoin(data)
- GameM.commonData.addDailyFinishTimesDataByType(6)
- GameM.commonData.roleData.treasureTotal++
- GameM.commonData.updateRoleData()
- AdM.onSendEvent(`item_${MONEY_TYPE.treasureCard}`, `使用道具${MONEY_TYPE.treasureCard}`, 'item')
- return
- }
- //第一次报名,没有夺宝券
- if (GameM.commonData.roleData.ticket <= 0 && this.data.joinTimes == 0) {
- // GameM.commonData.drawTreasureType = 1
- // if (GameM.commonData.isVideoTest) {
- // this.onDrawAdEnd()
- // }
- // else {
- // GameM.adM.watchVideo(VIDEO_TYPE.join)
- // }'
- // to do
- UiM.Instance.treasureNode.getComponent(Treasure).showVideoTip()
- return
- }
- // 重复报名
- if (this.data.joinTimes > 0 && this.data.joinTimes <= this.curCfg.limitman) {
- TreasureData.Instance.drawTreasureType = 1
- LogUtil.logV("GameM.commonData.treasureDailyTimes one", TreasureData.Instance.treasureDailyTimes)
- if (GameM.commonData.isVideoTest) {
- this.onDrawAdEnd()
- }
- else {
- GameM.adM.watchVideo(VIDEO_TYPE.join)
- }
- }
- }
- /** 看完广告返回 */
- onDrawAdEnd() {
- let data = {
- "isVideo": true,
- "phase": this.data.phase,
- "recordId": this.data.recordId,
- "isNoCumulative": 1 //不消耗报名次数
- }
- LogUtil.logV("GameM.commonData.treasureDailyTimes two", TreasureData.Instance.treasureDailyTimes)
- TreasureData.Instance.updateJoin(data)
- GameM.commonData.addDailyFinishTimesDataByType(6)
- GameM.commonData.roleData.treasureTotal++
- // switch (this.curCfg.name) {
- // case 0.3:
- // AdM.onSendEvent('videoEnd16', `参加${this.curCfg.name}元抽奖成功`)
- // break
- // case 0.5:
- // AdM.onSendEvent('videoEnd17', `参加${this.curCfg.name}元抽奖成功`)
- // break
- // case 1:
- // AdM.onSendEvent('videoEnd18', `参加${this.curCfg.name}元抽奖成功`)
- // break
- // case 5:
- // AdM.onSendEvent('videoEnd19', `参加${this.curCfg.name}元抽奖成功`)
- // break
- // case 20:
- // AdM.onSendEvent('videoEnd20', `参加${this.curCfg.name}元抽奖成功`)
- // break
- // case 100:
- // AdM.onSendEvent('videoEnd21', `参加${this.curCfg.name}元抽奖成功`)
- // break
- // }
- }
- resultJoin() {
- this.data.joinTimes++
- this.freshVideo()
- LogUtil.logV("GameM.commonData.treasureDailyTimes six", TreasureData.Instance.treasureDailyTimes)
- TreasureData.Instance.changeDrawLocalData(this.data.phase, true)
- LogUtil.logV("resultJoin", this.data)
- LogUtil.logV("GameM.commonData.treasureDailyTimes seven", TreasureData.Instance.treasureDailyTimes)
- TreasureData.Instance.getDailyLimit()
- this.refresh(false, true)
- }
- clickRecord() {
- GameM.audioM.playEffect(AUDIO_TYPE.button)
- AdM.onSendEvent('clickTreasureRecord', '点击开奖记录')
- UiM.Instance.treasureNode.getComponent(Treasure).showRecord(this.curCfg.drawid, this.curCfg.amount)
- }
- /**显示概率的Tip */
- showTip() {
- let joinTimes = this.data.joinTimes == 0 ? 1 : this.data.joinTimes;
- if (joinTimes != 1) {
- this.node_chanceUp.setPosition(0, 0);
- let worldPos = this.node_chanceUp.parent.convertToWorldSpaceAR(this.node_chanceUp.position)
- let targetWorldPos = this.changeUpParent.convertToWorldSpaceAR(cc.v2(0, 30))
- let targetPos = cc.find("Canvas").convertToNodeSpaceAR(targetWorldPos)
- this.node_chanceUp.parent = cc.find("Canvas")
- this.node_chanceUp.position = this.node_chanceUp.parent.convertToNodeSpaceAR(worldPos)
- this.node_chanceUp.opacity = 255;
- this.node_chanceUp.scaleX = this.node_chanceUp.scaleY = 0.1;
- // console.log("显示概率1!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", this.node_chanceUp);
- cc.tween(this.node_chanceUp)
- .to(0.2, { scaleX: 1.5, scaleY: 1.5 })
- .to(0.1, { scaleX: 1, scaleY: 1 })
- .to(0.2, { position: targetPos })
- .call(() => {
- // console.log("显示概率>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", this.node_chanceUp);
- this.updateChance();
- })
- .to(0.3, { opacity: 0 })
- .call(() => {
- if (cc.isValid(this.node_chanceUp)) {
- this.node_chanceUp.parent = this.changeUpParent
- this.node_chanceUp.position = cc.v3(0, 0)
- }
- })
- .start();
- }
- else {
- this.updateChance();
- }
- }
- /** */
- updateChance() {
- let joinTimes = this.data.joinTimes == 0 ? 1 : this.data.joinTimes;
- let gailv = ((1 / this.curCfg.mannum) * 100 * joinTimes).toFixed(2)
- cc.tween(this.labGailv.node)
- .to(0.2, { scaleX: 2, scaleY: 2 })
- .call(() => {
- this.labGailv.string = `${gailv}%`
- })
- .to(0.1, { scaleX: 1, scaleY: 1 })
- .call(() => {
- })
- .start();
- }
- }
|