common.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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,anwser_time:_this.data.sum},
  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. if( parseInt($("#py1_score").html())==data.total_score){
  157. $("#question .cur_answer").addClass("cur_error");
  158. }else{
  159. $("#question .cur_answer").addClass("cur_true");
  160. }
  161. _this.increment($("#py1_score"),data.total_score);
  162. }else if(_this.userInfo.playerId==data.user_id){
  163. _this.increment($("#py2_score"),data.total_score);
  164. }
  165. break;
  166. case 'game_end':
  167. _this.game_end(data);
  168. console.log("游戏结束");
  169. break;
  170. }
  171. //_this.data.question_id
  172. },startGame:function(){
  173. if(this.data.isReady=="1")
  174. this.Join(function(res){
  175. $(".welcome").hide();
  176. $(".gstart").show();
  177. common.tips(res.msg);
  178. });
  179. else this.tips("正在登录");
  180. },initSocket:function(){
  181. var _this=this;
  182. var ws = new WebSocket(this.data.WebSocket);
  183. ws.onmessage=function(e){
  184. _this.onSocket(e);
  185. }
  186. },randomNum:function(minNum,maxNum){
  187. switch(arguments.length){
  188. case 1:
  189. return parseInt(Math.random()*minNum+1,10);
  190. break;
  191. case 2:
  192. return parseInt(Math.random()*(maxNum-minNum+1)+minNum,10);
  193. break;
  194. default:
  195. return 0;
  196. break;
  197. }
  198. },loadQuestion(responsedata) {
  199. console.log(responsedata);
  200. var _this=this;
  201. _this.data.isAnswer=false;
  202. _this.data.question_id= responsedata.info.question.question_id;
  203. $("#Jqtitle").html(responsedata.info.question.title );
  204. var question="";
  205. var answers = responsedata.info.options
  206. for(var i in answers){
  207. 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>';
  208. }
  209. $("#question").html(question);
  210. $("#Jpage").html(responsedata.info.sequence+"/5");
  211. $(".g-doc .g-inner").addClass("doing");
  212. // is_end = responsedata.info.question.is_end;
  213. this.daoshu();
  214. var animate=["zoomInDown","bounceIn","flash"];
  215. var r = _this.randomNum(0,2);
  216. $("#Jqtitle").addClass(animate[r]);
  217. $("#Jqtitle").addClass("animated");
  218. $("#question .item").on("touchend",function(){
  219. var l = $("#question .item.cur_answer").length;
  220. if(l>0)return;
  221. $(this).addClass("cur_answer");
  222. _this.data.option_id=$(this).attr("data-option_id");
  223. if(!_this.data.isAnswer){
  224. _this.data.isAnswer=true;
  225. _this.Answer(function(){
  226. });
  227. }
  228. })
  229. },listen:function(){
  230. var _this=this;
  231. $(".rulebtn").on("touchend",function(){
  232. $(".popwind,.popbg").fadeIn(200);
  233. })
  234. $(".close").on("touchend",function(){
  235. $(".popwind,.popbg").fadeOut(200);
  236. })
  237. $("#start_game").on("touchend",function(){
  238. _this.startGame();
  239. })
  240. $("#Jagain").on("touchend",function(){
  241. window.location.href=window.location.href;
  242. })
  243. },increment:function(obj,num){
  244. var text=obj.text();
  245. obj.prop('Counter',text).animate({
  246. Counter: num
  247. },{
  248. duration: 1500,
  249. easing: 'swing',
  250. step: function (now){
  251. $(this).text(Math.ceil(now));
  252. }
  253. });
  254. },daoshu:function(argument) {
  255. var _this=this;
  256. clearInterval(this.data.ts);
  257. $("#Jqtitle").attr("class","tit");
  258. this.data.sum = 9;
  259. this.data.angle = 0;
  260. var leftContent = document.querySelector(".left-content");
  261. var rightContent = document.querySelector(".right-content");
  262. var textCircle = document.querySelector(".text-circle");
  263. leftContent.setAttribute('style', 'transform: rotate(0deg)');
  264. rightContent.setAttribute('style', 'transform: rotate(0deg)');
  265. var html="<b class=''>"+_this.data.sum+"</b>";
  266. this.data.ts = setInterval(function() {
  267. if(_this.data.sum<4){
  268. html="<b class='daoshu tada animated'>"+_this.data.sum+"</b>";
  269. setTimeout(function(){
  270. $(".text-circle b").removeClass("daoshu");
  271. $(".text-circle b").removeClass("animated");
  272. },900)
  273. }else{
  274. html="<b class=''>"+_this.data.sum+"</b>";
  275. }
  276. if (_this.data.sum >= 0) textCircle.innerHTML = html;
  277. _this.data.sum = _this.data.sum - 1;
  278. $("#Jvs .con").show();
  279. $("#Jvs .tit").html("抢答中");
  280. _this.data.angle += 36;
  281. if (_this.data.angle <= 360) {
  282. if (_this.data.angle > 180) {
  283. rightContent.setAttribute('style', 'transform: rotate(' + (_this.data.angle - 180) + 'deg)')
  284. } else {
  285. leftContent.setAttribute('style', 'transform: rotate(' + _this.data.angle + 'deg)')
  286. }
  287. }
  288. if (_this.data.sum < 0) {
  289. if(!_this.data.isAnswer)
  290. _this.Answer();
  291. _this.data.sum = 9;
  292. _this.data.angle = 0;
  293. leftContent.setAttribute('style', 'transform: rotate(0deg)');
  294. rightContent.setAttribute('style', 'transform: rotate(0deg)');
  295. clearInterval(_this.data.ts);
  296. }
  297. }, 1000)
  298. }, tips: function (text, time) {
  299. //弹窗工具
  300. time = time ? time : 1500;
  301. var para = document.createElement("p"); //创建新的<p> 元素
  302. para.innerHTML = text;
  303. para.setAttribute("class", "poptis");
  304. document.body.appendChild(para);
  305. para.style.marginLeft = -para.offsetWidth / 2+"px";
  306. setTimeout(function () {
  307. document.body.removeChild(para);
  308. }, time);
  309. },init:function(){
  310. var _this=this;
  311. this.listen();
  312. var ycy =this.GET("ycy");
  313. if(ycy){
  314. var b64 = new Base64();
  315. ycy=b64.decode(ycy);
  316. ycy=decodeURIComponent(ycy);
  317. ycy= JSON.parse(ycy);
  318. this.data.uid=ycy.uid;
  319. this.data.mt=ycy.mt;
  320. }
  321. if( this.GET("user_id")){
  322. this.data.uid=this.GET("user_id");
  323. this.data.mt=this.GET("mt");
  324. }
  325. this.getUserInfo(function(res){
  326. _this.userInfo=res.info;
  327. $("#py1_username").html(res.info.name);
  328. $("#py1_avatar").html('<img src="' + res.info.avatar + '" class="layui-circle">');
  329. _this.initSocket();
  330. })
  331. }
  332. }
  333. window.common=common;