common={ data:{ baseurl:"http://183.234.61.252:8090/", getUserInfo:"Home/User/Info", register:"", WebSocket:"", bdSocket:"Home/User/Bind", Join:"Home/User/Join", Answer:"Home/User/Answer", Join:"Home/Game/Join", uid:"", },userInfo:{ },GET: function (name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if (r != null) return unescape(r[2]); return null; },getUserInfo:function(fn){ var _this=this; $.ajax({ type: "GET", url: this.data.baseurl+this.data.getUserInfo, data: {user_id:this.data.uid , mt:this.data.mt }, dataType: "json", success: function(data){ fn&&fn(data); } }); },bdSocket:function(fn){ var _this=this; $.ajax({ type: "POST", url: this.data.baseurl+this.data.bdSocket, data: {client_id:this.data.client_id }, dataType: "json", success: function(data){ fn&&fn(data); } }); },Join:function(fn){ var _this=this; $.ajax({ type: "GET", url: this.data.baseurl+this.data.Join, dataType: "json", success: function(data){ fn&&fn(data); } }); },Answer:function(fn){ var _this=this; $.ajax({ type: "GET", url: this.data.baseurl+this.data.Answer, data:{question_id:_this.data.question_id,option_id:_this.data.option_id}, dataType: "json", success: function(data){ fn&&fn(data); } }); },player_join:function(data){ // if(responsedata.info.user){ // $("#username").html(responsedata.info.user.name); // $("#avatar").html(''); // } // if(data.info.user.user_id){ // $("#py1_avatar").html(''); // $("#py1_username").html(data.info.user.name + '(我)'); // } if(data.info.user.user_id){ $("#py2_avatar").html(''); $("#py2_username").html(data.info.user.name); } },onSocket:function(e){ var data=JSON.parse(e.data); var _this=this; var type=data.type; switch(type){ case 'init': _this.data.client_id=data.client_id; _this.bdSocket(function(res){ _this.data.isReady=1; console.log(res.msg); }); break; case 'player_join': _this.player_join(data); break; case 'question': _this.loadQuestion(data); break; case 'round_end': break; case 'game_end': $(".g-doc .g-inner").addClass("end"); $("#Jvs").hide(); break; } //_this.data.question_id },startGame:function(){ if(this.data.isReady=="1") this.Join(function(res){ $(".welcome").hide(); $(".gstart").show(); common.tips(res.msg); }); else this.tips("登录失败"); },initSocket:function(){ var _this=this; var ws = new WebSocket("ws://183.234.61.252:8282"); ws.onmessage=function(e){ _this.onSocket(e); } },loadQuestion(responsedata) { console.log(responsedata); _this.data.question_id= responsedata.info.question.question_id; $("#Jqtitle").html(responsedata.info.question.title ); var question=""; var answers = responsedata.info.options for(var i in answers){ question += '
  • '+answers[i].title+'
  • '; } $("#question").html(question); $("#Jpage").html(responsedata.info.questions_count+"/5"); $(".g-doc .g-inner").addClass("doing"); // is_end = responsedata.info.question.is_end; this.daoshu(); },listen:function(){ var _this=this; $(".rulebtn").on("touchend",function(){ $(".popwind,.popbg").fadeIn(200); }) $(".close").on("touchend",function(){ $(".popwind,.popbg").fadeOut(200); }) $("#start_game").on("touchend",function(){ _this.startGame(); }) $("#question .item").on("touchend",function(){ var l = $("#question .item.cur_answer").length; if(l>0)return; _this.data.option_id=this.attr("data-option_id"); this.addClass("cur_answer"); _this.Answer(); }) },daoshu:function(argument) { clearInterval(ts); this.data.sum = 9; this.data.angle = 0; var leftContent = document.querySelector(".left-content"); var rightContent = document.querySelector(".right-content"); var textCircle = document.querySelector(".text-circle"); leftContent.setAttribute('style', 'transform: rotate(0deg)'); rightContent.setAttribute('style', 'transform: rotate(0deg)'); this.data.ts = setInterval(function() { if (sum >= 0) textCircle.innerHTML = sum; sum = sum - 1; $("#Jvs .con").show(); $("#Jvs .tit").html("抢答中"); angle += 36; if (angle <= 360) { if (angle > 180) { rightContent.setAttribute('style', 'transform: rotate(' + (angle - 180) + 'deg)') } else { leftContent.setAttribute('style', 'transform: rotate(' + angle + 'deg)') } } if (sum < -1) { answer(); sum = 9; angle = 0; leftContent.setAttribute('style', 'transform: rotate(0deg)'); rightContent.setAttribute('style', 'transform: rotate(0deg)') } }, 1000) }, tips: function (text, time) { //弹窗工具 time = time ? time : 1500; var para = document.createElement("p"); //创建新的

    元素 para.innerHTML = text; para.setAttribute("class", "poptis"); document.body.appendChild(para); para.style.marginLeft = -para.offsetWidth / 2+"px"; setTimeout(function () { document.body.removeChild(para); }, time); },init:function(){ var _this=this; this.listen(); var ycy =this.GET("ycy"); if(ycy){ var b64 = new Base64(); ycy=b64.decode(ycy); ycy=decodeURIComponent(ycy); ycy= JSON.parse(ycy); this.data.uid=ycy.uid; this.data.mt=ycy.mt; } if( this.GET("user_id")){ this.data.uid=this.GET("user_id"); this.data.mt=this.GET("mt"); } this.getUserInfo(function(res){ _this.userInfo=res.info; $("#py1_username").html(res.info.name); $("#py1_avatar").html(''); _this.initSocket(); }) } } window.common=common;