common.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. // if(responsedata.info.user){
  62. // $("#username").html(responsedata.info.user.name);
  63. // $("#avatar").html('<img src="' + responsedata.info.user.avatar + '" class="layui-circle">');
  64. // }
  65. // if(data.info.user.user_id){
  66. // $("#py1_avatar").html('<img src="' + data.info.user.avatar+ '" class="layui-circle">');
  67. // $("#py1_username").html(data.info.user.name + '(我)');
  68. // }
  69. this.tips(data.info.user.name+"加入了房间")
  70. if(data.info.user.user_id!=this.userInfo.user_id){
  71. $("#py2_avatar").html('<img src="' + data.info.user.avatar+ '" class="layui-circle">');
  72. $("#py2_username").html(data.info.user.name);
  73. _this.data.playerId=data.info.user.user_id;
  74. }
  75. },round_end:function(data){
  76. var _this=this;
  77. // if(data.info.anwser==_this.data.option_id){
  78. // $(".cur_answer").addClass("cur_true");
  79. // }
  80. for (var i = 0; i < data.info.players_answer.length; i++) {
  81. if(data.info.players_answer[i].user_id==_this.userInfo.playerId){
  82. //对方的答案id
  83. _this.data.player_answerId=data.info.players_answer[i].question_id;
  84. }
  85. }
  86. $("#question .item").each(function(){
  87. if($(this).attr("option_id")==data.info.anwser){
  88. $(this).addClass("cur_true");
  89. }
  90. if($(this).attr("option_id")==_this.data.player_answerId){
  91. //对方答案
  92. $(this).addClass("player_cur");
  93. }
  94. })
  95. },onSocket:function(e){
  96. var data=JSON.parse(e.data);
  97. var _this=this;
  98. var type=data.type;
  99. switch(type){
  100. case 'init':
  101. _this.data.client_id=data.client_id;
  102. _this.bdSocket(function(res){
  103. _this.data.isReady=1;
  104. console.log(res.msg);
  105. });
  106. break;
  107. case 'player_join':
  108. _this.player_join(data);
  109. break;
  110. case 'question':
  111. _this.loadQuestion(data);
  112. break;
  113. case 'round_end':
  114. _this.round_end(data);
  115. console.log("答题结束");
  116. break;
  117. case "answer":
  118. if(_this.userInfo.user_id==data.user_id){
  119. _this.increment($("#py1_score"),data.score);
  120. }else if(_this.data.playerId==data.user_id){
  121. _this.increment($("#py2_score"),data.score);
  122. }
  123. break;
  124. case 'game_end':
  125. $(".g-doc .g-inner").addClass("end");
  126. $("#Jvs").hide();
  127. console.log("游戏结束");
  128. break;
  129. }
  130. //_this.data.question_id
  131. },startGame:function(){
  132. if(this.data.isReady=="1")
  133. this.Join(function(res){
  134. $(".welcome").hide();
  135. $(".gstart").show();
  136. common.tips(res.msg);
  137. });
  138. else this.tips("登录失败");
  139. },initSocket:function(){
  140. var _this=this;
  141. var ws = new WebSocket(this.data.WebSocket);
  142. ws.onmessage=function(e){
  143. _this.onSocket(e);
  144. }
  145. },loadQuestion(responsedata) {
  146. console.log(responsedata);
  147. var _this=this;
  148. _this.data.isAnswer=false;
  149. _this.data.question_id= responsedata.info.question.question_id;
  150. $("#Jqtitle").html(responsedata.info.question.title );
  151. var question="";
  152. var answers = responsedata.info.options
  153. for(var i in answers){
  154. 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>';
  155. }
  156. $("#question").html(question);
  157. $("#Jpage").html(responsedata.info.questions_count+"/5");
  158. $(".g-doc .g-inner").addClass("doing");
  159. // is_end = responsedata.info.question.is_end;
  160. this.daoshu();
  161. $("#question .item").on("touchend",function(){
  162. var l = $("#question .item.cur_answer").length;
  163. if(l>0)return;
  164. $(this).addClass("cur_answer");
  165. _this.data.option_id=$(this).attr("data-option_id");
  166. if(!_this.data.isAnswer){
  167. _this.data.isAnswer=true;
  168. _this.Answer(function(){
  169. });
  170. }
  171. })
  172. },listen:function(){
  173. var _this=this;
  174. $(".rulebtn").on("touchend",function(){
  175. $(".popwind,.popbg").fadeIn(200);
  176. })
  177. $(".close").on("touchend",function(){
  178. $(".popwind,.popbg").fadeOut(200);
  179. })
  180. $("#start_game").on("touchend",function(){
  181. _this.startGame();
  182. })
  183. $("#Jagain").on("touchend",function(){
  184. window.location.href=window.location.href;
  185. })
  186. },increment:function(bdclass,num){
  187. var obj=$("."+bdclass);
  188. var text=obj.text();
  189. obj.prop('Counter',text).animate({
  190. Counter: num
  191. },{
  192. duration: 1500,
  193. easing: 'swing',
  194. step: function (now){
  195. $(this).text(Math.ceil(now));
  196. }
  197. });
  198. },daoshu:function(argument) {
  199. var _this=this;
  200. clearInterval(this.data.ts);
  201. this.data.sum = 9;
  202. this.data.angle = 0;
  203. var leftContent = document.querySelector(".left-content");
  204. var rightContent = document.querySelector(".right-content");
  205. var textCircle = document.querySelector(".text-circle");
  206. leftContent.setAttribute('style', 'transform: rotate(0deg)');
  207. rightContent.setAttribute('style', 'transform: rotate(0deg)');
  208. this.data.ts = setInterval(function() {
  209. if (_this.data.sum >= 0) textCircle.innerHTML = _this.data.sum;
  210. _this.data.sum = _this.data.sum - 1;
  211. $("#Jvs .con").show();
  212. $("#Jvs .tit").html("抢答中");
  213. _this.data.angle += 36;
  214. if (_this.data.angle <= 360) {
  215. if (_this.data.angle > 180) {
  216. rightContent.setAttribute('style', 'transform: rotate(' + (_this.data.angle - 180) + 'deg)')
  217. } else {
  218. leftContent.setAttribute('style', 'transform: rotate(' + _this.data.angle + 'deg)')
  219. }
  220. }
  221. if(_this.data.sum<3){
  222. $(".text-circle").addClass("rubberBand");
  223. $(".text-circle").addClass("animated");
  224. setTimeout(function(){
  225. $(".text-circle").removeClass("rubberBand");
  226. $(".text-circle").removeClass("animated");
  227. },900)
  228. }
  229. if (_this.data.sum < -1) {
  230. if(!_this.data.isAnswer)
  231. _this.Answer();
  232. _this.data.sum = 9;
  233. _this.data.angle = 0;
  234. leftContent.setAttribute('style', 'transform: rotate(0deg)');
  235. rightContent.setAttribute('style', 'transform: rotate(0deg)')
  236. }
  237. }, 1000)
  238. }, tips: function (text, time) {
  239. //弹窗工具
  240. time = time ? time : 1500;
  241. var para = document.createElement("p"); //创建新的<p> 元素
  242. para.innerHTML = text;
  243. para.setAttribute("class", "poptis");
  244. document.body.appendChild(para);
  245. para.style.marginLeft = -para.offsetWidth / 2+"px";
  246. setTimeout(function () {
  247. document.body.removeChild(para);
  248. }, time);
  249. },init:function(){
  250. var _this=this;
  251. this.listen();
  252. var ycy =this.GET("ycy");
  253. if(ycy){
  254. var b64 = new Base64();
  255. ycy=b64.decode(ycy);
  256. ycy=decodeURIComponent(ycy);
  257. ycy= JSON.parse(ycy);
  258. this.data.uid=ycy.uid;
  259. this.data.mt=ycy.mt;
  260. }
  261. if( this.GET("user_id")){
  262. this.data.uid=this.GET("user_id");
  263. this.data.mt=this.GET("mt");
  264. }
  265. this.getUserInfo(function(res){
  266. _this.userInfo=res.info;
  267. $("#py1_username").html(res.info.name);
  268. $("#py1_avatar").html('<img src="' + res.info.avatar + '" class="layui-circle">');
  269. _this.initSocket();
  270. })
  271. }
  272. }
  273. window.common=common;