123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104 |
- // pages/pk/pk.js
- import NumberTimeChange from '../../utils/ntc.js'
- import Audio from '../../utils/audio.js'
- var app = getApp()
- var lock = require('../../utils/lock.js')
- var net = require('../../utils/net.js')
- var url = require('../../utils/url.js')
- var common = require('../../utils/common.js')
- var context
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- interval: null,
- wxuser: {},
- uid: 0,
- code: '',
- isJoin: false,
- rankGameId: null,
- gameId: 0,
- rankDan: 0,
- rankType: 1,
- roomType: 1,
- pageStatus: 0,
- pkUser: {},
- num: 1,
- questionTypes: [],
- questionType: '',
- question: {},
- pointC: 0,
- joinPointC: 0,
- nextPointC: 0,
- nextJoinPointc: 0,
- countDown: 10,
- countDownSize: 0,
- shareCost: 0,
- friend: {
- hidden: true,
- hiddenRadar: false,
- hiddenLeave: true,
- shareText: '再次邀请'
- },
- rankWaitHidden: true,
- pkIntoHidden: true,
- room: {
- hidden: true,
- hiddenQa: false,
- hiddenQaType: true,
- hiddenQaLast: true,
- hiddenQuestion: true,
- hiddenDown: true,
- hiddenResult: true,
- hiddenShareTip: false
- },
- gradeLeftStyle: "top: 456rpx",
- gradeRightStyle: "top: 456rpx",
- abcd: [
- { style: '', left: '', right: '' },
- { style: '', left: '', right: '' },
- { style: '', left: '', right: '' },
- { style: '', left: '', right: '' }
- ],
- result: {
- result: 1,
- winNickName: '',
- lostNickName: '',
- left: '',
- right: '',
- img: '',
- cell: 0,
- },
- intoTopAnimation: {},
- intoBottomAnimation: {},
- radarAnimationStatus: 0.2,
- radarAnimation1: {},
- radarAnimation2: {},
- radarAnimation3: {},
- radarAnimation4: {},
- bgmusic: null,
- bgsound: null,
- voice: '../../resource/audio.png',
- dialogGeneral: {
- hidden: true,
- content: '恭喜你获得免费抓娃娃一次!',
- tap: '',
- openType: 'contact'
- },
- exclude: {
- src: '../../resource/ic_exclude_invalid.png',
- className: '',
- use: false,
- },
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- wx.hideShareMenu()
- wx.updateShareMenu({
- withShareTicket: true
- })
- this.setData({
- wxuser: app.globalData.userInfo
- })
- this.data.uid = options.uid || 0
- this.data.roomType = options.type || 1
- this.data.rankType = options.rankType || 1
- this.data.rankDan = options.rankDan || 0
- this.data.rankGameId = options.rankGameId || 0
- this.data.code = app.globalData.identify.shareIdentify || options.shareIdentify || ''
- this.start()
- if (app.globalData.user.wrongCard > 0 && !this.data.exclude.use) {
- this.data.exclude.src = '../../resource/ic_exclude_valid.png'
- this.data.exclude.className = 'valid'
- } else {
- this.data.exclude.src = '../../resource/ic_exclude_invalid.png'
- this.data.exclude.className = ''
- }
- this.setData({
- exclude: this.data.exclude
- })
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady: function () {
- context = wx.createCanvasContext('countDownCanvas')
- var w = app.globalData.sys.screenWidth
- this.data.countDownSize = Math.floor(w * 124 / 750)
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow: function () {
- this.data.interval = setInterval(function (data) {
- this.myInterval()
- }.bind(this), 1000)
- 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)
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide: function () {
- if (this.data.interval) {
- clearInterval(this.data.interval)
- }
- this.stopBgMusic()
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload: function () {
- this.data.pageStatus = 11
- this.leave()
- if (this.data.interval) {
- clearInterval(this.data.interval)
- }
- this.stopBgMusic();
- },
- playBgMusic: function (key, url) {
- if (url == null) {
- return
- }
- this.data.bgmusic || (this.data.bgmusic = new Audio())
- this.setData({
- voice: this.data.bgmusic.getSwitchStorage(this.data.roomType == 1 ? this.data.bgmusic.pk : this.data.bgmusic.friend) ? '../../resource/audio.png' : '../../resource/no_audio.png'
- })
- if (!this.data.bgmusic.isPlay()) {
- this.data.bgmusic.play({
- loop: true,
- storage: true,
- type: this.data.roomType == 1 ? this.data.bgmusic.pk : this.data.bgmusic.friend,
- key: key,
- srcs: url,
- })
- }
- },
- playBgSound: function (key, url) {
- if (url == null) {
- return
- }
- this.data.bgsound || (this.data.bgsound = new Audio())
- this.data.bgsound.play({
- storage: true,
- type: this.data.roomType == 1 ? this.data.bgsound.pk : this.data.bgsound.friend,
- key: key,
- srcs: url,
- })
- },
- stopBgMusic: function () {
- this.data.bgmusic == null ? null : this.data.bgmusic.stop()
- this.data.bgsound == null ? null : this.data.bgsound.stop()
- },
- doVoice: function () {
- if (lock.lockTapDelay()) {
- return
- }
- this.data.bgmusic || (this.data.bgmusic = new Audio())
- var t = this.data.roomType == 1 ? this.data.bgmusic.pk : this.data.bgmusic.friend
- var b = this.data.bgmusic.getSwitchStorage(t)
- this.data.bgmusic.setSwitchStorage(t, !b)
- this.setData({
- voice: !b ? '../../resource/audio.png' : '../../resource/no_audio.png'
- })
- if (!b) {
- this.data.pageStatus > 20 ? this.playBgMusic('pkAudioBgList', this.data.pkUser.pkAudioBgList) : this.playBgMusic('pkAudioWaitList', this.data.pkUser.pkAudioWaitList)
- } else {
- this.stopBgMusic()
- }
- },
- start: function () {
- this.data.num = 1
- this.data.pageStatus = 0
- this.setData({
- roomType: this.data.roomType,
- pointC: 0,
- joinPointC: 0,
- nextPointC: 0,
- nextJoinPointc: 0,
- pkIntoHidden: true,
- room: {
- hidden: true,
- hiddenQa: false,
- hiddenQaType: true,
- hiddenQaLast: true,
- hiddenQuestion: true,
- hiddenDown: true,
- hiddenResult: true,
- },
- gradeLeftStyle: "top: 456rpx",
- gradeRightStyle: "top: 456rpx",
- })
- if (this.data.roomType == 1) {
- this.setData({
- rankWaitHidden: false,
- friend: { hidden: true },
- })
- this.play({ rankDan: this.data.rankDan, rankGameId: this.data.rankGameId })
- } else if (this.data.roomType == 2) {
- this.setData({
- rankWaitHidden: true,
- friend: {
- hidden: false,
- hiddenRadar: false,
- hiddenLeave: true,
- shareText: '再次邀请'
- }
- })
- if (this.data.uid == 0 || app.server.userId == this.data.uid) {
- this.play()
- } else {
- //好友进入房间,调用加入接口
- this.join()
- }
- }
- },
- play: function (data) {
- net.connect({
- url: url.host + url.build_pk,
- method: 'POST',
- data: data,
- success: res => {
- if (res.data.code == 200) {
- this.data.pkUser = res.data.data;
- this.setData({
- pkUser: this.data.pkUser
- })
- this.data.pageStatus = 20;
- this.playBgMusic('pkAudioWaitList', this.data.pkUser.pkAudioWaitList)
- if (this.data.pkUser.messageRule && this.data.pkUser.messageRule.sessionFrom) {
- this.data.dialogGeneral = {
- ...this.data.dialogGeneral, ...common.messageRuleMapToObject(this.data.pkUser.messageRule)
- }
- this.data.dialogGeneral.content = this.data.dialogGeneral.cont
- }
- } else {
- this.netException(res.data.code, res.data.msg)
- }
- }
- }, true)
- },
- join: function () {
- this.data.isJoin = true
- net.connect({
- url: url.host + url.join_pk,
- method: 'POST',
- data: {
- code: this.data.code
- },
- success: res => {
- if (res.data.code == 200) {
- this.ready(res.data.data)
- } else {
- this.netException(res.data.code, res.data.msg)
- }
- }
- }, true)
- },
- wait: function () {
- net.connect({
- url: url.host + url.try_pk,
- success: res => {
- if (res.data.code == 200) {
- this.ready(res.data.data)
- } else {
- this.netException(res.data.code, res.data.msg)
- }
- }
- }, true)
- },
- ready: function (user) {
- this.data.pkUser = user
- this.data.gameId = user.gameId
- this.data.questionTypes = user.questionTags.split('#')
- app.globalData.user.cell -= user.cost
- app.globalData.user.playGameC++
- this.into()
- },
- leave: function () {
- net.connect({
- url: url.host + url.leave_pk,
- method: 'POST',
- success: res => {
- if (res.data.code == 200) {
- if (this.data.roomType == 1 && res.data.data > 0) {
- wx.showModal({
- title: '',
- content: '放弃对战,折损脑力值' + common.unit(res.data.data),
- confirmText: '我知道了',
- showCancel: false,
- success: res => {
-
- }
- })
- }
- }
- }
- })
- },
- myInterval: function () {
- /**
- * 页面当前状态
- * 20:等待加入
- * 21:展示双方对战信息
- * 22:等待获取题目 --> 23|28
- * 23:获取题目成功,展示题目类型
- * 24:展示题目内容,等待答题 --> 25|27
- * 25:提交答题...
- * 26:答题完毕,等待对手答题
- * 27:全部答题完毕,展示双方答案 --> 22
- * 28:显示结果
- *
- * 11:已退出
- *
- */
- if (this.data.pageStatus >= 24 && this.data.pageStatus <= 26) {
- //倒计时
- this.countDown()
- }
- if (this.data.pageStatus == 20) {
- //等待PK对手加入
- this.playRadarAnimation()
- this.wait()
- } else if (this.data.pageStatus == 22) {
- //获取题目
- this.getQuestion()
- } else if (this.data.pageStatus == 24) {
- //若超过倒计时无答题,自动获取下一题
- var nowTime = new Date().getTime() + app.globalData.timeDifference
- var endTime = this.data.question.beginTime + this.data.question.countDown * 1000
- if (nowTime >= endTime) {
- this.getOtherAnswer()
- }
- } else if (this.data.pageStatus == 26) {
- //答题后,等待对手答题
- this.getOtherAnswer()
- }
- },
- countDown: function () {
- this.data.countDown--
- if (this.data.countDown >= 0) {
- this.setData({
- countDown: this.data.countDown
- })
- this.playCountDownAnimation(this.data.countDown)
- if (this.data.countDown == 3) {
- this.playBgSound('qCountDownList', app.server.config.audio.qCountDownList)
- }
- //当倒计时是1秒到0秒时,检查结束时间,根据差值显示
- if (this.data.countDown == 1) {
- var nowTime = new Date().getTime() + app.globalData.timeDifference
- var endTime = this.data.question.beginTime + this.data.question.countDown * 1000
- setTimeout(function () {
- this.setData({
- countDown: 0
- })
- this.playCountDownAnimation(0)
- }.bind(this), endTime - nowTime)
- }
- }
- },
- playCountDownAnimation: function (countDown) {
- //绘画 圆圈倒计时动画
- var size = this.data.countDownSize
- var center = size / 2
- var end = countDown > 7 ? (0.2 * (10 - countDown) + 1.51) * Math.PI : (8 - countDown) * 0.1875 * Math.PI
- context.setStrokeStyle("#483b8c")
- context.setLineWidth(10)
- context.beginPath()
- context.arc(center, center, size / 2 - 10, 0, 2 * Math.PI, true)
- context.stroke()
- context.setStrokeStyle("#bfbdff")
- context.setLineWidth(5)
- context.setLineCap('round')
- context.beginPath()
- context.arc(center, center, size / 2 - 10, 1.5 * Math.PI, end, true)
- context.stroke()
- context.draw()
- },
- playRadarAnimation: function () {
- //播放雷达动画
- var animation1 = wx.createAnimation()
- animation1.scale(this.data.radarAnimationStatus).step({ duration: 900 })
- var animation2 = wx.createAnimation()
- animation2.scale(this.data.radarAnimationStatus).step({ duration: 800 })
- var animation3 = wx.createAnimation()
- animation3.scale(this.data.radarAnimationStatus).step({ duration: 700 })
- var animation4 = wx.createAnimation()
- animation4.scale(this.data.radarAnimationStatus).step({ duration: 600 })
- this.setData({
- radarAnimation1: animation1.export(),
- radarAnimation2: animation2.export(),
- radarAnimation3: animation3.export(),
- radarAnimation4: animation4.export(),
- })
- this.data.radarAnimationStatus = this.data.radarAnimationStatus == 1 ? 0.2 : 1
- },
- playIntoAnimation: function () {
- //PK动画 top
- var topAnimation = wx.createAnimation()
- topAnimation.translate(0, 0).step({ duration: 500, timingFunction: 'ease-out' })
- //PK动画 bottom
- var bottomAnimation = wx.createAnimation()
- bottomAnimation.translate(0, 0).step({ duration: 500, timingFunction: 'ease-out' })
- this.setData({
- intoTopAnimation: topAnimation.export(),
- intoBottomAnimation: bottomAnimation.export()
- })
- },
- into: function () {
- //匹配到对手,进入双方信息页面
- this.data.pageStatus = 21;
- this.stopBgMusic();
- this.setData({
- friend: { hidden: true },
- rankWaitHidden: true,
- pkIntoHidden: false,
- pkUser: this.data.pkUser
- })
- setTimeout(function () {
- this.playIntoAnimation()
- }.bind(this), 300)
- setTimeout(function () {
- this.toRoom()
- this.data.pageStatus >= 21 ? this.playBgMusic('pkAudioBgList', this.data.pkUser.pkAudioBgList) : null
- }.bind(this), 2000)
- },
- toRoom: function () {
- //进入正式PK页面
- this.data.room.hidden = false
- this.data.room.hiddenQa = false
- this.setData({
- pkIntoHidden: true,
- room: this.data.room
- })
- this.showQuestionType()
- setTimeout(function () {
- this.data.pageStatus = 22;
- }.bind(this), 1800)
- },
- tagA: function () {
- this.answer(0);
- },
- tagB: function () {
- this.answer(1);
- },
- tagC: function () {
- this.answer(2);
- },
- tagD: function () {
- this.answer(3);
- },
- updateGradeUI: function (count, index, isOther) {
- //双方答题结束,展示答题结果
- var isJoin = (this.data.isJoin && !isOther) || (!this.data.isJoin && isOther)
- if (isJoin) {
- //参与者
- var isRight = (count - this.data.joinPointC) > 0
- this.data.nextJoinPointC = count
- if (isRight) {
- this.data.abcd[index].style = 'answer-show answer-right'
- this.data.abcd[index].right = '../../resource/ic_pk_right.png'
- } else {
- this.data.abcd[index].style = 'answer-show answer-error'
- this.data.abcd[index].right = '../../resource/ic_pk_wrong.png'
- }
- } else {
- //发起者
- var isRight = (count - this.data.pointC) > 0
- this.data.nextPointC = count
- if (isRight) {
- this.data.abcd[index].style = 'answer-show answer-right'
- this.data.abcd[index].left = '../../resource/ic_pk_right.png'
- } else {
- this.data.abcd[index].style = 'answer-show answer-error'
- this.data.abcd[index].left = '../../resource/ic_pk_wrong.png'
- }
- }
- },
- updateRightAnswer: function (answer) {
- if (answer == null) {
- return
- }
- var index = this.getAnswerIndex(answer)
- this.data.abcd[index].style = 'answer-show answer-right'
- },
- updateGradeUIByTime: function () {
- if (this.data.pointC < this.data.nextPointC) {
- var ntc1 = new NumberTimeChange({
- start: this.data.pointC,
- end: this.data.nextPointC,
- update: n => {
- this.updateChildGradeUIByTime(n, true)
- },
- complete: n => {
- this.updateChildGradeUIByTime(n, true)
- }
- })
- }
- if (this.data.joinPointC < this.data.nextJoinPointC) {
- var ntc2 = new NumberTimeChange({
- start: this.data.joinPointC,
- end: this.data.nextJoinPointC,
- update: n => {
- this.updateChildGradeUIByTime(n, false)
- },
- complete: n => {
- this.updateChildGradeUIByTime(n, false)
- }
- })
- }
- },
- updateChildGradeUIByTime: function (count, isLeft) {
- var grade = 456 - (count * 456 / this.data.pkUser.fullPointC)
- grade = grade < 0 ? 0 : parseInt(grade)
- var gradeStyle = "top:" + grade + "rpx;"
- if (isLeft) {
- this.setData({
- pointC: count,
- gradeLeftStyle: gradeStyle,
- })
- } else {
- this.setData({
- joinPointC: count,
- gradeRightStyle: gradeStyle,
- })
- }
- },
- doExclude: function () {
- if (this.data.pageStatus != 24) {
- return;
- }
- if (this.data.exclude.use) {
- return;
- }
- this.setData({
- exclude: {
- src: '../../resource/ic_exclude_invalid.png',
- className: '',
- use: true,
- }
- })
- //使用排错卡
- net.connect({
- url: url.host + url.pk_exclude,
- method: 'POST',
- data: {
- gameId: this.data.gameId,
- n: this.data.num - 1
- },
- success: res => {
- if (res.data.code !== 200) {
- return;
- }
- res.data.data.map(function (value) {
- app.log(value);
- var index = this.getAnswerIndex(value);
- this.data.abcd[index].style = 'answer-show answer-exclude'
- }.bind(this));
- this.setData({
- abcd: this.data.abcd
- })
- }
- })
- },
- getAnswerIndex: function (answer) {
- var index = 0
- if (answer === this.data.question.a) {
- index = 0
- } else if (answer === this.data.question.b) {
- index = 1
- } else if (answer === this.data.question.c) {
- index = 2
- } else if (answer === this.data.question.d) {
- index = 3
- }
- return index
- },
- doMixQuestion: function () {
- var arr = new Array(4)
- arr[0] = this.data.question.a
- arr[1] = this.data.question.b
- arr[2] = this.data.question.c
- arr[3] = this.data.question.d
- arr.sort(function (a, b) { return Math.random() >= 0.5 ? 1 : -1 })
- this.data.question.a = arr[0]
- this.data.question.b = arr[1]
- this.data.question.c = arr[2]
- this.data.question.d = arr[3]
- },
- showQuestionType: function () {
- if (this.data.num > this.data.pkUser.questionC) {
- return
- } else if (this.data.num < this.data.pkUser.questionC) {
- this.data.room.hiddenQaLast = true
- } else {
- this.data.room.hiddenQaLast = false
- }
- this.data.room.hiddenQaType = false
- this.data.room.hiddenQuestion = true
- this.data.room.hiddenDown = true
- this.data.questionType = common.getQuestionType(this.data.questionTypes[this.data.num - 1])
- this.setData({
- num: this.data.num,
- questionType: this.data.questionType,
- room: this.data.room,
- abcd: [
- { style: '', left: '', right: '' },
- { style: '', left: '', right: '' },
- { style: '', left: '', right: '' },
- { style: '', left: '', right: '' }
- ]
- })
- },
- showQuestion: function () {
- this.data.pageStatus = 24
- var nowTime = new Date().getTime() + app.globalData.timeDifference
- var endTime = this.data.question.beginTime + this.data.question.countDown * 1000
- this.data.countDown = Math.ceil((endTime - nowTime) / 1000)
- this.data.countDown = (this.data.countDown < 0 || this.data.countDown > 10) ? 10 : this.data.countDown
- this.data.room.hiddenQaType = true
- this.data.room.hiddenQaLast = true
- this.data.room.hiddenQuestion = false
- this.data.room.hiddenDown = false
- //混淆答案顺序
- this.doMixQuestion()
- this.setData({
- countDown: this.data.countDown,
- question: this.data.question,
- room: this.data.room,
- abcd: [
- { style: 'answer-show', left: '', right: '' },
- { style: 'answer-show', left: '', right: '' },
- { style: 'answer-show', left: '', right: '' },
- { style: 'answer-show', left: '', right: '' }
- ]
- })
- this.playCountDownAnimation(this.data.countDown)
- },
- getQuestion: function () {
- if (this.data.num > this.data.pkUser.questionC) {
- this.getResult()
- return
- }
- if (this.data.pageStatus != 22 && this.data.pageStatus != 27) {
- return
- }
- net.connect({
- url: url.host + url.pk_question,
- data: {
- gameId: this.data.gameId,
- n: this.data.num
- },
- success: res => {
- if (res.data.code == 200) {
- //状态锁,防止重复两次增加题目序号
- if (this.data.pageStatus != 22 && this.data.pageStatus != 27) {
- return
- }
- this.data.pageStatus = 23;
- this.data.num = this.data.num + 1
- this.data.question = res.data.data
- this.showQuestion()
- } else {
- if (this.data.pageStatus == 27) {
- //展示双方答案后获取题目失效,重新获取题目
- this.data.pageStatus = 22
- }
- this.netException(res.data.code, res.data.msg)
- }
- }
- }, true)
- },
- answer: function (index) {
- if (lock.lockTapDelay()) {
- return
- }
- //判断是否超过答题时间
- var nowTime = new Date().getTime() + app.globalData.timeDifference
- var endTime = this.data.question.beginTime + this.data.question.countDown * 1000
- if (nowTime >= endTime) {
- return
- }
- //防止重复点击
- if (this.data.pageStatus != 24) {
- return
- }
- this.data.pageStatus = 25;
- //设置选择样式
- this.data.abcd[index] = {
- style: 'answer-show answer-ed',
- left: '',
- right: '',
- }
- this.setData({
- abcd: this.data.abcd
- })
- //查询答案
- var answer
- switch (index) {
- case 0:
- answer = this.data.question.a
- break
- case 1:
- answer = this.data.question.b
- break
- case 2:
- answer = this.data.question.c
- break
- case 3:
- answer = this.data.question.d
- break
- default:
- break
- }
- net.connect({
- url: url.host + url.pk_answer,
- method: 'POST',
- data: {
- gameId: this.data.gameId,
- questionId: this.data.question.questionId,
- optionContent: answer,
- },
- success: res => {
- if (res.data.code == 200) {
- //隐藏其他答案
- this.data.abcd = [
- { style: '', left: '', right: '' },
- { style: '', left: '', right: '' },
- { style: '', left: '', right: '' },
- { style: '', left: '', right: '' }
- ]
- this.data.pageStatus = 26;
- this.updateGradeUI(res.data.data.pointC, index, false)
- } else {
- if (res.data.code == 500) {
- //若答题失败,恢复状态,可继续答题
- this.data.pageStatus == 24
- this.data.abcd[index] = {
- style: 'answer-show',
- left: '',
- right: '',
- }
- wx.showToast({
- title: res.data.msg,
- })
- return
- }
- this.netException(res.data.code, res.data.msg)
- }
- }
- }, true)
- },
- getOtherAnswer: function () {
- if (lock.lockTapDelay()) {
- return
- }
- if (this.data.pageStatus != 26 && this.data.pageStatus != 24) {
- return
- }
- if (this.data.pageStatus == 24) {
- //隐藏其他答案
- this.data.abcd = [
- { style: '', left: '', right: '' },
- { style: '', left: '', right: '' },
- { style: '', left: '', right: '' },
- { style: '', left: '', right: '' }
- ]
- }
- //获取对手的分数
- net.connect({
- url: url.host + url.pk_other_answer,
- data: {
- gameId: this.data.gameId,
- questionId: this.data.question.questionId,
- },
- success: res => {
- if (res.data.code == 200) {
- if (this.data.pageStatus != 26 && this.data.pageStatus != 24) {
- return
- }
- this.data.pageStatus = 27;
- var content = res.data.data.optionContent
- if (content != null) {
- //获得答案下标,刷新按钮样式
- var index = this.getAnswerIndex(content)
- this.updateGradeUI(res.data.data.pointC, index, true)
- }
- this.updateRightAnswer(res.data.data.correctContent)
- this.setData({
- abcd: this.data.abcd,
- })
- //播放答题音效
- var isRight = this.data.isJoin ? this.data.joinPointC < this.data.nextJoinPointC : this.data.pointC < this.data.nextPointC
- 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)
- //执行更新分数动画
- this.updateGradeUIByTime()
- setTimeout(function () {
- if (this.data.num <= this.data.pkUser.questionC) {
- this.showQuestionType()
- }
- setTimeout(function () {
- //获取下一题
- this.getQuestion()
- }.bind(this), 1200)
- }.bind(this), 1000)
- } else {
- this.netException(res.data.code, res.data.msg)
- }
- }
- }, true)
- },
- getResult: function () {
- //获取比赛结果
- net.connect({
- url: url.host + url.pk_result,
- method: 'POST',
- data: {
- gameId: this.data.gameId
- },
- success: res => {
- if (res.data.code == 200) {
- this.data.pageStatus = 28;
- this.showResult(res.data.data)
- } else {
- this.netException(res.data.code, res.data.msg)
- }
- }
- }, true)
- },
- showResult: function (data) {
- var result = data.result
- if (this.data.roomType == 1) {
- //排位赛,获取段位变化信息
- var gameId = this.data.rankType === 1 ? app.globalData.rankInfo.gameId : app.globalData.rankOneInfo.gameId
- net.connect({
- url: url.host + url.get_my_ranking,
- data: {
- gameId: gameId
- },
- success: res => {
- if (res.data.code == 200) {
- this.data.rankType === 1 ? app.server.rankStarMap.main.rank = res.data.data.rank : app.server.rankStarMap.topic.rank = res.data.data.rank
- var arr = common.starToFragment(app.server.config.rankDanList, res.data.data.starC)
- var rankDan = app.server.config.rankDanList[arr[0]]
- this.setData({
- shareCost: common.unit(rankDan.cellRankCost)
- })
- }
- }
- }, true)
- if (result != 1) {
- //若不是平手,修改列表页星星数
- var arr = getCurrentPages()
- var page = arr[1]
- page.getFragmentsResult(result == 2)
- }
- //是否达到最大分享数,隐藏分享提示
- if (this.data.rankType == 1) {
- if (app.server.rankStarMap.main.shareRankPkResult >= app.server.config.shareRankPkResultMax) {
- this.data.room.hiddenShareTip = true
- }
- } else {
- if (app.server.rankStarMap.topic.shareRankPkResult >= app.server.config.shareRankPkResultMax) {
- this.data.room.hiddenShareTip = true
- }
- }
- if (this.data.pkUser.messageRule && this.data.pkUser.messageRule.sessionFrom) {
- this.showGeneralDialog()
- }
- } else {
- //好友赛 隐藏分享脑力值提示
- this.data.room.hiddenShareTip = true
- }
- //显示结果
- this.data.result.result = result
- if (result == 1) {
- //平手
- this.data.result.winNickName = this.data.pkUser.nickName
- this.data.result.lostNickName = this.data.pkUser.joinNickName
- this.data.result.img = '../../resource/ic_pk_tie.png'
- } else if (result == 2) {
- //赢
- if (data.reverse == 2) {
- //绝杀
- this.data.result.img = '../../resource/ic_pk_last.png'
- } else {
- this.data.result.img = '../../resource/ic_pk_success.png'
- }
- if (data.giveup == 2) {
- //对方放弃
- this.data.result.img = '../../resource/ic_pk_give_up.png'
- }
- this.data.result.cell = data.addCell
- app.globalData.user.cell = app.globalData.user.cell + data.addCell
- if (this.data.isJoin) {
- this.data.result.winNickName = this.data.pkUser.joinNickName
- this.data.result.lostNickName = this.data.pkUser.nickName
- this.data.result.right = 'grade-win'
- } else {
- this.data.result.winNickName = this.data.pkUser.nickName
- this.data.result.lostNickName = this.data.pkUser.joinNickName
- this.data.result.left = 'grade-win'
- }
- } else {
- //输
- this.data.result.img = '../../resource/ic_pk_failure.png'
- if (this.data.isJoin) {
- this.data.result.winNickName = this.data.pkUser.nickName
- this.data.result.lostNickName = this.data.pkUser.joinNickName
- this.data.result.left = 'grade-win'
- } else {
- this.data.result.winNickName = this.data.pkUser.joinNickName
- this.data.result.lostNickName = this.data.pkUser.nickName
- this.data.result.right = 'grade-win'
- }
- }
- this.data.room.hiddenQa = true
- this.data.room.hiddenDown = true
- this.data.room.hiddenResult = false
- this.setData({
- result: this.data.result,
- room: this.data.room,
- roomType: this.data.roomType,
- isJoin: this.data.isJoin
- })
- this.data.bgmusic == null ? null : this.data.bgmusic.stop()
- },
- againRank: function () {
- wx.navigateBack({
- })
- },
- toHome: function () {
- var arr = getCurrentPages()
- if (arr.length == 1) {
- wx.redirectTo({
- url: '/pages/home/home',
- })
- } else {
- wx.navigateBack({
- })
- }
- },
- netException: function (code, msg) {
- if (code == 20102) {
- if (this.data.pageStatus > 20) {
- //PK已开始,对手离开
- this.getResult()
- } else {
- if (this.data.roomType == 1) {
- this.pkOver(msg)
- } else {
- //正在等待,发起者离开
- this.setData({
- friend: {
- hidden: false,
- hiddenRadar: true,
- hiddenLeave: false,
- shareText: '我要邀请'
- }
- })
- }
- }
- this.data.pageStatus = 28
- } else if (code == 20107) {
- this.start()
- } else if (code == 20106) {
- //重试,无操作
- } else {
- this.pkOver(msg)
- }
- },
- pkOver: function (msg) {
- if (this.data.pageStatus == 11) {
- return
- }
- wx.showToast({
- title: msg,
- icon: 'none'
- })
- setTimeout(function () {
- if (this.data.pageStatus == 11) {
- return
- }
- this.toHome()
- }.bind(this), 1500)
- },
- //通用弹框,通知弹框
- showGeneralDialog: function () {
- this.data.dialogGeneral.hidden = false
- this.setData({
- dialogGeneral: this.data.dialogGeneral
- })
- },
- hideGeneralDialog: function () {
- this.data.dialogGeneral.hidden = true
- this.setData({
- dialogGeneral: this.data.dialogGeneral
- })
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage: function (res) {
- if ('menu' === res.from) {
- return common.shareAction({
- title: this.data.wxuser.nickName + '正在用脑力征服世界并掠夺现金红包哦',
- path: '',
- type: 1,
- success: res => {
- }
- })
- } else {
- if (res.target.id === 'againInvite') {
- return common.shareAction({
- title: app.globalData.userInfo.nickName + '向你发起脑力挑战,输的罚XXX!',
- path: '&page=pk&type=2&uid=' + app.server.userId + '&',
- imageUrl: '../../resource/share_friend_pk.png',
- type: 9,
- success: res => {
- this.data.uid = 0
- this.start()
- }
- })
- } else if (res.target.id === 'share') {
- var t = this.data.roomType == 1 ? 13 : 10
- var title
- if (this.data.result.result == 2) {
- title = this.data.result.winNickName + '刚刚战胜了' + this.data.result.lostNickName + ',谁敢来战?'
- } else {
- title = this.data.result.lostNickName + '刚刚马失前蹄败给了' + this.data.result.winNickName + ',谁敢迎战?'
- }
- return common.shareAction({
- title: title,
- path: '',
- gameId: this.data.gameId,
- type: t,
- rankType: this.data.rankType,
- success: res => {
- if (res.data.data.addCell > 0) {
- wx.showToast({
- title: '分享+' + common.unit(res.data.data.addCell) + '脑力值',
- icon: 'none',
- duration: 2500
- })
- }
- }
- })
- }
- }
- }
- })
|