From 0c02e83bff0fb05874ddfe899e6311e7fd59d6ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Thu, 15 Sep 2022 03:13:05 +0200 Subject: [PATCH] add first version of onlinetichu.com card counter --- .../qutebrowser/greasemonkey/onlinetichu.js | 205 ++++++++++++++++++ 1 file changed, 205 insertions(+) create mode 100644 .config/qutebrowser/greasemonkey/onlinetichu.js diff --git a/.config/qutebrowser/greasemonkey/onlinetichu.js b/.config/qutebrowser/greasemonkey/onlinetichu.js new file mode 100644 index 0000000..b0347a6 --- /dev/null +++ b/.config/qutebrowser/greasemonkey/onlinetichu.js @@ -0,0 +1,205 @@ +// ==UserScript== +// @name onlinetichu.com card counter +// @version 1.0.0 +// @description automatically count cards at onlinetichu.com +// @author kesslermaximilian +// @match *://*.onlinetichu.com/Site/Game/Table/* +// ==/UserScript== + +// disable auto fold functionality completely +if (ot.AutoFold) { + ot.$autoFold.click() + ot.$autoFold.hide() +} +var myStyles = ` + #playedHigh { + position: absolute; + top: 620px; + width: 850px; + height: 580px; + } + + #playedLow { + position: absolute; + top: 750px; + width: 850px; + height: 580px; + } + + .playedCardsLayout { + position: absolute; + height: 125px; + } + + .playedCardsLayout .card { + width: 80px; + height: 120px; + border-radius: 5px; + float: left; + margin-left: -50px; + text-align: left; + } + + .playedCardsLayout .card:hover { + z-index: 1400; + } +` + +var styleSheet = document.createElement("style") +styleSheet.innerText = myStyles; +console.log(styleSheet); +document.head.appendChild(styleSheet); + +// add an extra field where we will put down the played cards + +var playedHigh = document.createElement('div'); +playedHigh.id = 'playedHigh'; +playedHigh.style.marginLEft = '25px'; +playedHigh.className = 'row'; + +var playedLow = document.createElement('div'); +playedLow.id = 'playedLow'; +playedLow.style.marginLEft = '25px'; +playedLow.className = 'row'; + +tableIG = document.getElementById('gameField').parentNode; +tableIG.appendChild(playedHigh); +tableIG.appendChild(playedLow); + +var createCardPlace = function(value, shift, node) { + var playedCards = document.createElement('ul'); + playedCards.id = 'playedCards' + value; + // playedCards.className = 'layoutPlayedCards list-unstyled col-lg-2'; + playedCards.className = 'playedCardsLayout list-unstyled'; + playedCards.style.width = '160px'; + playedCards.style.marginLeft = '25px'; + playedCards.style.left = shift*135 + 'px'; + + node.appendChild(playedCards); +} + +createCardPlace(1, 0, document.getElementById('playedHigh')); +for(let i = 14; i > 8; i--) { + createCardPlace(i, 14 - i + 1, document.getElementById('playedHigh')); +} + +for(let i = 8; i > 1; i--) { + createCardPlace(i, 8 - i, document.getElementById('playedLow')); +} + +// playedCards. +// ot.$gameField.insertAfter(playedCards, ot.$gameField.lastChild); + +// set up counting of played cards +var OnlineTichuCounter = { + $playedCards: {}, + played: {}, + total_played: [], + + action: { + + handlePlayedCard: function(card) { +// console.log('Handling a card:'); +// console.log(card); +// console.log(otc.played); + index = 0; + if(card.Shape > 4) { + index = 1; + } else { + index = card.Value; + } + otc.played[index].push(card); + otc.total_played.push(card); + + otc.played[index].sort(function(c1, c2){ + return c2.Shape - c1.Shape; + }); + }, + + handlePlayedCards: function(cards) { + if (cards.length > 0) { + contained = false; + // console.log(message.Table.TableCards[0].Shape); + // console.log(message.Table.TableCards[0].Value); + $(otc.total_played).each(function (index, item) { + if (item.Shape == cards[0].Shape) { + if (item.Value == cards[0].Value) { + contained = true; + } + } + }); + + if(!contained) { + $(message.Table.TableCards).each( + (index, card) => otc.action.handlePlayedCard(card) + ); + } + } + }, + + resetPlayedCards: function() { + for(let i = 1; i < 15; i++) { + otc.played[i] = []; + } + }, + + drawPlayedCards: function() { + console.log('drawing played cards'); + console.log(otc.played); + for(let i = 1; i < 15; i++) { + displayCardsHtmlString = ""; + $(otc.played[i]).each(function () { + var value = this.Value; + if (this.Shape == 7) { + value = 0; + } + displayCardsHtmlString += '
  • '; + }); + otc.$playedCards[i].html(displayCardsHtmlString); + } + } + } +} + +if (!window.otc) { + window.otc = OnlineTichuCounter; +} + +for(let i = 1; i < 15; i++) { + otc.$playedCards[i] = $('#playedCards' + i); +} + +otc.action.resetPlayedCards(); + +MyTableState = (function() { + var cached_function = ot.reaction.TableState; + + return function() { + var result = cached_function.apply(this, arguments); + + message = arguments[0]; + +// console.log('detected table state change event'); + console.log(message.Table.Status); + + // reset played cards at start of round + if(message.Table.Status == 'WaitForNextCards') { + otc.action.resetPlayedCards(); + } + + if(message.Table.Status == 'Playing') { + otc.action.handlePlayedCards(message.Table.TableCards); + } + + otc.action.drawPlayedCards(); + +// otc.$playedCardsQ.html(displayCardsHtmlString); +// $('.horizontalLayouta .card').css('margin-left', '-64px'); +// $('#playedCards').css('margin-left', '32px'); +// ot.HorizontalAlign(otc.$playedCards); + } + + return result; +})(); + +ot.reaction.TableState = MyTableState;