game.js 9.1 KB

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