common.js 12 KB

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