chess.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. AV.init({
  2. appId: "BmologYYnRqCv0SLHDeDdA17-gzGzoHsz",
  3. appKey: "w9mVebFMdCmY6Nh9vfcBGaGt",
  4. serverURL: "https://bmologyy.lc-cn-n1-shared.com",
  5. });
  6. $(window).on({
  7. contextmenu: function () {
  8. return false;
  9. },
  10. dragstart: function () {
  11. return false;
  12. },
  13. beforeunload: function () {
  14. return true;
  15. }
  16. })
  17. var curCol = 0, myCol = 2, cur, messages = [0, 0, 0];
  18. function isMobile() {
  19. let flag = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
  20. // alert(navigator.userAgent + '\n' + flag)
  21. return flag;
  22. }
  23. function setCookie(cname, cvalue, exdays) {
  24. var d = new Date();
  25. d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
  26. var expires = "expires=" + d.toGMTString();
  27. document.cookie = cname + "=" + cvalue + "; " + expires;
  28. }
  29. function getCookie(cname) {
  30. var name = cname + "=";
  31. var ca = document.cookie.split(';');
  32. for (var i = 0; i < ca.length; i++) {
  33. var c = ca[i].trim();
  34. if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
  35. }
  36. return "";
  37. }
  38. function checkCookie() {
  39. myCol = parseInt(getCookie('color'))
  40. if (myCol != 0 && myCol != 1) myCol = 2;
  41. }
  42. function updateCurLight() {
  43. if (curCol == 1) {
  44. $('.light').removeClass("black");
  45. $('.light').addClass("white");
  46. } else {
  47. $('.light').removeClass("white");
  48. $('.light').addClass("black");
  49. }
  50. }
  51. function updateMyLight() {
  52. if (myCol == 1) {
  53. $('.mylight').removeClass("black");
  54. $('.mylight').addClass("white");
  55. } else if (myCol == 0) {
  56. $('.mylight').removeClass("white");
  57. $('.mylight').addClass("black");
  58. } else {
  59. $('.mylight').removeClass("black white");
  60. }
  61. }
  62. function updateMessages() {
  63. if (messages[0]) $('.message1').addClass("red");
  64. else $('.message1').removeClass("red");
  65. if (messages[1]) $('.message2').addClass("red");
  66. else $('.message2').removeClass("red");
  67. if (messages[2]) $('.message3').addClass("red");
  68. else $('.message3').removeClass("red");
  69. }
  70. var timer, st = 1;
  71. function uploadMessages() {
  72. timer && clearTimeout(timer);
  73. timer = setTimeout(function () { st = 1; }, 500);
  74. //FUUUULL BRiDGE RECtiFyer!!!
  75. if (st) {
  76. console.log('Messages updated.')
  77. cur.set('messages', messages)
  78. cur.save();
  79. }
  80. st = 0;
  81. }
  82. function updateCol() {
  83. updateCurLight();
  84. if (curCol) {
  85. $('.pointer').removeClass("black");
  86. $('.pointer').addClass("white");
  87. } else {
  88. $('.pointer').removeClass("white");
  89. $('.pointer').addClass("black");
  90. }
  91. }
  92. class Chess {
  93. constructor(ele) {
  94. this.ele = ele;
  95. this.init();
  96. }
  97. init() {
  98. this.arr = [];
  99. this.render();
  100. $('.pointer')[0].style.display = 'block'
  101. $('.pointer').animate({
  102. left: $('#game')[0].offsetLeft - 50,
  103. top: $('#game')[0].offsetTop + 30,
  104. }, 0);
  105. if (isMobile()) $('.pointer').hide();
  106. $('.pos').on('mouseenter', function () {
  107. if ($(this).hasClass("black") || $(this).hasClass("white") || (myCol != 2 && myCol != curCol)) return;
  108. document.onmousemove = null
  109. $('.pointer').animate({
  110. left: this.parentNode.parentNode.parentNode.parentNode.offsetLeft + this.parentNode.offsetLeft + 3,
  111. top: this.parentNode.parentNode.parentNode.parentNode.offsetTop + this.parentNode.offsetTop + 3,
  112. opacity: 0.7,
  113. }, 0)
  114. $('.pointer')[0].style.boxShadow = '3px 3px 7px rgba(15,15,15,0.5)'
  115. $('.pointer')[0].style.transform = 'scale(100%)'
  116. })
  117. $('.pos').mouseleave(function (e) {
  118. updateCol();
  119. document.onmousemove = function (e) {
  120. $('.pointer').animate({
  121. left: e.pageX - 14,
  122. top: e.pageY - 14,
  123. opacity: 0.3,
  124. scale: 1
  125. }, 0)
  126. }
  127. $('.pointer')[0].style.boxShadow = ''
  128. $('.pointer')[0].style.transform = ''
  129. })
  130. this.span = $("#tbodys span");
  131. let that = this
  132. $("#tbodys").on('click', 'tr td span', function () {
  133. that.click(this);
  134. });
  135. $("#regret").on('click', function () {
  136. that.regret();
  137. })
  138. $("#agin").on('click', function () {
  139. that.agin();
  140. })
  141. $("#check").on('click', function () {
  142. that.check();
  143. })
  144. $(".message1").on('click', function () {
  145. messages[0] = !messages[0];
  146. updateMessages();
  147. uploadMessages();
  148. })
  149. $(".message2").on('click', function () {
  150. messages[1] = !messages[1];
  151. updateMessages();
  152. uploadMessages();
  153. })
  154. $(".message3").on('click', function () {
  155. messages[2] = !messages[2];
  156. updateMessages();
  157. uploadMessages();
  158. })
  159. }
  160. render() {
  161. let div = $("<div id='xq-tips'>").appendTo($(this.ele))
  162. $("<div class='xq-txt'>").appendTo($(div))
  163. let table2 = $("<table><tbody id='tbodys'>").addClass("tables").attr("border", "0").appendTo($(this.ele));
  164. for (let i = 0; i < 16; i++) {
  165. let tr2 = $("<tr>").appendTo($("#tbodys"));
  166. for (let j = 0; j < 16; j++) {
  167. $("<td><span class='pos' id=" + (i * 16 + j) + ">").appendTo($(tr2));
  168. }
  169. }
  170. }
  171. click(_this) {
  172. console.log(_this)
  173. this.ifs(_this)
  174. this.win();
  175. }
  176. upd() {
  177. $(this.span).each((index, item) => {
  178. this.arr.forEach((arrItem, arrIndex) => {
  179. if (index == arrItem) {
  180. if (arrIndex % 2 == 0) $(item).addClass("black");
  181. else $(item).addClass("white");
  182. }
  183. })
  184. })
  185. let num = this.arr.length;
  186. curCol = num % 2;
  187. updateCurLight();
  188. }
  189. ifs(_this) {
  190. if (myCol == 2) {
  191. myCol = curCol;
  192. setCookie('color', myCol, 1)
  193. }
  194. if ($(_this).hasClass("black") || $(_this).hasClass("white") || myCol != curCol) return;
  195. myCol = curCol;
  196. updateMyLight();
  197. let num = this.arr.length;
  198. console.log(this.arr)
  199. $(this.span).each((index, item) => {
  200. $(item).removeClass("highlight");
  201. if (item === _this) {
  202. this.arr.push(index);
  203. curCol = (num + 1) % 2;
  204. updateCurLight();
  205. if (num % 2 == 0) {
  206. $(_this).addClass("black");
  207. }
  208. else {
  209. $(_this).addClass("white");
  210. }
  211. // return;
  212. }
  213. })
  214. cur.set('data', this.arr)
  215. cur.save();
  216. }
  217. win() {
  218. let this_ = this;
  219. let brr = [], wrr = [];
  220. $(this_.arr).each((index, item) => {
  221. if ($(this_.span[item]).hasClass("black")) {
  222. brr.push(item);
  223. }
  224. else {
  225. wrr.push(item);
  226. }
  227. })
  228. brr = brr.sort(function (a, b) {
  229. return a - b;
  230. })
  231. wrr = wrr.sort(function (a, b) {
  232. return a - b;
  233. })
  234. if (this.isInclude(brr, 1) || this.isInclude(brr, 16) || this.isInclude(brr, 17) || this.isInclude(brr, 15)) {
  235. this.arr = [];
  236. $("#xq-tips").show();
  237. $(".xq-txt").html("Black Wins!");
  238. $("#regret").hide();
  239. }
  240. if (this.isInclude(wrr, 1) || this.isInclude(wrr, 16) || this.isInclude(wrr, 17) || this.isInclude(wrr, 15)) {
  241. this.arr = [];
  242. $("#xq-tips").show();
  243. $(".xq-txt").html("White Wins!");
  244. $("#regret").hide();
  245. }
  246. }
  247. isInclude(brr, x) {
  248. for (let i = 0; i < brr.length; i++) {
  249. if (brr.includes(brr[i]) && brr.includes(brr[i] + x * 1) && brr.includes(brr[i] + x * 2) && brr.includes(brr[i] + x * 3) && brr.includes(brr[i] + x * 4)) {
  250. return true;
  251. }
  252. }
  253. }
  254. regret() {
  255. if (this.arr.length == 0 || curCol == myCol) {
  256. return false;
  257. }
  258. let len = this.arr.length - 1;
  259. let num = this.arr[len]
  260. $(this.span[num]).removeClass("black white");
  261. curCol = num % 2;
  262. updateCol();
  263. this.arr.pop();
  264. cur.set('data', this.arr)
  265. cur.save();
  266. }
  267. reset() {
  268. this.arr = [];
  269. for (let i = 0; i < this.span.length; i++) {
  270. $(this.span[i]).removeClass("black white");
  271. }
  272. }
  273. agin() {
  274. $('.pointer').removeClass("white");
  275. $('.pointer').addClass("black");
  276. checkCookie();
  277. var exp = new Date();
  278. exp.setTime(exp.getTime() - 1)
  279. setCookie('color', myCol, exp)
  280. myCol = 2;
  281. updateMyLight();
  282. this.reset();
  283. $("#xq-tips").hide();
  284. $(".xq-txt").html("");
  285. $("#regret").show();
  286. cur.set('data', this.arr)
  287. cur.save();
  288. }
  289. check() {
  290. $(this.span).each((index, item) => {
  291. if (index == this.arr[this.arr.length - 1]) {
  292. $(item).addClass("highlight");
  293. return;
  294. }
  295. })
  296. }
  297. }
  298. var game;
  299. checkCookie();
  300. const query = new AV.Query('chess');
  301. query.descending('updatedAt')
  302. query.limit(1)
  303. query.find().then((result) => {
  304. $("#begin").hide();
  305. $("#regret").show();
  306. $("#agin").show();
  307. $("#game").show();
  308. if (result.length) {
  309. console.log('Data loaded from LeanCloud')
  310. cur = result[0]
  311. var map = result[0].get('data')
  312. messages = result[0].get('messages')
  313. updateMessages();
  314. if (map.length) {
  315. game = new Chess($("#game"));
  316. game.arr = map;
  317. game.upd();
  318. updateCol();
  319. } else {
  320. game = new Chess($("#game"));
  321. }
  322. }
  323. })
  324. setTimeout(function () {
  325. query.subscribe().then((liveQuery) => {
  326. console.log('Subscribed')
  327. liveQuery.on('update', (result) => {
  328. var map = result.get('data')
  329. messages = result.get('messages')
  330. updateMessages();
  331. console.log('Updated')
  332. game.reset();
  333. game.arr = map;
  334. game.upd();
  335. curCol = map.length % 2;
  336. if (map.length) {
  337. game.win()
  338. } else {
  339. $('.pointer').removeClass("white");
  340. $('.pointer').addClass("black");
  341. checkCookie();
  342. var exp = new Date();
  343. exp.setTime(exp.getTime() - 1)
  344. setCookie('color', myCol, exp)
  345. myCol = 2;
  346. $("#xq-tips").hide();
  347. $(".xq-txt").html("");
  348. $("#regret").show();
  349. }
  350. });
  351. }, (error) => {
  352. alert(error + "\n无法与服务器同步,请稍后再试")
  353. });
  354. }, 1000)