TreasureItem.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. import { MONEY_TYPE } from "../datas/CommonData";
  2. import TreasureData from "../datas/TreasureData";
  3. import AdM from "../manager/AdM";
  4. // Learn TypeScript:
  5. // - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
  6. // Learn Attribute:
  7. // - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
  8. // Learn life-cycle callbacks:
  9. // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
  10. import GameM, { AUDIO_TYPE, VIDEO_TYPE } from "../manager/GameM";
  11. import UiM from "../manager/UiM";
  12. import EffectNode from "../ui/EffectNode";
  13. import Treasure from "../ui/Treasure";
  14. import LogUtil from "../utils/LogUtil";
  15. const { ccclass, property } = cc._decorator;
  16. @ccclass
  17. export default class TreasureItem extends cc.Component {
  18. @property(cc.Sprite)
  19. lyHeadArr: cc.Sprite[] = [];
  20. @property(cc.Sprite)
  21. spPro: cc.Sprite = null;
  22. @property(cc.Label)
  23. labJoin: cc.Label = null;
  24. @property(cc.Node)
  25. upReward: cc.Node = null;
  26. @property(cc.Label)
  27. labGailv: cc.Label = null;
  28. @property(cc.Label)
  29. labAwardNum: cc.Label = null;
  30. @property(cc.Label)
  31. labJoinNum: cc.Label = null;
  32. @property(cc.Label)
  33. labIssue: cc.Label = null;
  34. @property(cc.Label)
  35. labMoney: cc.Label = null;
  36. @property(cc.Label)
  37. labPro: cc.Label = null;
  38. @property(cc.Node)
  39. iconVideo: cc.Node = null;
  40. @property(cc.Label)
  41. labAdd: cc.Label = null;
  42. @property(cc.Animation)
  43. aniAdd: cc.Animation = null;
  44. @property(sp.Skeleton)
  45. openDraw: sp.Skeleton = null;
  46. /**概率增加 */
  47. @property(cc.Node)
  48. node_chanceUp: cc.Node = null;
  49. @property([cc.SpriteFrame])
  50. spriteFrame: cc.SpriteFrame[] = [];
  51. @property(cc.Sprite)
  52. clickBtn: cc.Sprite = null
  53. @property(cc.Node)
  54. changeUpParent: cc.Node = null
  55. private addTotal = 0
  56. private addNum = 0
  57. private addState = true
  58. // LIFE-CYCLE CALLBACKS:
  59. // onLoad () {}
  60. private curCfg = null
  61. private data = null
  62. private hasOpen = false
  63. private headInfoArr = []
  64. private lastPhase = 0
  65. private lastPro = 0
  66. start() {
  67. }
  68. init(cfg) {
  69. this.curCfg = cfg
  70. LogUtil.logV('this.curCfg ', this.curCfg)
  71. this.data = TreasureData.Instance.getTreasureDataById(this.curCfg.drawid)
  72. let joinTimes = this.data.joinTimes == 0 ? 1 : this.data.joinTimes;
  73. let gailv = ((1 / this.curCfg.mannum) * 100 * joinTimes).toFixed(2)
  74. this.labGailv.string = `${gailv}%`
  75. this.labAwardNum.string = `中奖人数:${1}`
  76. this.labMoney.string = this.curCfg.name
  77. this.headInfoArr = this.data.joinManInfoQueue
  78. this.addTotal = this.headInfoArr.length
  79. this.addNum = 0
  80. this.addState = false
  81. this.refresh()
  82. let time = 0
  83. if (this.curCfg.requestfrequency > TreasureData.Instance.treasureOpenTime) {
  84. time = this.curCfg.requestfrequency - TreasureData.Instance.treasureOpenTime
  85. }
  86. else {
  87. time = this.curCfg.requestfrequency - (TreasureData.Instance.treasureOpenTime % this.curCfg.requestfrequency)
  88. }
  89. this.freshVideo()
  90. this.scheduleOnce(() => {
  91. TreasureData.Instance.freshTreasureData(this.curCfg.drawid)
  92. this.schedule(this.freshServerData, this.curCfg.requestfrequency)
  93. }, time)
  94. }
  95. freshVideo() {
  96. if (this.data.joinTimes == 0 || this.data.joinTimes >= this.curCfg.limitman) {
  97. this.iconVideo.active = false
  98. }
  99. else {
  100. this.iconVideo.active = true
  101. }
  102. }
  103. onDestroy() {
  104. this.unschedule(this.freshServerData)
  105. }
  106. repeatSchedule() {
  107. this.index = 0
  108. this.schedule(this.startAction, 1, cc.macro.REPEAT_FOREVER)
  109. }
  110. index = 0
  111. startAction() {
  112. if (cc.isValid(this.labJoin)) {
  113. if (this.index == 0) {
  114. this.labJoin.string = "等待开奖。"
  115. this.index = 1
  116. } else if (this.index == 1) {
  117. this.labJoin.string = "等待开奖。。"
  118. this.index = 2
  119. } else if (this.index == 2) {
  120. this.labJoin.string = "等待开奖。。。"
  121. this.index = 0
  122. }
  123. }
  124. }
  125. refresh(server: boolean = false, ifUpdateJoinTime: boolean = false) {
  126. this.labPro.string = `${this.data.joinTotal}/${this.curCfg.mannum}`
  127. this.labIssue.string = `第${this.data.phase}期`
  128. this.labJoinNum.string = `已有${this.data.joinTotal}人参与`
  129. // this.labJoin.string = `参与夺宝(${this.data.joinTimes}/${this.curCfg.limitman})`
  130. this.labJoin.node.stopAllActions()
  131. let layout = this.labJoin.node.parent.getComponent(cc.Layout)
  132. layout.enabled = true
  133. if (this.data.joinTimes == 0) {
  134. this.clickBtn.spriteFrame = this.spriteFrame[0]
  135. this.labJoin.string = '参与夺宝'
  136. this.upReward.active = true
  137. this.iconVideo.active = false
  138. } else if (this.data.joinTimes > 0 && this.data.joinTimes < this.curCfg.limitman) {
  139. this.clickBtn.spriteFrame = this.spriteFrame[1]
  140. this.labJoin.string = '提升概率(' + (this.data.joinTimes - 1) + "/" + (this.curCfg.limitman - 1) + ")"
  141. this.upReward.active = false
  142. this.iconVideo.active = true
  143. } else if (this.data.joinTimes >= this.curCfg.limitman) {
  144. this.clickBtn.spriteFrame = this.spriteFrame[2]
  145. this.labJoin.string = '等待开奖'
  146. this.upReward.active = false
  147. this.iconVideo.active = false
  148. this.index == 0
  149. layout.enabled = false
  150. this.labJoin.node.x = -90
  151. this.labJoin.node.runAction(cc.repeatForever(cc.sequence(cc.callFunc(() => {
  152. if (cc.isValid(this.labJoin)) {
  153. if (this.index == 0) {
  154. this.labJoin.string = "等待开奖。"
  155. this.index = 1
  156. } else if (this.index == 1) {
  157. this.labJoin.string = "等待开奖。。"
  158. this.index = 2
  159. } else if (this.index == 2) {
  160. this.labJoin.string = "等待开奖。。。"
  161. this.index = 0
  162. }
  163. }
  164. }), cc.delayTime(1))))
  165. }
  166. let pro = this.data.joinTotal / this.curCfg.mannum
  167. this.spPro.fillRange = pro
  168. if (server) {
  169. if (this.lastPhase != this.data.phase) {
  170. //清空头像
  171. for (var i = 0; i < 5; i++) {
  172. this.lyHeadArr[i].spriteFrame = null
  173. }
  174. if (TreasureData.Instance.getDrawLocalData(this.lastPhase)) {
  175. this.scheduleOnce(() => {
  176. TreasureData.Instance.getLotteryresults()
  177. }, 1)
  178. }
  179. this.lastPhase = this.data.phase
  180. this.lastPro = 0
  181. this.openDraw.setAnimation(0, 'animation', false)
  182. }
  183. if (this.data.joinTotal > this.lastPro) {
  184. this.aniAdd.play('addUp', 0)
  185. this.labAdd.string = `+${this.data.joinTotal - this.lastPro}`
  186. this.lastPro = this.data.joinTotal
  187. }
  188. this.addTotal = this.headInfoArr.length
  189. this.addNum = 0
  190. this.addState = false
  191. //开奖特效
  192. if (this.hasOpen) {
  193. this.hasOpen = false
  194. }
  195. }
  196. else {
  197. this.lastPhase = this.data.phase
  198. this.lastPro = this.data.joinTotal
  199. }
  200. //FC:是否更新
  201. if (ifUpdateJoinTime) {
  202. let timeOut = setTimeout(() => {
  203. this.showTip();
  204. clearTimeout(timeOut);
  205. }, 500)
  206. }
  207. }
  208. /** 检查头像是否更新 */
  209. checkNeedQueue(newData) {
  210. let len = newData.joinManInfoQueue.length
  211. let len1 = this.data.joinManInfoQueue.length
  212. let diff = false
  213. if (len != len1) {
  214. diff = true
  215. }
  216. else {
  217. for (var i = 0; i < len; i++) {
  218. for (var j = 0; j < len1; j++) {
  219. if (newData.joinManInfoQueue[i].headimgurl != this.data.joinManInfoQueue.headimgurl) {
  220. diff = true
  221. break
  222. }
  223. }
  224. }
  225. }
  226. if (diff) {
  227. this.headInfoArr = newData.joinManInfoQueue
  228. }
  229. }
  230. freshServerData() {
  231. TreasureData.Instance.freshTreasureData(this.curCfg.drawid)
  232. }
  233. onFreshServerData(hasOpen, data) {
  234. this.checkNeedQueue(data)
  235. this.data = data
  236. this.hasOpen = hasOpen
  237. this.refresh(true)
  238. }
  239. update(dt) {
  240. if (!this.addState) {
  241. if (this.addNum < this.addTotal) {
  242. this.addState = true
  243. if (this.headInfoArr[this.addNum].headimgurl == '') {
  244. this.lyHeadArr[this.addNum].spriteFrame = null
  245. this.addNum++
  246. this.addState = false
  247. }
  248. else {
  249. cc.loader.load(this.headInfoArr[this.addNum].headimgurl + "?aaa=aa.jpg", (err, res) => {
  250. if (err) {
  251. console.log('err:', err);
  252. }
  253. else {
  254. let tex: cc.Texture2D = res as cc.Texture2D;
  255. if (this.lyHeadArr) {
  256. this.lyHeadArr[this.addNum].spriteFrame = new cc.SpriteFrame(tex);
  257. }
  258. this.addState = false
  259. }
  260. this.addNum++
  261. })
  262. }
  263. }
  264. else {
  265. this.addState = true
  266. }
  267. }
  268. }
  269. /** 点击抽奖 */
  270. clickDrawTreasure() {
  271. GameM.audioM.playEffect(AUDIO_TYPE.button)
  272. if (this.data.joinTimes >= this.curCfg.limitman) {
  273. EffectNode.instance.PlayTip('开奖进度满即可开奖')
  274. return
  275. }
  276. if (this.data.joinTimes == 0 && TreasureData.Instance.treasureDailyTimes >= GameM.commonData.globalCfg.treasureDailylimit) {
  277. EffectNode.instance.PlayTip('今日夺宝次数已达上限,请明日再来')
  278. return
  279. }
  280. if (TreasureData.Instance.joinCool) {
  281. EffectNode.instance.PlayTip('你点击太快了,请稍后重试')
  282. return
  283. }
  284. TreasureData.Instance.curTreasureItem = this.node
  285. //第一次报名,有夺宝券
  286. if (GameM.commonData.roleData.ticket > 0 && this.data.joinTimes == 0) {
  287. TreasureData.Instance.drawTreasureType = 0
  288. let data = {
  289. "isVideo": false,
  290. "phase": this.data.phase,
  291. "recordId": this.data.recordId
  292. // "isNoCumulative":0
  293. }
  294. TreasureData.Instance.updateJoin(data)
  295. GameM.commonData.addDailyFinishTimesDataByType(6)
  296. GameM.commonData.roleData.treasureTotal++
  297. GameM.commonData.updateRoleData()
  298. AdM.onSendEvent(`item_${MONEY_TYPE.treasureCard}`, `使用道具${MONEY_TYPE.treasureCard}`, 'item')
  299. return
  300. }
  301. //第一次报名,没有夺宝券
  302. if (GameM.commonData.roleData.ticket <= 0 && this.data.joinTimes == 0) {
  303. // GameM.commonData.drawTreasureType = 1
  304. // if (GameM.commonData.isVideoTest) {
  305. // this.onDrawAdEnd()
  306. // }
  307. // else {
  308. // GameM.adM.watchVideo(VIDEO_TYPE.join)
  309. // }'
  310. // to do
  311. UiM.Instance.treasureNode.getComponent(Treasure).showVideoTip()
  312. return
  313. }
  314. // 重复报名
  315. if (this.data.joinTimes > 0 && this.data.joinTimes <= this.curCfg.limitman) {
  316. TreasureData.Instance.drawTreasureType = 1
  317. LogUtil.logV("GameM.commonData.treasureDailyTimes one", TreasureData.Instance.treasureDailyTimes)
  318. if (GameM.commonData.isVideoTest) {
  319. this.onDrawAdEnd()
  320. }
  321. else {
  322. GameM.adM.watchVideo(VIDEO_TYPE.join)
  323. }
  324. }
  325. }
  326. /** 看完广告返回 */
  327. onDrawAdEnd() {
  328. let data = {
  329. "isVideo": true,
  330. "phase": this.data.phase,
  331. "recordId": this.data.recordId,
  332. "isNoCumulative": 1 //不消耗报名次数
  333. }
  334. LogUtil.logV("GameM.commonData.treasureDailyTimes two", TreasureData.Instance.treasureDailyTimes)
  335. TreasureData.Instance.updateJoin(data)
  336. GameM.commonData.addDailyFinishTimesDataByType(6)
  337. GameM.commonData.roleData.treasureTotal++
  338. // switch (this.curCfg.name) {
  339. // case 0.3:
  340. // AdM.onSendEvent('videoEnd16', `参加${this.curCfg.name}元抽奖成功`)
  341. // break
  342. // case 0.5:
  343. // AdM.onSendEvent('videoEnd17', `参加${this.curCfg.name}元抽奖成功`)
  344. // break
  345. // case 1:
  346. // AdM.onSendEvent('videoEnd18', `参加${this.curCfg.name}元抽奖成功`)
  347. // break
  348. // case 5:
  349. // AdM.onSendEvent('videoEnd19', `参加${this.curCfg.name}元抽奖成功`)
  350. // break
  351. // case 20:
  352. // AdM.onSendEvent('videoEnd20', `参加${this.curCfg.name}元抽奖成功`)
  353. // break
  354. // case 100:
  355. // AdM.onSendEvent('videoEnd21', `参加${this.curCfg.name}元抽奖成功`)
  356. // break
  357. // }
  358. }
  359. resultJoin() {
  360. this.data.joinTimes++
  361. this.freshVideo()
  362. LogUtil.logV("GameM.commonData.treasureDailyTimes six", TreasureData.Instance.treasureDailyTimes)
  363. TreasureData.Instance.changeDrawLocalData(this.data.phase, true)
  364. LogUtil.logV("resultJoin", this.data)
  365. LogUtil.logV("GameM.commonData.treasureDailyTimes seven", TreasureData.Instance.treasureDailyTimes)
  366. TreasureData.Instance.getDailyLimit()
  367. this.refresh(false, true)
  368. }
  369. clickRecord() {
  370. GameM.audioM.playEffect(AUDIO_TYPE.button)
  371. AdM.onSendEvent('clickTreasureRecord', '点击开奖记录')
  372. UiM.Instance.treasureNode.getComponent(Treasure).showRecord(this.curCfg.drawid, this.curCfg.amount)
  373. }
  374. /**显示概率的Tip */
  375. showTip() {
  376. let joinTimes = this.data.joinTimes == 0 ? 1 : this.data.joinTimes;
  377. if (joinTimes != 1) {
  378. this.node_chanceUp.setPosition(0, 0);
  379. let worldPos = this.node_chanceUp.parent.convertToWorldSpaceAR(this.node_chanceUp.position)
  380. let targetWorldPos = this.changeUpParent.convertToWorldSpaceAR(cc.v2(0, 30))
  381. let targetPos = cc.find("Canvas").convertToNodeSpaceAR(targetWorldPos)
  382. this.node_chanceUp.parent = cc.find("Canvas")
  383. this.node_chanceUp.position = this.node_chanceUp.parent.convertToNodeSpaceAR(worldPos)
  384. this.node_chanceUp.opacity = 255;
  385. this.node_chanceUp.scaleX = this.node_chanceUp.scaleY = 0.1;
  386. // console.log("显示概率1!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", this.node_chanceUp);
  387. cc.tween(this.node_chanceUp)
  388. .to(0.2, { scaleX: 1.5, scaleY: 1.5 })
  389. .to(0.1, { scaleX: 1, scaleY: 1 })
  390. .to(0.2, { position: targetPos })
  391. .call(() => {
  392. // console.log("显示概率>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", this.node_chanceUp);
  393. this.updateChance();
  394. })
  395. .to(0.3, { opacity: 0 })
  396. .call(() => {
  397. if (cc.isValid(this.node_chanceUp)) {
  398. this.node_chanceUp.parent = this.changeUpParent
  399. this.node_chanceUp.position = cc.v3(0, 0)
  400. }
  401. })
  402. .start();
  403. }
  404. else {
  405. this.updateChance();
  406. }
  407. }
  408. /** */
  409. updateChance() {
  410. let joinTimes = this.data.joinTimes == 0 ? 1 : this.data.joinTimes;
  411. let gailv = ((1 / this.curCfg.mannum) * 100 * joinTimes).toFixed(2)
  412. cc.tween(this.labGailv.node)
  413. .to(0.2, { scaleX: 2, scaleY: 2 })
  414. .call(() => {
  415. this.labGailv.string = `${gailv}%`
  416. })
  417. .to(0.1, { scaleX: 1, scaleY: 1 })
  418. .call(() => {
  419. })
  420. .start();
  421. }
  422. }