common.js 8.1 KB

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