rank.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>大屏首页</title>
  6. <style>
  7. body,
  8. p,
  9. a {
  10. margin: 0;
  11. padding: 0;
  12. }
  13. body {
  14. /* background: #ab0b0b url(img/pbg.jpg) no-repeat center top; */
  15. background-size: 100% auto;
  16. }
  17. html,
  18. body {
  19. width: 100%;
  20. height: 100%;
  21. }
  22. .full {
  23. position: absolute;
  24. top: 0;
  25. left: 0;
  26. right: 0;
  27. bottom: 0;
  28. /* background-color: rgba(0, 0, 0, 0.6); */
  29. }
  30. .p-top {
  31. width: 80%;
  32. height: auto;
  33. display: block;
  34. margin: 50px auto;
  35. }
  36. .text {
  37. font-size: 20px;
  38. color: #fff;
  39. margin: 200px auto 50px;
  40. width: 50%;
  41. }
  42. .btn-box {
  43. margin: 0 auto;
  44. width: 50%;
  45. margin-top: 20px;
  46. display: flex;
  47. justify-content: space-around;
  48. }
  49. .btn-box a {
  50. display: block;
  51. }
  52. h3{
  53. /* color: #fff; */
  54. text-align: center;
  55. }
  56. table th,td{
  57. /* color: #fff; */
  58. text-align: center;
  59. }
  60. </style>
  61. </head>
  62. <body>
  63. <div class="full">
  64. <!-- <img src="img/ptop.png" class="p-top" alt=""> -->
  65. <div class="btn-box">
  66. <a href="/rank.html?round_id=1" class="btn1"><button>第一轮</button></a>
  67. <a href="/rank.html?round_id=2" class="btn2"><button>第二轮</button></a>
  68. <a href="/rank.html?round_id=3" class="btn2"><button>第三轮</button></a>
  69. </div>
  70. <p></p>
  71. <h3>第<span id="round"></span>轮(当前答题人数:<span id="nowWait"></span>) <button onclick="setStatus(999)">清空答题记录</button></h3>
  72. <table border="1" cellspacing="0" cellpadding="0" width="70%" align="center" id="rankTbl">
  73. </table>
  74. </div>
  75. <script src="js/jquery.js"></script>
  76. <script>
  77. var limitConnect = 3; // 断线重连次数
  78. var timeConnect = 0;
  79. var baseUrl = "gj.100t.com";
  80. var wsWaitUrl = "ws://" + baseUrl + "/ws/quepass/onlinewaiting/1000";
  81. var wsCounter = "ws://" + baseUrl + "/ws/quepass/onlinecounter/1000";
  82. var startGameUrl="http://"+baseUrl+"/api/quepass/status";
  83. var ansObj = {0:"A",1:"B",2:"C","-1":"超时未答"}
  84. var round_id = getQueryString("round_id")
  85. var onlineAns="http://"+baseUrl+"/api/quepass/onlineans";
  86. $("#round").text(round_id)
  87. function getQueryString(name) {
  88. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  89. var r = window.location.search.substr(1).match(reg);
  90. if (r != null) return unescape(r[2]); return null;
  91. }
  92. function webSocketInit(service) {
  93. var ws = new WebSocket(service);
  94. var timer = null
  95. ws.onopen = function () {
  96. console.log("open success");
  97. localStorage.removeItem("lockReconnect");
  98. var roundId = getQueryString("round_id")
  99. ws.send("roundId_"+roundId)
  100. // //定时发送
  101. // timer = window.setInterval(function () {
  102. // ws.send("roundId_"+roundId)
  103. // }, 1000)
  104. }
  105. ws.onmessage = function (e) {
  106. var res = JSON.parse(e.data);
  107. var len = res.length;
  108. $("#nowWait").text(len)
  109. var tr = "<tr><th>id</th><th>姓名</th><th>答对题目数量</th><th>总分</th><th>总耗时</th><th>排名</th></tr>"
  110. for(var i=0;i<len;i++){
  111. rank = i+1
  112. var td = "<tr><td>"+res[i].uid+"</td>"
  113. td += "<td>"+res[i].uname+"</td>"
  114. td += "<td>"+res[i].num+"</td>"
  115. td += "<td>"+res[i].total+"</td>"
  116. td += "<td>"+res[i].total_time+"</td>"
  117. td += "<td>"+rank+"</td>"
  118. tr += td
  119. }
  120. if(len){
  121. $("#rankTbl").html(tr)
  122. }
  123. }
  124. ws.onclose = function () {
  125. console.log("closed...")
  126. window.clearInterval(timer)
  127. reconnect(service)
  128. }
  129. ws.onerror = function (err) {
  130. window.clearInterval(timer)
  131. reconnect(service);
  132. };
  133. return ws
  134. }
  135. // 重连
  136. function reconnect(service) {
  137. // lockReconnect加锁,防止onclose、onerror两次重连
  138. if (limitConnect > 0) {
  139. if (localStorage.getItem('lockReconnect') != true) {
  140. localStorage.setItem("lockReconnect", 1);
  141. limitConnect--;
  142. timeConnect++;
  143. console.log("第" + timeConnect + "次重连");
  144. // 进行重连
  145. setTimeout(function () {
  146. webSocketInit(service);
  147. localStorage.removeItem("lockReconnect");
  148. }, 2000);
  149. }
  150. } else {
  151. console.log("TCP连接已超时");
  152. }
  153. }
  154. // 初始化ws
  155. //var ws = webSocketInit(wsCounter)
  156. // 清空答题记录
  157. function setStatus(num) {
  158. if(window.confirm("你确认要清除当前轮的答题记录?清除后游戏状态也会重置,需要重新开始游戏!"))
  159. $.post(startGameUrl, JSON.stringify({ action: num,roundId:round_id }),function(){
  160. flag=true;
  161. });
  162. }
  163. $.post(onlineAns,JSON.stringify({roundId:round_id}),function(res){
  164. console.log(res)
  165. var res = res.data;
  166. var len = res.length;
  167. $("#nowWait").text(len)
  168. var tr = "<tr><th>id</th><th>姓名</th><th>答对题目数量</th><th>总分</th><th>总耗时</th><th>排名</th></tr>"
  169. for(var i=0;i<len;i++){
  170. rank = i+1
  171. var td = "<tr><td>"+res[i].uid+"</td>"
  172. td += "<td>"+res[i].uname+"</td>"
  173. td += "<td>"+res[i].num+"</td>"
  174. td += "<td>"+res[i].total+"</td>"
  175. td += "<td>"+res[i].total_time+"</td>"
  176. td += "<td>"+rank+"</td>"
  177. tr += td
  178. }
  179. if(len){
  180. $("#rankTbl").html(tr)
  181. }
  182. })
  183. </script>
  184. </body>
  185. </html>