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