Tableau Proba Blackjack
Jul 31, 2012 - This spreadsheet uses Microsoft Excel to calculate odds of hand value in Blackjack. It uses macros and formulas to deal you a hand and predict your next card. Search the history of over 446 billion web pages on the Internet. I wanted to add to this, glad you got it figured out below, but that didn't work for me in Tableau 10. I had the same issue where the columns option was grayed out. Select 'Show Field Labels for Rows' then swap columns and rows and then swap back, This added field labels back to columns for me.
This year I had the honor to present at Tableau Customer Conference in Las Vegas together two hyper-blackbelt-jedi masters: Michael Kovner and Russell Christopher. The track’s name was “You did WHAT with the Javascript API?!” and as you can imagine we had some heavyweight stuff to throw. One of my API example was a complete multiplayer blackjack game using Tableau Public as gfx engine and clojure/script as backend and frontend programming language.
Let’s see how I built it from Tableau and programming perspective. But first… the game.
The Game
It’s a classic black jack game: all players (well, maximum two in our case) plays against the dealer. First the players hit until they’re fine with their cards, then stand. Dealer must always hit if his score is less than 17, otherwise stand. Now imagine this in Tableau. (But unlike vizpainter’s black jack game the logic here is outside of tableau).
Since the presentation was recorded the easiest way is to watch this video:
You can try it here as well: http://blackjack.starschema.biz/
Now a couple of things (disclaimer or user’s guide):
- When the page loads you have to add your name and chose your role (player 1 or player 2)
- For new game click “New”
- Hit is hit, stand is stand. If two of you are playing then both of you need to stand before the dealer reveals his cards.
- If anyone logs in with the same role in the meantime fun things will happen. You might be kicked off or other unexpected things might arise (like switch to cooperative mode)
GL/HF.
Details, Tableau
How does this work? Since I built the whole game in less than a day you can imagine: it’s not too complex. The game area is a dashboard. On the top everything is parameter driven: the opponent’s name, score and bar chart with score all rely on parameters.
The cards are coming from two separate sheets. Those sheets are using the same data set: numbers from zero to 851. The first digit (after left padding with zero) shows the location in hand while the second and third describes the card itself (it can be empty, a standard card from 52 cards deck or a face down card).
Every card has its location and card identifier. This used to position it to a scatter plot using custom shapes
Tableau Proba Blackjack Definition
The viz is shape based with the card position on the columns shelf. Shapes are custom, each one for a card type.
You can have a look on the raw tableu workbook here: https://public.tableau.com/views/Blackjack/BlackjackTableau . Feel free to download and have look in it.
Even more Details, Clojure, ClojureScript
Okay, let’s switch to hardcore mode. First of all all sources are on github, but this is not everything. Since I used clojure docstrings and comments I was able to generate developer docs using Marginalia (not the original, but Michael Blume‘s fork). Marginalia produces similar output as Docco for CoffeScript: on the left side you can see your docstrings while the right side contains the code. In my opinion this is the best approach: usually I start reading the source codes before any developer documentation but here I can read both in the same time.:
This is how the generated documentation looks: code and explanation in the same line.
Tableau Proba Blackjack Game
You can read the generated documentation here: http://tfoldi.github.io/data15-blackjack/.
Architecture
My main goal was to show how multiple people can see the same dataset from different angles from multiple locations. Thus, I needed a server that receives interactions from users, interprets them and sends back the shared state. And this needs to be real time: if one player hits a card then the other should see it immediately (like a push notification based tableau data change). This just cries for web sockets that allow to send and receive asynchronous messages between browsers and server.
I decided to put everything tableau related to the tableau namespace on client side (=clojurescript, running in browser), blackjack game related functions went to blackjack namespace on server side while client-server communications are in client and server on their respective sides. The client does nothing just sends the click events to server (logon, hit, stand, new) and renders the tableau report when the server asks for it.
The server sends the full state information always in the same format. It has the player names and scores, their cards, status messages. When it arrives to client I just use the Tableau JS API to set the correct filters and parameters. Tableau JS API is smart enough to not update the dashboard if I set the same filters again and again.
2 4 6 8 10 12 14 16 18 | [uidstate] my-role(if(=uidplayer1-name):player1:player2) [other-statusyour-statusfeedbackother-nameyour-hand] [(keywordizeother-user'status') (keywordizemy-role'feedback') (keywordizemy-role'hand')])] (update-cards'Dealer'(get-cardsdealer-hand)) (parameter-update(workbook)'other-score'(firstother-status)) (parameter-update(workbook)'your-score'(firstyour-status)) (parameter-update(workbook)'other-name'(orother-name'Player2')))) |
For managing websocket I have used sente which provided a core.async compatible interface to send and receive messages. It was cool.
Avoiding “callback hell”
Last time when I built my snake game I used asynchronous messaging to notify the game engine when the tableau visualization loaded. This time I used a different method to avoid nesting callbacks and have dependency between modules (by design your tableau workbook should not know about your game logic and your game logic should not know about your tableau workbook). I end up with using atom . When the workbook has loaded it changes its state to loaded. Then, other modules can sign up to this atom’s changes.
2 4 6 8 10 12 14 16 18 20 22 24 26 28 | ;; (defviz 'Map (atom) to store tableau viz load status & object reference ' 'Javascript object to interface with `Viz` constructor. Hide tabs & toolbars. After initalizing the Viz change `:status` to `:viz-ready` in our `viz` atom. The Client UI watches this atom: when the status changes the dom node will be visible (js-obj 'hideToolbar'true 'onFirstInteractive'#(swap!vizassoc:status:viz-ready))) ;; In client.clj ;; Watch for `:viz-ready` status in `tableau/viz` atom. If the `viz` ;; status have changed and the new status is :viz-ready, then send (add-watchtableau/viz:ui (when(=:viz-ready(new-state:status)) |
When the viz is ready just send a “load” message to the server and start the fun!
The WHY
There is always a purpose. With this mini-project my goal was to showcase that with Tableau JS API your only limitation is your imagination. It was a few hours of work to build an application where different users were able to see and understand play with the same set of data, together but from different perspectives. Or if you logon with the same username from multiple devices and change something it will visible on the other device immediately. Again, only matters of few hours of work.
It’s fun and easy.
- Tableau Extensions Addons Introduction: Synchronized Scrollbars - December 2, 2019
- Tableau External Services API: Adding Haskell Expressions as Calculations - November 20, 2019
- Scaling out Tableau Extracts – Building a distributed, multi-node MPP Hyper Cluster - August 11, 2019