chess.js 11 KB

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