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){
$("#py1_avatar").html('
');
$("#py1_username").html(data.info.user.name + '(我)');
}else if(data.info.players){
$("#py2_avatar").html('
');
$("#py2_username").html(data.info.players.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){
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 += '
元素 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;