GC Cheat! Helper

Adds play history for the current round and pile contents to the game Cheat! on Grundo's Cafe

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