GC Cheat! Helper

Adds play history for the current hand to the game Cheat! on Grundo's Cafe

当前为 2024-09-11 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name GC Cheat! Helper
  3. // @namespace https://greasyfork.org/en/users/1175371
  4. // @version 1.2
  5. // @description Adds play history for the current hand to the game Cheat! on Grundo's Cafe
  6. // @author sanjix
  7. // @match https://www.grundos.cafe/games/cheat/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=grundos.cafe
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12. (function() {
  13. 'use strict';
  14.  
  15. function CheatGameData(cards, plays, yourOldHand) {
  16. this.cards = cards;
  17. this.plays = plays;
  18. this.yourOldHand = yourOldHand;
  19. }
  20. var cheatGameData = new CheatGameData([],[],[]);
  21. cheatGameData = JSON.parse(localStorage.getItem('cheatGameData')) || new CheatGameData([],[],[]);
  22. console.log('page loaded');
  23. console.log(cheatGameData);
  24. var cheatRoundCards = cheatGameData.cards;
  25. var cheatRoundPlays = cheatGameData.plays;
  26. var pileStatus = document.querySelector('#cheat-cast + p');
  27. var pileContents = document.createElement('p');
  28. var returnToHome = document.querySelector('#cheat-cast ~ #cards ~ p.center ~ div.button-group');
  29. var playHistory = document.createElement('div');
  30.  
  31. function Play(player, cards, accused, innocence) {
  32. this.player = player;
  33. this.cards = cards;
  34. this.accused = accused;
  35. this.innocence = innocence;
  36. }
  37. function updatePlayHistory(plays) {
  38. var exceptLatest = plays.slice(0,plays.length - 1);
  39. exceptLatest.forEach((play) => {
  40. let cur = document.createElement('p');
  41. cur.innerHTML = play.player + ' played <b>' + play.cards.join(', ') + '</b>.';
  42. let pronounObj = play.player == 'You' ? 'you' : 'them';
  43. let pronounSub = play.player == 'You' ? 'you' : 'they';
  44. if (play.accused != null) {
  45. cur.innerHTML += ' ' + play.accused + ' ';
  46. if (play.innocence) {
  47. cur.innerHTML += 'accused ' + pronounObj + ' but ' + pronounSub + ' weren\'t cheating!';
  48. } else {
  49. cur.innerHTML += 'caught ' + pronounObj + ' cheating!';
  50. }
  51. } else {
  52. cur.innerHTML += ' No one accused ' + pronounObj + ' of cheating.';
  53. }
  54. playHistory.prepend(cur);
  55. });
  56. }
  57. function updateInfo(cards, plays) {
  58. console.log(cards);
  59. console.log(plays);
  60. cheatGameData.cards = cards;
  61. cheatGameData.plays = plays;
  62. localStorage.setItem('cheatGameData', JSON.stringify(cheatGameData));
  63. cheatRoundCards = cards;
  64. cheatRoundPlays = plays;
  65. }
  66. function buildPlayedCards(quant, card) {
  67. let playedCards = []
  68. for (let i = 0; i < quant; i++) {
  69. playedCards.push(card);
  70. }
  71. return playedCards;
  72. }
  73. function parsePlay(text) {
  74. let parsed = text.match(/(.*?) Played (\d) (\w+)$/);
  75. let cards = []
  76. switch (parsed[3]) {
  77. case 'Ace':
  78. case 'Aces':
  79. cards = buildPlayedCards(parsed[2], 'A');
  80. break;
  81. case 'Two':
  82. case 'Twos':
  83. cards = buildPlayedCards(parsed[2], '2');
  84. break;
  85. case 'Three':
  86. case 'Threes':
  87. cards = buildPlayedCards(parsed[2], '3');
  88. break;
  89. case 'Four':
  90. case 'Fours':
  91. cards = buildPlayedCards(parsed[2], '4');
  92. break;
  93. case 'Five':
  94. case 'Fives':
  95. cards = buildPlayedCards(parsed[2], '5');
  96. break;
  97. case 'Six':
  98. case 'Sixes':
  99. cards = buildPlayedCards(parsed[2], '6');
  100. break;
  101. case 'Seven':
  102. case 'Sevens':
  103. cards = buildPlayedCards(parsed[2], '7');
  104. break;
  105. case 'Eight':
  106. case 'Eights':
  107. cards = buildPlayedCards(parsed[2], '8');
  108. break;
  109. case 'Nine':
  110. case 'Nines':
  111. cards = buildPlayedCards(parsed[2], '9');
  112. break;
  113. case 'Ten':
  114. case 'Tens':
  115. cards = buildPlayedCards(parsed[2], '10');
  116. break;
  117. case 'Jack':
  118. case 'Jacks':
  119. cards = buildPlayedCards(parsed[2], 'J');
  120. break;
  121. case 'Queen':
  122. case 'Queens':
  123. cards = buildPlayedCards(parsed[2], 'Q');
  124. break;
  125. case 'King':
  126. case 'Kings':
  127. cards = buildPlayedCards(parsed[2], 'K');
  128. break;
  129. }
  130. return new Play(parsed[1], cards, null, null);
  131. }
  132. function objection(play, accusedBy, innocence) {
  133. play.accused = accusedBy;
  134. play.innocence = innocence;
  135. console.log('someone objected to:');
  136. console.log(play);
  137. if ((play.player == 'You') && (play.innocence == false)) {
  138. console.log('oh no the cheater was you!');
  139. play.cards = ['something'];
  140. }
  141. return play;
  142. }
  143. function getHand(hand) {
  144. //input ex "12_diamonds,11_hearts"
  145. document.querySelectorAll('#cards .card img.arrow.hide').forEach((card) => {
  146. //console.log(card.id);
  147. switch (card.id.split('_')[1]) {
  148. case '11':
  149. hand.push('J');
  150. break;
  151. case '12':
  152. hand.push('Q');
  153. break;
  154. case '13':
  155. hand.push('K');
  156. break;
  157. case '14':
  158. hand.push('A');
  159. break
  160. default:
  161. hand.push(card.id.split('_')[1]);
  162. break;
  163. }
  164. });
  165. return hand;
  166. }
  167. function handToObj(hand) {
  168. let handObj = {};
  169. hand.forEach((card) => {
  170. handObj[card] = (handObj[card] || 0) + 1;
  171. });
  172. return handObj;
  173. }
  174. function subtractHands(a, b){
  175. let result = {};
  176. for (let card in a) {
  177. result[card] = Math.max(a[card] - (b[card] || 0), 0);
  178. }
  179. return result;
  180. }
  181. var currentPlay = document.querySelector('#cards + p strong');
  182. var yourTurn = document.querySelector('form[action="/games/cheat/play/"]');
  183. if (currentPlay != null) {
  184. cheatGameData = JSON.parse(localStorage.getItem('cheatGameData'));
  185. cheatRoundCards = cheatGameData.cards;
  186. cheatRoundPlays = cheatGameData.plays;
  187. if ((yourTurn != null) && (document.querySelector('select[name="card_type"]') != null)) {
  188. console.log('it\'s your turn!');
  189. cheatGameData.yourOldHand = getHand([]);
  190. localStorage.setItem('cheatGameData',JSON.stringify(cheatGameData.yourOldHand));
  191. cheatRoundPlays.push(new Play('You', [], null, null));
  192. updateInfo(cheatRoundCards, cheatRoundPlays);
  193. } else {
  194. console.log('it\'s not your turn!');
  195. var storedHand = cheatGameData.yourOldHand;
  196. let hand = getHand([]);
  197. let playedCards = [];
  198. let lastPlay = cheatRoundPlays.at(-1) || 0;
  199. if ((JSON.stringify(hand) != JSON.stringify(storedHand)) && (lastPlay.player == 'You')) {
  200. console.log('it was just your turn');
  201. let storedHandObj = handToObj(storedHand);
  202. let curHandObj = handToObj(hand);
  203. if (hand.length < storedHand.length) {
  204. console.log('figuring out what cards you played');
  205. for (var i in storedHandObj) {
  206. var curHandCount = curHandObj[i] || 0;
  207. if (storedHandObj[i] != curHandCount) {
  208. for (let j = 0; j < storedHandObj[i] - curHandCount; j++) {
  209. playedCards.push(i);
  210. }
  211. }
  212. }
  213. }
  214. if (lastPlay.accused == null) {
  215. cheatRoundCards = cheatRoundCards.concat(playedCards);
  216. }
  217. cheatRoundPlays.at(-1).cards = playedCards;
  218. updateInfo(cheatRoundCards, cheatRoundPlays);
  219. pileContents.textContent = JSON.parse(localStorage.getItem('cheatGameData')).plays.join(', ');
  220. pileStatus.after(pileContents);
  221. updatePlayHistory(cheatRoundPlays);
  222. //console.log(playHistory);
  223. }
  224. let cur = parsePlay(currentPlay.textContent);
  225. //console.log(cur);
  226. cheatRoundCards = cheatRoundCards.concat(cur.cards);
  227. //console.log(cheatRoundCards);
  228. cheatRoundPlays.push(cur);
  229. updateInfo(cheatRoundCards, cheatRoundPlays);
  230. }
  231. }
  232. var cheating = document.evaluate(
  233. '//p[contains(.,"caught")]',
  234. document,
  235. null,
  236. XPathResult.FIRST_ORDERED_NODE_TYPE,
  237. null).singleNodeValue;
  238. var accused = document.evaluate(
  239. '//p[contains(.,"accused")][contains(strong,"NOT CHEATING")]',
  240. document,
  241. null,
  242. XPathResult.FIRST_ORDERED_NODE_TYPE,
  243. null).singleNodeValue;
  244. function accusationHelper(text, match, innocence) {
  245. let lastPlay = cheatRoundPlays.pop();
  246. cheatRoundPlays.push(objection(lastPlay, text.match(match)[1], innocence));
  247. updateInfo([], cheatRoundPlays);
  248. }
  249. if (cheating != null) {
  250. console.log('someone\'s a cheater');
  251. //they cheated
  252. accusationHelper(cheating.textContent, /^(.*?) caught/, false);
  253. } else if (accused != null) {
  254. console.log('no one cheated tho');
  255. //they didn't cheat tho
  256. accusationHelper(accused.textContent, /^(.*?) accused \w+/, true);
  257. }
  258. if (pileStatus != null) {
  259. pileContents.textContent = JSON.parse(localStorage.getItem('cheatGameData')).cards.join(', ');
  260. pileStatus.after(pileContents);
  261. updatePlayHistory(cheatRoundPlays);
  262. // console.log(playHistory);
  263. returnToHome.after(playHistory);
  264. }
  265. if ((document.evaluate(
  266. '//p[contains(.,"You have won") or contains(.,"lost")]',
  267. document,
  268. null,
  269. XPathResult.FIRST_ORDERED_NODE_TYPE,
  270. null).singleNodeValue != null
  271. ) || (document.querySelector('input[value="Start a New Game"]') != null)) {
  272. console.log('resetting for new game');
  273. updateInfo([],[]);
  274. }
  275. })();