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