game.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. var baseUrl = "gj.100t.com";
  2. var registUrl = "http://" + baseUrl + "/api/quepass/regist";
  3. var ansPostUrl = "http://" + baseUrl + "/api/quepass/anspost";
  4. var wsCountUrl = "ws://" + baseUrl + "/ws/quepass/onlinecounter";
  5. var wsWaitUrl = "ws://" + baseUrl + "/ws/quepass/onlinewaiting";
  6. var $box = $("#figureBox"),
  7. people = 0;
  8. var me = {
  9. row: 1,
  10. col: 1
  11. };
  12. var $go = $("#go"),
  13. $countDown = $("#countDown"),
  14. $ques = $("#ques"),
  15. $trap = $("#trapID"),
  16. $num = $("#num"),
  17. $fail = $("#fail"),
  18. $success = $("#success"),
  19. $endBox = $("#endBox"),
  20. $ren = $("#ren");
  21. var quesData, quesLen = 0,
  22. quesFlag = 0,
  23. quesHtml, quesTimer, quesInter, quesCount = quesEnd = 10,
  24. isQuesEnd = false;
  25. function getQueryString(name) {
  26. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  27. var r = window.location.search.substr(1).match(reg);
  28. if (r != null) return unescape(r[2]);
  29. return null;
  30. }
  31. var uid = getQueryString('uid');
  32. var uname = getQueryString('uname');
  33. var round_id = getQueryString('round_id');
  34. var peopleArr = [];
  35. var questionUrl = "question.json";
  36. if (round_id == 2) {
  37. questionUrl = "question2.json";
  38. } else if (round_id == 3) {
  39. questionUrl = "question3.json";
  40. }
  41. $.getJSON(questionUrl, function(res) {
  42. quesData = res.ques;
  43. quesLen = quesData.length;
  44. $("#allQues").html(quesLen);
  45. });
  46. var ansArr = ["A", -1, "B"]
  47. // 预加载
  48. var $load = $("#load"),
  49. images = new Array();
  50. function preload(imgs, callback) {
  51. var iCur = 0,
  52. i = 0;
  53. for (i = 0; i < imgs.length; i++) {
  54. images[i] = new Image();
  55. images[i].src = "img/" + imgs[i];
  56. images[i].onload = function() {
  57. iCur++;
  58. if (iCur == imgs.length) {
  59. console.log("加载完成");
  60. callback();
  61. }
  62. }
  63. }
  64. }
  65. if (!uid || !uname) {
  66. window.location.href = "http://gjnh.100t.com/gjnh/index.php/Index/";
  67. } else {
  68. registUrl = registUrl + "/"+uid
  69. $.post(registUrl, JSON.stringify({
  70. uid,
  71. uname
  72. }), function(res) {})
  73. preload(["bg.png", "figure.png", "img1.png", "img2.png", "img3.png", "line.png",
  74. "qus-top.png",
  75. "text-bg.png",
  76. "time-box.png", "trap1.png", "trap2.png", "trap3.png", "trap4.png","mute.png"
  77. ], function() {})
  78. waiting(function() {
  79. $load.fadeOut(function() {
  80. $(this).remove();
  81. init();
  82. });
  83. })
  84. }
  85. // 初始化
  86. function init() {
  87. var html = "",
  88. figure = "";
  89. for (var i = 0; i < 108; i++) {
  90. var left = Math.floor(Math.random() * 30) + 35 + "vw";
  91. figure = 'figure f' + Math.floor(Math.random() * 20 + 1);
  92. html += "<div class='" + figure + " row1' id='ren" + i + "' style='left:" + left + "'></div>";
  93. if (i == 1) {
  94. continue;
  95. }
  96. peopleArr.push("#ren" + i);
  97. }
  98. $box.append(html);
  99. me.$id = $("#ren1");
  100. var timer1 = setTimeout(function() {
  101. for (var j = 0; j < 36; j++) {
  102. if (peopleArr[j]) {
  103. var delay = j * 10 + "ms";
  104. me.$id.addClass("me");
  105. getIn(peopleArr[j], 1, j, delay);
  106. }
  107. }
  108. getIn(me.$id, 1, 1, "20ms");
  109. clearTimeout(timer1);
  110. }, 100);
  111. setTimeout(function() {
  112. startQues();
  113. }, 1000);
  114. }
  115. function getIn(str, i, j, delay) {
  116. var $ren = $(str);
  117. removePos($ren).addClass("d" + i + "-" + j).addClass("row" + i);
  118. $ren.removeAttr("style");
  119. if (delay) {
  120. $ren.css("transition-delay", delay);
  121. }
  122. }
  123. function startQues() {
  124. if (!me.$id.hasClass('row1')) {
  125. postAns(-1, 0, 30);
  126. fail();
  127. isQuesEnd = true;
  128. window.clearInterval(timer);
  129. return false;
  130. }
  131. resetGame();
  132. console.log(quesLen);
  133. if(!quesLen){
  134. alert("题目出现错误");
  135. return false;
  136. }
  137. if (quesLen != 0 && quesFlag < quesLen && !isQuesEnd) {
  138. $num.html(quesFlag + 1);
  139. var ques = quesData[quesFlag];
  140. quesHtml = ques.text;
  141. $ques.html(quesHtml);
  142. $go.fadeIn();
  143. getRandAns();
  144. quesInter = setInterval(function() {
  145. if (quesCount == 1) {
  146. clearInterval(quesInter);
  147. $go.fadeOut();
  148. quesFlag++;
  149. var tt = setTimeout(function() {
  150. nextGame(ques);
  151. clearTimeout(tt);
  152. }, 2000);
  153. }
  154. quesCount--;
  155. $countDown.html(quesCount);
  156. }, 1000);
  157. } else {
  158. isQuesEnd = true;
  159. clearTimeout(quesTimer);
  160. window.clearInterval(timer);
  161. success();
  162. }
  163. }
  164. function nextGame(ques) {
  165. var trapHtml = "";
  166. trapHtml += emptyRow(1);
  167. $(".row1").removeClass("row1");
  168. if (ques.right == "A") {
  169. trapHtml += emptyRow(2);
  170. $(".row2").removeClass("row2");
  171. $(".row0").removeClass("row0").addClass("row1");
  172. } else {
  173. trapHtml += emptyRow(0);
  174. $(".row0").removeClass("row0");
  175. $(".row2").removeClass("row2").addClass("row1");
  176. }
  177. $trap.html(trapHtml).show();
  178. setTimeout(function() {
  179. var $rows = $(".row1"),
  180. rowsLen = $rows.length;
  181. for (var i = 0; i < rowsLen; i++) {
  182. var $k = $rows.eq(i);
  183. removePos($k).removeAttr("style").addClass("d1-" + i).addClass("row1");
  184. }
  185. }, 2000);
  186. quesTimer = setTimeout(startQues, 3000);
  187. }
  188. function emptyRow(i) {
  189. $(".row" + i).removeAttr("style").transition({
  190. y: "-100vh"
  191. }, 2000, "linear", function() {
  192. removePos($(this)).css("left", 40 + "vw");
  193. })
  194. if (i == 1) {
  195. var rand = 4;
  196. } else {
  197. var rand = Math.floor(Math.random() * 3) + 1;
  198. }
  199. return '<div class="trap trap' + rand + ' r' + i + '"></div>';
  200. }
  201. function resetGame() {
  202. isAns = false;
  203. quesCount = quesEnd;
  204. $countDown.html(quesCount);
  205. $trap.html("").hide();
  206. $("#ansBox .ans").show().removeClass("active");
  207. }
  208. function fail() {
  209. window.location.href = "fail.html";
  210. }
  211. function success() {
  212. // window.location.href = "success.html";
  213. console.log("success");
  214. }
  215. // 获取随机答案
  216. function getRandAns() {
  217. var tempPeople = peopleArr.concat();
  218. for (var i = 0; i < 3; i++) {
  219. for (var j = 0; j < 36; j++) {
  220. if (i == 1) {
  221. continue;
  222. }
  223. if (j == 1) {
  224. continue;
  225. }
  226. var rand = Math.floor(Math.random() * tempPeople.length);
  227. var id = tempPeople.splice(rand, 1)[0];
  228. delay = Math.floor(Math.random() * 8000) + "ms";
  229. if (id) {
  230. getIn(id, i, j, delay);
  231. } else {
  232. console.log(id);
  233. }
  234. }
  235. }
  236. }
  237. // 清除原来位置
  238. function removePos($obj) {
  239. if ($obj) {
  240. var c = $obj.attr("class");
  241. var match = c.match(/d\d\-\d{0,2}/);
  242. var m2 = c.match(/row\d{1}/);
  243. if (match) {
  244. $obj.removeClass(function() {
  245. return match[0];
  246. })
  247. }
  248. if (m2) {
  249. $obj.removeClass(function() {
  250. return m2[0];
  251. })
  252. }
  253. return $obj;
  254. }
  255. }
  256. // 选择答案
  257. var isAns = false;
  258. $("#ansBox").on("click", ".ans", function() {
  259. if (quesCount >= 0 && quesCount <= quesEnd) {
  260. if (!isAns) {
  261. var $this = $(this);
  262. var dataId = $this.attr("data-id");
  263. isAns = true;
  264. $this.addClass("active").siblings().fadeOut();
  265. removePos(me.$id).removeAttr("style").addClass("d" + dataId + "-" + 1).addClass("row" + dataId);
  266. me.row = dataId;
  267. var ans = -1,
  268. time = quesEnd - quesCount,
  269. iscorrect = 0;
  270. if (quesData[quesFlag].right == "A") {
  271. ans = dataId == 0 ? 1 : 0;
  272. iscorrect = dataId == 0 ? 1 : 0;
  273. } else if (quesData[quesFlag].right == "B") {
  274. ans = dataId == 2 ? 0 : 1;
  275. iscorrect = dataId == 2 ? 1 : 0;
  276. }
  277. postAns(ans, iscorrect, time);
  278. }
  279. }
  280. });
  281. function postAns(ans, iscorrect, time) {
  282. $.post(ansPostUrl, JSON.stringify({
  283. round_id,
  284. uid,
  285. uname,
  286. qno: (quesFlag + 1),
  287. ans,
  288. iscorrect,
  289. time
  290. }));
  291. }
  292. // 重连
  293. var limitConnect = 10; // 断线重连次数
  294. var timeConnect = 0;
  295. function reconnect(service) {
  296. // lockReconnect加锁,防止onclose、onerror两次重连
  297. if (limitConnect > 0) {
  298. if (localStorage.getItem('lockReconnect') != true) {
  299. localStorage.setItem("lockReconnect", 1);
  300. limitConnect--;
  301. timeConnect++;
  302. console.log("第" + timeConnect + "次重连");
  303. // 进行重连
  304. setTimeout(function() {
  305. webSocketInit(service);
  306. localStorage.removeItem("lockReconnect");
  307. }, 2000);
  308. }
  309. } else {
  310. console.log("TCP连接已超时");
  311. }
  312. }
  313. var isWait = true;
  314. var timer = null;
  315. var limit=getQueryString("limit")||1;
  316. function waiting(callback) {
  317. wsWaitUrl = wsWaitUrl +"/"+uid;
  318. var ws = new WebSocket(wsWaitUrl);
  319. ws.onopen = function() {
  320. console.log("open success");
  321. localStorage.removeItem("lockReconnect");
  322. //定时发送
  323. // timer = window.setInterval(function() {
  324. // console.log(uid,uname)
  325. // ws.send(uid + "_" + uname)
  326. // }, 1000)
  327. }
  328. ws.onmessage = function(e) {
  329. var res = JSON.parse(e.data);
  330. console.log(res)
  331. if (res.status == 1 && isWait && res.game_status == 1) {
  332. window.location.href = "gameready.html";
  333. }
  334. if (res.status == 1 && isWait && res.game_status != 1) {
  335. isWait = false;
  336. callback();
  337. }
  338. if (res.status == 2 && !isWait && res.game_status == 1) {
  339. isQuesEnd = true;
  340. clearTimeout(quesTimer);
  341. success();
  342. }
  343. if(res.status == 1 && !isWait && res.data.length<=limit){
  344. isQuesEnd = true;
  345. clearTimeout(quesTimer);
  346. window.clearInterval(timer);
  347. success();
  348. }
  349. $ren.html(res.data.length);
  350. }
  351. ws.onclose = function() {
  352. console.log("closed...")
  353. window.clearInterval(timer)
  354. //这里仿照page.html、
  355. ws.onerror = function(err) {
  356. window.clearInterval(timer)
  357. };
  358. }
  359. ws.onerror = function (err) {
  360. window.clearInterval(timer)
  361. };
  362. }
  363. function getQueryString(name) {
  364. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  365. var r = decodeURI(window.location.search.substr(1)).match(reg);
  366. if (r != null) return unescape(r[2]); return null;
  367. }
  368. var uid=getQueryString('uid');
  369. var uname=getQueryString('uname');
  370. if(!uid || !uname){
  371. window.location.href="https://www.100t.com/gjnh/2022.php";
  372. }else{
  373. registUrl = registUrl + "/"+uid
  374. $.post(registUrl,JSON.stringify({uid,uname}),function(res){
  375. preload(["bg.png","figure.png","img1.png","img2.png","img3.png","line.png","qus-top.png","text-bg.png","time-box.png","trap1.png","trap2.png","trap3.png","trap4.png"],function(){
  376. waiting(function(){
  377. $load.fadeOut(function(){
  378. $(this).remove();
  379. init();
  380. });
  381. })
  382. })
  383. })
  384. var timer = window.setInterval(function() {
  385. $.post(registUrl,JSON.stringify({uid,uname}),function(res){})
  386. }, 2000)
  387. }