common.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. common={
  2. data:{
  3. baseurl:"http://183.234.61.252:8090/",
  4. getUserInfo:"Home/User/Info",
  5. WebSocket:"ws://183.234.61.252:8282",
  6. bdSocket:"Home/User/Bind",
  7. Join:"Home/User/Join",
  8. Answer:"Home/Game/Answer",
  9. Join:"Home/Game/Join",
  10. uid:"",
  11. option_id:0
  12. },userInfo:{
  13. },GET: function (name) {
  14. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  15. var r = window.location.search.substr(1).match(reg);
  16. if (r != null) return r[2];
  17. return null;
  18. },getUserInfo:function(fn){
  19. var _this=this;
  20. $.ajax({
  21. type: "GET",
  22. url: this.data.baseurl+this.data.getUserInfo,
  23. data: {user_id:this.data.uid , mt:this.data.mt },
  24. dataType: "json",
  25. success: function(data){
  26. fn&&fn(data);
  27. }
  28. });
  29. },bdSocket:function(fn){
  30. var _this=this;
  31. $.ajax({
  32. type: "POST",
  33. url: this.data.baseurl+this.data.bdSocket,
  34. data: {client_id:this.data.client_id },
  35. dataType: "json",
  36. success: function(data){
  37. fn&&fn(data);
  38. }
  39. });
  40. },Join:function(fn){
  41. var _this=this;
  42. $.ajax({
  43. type: "GET",
  44. url: this.data.baseurl+this.data.Join,
  45. dataType: "json",
  46. success: function(data){
  47. fn&&fn(data);
  48. }
  49. });
  50. },Answer:function(fn){
  51. var _this=this;
  52. $.ajax({
  53. type: "POST",
  54. url: this.data.baseurl+this.data.Answer,
  55. data:{question_id:_this.data.question_id,option_id:_this.data.option_id},
  56. dataType: "json",
  57. success: function(data){
  58. fn&&fn(data);
  59. }
  60. });
  61. },player_join:function(data){
  62. var _this=this;
  63. // if(responsedata.info.user){
  64. // $("#username").html(responsedata.info.user.name);
  65. // $("#avatar").html('<img src="' + responsedata.info.user.avatar + '" class="layui-circle">');
  66. // }
  67. // if(data.info.user.user_id){
  68. // $("#py1_avatar").html('<img src="' + data.info.user.avatar+ '" class="layui-circle">');
  69. // $("#py1_username").html(data.info.user.name + '(我)');
  70. // }
  71. this.tips(data.info.user.name+"加入了房间")
  72. for (var i = 0; i < data.info.players.length; i++) {
  73. if(data.info.players[i].user_id!=_this.userInfo.user_id){
  74. $("#py2_avatar").html('<img src="' + data.info.players[i].avatar+ '" class="layui-circle">');
  75. $("#py2_username").html(data.info.players[i].name);
  76. _this.userInfo.playerId=data.info.players[i].user_id;
  77. }
  78. }
  79. },round_end:function(data){
  80. var _this=this;
  81. // if(data.info.anwser==_this.data.option_id){
  82. // $(".cur_answer").addClass("cur_true");
  83. // }
  84. for (var i = 0; i < data.info.players_answer.length; i++) {
  85. if(data.info.players_answer[i].user_id==_this.userInfo.playerId){
  86. //对方的答案id
  87. _this.data.player_answerId=data.info.players_answer[i].option_id;
  88. }
  89. }
  90. $("#question .item").each(function(){
  91. if($(this).attr("data-option_id")==data.info.anwser){
  92. $(this).addClass("cur_true");
  93. }
  94. if($(this).attr("data-option_id")!=data.info.anwser&&$(this).hasClass("cur_answer")){
  95. $(this).addClass("cur_error");
  96. }
  97. if($(this).attr("data-option_id")==_this.data.player_answerId&&$(this).attr("data-option_id")==data.info.anwser){
  98. //对方答案
  99. $(this).addClass("player_true");
  100. }
  101. if($(this).attr("data-option_id")==_this.data.player_answerId&&$(this).attr("data-option_id")!=data.info.anwser){
  102. //对方答案
  103. $(this).addClass("player_error");
  104. }
  105. })
  106. clearInterval(this.data.ts);
  107. },game_end:function(){
  108. var _this=this;
  109. setTimeout(function(){
  110. var py1_score= parseInt($("#py1_score").text()) ;
  111. var py2_score=parseInt($("#py2_score").text());
  112. if(py1_score > py2_score){
  113. _this.tips('恭喜你获得胜利');
  114. $("#Jresult").removeClass("faild");
  115. $("#Jresult").addClass("success");
  116. $("#Jlastmsg").html("恭喜你获得胜利")
  117. }else if(py1_score == py2_score){
  118. _this.tips('打成平手');
  119. $("#Jlastmsg").html("打成平手")
  120. $("#Jresult").removeClass("success");
  121. $("#Jresult").removeClass("faild");
  122. $("#Jresult").html($("#avatar").html())
  123. }else{
  124. _this.tips('您输了比赛');
  125. $("#Jlastmsg").html("您输了比赛")
  126. $("#Jresult").removeClass("success");
  127. $("#Jresult").addClass("faild");
  128. }
  129. $(".g-doc .g-inner").addClass("end");
  130. $("#Jvs").hide();
  131. },3000)
  132. },onSocket:function(e){
  133. var data=JSON.parse(e.data);
  134. var _this=this;
  135. var type=data.type;
  136. switch(type){
  137. case 'init':
  138. _this.data.client_id=data.client_id;
  139. _this.bdSocket(function(res){
  140. _this.data.isReady=1;
  141. console.log(res.msg);
  142. });
  143. break;
  144. case 'player_join':
  145. _this.player_join(data);
  146. break;
  147. case 'question':
  148. _this.loadQuestion(data);
  149. break;
  150. case 'round_end':
  151. _this.round_end(data);
  152. console.log("答题结束");
  153. break;
  154. case "answer":
  155. if(_this.userInfo.user_id==data.user_id){
  156. _this.increment($("#py1_score"),data.total_score);
  157. }else if(_this.userInfo.playerId==data.user_id){
  158. _this.increment($("#py2_score"),data.total_score);
  159. }
  160. break;
  161. case 'game_end':
  162. _this.game_end(data);
  163. console.log("游戏结束");
  164. break;
  165. }
  166. //_this.data.question_id
  167. },startGame:function(){
  168. if(this.data.isReady=="1")
  169. this.Join(function(res){
  170. $(".welcome").hide();
  171. $(".gstart").show();
  172. common.tips(res.msg);
  173. });
  174. else this.tips("登录失败");
  175. },initSocket:function(){
  176. var _this=this;
  177. var ws = new WebSocket(this.data.WebSocket);
  178. ws.onmessage=function(e){
  179. _this.onSocket(e);
  180. }
  181. },loadQuestion(responsedata) {
  182. console.log(responsedata);
  183. var _this=this;
  184. _this.data.isAnswer=false;
  185. _this.data.question_id= responsedata.info.question.question_id;
  186. $("#Jqtitle").html(responsedata.info.question.title );
  187. var question="";
  188. var answers = responsedata.info.options
  189. for(var i in answers){
  190. question += '<li class="btn-2 item item'+i+' answer layui-btn layui-btn-primary" data-option_id="'+answers[i].option_id+'"><em></em>'+answers[i].title+'</li>';
  191. }
  192. $("#question").html(question);
  193. $("#Jpage").html(responsedata.info.sequence+"/5");
  194. $(".g-doc .g-inner").addClass("doing");
  195. // is_end = responsedata.info.question.is_end;
  196. this.daoshu();
  197. $("#question .item").on("touchend",function(){
  198. var l = $("#question .item.cur_answer").length;
  199. if(l>0)return;
  200. $(this).addClass("cur_answer");
  201. _this.data.option_id=$(this).attr("data-option_id");
  202. if(!_this.data.isAnswer){
  203. _this.data.isAnswer=true;
  204. _this.Answer(function(){
  205. });
  206. }
  207. })
  208. },listen:function(){
  209. var _this=this;
  210. $(".rulebtn").on("touchend",function(){
  211. $(".popwind,.popbg").fadeIn(200);
  212. })
  213. $(".close").on("touchend",function(){
  214. $(".popwind,.popbg").fadeOut(200);
  215. })
  216. $("#start_game").on("touchend",function(){
  217. _this.startGame();
  218. })
  219. $("#Jagain").on("touchend",function(){
  220. window.location.href=window.location.href;
  221. })
  222. },increment:function(obj,num){
  223. var text=obj.text();
  224. obj.prop('Counter',text).animate({
  225. Counter: num
  226. },{
  227. duration: 1500,
  228. easing: 'swing',
  229. step: function (now){
  230. $(this).text(Math.ceil(now));
  231. }
  232. });
  233. },daoshu:function(argument) {
  234. var _this=this;
  235. clearInterval(this.data.ts);
  236. this.data.sum = 9;
  237. this.data.angle = 0;
  238. var leftContent = document.querySelector(".left-content");
  239. var rightContent = document.querySelector(".right-content");
  240. var textCircle = document.querySelector(".text-circle");
  241. leftContent.setAttribute('style', 'transform: rotate(0deg)');
  242. rightContent.setAttribute('style', 'transform: rotate(0deg)');
  243. this.data.ts = setInterval(function() {
  244. if (_this.data.sum >= 0) textCircle.innerHTML = _this.data.sum;
  245. _this.data.sum = _this.data.sum - 1;
  246. $("#Jvs .con").show();
  247. $("#Jvs .tit").html("抢答中");
  248. _this.data.angle += 36;
  249. if (_this.data.angle <= 360) {
  250. if (_this.data.angle > 180) {
  251. rightContent.setAttribute('style', 'transform: rotate(' + (_this.data.angle - 180) + 'deg)')
  252. } else {
  253. leftContent.setAttribute('style', 'transform: rotate(' + _this.data.angle + 'deg)')
  254. }
  255. }
  256. if(_this.data.sum<3){
  257. $(".text-circle").addClass("rubberBand");
  258. $(".text-circle").addClass("animated");
  259. setTimeout(function(){
  260. $(".text-circle").removeClass("rubberBand");
  261. $(".text-circle").removeClass("animated");
  262. },900)
  263. }
  264. if (_this.data.sum < -1) {
  265. if(!_this.data.isAnswer)
  266. _this.Answer();
  267. _this.data.sum = 9;
  268. _this.data.angle = 0;
  269. leftContent.setAttribute('style', 'transform: rotate(0deg)');
  270. rightContent.setAttribute('style', 'transform: rotate(0deg)');
  271. clearInterval(_this.data.ts);
  272. }
  273. }, 1000)
  274. }, tips: function (text, time) {
  275. //弹窗工具
  276. time = time ? time : 1500;
  277. var para = document.createElement("p"); //创建新的<p> 元素
  278. para.innerHTML = text;
  279. para.setAttribute("class", "poptis");
  280. document.body.appendChild(para);
  281. para.style.marginLeft = -para.offsetWidth / 2+"px";
  282. setTimeout(function () {
  283. document.body.removeChild(para);
  284. }, time);
  285. },init:function(){
  286. var _this=this;
  287. this.listen();
  288. var ycy =this.GET("ycy");
  289. if(ycy){
  290. var b64 = new Base64();
  291. ycy=b64.decode(ycy);
  292. ycy=decodeURIComponent(ycy);
  293. ycy= JSON.parse(ycy);
  294. this.data.uid=ycy.uid;
  295. this.data.mt=ycy.mt;
  296. }
  297. if( this.GET("user_id")){
  298. this.data.uid=this.GET("user_id");
  299. this.data.mt=this.GET("mt");
  300. }
  301. this.getUserInfo(function(res){
  302. _this.userInfo=res.info;
  303. $("#py1_username").html(res.info.name);
  304. $("#py1_avatar").html('<img src="' + res.info.avatar + '" class="layui-circle">');
  305. _this.initSocket();
  306. })
  307. }
  308. }
  309. window.common=common;