123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- 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.uid },
- 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(responsedata){
- // if(responsedata.info.user){
- // $("#username").html(responsedata.info.user.name);
- // $("#avatar").html('<img src="' + responsedata.info.user.avatar + '" class="layui-circle">');
- // }
- if(responsedata.info.user){
- $("#py1_avatar").html('<img src="' + data.info.user.avatar+ '" class="layui-circle">');
- $("#py1_username").html(data.info.user.name + '(我)');
- }else if(responsedata.info.players){
- $("#py2_avatar").html('<img src="' + data.info.players.avatar + '" class="layui-circle">');
- $("#py2_username").html(data.info.players.name);
- }
-
- },onSocket:function(e){
- e=JSON.stringify(e);
- var _this=this;
- var type=e.type;
- switch(type){
- case 'init':
- _this.bdSocket(function(res){
- console.log(res.msg);
- });
- break;
- case 'player_join':
- _this.player_join(e);
- break;
- case 'question':
- _this.loadQuestion(e);
- 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.userInfo.user_id)
- this.Join();
- 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 += '<li class="btn-2 item item'+i+' answer layui-btn layui-btn-primary" data-option_id="'+answers[i].option_id+'">'+answers[i].title+'</li>';
- }
- $("#question").html(question);
- $("#Jpage").html(responsedata.info.questions_count+"/5")
- // is_end = responsedata.info.question.is_end;
- 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);
- sum = 9;
- angle = 0;
- leftContent.setAttribute('style', 'transform: rotate(0deg)');
- rightContent.setAttribute('style', 'transform: rotate(0deg)');
- 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"); //创建新的<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;
- _this.initSocket();
- })
-
-
- }
- }
- window.common=common;
|