common.js 13 KB

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