pk.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  1. // pages/pk/pk.js
  2. import NumberTimeChange from '../../utils/ntc.js'
  3. import Audio from '../../utils/audio.js'
  4. var app = getApp()
  5. var lock = require('../../utils/lock.js')
  6. var net = require('../../utils/net.js')
  7. var url = require('../../utils/url.js')
  8. var common = require('../../utils/common.js')
  9. var context
  10. Page({
  11. /**
  12. * 页面的初始数据
  13. */
  14. data: {
  15. interval: null,
  16. wxuser: {},
  17. uid: 0,
  18. code: '',
  19. isJoin: false,
  20. rankGameId: null,
  21. gameId: 0,
  22. rankDan: 0,
  23. rankType: 1,
  24. roomType: 1,
  25. pageStatus: 0,
  26. pkUser: {},
  27. num: 1,
  28. questionTypes: [],
  29. questionType: '',
  30. question: {},
  31. pointC: 0,
  32. joinPointC: 0,
  33. nextPointC: 0,
  34. nextJoinPointc: 0,
  35. countDown: 10,
  36. countDownSize: 0,
  37. shareCost: 0,
  38. friend: {
  39. hidden: true,
  40. hiddenRadar: false,
  41. hiddenLeave: true,
  42. shareText: '再次邀请'
  43. },
  44. rankWaitHidden: true,
  45. pkIntoHidden: true,
  46. room: {
  47. hidden: true,
  48. hiddenQa: false,
  49. hiddenQaType: true,
  50. hiddenQaLast: true,
  51. hiddenQuestion: true,
  52. hiddenDown: true,
  53. hiddenResult: true,
  54. hiddenShareTip: false
  55. },
  56. gradeLeftStyle: "top: 456rpx",
  57. gradeRightStyle: "top: 456rpx",
  58. abcd: [
  59. { style: '', left: '', right: '' },
  60. { style: '', left: '', right: '' },
  61. { style: '', left: '', right: '' },
  62. { style: '', left: '', right: '' }
  63. ],
  64. result: {
  65. result: 1,
  66. winNickName: '',
  67. lostNickName: '',
  68. left: '',
  69. right: '',
  70. img: '',
  71. cell: 0,
  72. },
  73. intoTopAnimation: {},
  74. intoBottomAnimation: {},
  75. radarAnimationStatus: 0.2,
  76. radarAnimation1: {},
  77. radarAnimation2: {},
  78. radarAnimation3: {},
  79. radarAnimation4: {},
  80. bgmusic: null,
  81. bgsound: null,
  82. voice: '../../resource/audio.png',
  83. dialogGeneral: {
  84. hidden: true,
  85. content: '恭喜你获得免费抓娃娃一次!',
  86. tap: '',
  87. openType: 'contact'
  88. },
  89. exclude: {
  90. src: '../../resource/ic_exclude_invalid.png',
  91. className: '',
  92. use: false,
  93. },
  94. },
  95. /**
  96. * 生命周期函数--监听页面加载
  97. */
  98. onLoad: function (options) {
  99. wx.hideShareMenu()
  100. wx.updateShareMenu({
  101. withShareTicket: true
  102. })
  103. this.setData({
  104. wxuser: app.globalData.userInfo
  105. })
  106. this.data.uid = options.uid || 0
  107. this.data.roomType = options.type || 1
  108. this.data.rankType = options.rankType || 1
  109. this.data.rankDan = options.rankDan || 0
  110. this.data.rankGameId = options.rankGameId || 0
  111. this.data.code = app.globalData.identify.shareIdentify || options.shareIdentify || ''
  112. this.start()
  113. if (app.globalData.user.wrongCard > 0 && !this.data.exclude.use) {
  114. this.data.exclude.src = '../../resource/ic_exclude_valid.png'
  115. this.data.exclude.className = 'valid'
  116. } else {
  117. this.data.exclude.src = '../../resource/ic_exclude_invalid.png'
  118. this.data.exclude.className = ''
  119. }
  120. this.setData({
  121. exclude: this.data.exclude
  122. })
  123. },
  124. /**
  125. * 生命周期函数--监听页面初次渲染完成
  126. */
  127. onReady: function () {
  128. context = wx.createCanvasContext('countDownCanvas')
  129. var w = app.globalData.sys.screenWidth
  130. this.data.countDownSize = Math.floor(w * 124 / 750)
  131. },
  132. /**
  133. * 生命周期函数--监听页面显示
  134. */
  135. onShow: function () {
  136. this.data.interval = setInterval(function (data) {
  137. this.myInterval()
  138. }.bind(this), 1000)
  139. this.data.roomType == 1 ? (this.data.pageStatus > 20 ? this.playBgMusic('pkAudioBgList', this.data.pkUser.pkAudioBgList) : null) : (this.data.pageStatus == 20 ? this.playBgMusic('pkAudioWaitList', this.data.pkUser.pkAudioWaitList) : null)
  140. },
  141. /**
  142. * 生命周期函数--监听页面隐藏
  143. */
  144. onHide: function () {
  145. if (this.data.interval) {
  146. clearInterval(this.data.interval)
  147. }
  148. this.stopBgMusic()
  149. },
  150. /**
  151. * 生命周期函数--监听页面卸载
  152. */
  153. onUnload: function () {
  154. this.data.pageStatus = 11
  155. this.leave()
  156. if (this.data.interval) {
  157. clearInterval(this.data.interval)
  158. }
  159. this.stopBgMusic();
  160. },
  161. playBgMusic: function (key, url) {
  162. if (url == null) {
  163. return
  164. }
  165. this.data.bgmusic || (this.data.bgmusic = new Audio())
  166. this.setData({
  167. voice: this.data.bgmusic.getSwitchStorage(this.data.roomType == 1 ? this.data.bgmusic.pk : this.data.bgmusic.friend) ? '../../resource/audio.png' : '../../resource/no_audio.png'
  168. })
  169. if (!this.data.bgmusic.isPlay()) {
  170. this.data.bgmusic.play({
  171. loop: true,
  172. storage: true,
  173. type: this.data.roomType == 1 ? this.data.bgmusic.pk : this.data.bgmusic.friend,
  174. key: key,
  175. srcs: url,
  176. })
  177. }
  178. },
  179. playBgSound: function (key, url) {
  180. if (url == null) {
  181. return
  182. }
  183. this.data.bgsound || (this.data.bgsound = new Audio())
  184. this.data.bgsound.play({
  185. storage: true,
  186. type: this.data.roomType == 1 ? this.data.bgsound.pk : this.data.bgsound.friend,
  187. key: key,
  188. srcs: url,
  189. })
  190. },
  191. stopBgMusic: function () {
  192. this.data.bgmusic == null ? null : this.data.bgmusic.stop()
  193. this.data.bgsound == null ? null : this.data.bgsound.stop()
  194. },
  195. doVoice: function () {
  196. if (lock.lockTapDelay()) {
  197. return
  198. }
  199. this.data.bgmusic || (this.data.bgmusic = new Audio())
  200. var t = this.data.roomType == 1 ? this.data.bgmusic.pk : this.data.bgmusic.friend
  201. var b = this.data.bgmusic.getSwitchStorage(t)
  202. this.data.bgmusic.setSwitchStorage(t, !b)
  203. this.setData({
  204. voice: !b ? '../../resource/audio.png' : '../../resource/no_audio.png'
  205. })
  206. if (!b) {
  207. this.data.pageStatus > 20 ? this.playBgMusic('pkAudioBgList', this.data.pkUser.pkAudioBgList) : this.playBgMusic('pkAudioWaitList', this.data.pkUser.pkAudioWaitList)
  208. } else {
  209. this.stopBgMusic()
  210. }
  211. },
  212. start: function () {
  213. this.data.num = 1
  214. this.data.pageStatus = 0
  215. this.setData({
  216. roomType: this.data.roomType,
  217. pointC: 0,
  218. joinPointC: 0,
  219. nextPointC: 0,
  220. nextJoinPointc: 0,
  221. pkIntoHidden: true,
  222. room: {
  223. hidden: true,
  224. hiddenQa: false,
  225. hiddenQaType: true,
  226. hiddenQaLast: true,
  227. hiddenQuestion: true,
  228. hiddenDown: true,
  229. hiddenResult: true,
  230. },
  231. gradeLeftStyle: "top: 456rpx",
  232. gradeRightStyle: "top: 456rpx",
  233. })
  234. if (this.data.roomType == 1) {
  235. this.setData({
  236. rankWaitHidden: false,
  237. friend: { hidden: true },
  238. })
  239. this.play({ rankDan: this.data.rankDan, rankGameId: this.data.rankGameId })
  240. } else if (this.data.roomType == 2) {
  241. this.setData({
  242. rankWaitHidden: true,
  243. friend: {
  244. hidden: false,
  245. hiddenRadar: false,
  246. hiddenLeave: true,
  247. shareText: '再次邀请'
  248. }
  249. })
  250. if (this.data.uid == 0 || app.server.userId == this.data.uid) {
  251. this.play()
  252. } else {
  253. //好友进入房间,调用加入接口
  254. this.join()
  255. }
  256. }
  257. },
  258. play: function (data) {
  259. net.connect({
  260. url: url.host + url.build_pk,
  261. method: 'POST',
  262. data: data,
  263. success: res => {
  264. if (res.data.code == 200) {
  265. this.data.pkUser = res.data.data;
  266. this.setData({
  267. pkUser: this.data.pkUser
  268. })
  269. this.data.pageStatus = 20;
  270. this.playBgMusic('pkAudioWaitList', this.data.pkUser.pkAudioWaitList)
  271. if (this.data.pkUser.messageRule && this.data.pkUser.messageRule.sessionFrom) {
  272. this.data.dialogGeneral = {
  273. ...this.data.dialogGeneral, ...common.messageRuleMapToObject(this.data.pkUser.messageRule)
  274. }
  275. this.data.dialogGeneral.content = this.data.dialogGeneral.cont
  276. }
  277. } else {
  278. this.netException(res.data.code, res.data.msg)
  279. }
  280. }
  281. }, true)
  282. },
  283. join: function () {
  284. this.data.isJoin = true
  285. net.connect({
  286. url: url.host + url.join_pk,
  287. method: 'POST',
  288. data: {
  289. code: this.data.code
  290. },
  291. success: res => {
  292. if (res.data.code == 200) {
  293. this.ready(res.data.data)
  294. } else {
  295. this.netException(res.data.code, res.data.msg)
  296. }
  297. }
  298. }, true)
  299. },
  300. wait: function () {
  301. net.connect({
  302. url: url.host + url.try_pk,
  303. success: res => {
  304. if (res.data.code == 200) {
  305. this.ready(res.data.data)
  306. } else {
  307. this.netException(res.data.code, res.data.msg)
  308. }
  309. }
  310. }, true)
  311. },
  312. ready: function (user) {
  313. this.data.pkUser = user
  314. this.data.gameId = user.gameId
  315. this.data.questionTypes = user.questionTags.split('#')
  316. app.globalData.user.cell -= user.cost
  317. app.globalData.user.playGameC++
  318. this.into()
  319. },
  320. leave: function () {
  321. net.connect({
  322. url: url.host + url.leave_pk,
  323. method: 'POST',
  324. success: res => {
  325. if (res.data.code == 200) {
  326. if (this.data.roomType == 1 && res.data.data > 0) {
  327. wx.showModal({
  328. title: '',
  329. content: '放弃对战,折损脑力值' + common.unit(res.data.data),
  330. confirmText: '我知道了',
  331. showCancel: false,
  332. success: res => {
  333. }
  334. })
  335. }
  336. }
  337. }
  338. })
  339. },
  340. myInterval: function () {
  341. /**
  342. * 页面当前状态
  343. * 20:等待加入
  344. * 21:展示双方对战信息
  345. * 22:等待获取题目 --> 23|28
  346. * 23:获取题目成功,展示题目类型
  347. * 24:展示题目内容,等待答题 --> 25|27
  348. * 25:提交答题...
  349. * 26:答题完毕,等待对手答题
  350. * 27:全部答题完毕,展示双方答案 --> 22
  351. * 28:显示结果
  352. *
  353. * 11:已退出
  354. *
  355. */
  356. if (this.data.pageStatus >= 24 && this.data.pageStatus <= 26) {
  357. //倒计时
  358. this.countDown()
  359. }
  360. if (this.data.pageStatus == 20) {
  361. //等待PK对手加入
  362. this.playRadarAnimation()
  363. this.wait()
  364. } else if (this.data.pageStatus == 22) {
  365. //获取题目
  366. this.getQuestion()
  367. } else if (this.data.pageStatus == 24) {
  368. //若超过倒计时无答题,自动获取下一题
  369. var nowTime = new Date().getTime() + app.globalData.timeDifference
  370. var endTime = this.data.question.beginTime + this.data.question.countDown * 1000
  371. if (nowTime >= endTime) {
  372. this.getOtherAnswer()
  373. }
  374. } else if (this.data.pageStatus == 26) {
  375. //答题后,等待对手答题
  376. this.getOtherAnswer()
  377. }
  378. },
  379. countDown: function () {
  380. this.data.countDown--
  381. if (this.data.countDown >= 0) {
  382. this.setData({
  383. countDown: this.data.countDown
  384. })
  385. this.playCountDownAnimation(this.data.countDown)
  386. if (this.data.countDown == 3) {
  387. this.playBgSound('qCountDownList', app.server.config.audio.qCountDownList)
  388. }
  389. //当倒计时是1秒到0秒时,检查结束时间,根据差值显示
  390. if (this.data.countDown == 1) {
  391. var nowTime = new Date().getTime() + app.globalData.timeDifference
  392. var endTime = this.data.question.beginTime + this.data.question.countDown * 1000
  393. setTimeout(function () {
  394. this.setData({
  395. countDown: 0
  396. })
  397. this.playCountDownAnimation(0)
  398. }.bind(this), endTime - nowTime)
  399. }
  400. }
  401. },
  402. playCountDownAnimation: function (countDown) {
  403. //绘画 圆圈倒计时动画
  404. var size = this.data.countDownSize
  405. var center = size / 2
  406. var end = countDown > 7 ? (0.2 * (10 - countDown) + 1.51) * Math.PI : (8 - countDown) * 0.1875 * Math.PI
  407. context.setStrokeStyle("#483b8c")
  408. context.setLineWidth(10)
  409. context.beginPath()
  410. context.arc(center, center, size / 2 - 10, 0, 2 * Math.PI, true)
  411. context.stroke()
  412. context.setStrokeStyle("#bfbdff")
  413. context.setLineWidth(5)
  414. context.setLineCap('round')
  415. context.beginPath()
  416. context.arc(center, center, size / 2 - 10, 1.5 * Math.PI, end, true)
  417. context.stroke()
  418. context.draw()
  419. },
  420. playRadarAnimation: function () {
  421. //播放雷达动画
  422. var animation1 = wx.createAnimation()
  423. animation1.scale(this.data.radarAnimationStatus).step({ duration: 900 })
  424. var animation2 = wx.createAnimation()
  425. animation2.scale(this.data.radarAnimationStatus).step({ duration: 800 })
  426. var animation3 = wx.createAnimation()
  427. animation3.scale(this.data.radarAnimationStatus).step({ duration: 700 })
  428. var animation4 = wx.createAnimation()
  429. animation4.scale(this.data.radarAnimationStatus).step({ duration: 600 })
  430. this.setData({
  431. radarAnimation1: animation1.export(),
  432. radarAnimation2: animation2.export(),
  433. radarAnimation3: animation3.export(),
  434. radarAnimation4: animation4.export(),
  435. })
  436. this.data.radarAnimationStatus = this.data.radarAnimationStatus == 1 ? 0.2 : 1
  437. },
  438. playIntoAnimation: function () {
  439. //PK动画 top
  440. var topAnimation = wx.createAnimation()
  441. topAnimation.translate(0, 0).step({ duration: 500, timingFunction: 'ease-out' })
  442. //PK动画 bottom
  443. var bottomAnimation = wx.createAnimation()
  444. bottomAnimation.translate(0, 0).step({ duration: 500, timingFunction: 'ease-out' })
  445. this.setData({
  446. intoTopAnimation: topAnimation.export(),
  447. intoBottomAnimation: bottomAnimation.export()
  448. })
  449. },
  450. into: function () {
  451. //匹配到对手,进入双方信息页面
  452. this.data.pageStatus = 21;
  453. this.stopBgMusic();
  454. this.setData({
  455. friend: { hidden: true },
  456. rankWaitHidden: true,
  457. pkIntoHidden: false,
  458. pkUser: this.data.pkUser
  459. })
  460. setTimeout(function () {
  461. this.playIntoAnimation()
  462. }.bind(this), 300)
  463. setTimeout(function () {
  464. this.toRoom()
  465. this.data.pageStatus >= 21 ? this.playBgMusic('pkAudioBgList', this.data.pkUser.pkAudioBgList) : null
  466. }.bind(this), 2000)
  467. },
  468. toRoom: function () {
  469. //进入正式PK页面
  470. this.data.room.hidden = false
  471. this.data.room.hiddenQa = false
  472. this.setData({
  473. pkIntoHidden: true,
  474. room: this.data.room
  475. })
  476. this.showQuestionType()
  477. setTimeout(function () {
  478. this.data.pageStatus = 22;
  479. }.bind(this), 1800)
  480. },
  481. tagA: function () {
  482. this.answer(0);
  483. },
  484. tagB: function () {
  485. this.answer(1);
  486. },
  487. tagC: function () {
  488. this.answer(2);
  489. },
  490. tagD: function () {
  491. this.answer(3);
  492. },
  493. updateGradeUI: function (count, index, isOther) {
  494. //双方答题结束,展示答题结果
  495. var isJoin = (this.data.isJoin && !isOther) || (!this.data.isJoin && isOther)
  496. if (isJoin) {
  497. //参与者
  498. var isRight = (count - this.data.joinPointC) > 0
  499. this.data.nextJoinPointC = count
  500. if (isRight) {
  501. this.data.abcd[index].style = 'answer-show answer-right'
  502. this.data.abcd[index].right = '../../resource/ic_pk_right.png'
  503. } else {
  504. this.data.abcd[index].style = 'answer-show answer-error'
  505. this.data.abcd[index].right = '../../resource/ic_pk_wrong.png'
  506. }
  507. } else {
  508. //发起者
  509. var isRight = (count - this.data.pointC) > 0
  510. this.data.nextPointC = count
  511. if (isRight) {
  512. this.data.abcd[index].style = 'answer-show answer-right'
  513. this.data.abcd[index].left = '../../resource/ic_pk_right.png'
  514. } else {
  515. this.data.abcd[index].style = 'answer-show answer-error'
  516. this.data.abcd[index].left = '../../resource/ic_pk_wrong.png'
  517. }
  518. }
  519. },
  520. updateRightAnswer: function (answer) {
  521. if (answer == null) {
  522. return
  523. }
  524. var index = this.getAnswerIndex(answer)
  525. this.data.abcd[index].style = 'answer-show answer-right'
  526. },
  527. updateGradeUIByTime: function () {
  528. if (this.data.pointC < this.data.nextPointC) {
  529. var ntc1 = new NumberTimeChange({
  530. start: this.data.pointC,
  531. end: this.data.nextPointC,
  532. update: n => {
  533. this.updateChildGradeUIByTime(n, true)
  534. },
  535. complete: n => {
  536. this.updateChildGradeUIByTime(n, true)
  537. }
  538. })
  539. }
  540. if (this.data.joinPointC < this.data.nextJoinPointC) {
  541. var ntc2 = new NumberTimeChange({
  542. start: this.data.joinPointC,
  543. end: this.data.nextJoinPointC,
  544. update: n => {
  545. this.updateChildGradeUIByTime(n, false)
  546. },
  547. complete: n => {
  548. this.updateChildGradeUIByTime(n, false)
  549. }
  550. })
  551. }
  552. },
  553. updateChildGradeUIByTime: function (count, isLeft) {
  554. var grade = 456 - (count * 456 / this.data.pkUser.fullPointC)
  555. grade = grade < 0 ? 0 : parseInt(grade)
  556. var gradeStyle = "top:" + grade + "rpx;"
  557. if (isLeft) {
  558. this.setData({
  559. pointC: count,
  560. gradeLeftStyle: gradeStyle,
  561. })
  562. } else {
  563. this.setData({
  564. joinPointC: count,
  565. gradeRightStyle: gradeStyle,
  566. })
  567. }
  568. },
  569. doExclude: function () {
  570. if (this.data.pageStatus != 24) {
  571. return;
  572. }
  573. if (this.data.exclude.use) {
  574. return;
  575. }
  576. this.setData({
  577. exclude: {
  578. src: '../../resource/ic_exclude_invalid.png',
  579. className: '',
  580. use: true,
  581. }
  582. })
  583. //使用排错卡
  584. net.connect({
  585. url: url.host + url.pk_exclude,
  586. method: 'POST',
  587. data: {
  588. gameId: this.data.gameId,
  589. n: this.data.num - 1
  590. },
  591. success: res => {
  592. if (res.data.code !== 200) {
  593. return;
  594. }
  595. res.data.data.map(function (value) {
  596. app.log(value);
  597. var index = this.getAnswerIndex(value);
  598. this.data.abcd[index].style = 'answer-show answer-exclude'
  599. }.bind(this));
  600. this.setData({
  601. abcd: this.data.abcd
  602. })
  603. }
  604. })
  605. },
  606. getAnswerIndex: function (answer) {
  607. var index = 0
  608. if (answer === this.data.question.a) {
  609. index = 0
  610. } else if (answer === this.data.question.b) {
  611. index = 1
  612. } else if (answer === this.data.question.c) {
  613. index = 2
  614. } else if (answer === this.data.question.d) {
  615. index = 3
  616. }
  617. return index
  618. },
  619. doMixQuestion: function () {
  620. var arr = new Array(4)
  621. arr[0] = this.data.question.a
  622. arr[1] = this.data.question.b
  623. arr[2] = this.data.question.c
  624. arr[3] = this.data.question.d
  625. arr.sort(function (a, b) { return Math.random() >= 0.5 ? 1 : -1 })
  626. this.data.question.a = arr[0]
  627. this.data.question.b = arr[1]
  628. this.data.question.c = arr[2]
  629. this.data.question.d = arr[3]
  630. },
  631. showQuestionType: function () {
  632. if (this.data.num > this.data.pkUser.questionC) {
  633. return
  634. } else if (this.data.num < this.data.pkUser.questionC) {
  635. this.data.room.hiddenQaLast = true
  636. } else {
  637. this.data.room.hiddenQaLast = false
  638. }
  639. this.data.room.hiddenQaType = false
  640. this.data.room.hiddenQuestion = true
  641. this.data.room.hiddenDown = true
  642. this.data.questionType = common.getQuestionType(this.data.questionTypes[this.data.num - 1])
  643. this.setData({
  644. num: this.data.num,
  645. questionType: this.data.questionType,
  646. room: this.data.room,
  647. abcd: [
  648. { style: '', left: '', right: '' },
  649. { style: '', left: '', right: '' },
  650. { style: '', left: '', right: '' },
  651. { style: '', left: '', right: '' }
  652. ]
  653. })
  654. },
  655. showQuestion: function () {
  656. this.data.pageStatus = 24
  657. var nowTime = new Date().getTime() + app.globalData.timeDifference
  658. var endTime = this.data.question.beginTime + this.data.question.countDown * 1000
  659. this.data.countDown = Math.ceil((endTime - nowTime) / 1000)
  660. this.data.countDown = (this.data.countDown < 0 || this.data.countDown > 10) ? 10 : this.data.countDown
  661. this.data.room.hiddenQaType = true
  662. this.data.room.hiddenQaLast = true
  663. this.data.room.hiddenQuestion = false
  664. this.data.room.hiddenDown = false
  665. //混淆答案顺序
  666. this.doMixQuestion()
  667. this.setData({
  668. countDown: this.data.countDown,
  669. question: this.data.question,
  670. room: this.data.room,
  671. abcd: [
  672. { style: 'answer-show', left: '', right: '' },
  673. { style: 'answer-show', left: '', right: '' },
  674. { style: 'answer-show', left: '', right: '' },
  675. { style: 'answer-show', left: '', right: '' }
  676. ]
  677. })
  678. this.playCountDownAnimation(this.data.countDown)
  679. },
  680. getQuestion: function () {
  681. if (this.data.num > this.data.pkUser.questionC) {
  682. this.getResult()
  683. return
  684. }
  685. if (this.data.pageStatus != 22 && this.data.pageStatus != 27) {
  686. return
  687. }
  688. net.connect({
  689. url: url.host + url.pk_question,
  690. data: {
  691. gameId: this.data.gameId,
  692. n: this.data.num
  693. },
  694. success: res => {
  695. if (res.data.code == 200) {
  696. //状态锁,防止重复两次增加题目序号
  697. if (this.data.pageStatus != 22 && this.data.pageStatus != 27) {
  698. return
  699. }
  700. this.data.pageStatus = 23;
  701. this.data.num = this.data.num + 1
  702. this.data.question = res.data.data
  703. this.showQuestion()
  704. } else {
  705. if (this.data.pageStatus == 27) {
  706. //展示双方答案后获取题目失效,重新获取题目
  707. this.data.pageStatus = 22
  708. }
  709. this.netException(res.data.code, res.data.msg)
  710. }
  711. }
  712. }, true)
  713. },
  714. answer: function (index) {
  715. if (lock.lockTapDelay()) {
  716. return
  717. }
  718. //判断是否超过答题时间
  719. var nowTime = new Date().getTime() + app.globalData.timeDifference
  720. var endTime = this.data.question.beginTime + this.data.question.countDown * 1000
  721. if (nowTime >= endTime) {
  722. return
  723. }
  724. //防止重复点击
  725. if (this.data.pageStatus != 24) {
  726. return
  727. }
  728. this.data.pageStatus = 25;
  729. //设置选择样式
  730. this.data.abcd[index] = {
  731. style: 'answer-show answer-ed',
  732. left: '',
  733. right: '',
  734. }
  735. this.setData({
  736. abcd: this.data.abcd
  737. })
  738. //查询答案
  739. var answer
  740. switch (index) {
  741. case 0:
  742. answer = this.data.question.a
  743. break
  744. case 1:
  745. answer = this.data.question.b
  746. break
  747. case 2:
  748. answer = this.data.question.c
  749. break
  750. case 3:
  751. answer = this.data.question.d
  752. break
  753. default:
  754. break
  755. }
  756. net.connect({
  757. url: url.host + url.pk_answer,
  758. method: 'POST',
  759. data: {
  760. gameId: this.data.gameId,
  761. questionId: this.data.question.questionId,
  762. optionContent: answer,
  763. },
  764. success: res => {
  765. if (res.data.code == 200) {
  766. //隐藏其他答案
  767. this.data.abcd = [
  768. { style: '', left: '', right: '' },
  769. { style: '', left: '', right: '' },
  770. { style: '', left: '', right: '' },
  771. { style: '', left: '', right: '' }
  772. ]
  773. this.data.pageStatus = 26;
  774. this.updateGradeUI(res.data.data.pointC, index, false)
  775. } else {
  776. if (res.data.code == 500) {
  777. //若答题失败,恢复状态,可继续答题
  778. this.data.pageStatus == 24
  779. this.data.abcd[index] = {
  780. style: 'answer-show',
  781. left: '',
  782. right: '',
  783. }
  784. wx.showToast({
  785. title: res.data.msg,
  786. })
  787. return
  788. }
  789. this.netException(res.data.code, res.data.msg)
  790. }
  791. }
  792. }, true)
  793. },
  794. getOtherAnswer: function () {
  795. if (lock.lockTapDelay()) {
  796. return
  797. }
  798. if (this.data.pageStatus != 26 && this.data.pageStatus != 24) {
  799. return
  800. }
  801. if (this.data.pageStatus == 24) {
  802. //隐藏其他答案
  803. this.data.abcd = [
  804. { style: '', left: '', right: '' },
  805. { style: '', left: '', right: '' },
  806. { style: '', left: '', right: '' },
  807. { style: '', left: '', right: '' }
  808. ]
  809. }
  810. //获取对手的分数
  811. net.connect({
  812. url: url.host + url.pk_other_answer,
  813. data: {
  814. gameId: this.data.gameId,
  815. questionId: this.data.question.questionId,
  816. },
  817. success: res => {
  818. if (res.data.code == 200) {
  819. if (this.data.pageStatus != 26 && this.data.pageStatus != 24) {
  820. return
  821. }
  822. this.data.pageStatus = 27;
  823. var content = res.data.data.optionContent
  824. if (content != null) {
  825. //获得答案下标,刷新按钮样式
  826. var index = this.getAnswerIndex(content)
  827. this.updateGradeUI(res.data.data.pointC, index, true)
  828. }
  829. this.updateRightAnswer(res.data.data.correctContent)
  830. this.setData({
  831. abcd: this.data.abcd,
  832. })
  833. //播放答题音效
  834. var isRight = this.data.isJoin ? this.data.joinPointC < this.data.nextJoinPointC : this.data.pointC < this.data.nextPointC
  835. isRight ? this.playBgSound('qCorrectList', app.server.config.audio.qCorrectList) : this.data.num > this.data.pkUser.questionC ? this.playBgSound('qLastWrongList', app.server.config.audio.qLastWrongList) : this.playBgSound('qWronglist', app.server.config.audio.qWronglist)
  836. //执行更新分数动画
  837. this.updateGradeUIByTime()
  838. setTimeout(function () {
  839. if (this.data.num <= this.data.pkUser.questionC) {
  840. this.showQuestionType()
  841. }
  842. setTimeout(function () {
  843. //获取下一题
  844. this.getQuestion()
  845. }.bind(this), 1200)
  846. }.bind(this), 1000)
  847. } else {
  848. this.netException(res.data.code, res.data.msg)
  849. }
  850. }
  851. }, true)
  852. },
  853. getResult: function () {
  854. //获取比赛结果
  855. net.connect({
  856. url: url.host + url.pk_result,
  857. method: 'POST',
  858. data: {
  859. gameId: this.data.gameId
  860. },
  861. success: res => {
  862. if (res.data.code == 200) {
  863. this.data.pageStatus = 28;
  864. this.showResult(res.data.data)
  865. } else {
  866. this.netException(res.data.code, res.data.msg)
  867. }
  868. }
  869. }, true)
  870. },
  871. showResult: function (data) {
  872. var result = data.result
  873. if (this.data.roomType == 1) {
  874. //排位赛,获取段位变化信息
  875. var gameId = this.data.rankType === 1 ? app.globalData.rankInfo.gameId : app.globalData.rankOneInfo.gameId
  876. net.connect({
  877. url: url.host + url.get_my_ranking,
  878. data: {
  879. gameId: gameId
  880. },
  881. success: res => {
  882. if (res.data.code == 200) {
  883. this.data.rankType === 1 ? app.server.rankStarMap.main.rank = res.data.data.rank : app.server.rankStarMap.topic.rank = res.data.data.rank
  884. var arr = common.starToFragment(app.server.config.rankDanList, res.data.data.starC)
  885. var rankDan = app.server.config.rankDanList[arr[0]]
  886. this.setData({
  887. shareCost: common.unit(rankDan.cellRankCost)
  888. })
  889. }
  890. }
  891. }, true)
  892. if (result != 1) {
  893. //若不是平手,修改列表页星星数
  894. var arr = getCurrentPages()
  895. var page = arr[1]
  896. page.getFragmentsResult(result == 2)
  897. }
  898. //是否达到最大分享数,隐藏分享提示
  899. if (this.data.rankType == 1) {
  900. if (app.server.rankStarMap.main.shareRankPkResult >= app.server.config.shareRankPkResultMax) {
  901. this.data.room.hiddenShareTip = true
  902. }
  903. } else {
  904. if (app.server.rankStarMap.topic.shareRankPkResult >= app.server.config.shareRankPkResultMax) {
  905. this.data.room.hiddenShareTip = true
  906. }
  907. }
  908. if (this.data.pkUser.messageRule && this.data.pkUser.messageRule.sessionFrom) {
  909. this.showGeneralDialog()
  910. }
  911. } else {
  912. //好友赛 隐藏分享脑力值提示
  913. this.data.room.hiddenShareTip = true
  914. }
  915. //显示结果
  916. this.data.result.result = result
  917. if (result == 1) {
  918. //平手
  919. this.data.result.winNickName = this.data.pkUser.nickName
  920. this.data.result.lostNickName = this.data.pkUser.joinNickName
  921. this.data.result.img = '../../resource/ic_pk_tie.png'
  922. } else if (result == 2) {
  923. //赢
  924. if (data.reverse == 2) {
  925. //绝杀
  926. this.data.result.img = '../../resource/ic_pk_last.png'
  927. } else {
  928. this.data.result.img = '../../resource/ic_pk_success.png'
  929. }
  930. if (data.giveup == 2) {
  931. //对方放弃
  932. this.data.result.img = '../../resource/ic_pk_give_up.png'
  933. }
  934. this.data.result.cell = data.addCell
  935. app.globalData.user.cell = app.globalData.user.cell + data.addCell
  936. if (this.data.isJoin) {
  937. this.data.result.winNickName = this.data.pkUser.joinNickName
  938. this.data.result.lostNickName = this.data.pkUser.nickName
  939. this.data.result.right = 'grade-win'
  940. } else {
  941. this.data.result.winNickName = this.data.pkUser.nickName
  942. this.data.result.lostNickName = this.data.pkUser.joinNickName
  943. this.data.result.left = 'grade-win'
  944. }
  945. } else {
  946. //输
  947. this.data.result.img = '../../resource/ic_pk_failure.png'
  948. if (this.data.isJoin) {
  949. this.data.result.winNickName = this.data.pkUser.nickName
  950. this.data.result.lostNickName = this.data.pkUser.joinNickName
  951. this.data.result.left = 'grade-win'
  952. } else {
  953. this.data.result.winNickName = this.data.pkUser.joinNickName
  954. this.data.result.lostNickName = this.data.pkUser.nickName
  955. this.data.result.right = 'grade-win'
  956. }
  957. }
  958. this.data.room.hiddenQa = true
  959. this.data.room.hiddenDown = true
  960. this.data.room.hiddenResult = false
  961. this.setData({
  962. result: this.data.result,
  963. room: this.data.room,
  964. roomType: this.data.roomType,
  965. isJoin: this.data.isJoin
  966. })
  967. this.data.bgmusic == null ? null : this.data.bgmusic.stop()
  968. },
  969. againRank: function () {
  970. wx.navigateBack({
  971. })
  972. },
  973. toHome: function () {
  974. var arr = getCurrentPages()
  975. if (arr.length == 1) {
  976. wx.redirectTo({
  977. url: '/pages/home/home',
  978. })
  979. } else {
  980. wx.navigateBack({
  981. })
  982. }
  983. },
  984. netException: function (code, msg) {
  985. if (code == 20102) {
  986. if (this.data.pageStatus > 20) {
  987. //PK已开始,对手离开
  988. this.getResult()
  989. } else {
  990. if (this.data.roomType == 1) {
  991. this.pkOver(msg)
  992. } else {
  993. //正在等待,发起者离开
  994. this.setData({
  995. friend: {
  996. hidden: false,
  997. hiddenRadar: true,
  998. hiddenLeave: false,
  999. shareText: '我要邀请'
  1000. }
  1001. })
  1002. }
  1003. }
  1004. this.data.pageStatus = 28
  1005. } else if (code == 20107) {
  1006. this.start()
  1007. } else if (code == 20106) {
  1008. //重试,无操作
  1009. } else {
  1010. this.pkOver(msg)
  1011. }
  1012. },
  1013. pkOver: function (msg) {
  1014. if (this.data.pageStatus == 11) {
  1015. return
  1016. }
  1017. wx.showToast({
  1018. title: msg,
  1019. icon: 'none'
  1020. })
  1021. setTimeout(function () {
  1022. if (this.data.pageStatus == 11) {
  1023. return
  1024. }
  1025. this.toHome()
  1026. }.bind(this), 1500)
  1027. },
  1028. //通用弹框,通知弹框
  1029. showGeneralDialog: function () {
  1030. this.data.dialogGeneral.hidden = false
  1031. this.setData({
  1032. dialogGeneral: this.data.dialogGeneral
  1033. })
  1034. },
  1035. hideGeneralDialog: function () {
  1036. this.data.dialogGeneral.hidden = true
  1037. this.setData({
  1038. dialogGeneral: this.data.dialogGeneral
  1039. })
  1040. },
  1041. /**
  1042. * 用户点击右上角分享
  1043. */
  1044. onShareAppMessage: function (res) {
  1045. if ('menu' === res.from) {
  1046. return common.shareAction({
  1047. title: this.data.wxuser.nickName + '正在用脑力征服世界并掠夺现金红包哦',
  1048. path: '',
  1049. type: 1,
  1050. success: res => {
  1051. }
  1052. })
  1053. } else {
  1054. if (res.target.id === 'againInvite') {
  1055. return common.shareAction({
  1056. title: app.globalData.userInfo.nickName + '向你发起脑力挑战,输的罚XXX!',
  1057. path: '&page=pk&type=2&uid=' + app.server.userId + '&',
  1058. imageUrl: '../../resource/share_friend_pk.png',
  1059. type: 9,
  1060. success: res => {
  1061. this.data.uid = 0
  1062. this.start()
  1063. }
  1064. })
  1065. } else if (res.target.id === 'share') {
  1066. var t = this.data.roomType == 1 ? 13 : 10
  1067. var title
  1068. if (this.data.result.result == 2) {
  1069. title = this.data.result.winNickName + '刚刚战胜了' + this.data.result.lostNickName + ',谁敢来战?'
  1070. } else {
  1071. title = this.data.result.lostNickName + '刚刚马失前蹄败给了' + this.data.result.winNickName + ',谁敢迎战?'
  1072. }
  1073. return common.shareAction({
  1074. title: title,
  1075. path: '',
  1076. gameId: this.data.gameId,
  1077. type: t,
  1078. rankType: this.data.rankType,
  1079. success: res => {
  1080. if (res.data.data.addCell > 0) {
  1081. wx.showToast({
  1082. title: '分享+' + common.unit(res.data.data.addCell) + '脑力值',
  1083. icon: 'none',
  1084. duration: 2500
  1085. })
  1086. }
  1087. }
  1088. })
  1089. }
  1090. }
  1091. }
  1092. })