Everyday is an API day with BlackJack
Tags: mulesofthackathon, blackjack, mulesoft, api
Published: 2020-11-29 19:34:29

Although they say 'What Happens in Vegas, Stays in Vegas.', but there is one thing that you definitely want to get back home to re-live your memories and that would be the Casino games. Even though you might not be able to Visit Vegas at this point in time due to the present Pandemic situation, we have got you covered. We present to you the online version of Black Jack that you can enjoy playing in your leisure time.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/qdz2d846jzuj8b69p7tw.png)

This project has been implemented using React JS & MuleSoft. The APIs used in the project have been created using MuleSoft & deployed on CloudHub. CloudHub is an integration platform as a service (iPaaS) where you can deploy sophisticated cross-cloud integration applications in the cloud, create new APIs on top of existing data sources, integrate on-premises applications with cloud services, and much more.

This project also makes use of Object Store, to store the player's data. Object Store lets CloudHub applications store data and states across batch processes, Mule components, and applications, from within an application or by using the Object Store REST API. 

#MuleSoft APIs description:

* **/initialize** - 
This is the first API to be invoked when the player enters the name. The client sends this name as a query parameter named 'uid'. This query param is mandatory in all other APIs as well. MuleSoft creates an object in Object Store & sets initial coins which is configurable through the config.properties.

* **/bet** - 
At the start of each turn, the player needs to bet a certain amount that is less than or equal to its coin balance. This value is sent from Client to MuleSoft through 'betAmount' query parameter. This API generates 2 cards for the dealer (One of them hidden) as well as the player and returns it to the source. At the same time, card data along with updated balance is set in object-store.

* **/hit** - 
Whenever the player hits, this API is invoked which draws a new card from the deck. It re-calculates the player's hand & returns it to the caller along with the state of the game (Win/Continue/Bust) & the updated coin balance. Based on the state returned, the client allows the user to continue playing or declare win or bust. 

* **/stand** -
When the player fears getting busted and doesn't want to draw any new card, the dealer's hidden card is revealed and his hand value is calculated. Until the dealer's hand value crosses 17, a new card is pulled out of the deck. Based on Player's and Dealer's Hand value, the winner is declared. 

* **/delete** - 
This resets the game by deleting the player's data from Object Store.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/qcvg0umb99kh9lirlv71.PNG)

###How to play - 

####Rules: 
* The goal of blackjack is to beat the dealer's hand without going over 21.
* Face cards are worth 10. Aces are worth 1 or 11, whichever makes a better hand.
* Player and Dealer starts with two cards, one of the dealer's cards is hidden until the player stands
* To 'Hit' is to ask for another card. To 'Stand' is to hold your total and end your turn.
* If you go over 21 you bust, and the dealer wins regardless of the dealer's hand.
* If you are dealt 21 from the start (Ace and 10), you got a blackjack.
* You win when your hand totals higher than the dealer's hand or the dealer busts.

{% youtube MZNQCuDc-kA %}

####Configuration:
*As this application is hosted on github.io which is HTTPS and it invokes the MuleSoft application hosted on CloudHub, you might see the mixed content error. To fix this please follow the below steps -*

Step 1. Click on the lock icon near the URL and then click on the site settings.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/xu3vzx96qvr3ig8plu0i.png)

Step 2. Select Allow from the dropdown for Insecure content.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/74ndo30c12xwik7uvoqb.png)

Step 3. You are all set! Click [here](https://ashwija.github.io/blackjack/) to launch the game and have fun.

###Project URLs -
* Application - https://ashwija.github.io/blackjack/
* API Endpoints - http://blackjack.us-e2.cloudhub.io/api/*
* Frontend Code - {% github ashwija/blackjack %}
* MuleSoft Code - {% github kevinmaru2020/blackjack %}

###Team - 
* [Kevin Maru](https://www.linkedin.com/in/kevinmaru/)  (kevinmaru2020@gmail.com)
* [Ashwija Shetty](https://www.linkedin.com/in/ashwijashetty/) (ashwija.nshetty@gmail.com)


#### Challenges & Future Scope - 
We could understand the true potential of MuleSoft as we could leverage most of the capabilities provided by MuleSoft ranging from Design Center for designing APIs and testing RAMLs to Anypoint Studio for building actual APIs to using Object Store all the way till application deployment on MuleSoft's very own CloudHub. We also learned transforming messages into dataweave by exploring its vast set of capabilities. 

While we were facing the issue with the Mixed Content error due to HTTPS in github.io & HTTP at the API end, we will be working towards configuring TLS and exposing our BlackJack APIs over HTTPS.

This application would further be enhanced by adding multi-player capabilities over the internet. We also plan to publish a connector for this application.

##### References - 
1. https://docs.mulesoft.com/runtime-manager/cloudhub
2. https://docs.mulesoft.com/object-store/