common.js 11 KB

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