common.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. if(data.info.players.length>=2){
  80. $(".gstart").addClass("ready");
  81. }
  82. },round_end:function(data){
  83. var _this=this;
  84. // if(data.info.anwser==_this.data.option_id){
  85. // $(".cur_answer").addClass("cur_true");
  86. // }
  87. for (var i = 0; i < data.info.players_answer.length; i++) {
  88. if(data.info.players_answer[i].user_id==_this.userInfo.playerId){
  89. //对方的答案id
  90. _this.data.player_answerId=data.info.players_answer[i].option_id;
  91. }
  92. }
  93. $("#question .item").each(function(){
  94. if($(this).attr("data-option_id")==data.info.anwser){
  95. $(this).addClass("cur_true");
  96. }
  97. if($(this).attr("data-option_id")!=data.info.anwser&&$(this).hasClass("cur_answer")){
  98. $(this).addClass("cur_error");
  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_true");
  103. }
  104. if($(this).attr("data-option_id")==_this.data.player_answerId&&$(this).attr("data-option_id")!=data.info.anwser){
  105. //对方答案
  106. $(this).addClass("player_error");
  107. }
  108. })
  109. clearInterval(this.data.ts);
  110. },game_end:function(){
  111. var _this=this;
  112. setTimeout(function(){
  113. var py1_score= parseInt($("#py1_score").text()) ;
  114. var py2_score=parseInt($("#py2_score").text());
  115. if(py1_score > py2_score){
  116. // _this.tips('恭喜你获得胜利');
  117. $("#Jresult").removeClass("faild");
  118. $("#Jresult").addClass("success");
  119. $("#Jlastmsg").html("恭喜你获得胜利")
  120. }else if(py1_score == py2_score){
  121. //_this.tips('打成平手');
  122. $("#Jlastmsg").html("打成平手")
  123. $("#Jresult").removeClass("success");
  124. $("#Jresult").removeClass("faild");
  125. $("#Jresult").html($("#avatar").html())
  126. }else{
  127. //_this.tips('您输了比赛');
  128. $("#Jlastmsg").html("您输了比赛")
  129. $("#Jresult").removeClass("success");
  130. $("#Jresult").addClass("faild");
  131. }
  132. $(".g-doc .g-inner").addClass("end");
  133. $("#Jvs").hide();
  134. },3000)
  135. },onSocket:function(e){
  136. var data=JSON.parse(e.data);
  137. var _this=this;
  138. var type=data.type;
  139. switch(type){
  140. case 'init':
  141. _this.data.client_id=data.client_id;
  142. _this.bdSocket(function(res){
  143. _this.data.isReady=1;
  144. console.log(res.msg);
  145. });
  146. break;
  147. case 'player_join':
  148. _this.player_join(data);
  149. break;
  150. case 'question':
  151. _this.loadQuestion(data);
  152. break;
  153. case 'round_end':
  154. _this.round_end(data);
  155. console.log("答题结束");
  156. break;
  157. case "answer":
  158. if(_this.userInfo.user_id==data.user_id){
  159. if( parseInt($("#py1_score").html())==data.total_score){
  160. $("#question .cur_answer").addClass("cur_error");
  161. }else{
  162. $("#question .cur_answer").addClass("cur_true");
  163. }
  164. _this.increment($("#py1_score"),data.total_score);
  165. }else if(_this.userInfo.playerId==data.user_id){
  166. _this.increment($("#py2_score"),data.total_score);
  167. }
  168. break;
  169. case 'game_end':
  170. _this.game_end(data);
  171. console.log("游戏结束");
  172. break;
  173. }
  174. //_this.data.question_id
  175. },startGame:function(){
  176. if(this.data.isReady=="1")
  177. this.Join(function(res){
  178. $(".welcome").hide();
  179. $(".gstart").show();
  180. common.tips(res.msg);
  181. });
  182. else this.tips("正在登录");
  183. },initSocket:function(){
  184. var _this=this;
  185. var ws = new WebSocket(this.data.WebSocket);
  186. ws.onmessage=function(e){
  187. _this.onSocket(e);
  188. }
  189. },randomNum:function(minNum,maxNum){
  190. switch(arguments.length){
  191. case 1:
  192. return parseInt(Math.random()*minNum+1,10);
  193. break;
  194. case 2:
  195. return parseInt(Math.random()*(maxNum-minNum+1)+minNum,10);
  196. break;
  197. default:
  198. return 0;
  199. break;
  200. }
  201. },loadQuestion(responsedata) {
  202. console.log(responsedata);
  203. var _this=this;
  204. _this.data.isAnswer=false;
  205. _this.data.question_id= responsedata.info.question.question_id;
  206. $("#Jqtitle").html(responsedata.info.question.title );
  207. var question="";
  208. var answers = responsedata.info.options
  209. for(var i in answers){
  210. 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>';
  211. }
  212. $("#question").html(question);
  213. $("#Jpage").html(responsedata.info.sequence+"/5");
  214. $(".g-doc .g-inner").addClass("doing");
  215. // is_end = responsedata.info.question.is_end;
  216. this.daoshu();
  217. var animate=["zoomInDown","bounceIn","flash"];
  218. var r = _this.randomNum(0,2);
  219. $("#Jqtitle").addClass(animate[r]);
  220. $("#Jqtitle").addClass("animated");
  221. $("#question .item").on("touchend",function(){
  222. var l = $("#question .item.cur_answer").length;
  223. if(l>0)return;
  224. $(this).addClass("cur_answer");
  225. _this.data.option_id=$(this).attr("data-option_id");
  226. if(!_this.data.isAnswer){
  227. _this.data.isAnswer=true;
  228. _this.Answer(function(){
  229. });
  230. }
  231. })
  232. },listen:function(){
  233. var _this=this;
  234. $(".rulebtn").on("touchend",function(){
  235. $(".popwind,.popbg").fadeIn(200);
  236. })
  237. $(".close").on("touchend",function(){
  238. $(".popwind,.popbg").fadeOut(200);
  239. })
  240. $("#start_game").on("touchend",function(){
  241. _this.startGame();
  242. })
  243. $("#Jagain").on("touchend",function(){
  244. window.location.href=window.location.href;
  245. })
  246. },increment:function(obj,num){
  247. var text=obj.text();
  248. obj.prop('Counter',text).animate({
  249. Counter: num
  250. },{
  251. duration: 1500,
  252. easing: 'swing',
  253. step: function (now){
  254. $(this).text(Math.ceil(now));
  255. }
  256. });
  257. },daoshu:function(argument) {
  258. var _this=this;
  259. clearInterval(this.data.ts);
  260. $("#Jqtitle").attr("class","tit");
  261. this.data.sum = 9;
  262. this.data.angle = 0;
  263. var leftContent = document.querySelector(".left-content");
  264. var rightContent = document.querySelector(".right-content");
  265. var textCircle = document.querySelector(".text-circle");
  266. leftContent.setAttribute('style', 'transform: rotate(0deg)');
  267. rightContent.setAttribute('style', 'transform: rotate(0deg)');
  268. var html="<b class=''>"+_this.data.sum+"</b>";
  269. this.data.ts = setInterval(function() {
  270. if(_this.data.sum<4){
  271. html="<b class='daoshu tada animated'>"+_this.data.sum+"</b>";
  272. setTimeout(function(){
  273. $(".text-circle b").removeClass("daoshu");
  274. $(".text-circle b").removeClass("animated");
  275. },900)
  276. }else{
  277. html="<b class=''>"+_this.data.sum+"</b>";
  278. }
  279. if (_this.data.sum >= 0) textCircle.innerHTML = html;
  280. _this.data.sum = _this.data.sum - 1;
  281. $("#Jvs .con").show();
  282. $("#Jvs .tit").html("抢答中");
  283. _this.data.angle += 36;
  284. if (_this.data.angle <= 360) {
  285. if (_this.data.angle > 180) {
  286. rightContent.setAttribute('style', 'transform: rotate(' + (_this.data.angle - 180) + 'deg)')
  287. } else {
  288. leftContent.setAttribute('style', 'transform: rotate(' + _this.data.angle + 'deg)')
  289. }
  290. }
  291. if (_this.data.sum < 0) {
  292. if(!_this.data.isAnswer)
  293. _this.Answer();
  294. _this.data.sum = 9;
  295. _this.data.angle = 0;
  296. leftContent.setAttribute('style', 'transform: rotate(0deg)');
  297. rightContent.setAttribute('style', 'transform: rotate(0deg)');
  298. clearInterval(_this.data.ts);
  299. }
  300. }, 1000)
  301. }, tips: function (text, time) {
  302. //弹窗工具
  303. time = time ? time : 1500;
  304. var para = document.createElement("p"); //创建新的<p> 元素
  305. para.innerHTML = text;
  306. para.setAttribute("class", "poptis");
  307. document.body.appendChild(para);
  308. para.style.marginLeft = -para.offsetWidth / 2+"px";
  309. setTimeout(function () {
  310. document.body.removeChild(para);
  311. }, time);
  312. },init:function(){
  313. var _this=this;
  314. this.listen();
  315. var ycy =this.GET("ycy");
  316. if(ycy){
  317. var b64 = new Base64();
  318. ycy=b64.decode(ycy);
  319. ycy=decodeURIComponent(ycy);
  320. ycy= JSON.parse(ycy);
  321. this.data.uid=ycy.uid;
  322. this.data.mt=ycy.mt;
  323. }
  324. if( this.GET("user_id")){
  325. this.data.uid=this.GET("user_id");
  326. this.data.mt=this.GET("mt");
  327. }
  328. this.getUserInfo(function(res){
  329. _this.userInfo=res.info;
  330. $("#py1_username").html(res.info.name);
  331. $("#py1_avatar").html('<img src="' + res.info.avatar + '" class="layui-circle">');
  332. _this.initSocket();
  333. })
  334. }
  335. }
  336. window.common=common;