Mule Invaders: Destroy enemies with APIs!
Tags: mulesofthackathon
Published: 2020-10-21 21:09:44

## Example Hackathon Submission

This is a great example of an ideal Hackathon submission. To submit your post, go to [this link](https://dev.to/t/mulesofthackathon/latest) and click Write a Post. Make sure to include a download link to your code, screenshots, and a detailed description of how your flow works with instructions on how to run your application.

# Everyday is an API day: What is Mule Invaders?

Welcome to Mule Invaders, the video game inspired by the classic retro shooters Galaga and Space Invaders. In Mule Invaders, help Max the Mule destroy waves of enemies by spawning powerups using APIs. To protect Max on his journey to integrate the universe, create APIs in Anypoint Platform, deploy your application on CloudHub, then make HTTP requests to your APIs to spawn powerups while playing the game in realtime. When you use a REST client to send a POST request to your API, the flow will run and will trigger a powerup to spawn in the video game. The more powerups you trigger, the higher the score you can get.

{% youtube z3VH8DzGMbU %}

# Step 1: Sign up for Anypoint Platform

To play Mule Invaders and build your first API or integration with MuleSoft, go to MuleSoft.com and signup for a free account!

# Step 2: Build the integration

Now that we have created our MuleSoft account, let's developed the backend logic of the video game using Flow Designer. If you click the blue button below, you will be taken to the Mule Invaders Exchange page where you can download and modify the integration right in your web browser. When you navigate to Exchange by clicking the button below, click Open Flow to open up the integration in Design Center.

[Download the Exchange Asset](https://anypoint.mulesoft.com/exchange/68ef9520-24e9-4cf2-b2f5-620025690913/mule-invaders/)

Once you click Open Flow, you will need to click: Use this template which will import the entire project into your Anypoint Platform account. When you are prompted to provide a name for the Mule Applicaiton, name it: MuleInvaders.

When you click on the Use this template button, you will see some errors when loading the project. These errors are can be fixed easily! All you have to do is set up your HTTP Listener. An HTTP Listener is a very helpful Connector that enables you to listen to a URL for any new HTTP Request. When a new HTTP request is received by the HTTP Listener, anything that comes after the HTTP Listener in the flow will be executed at runtime.

To set up the HTTP Listener, click on cloudhub_http on the left navigation bar, and then click Add Connection. Insert the credentials listed in the screenshot below by adding the Host as 0.0.0.0 and the Port as 8081.

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

# Step 3: Setting up the integration

In the video game Mule Invaders, there are four powerups that you can unlock in the actual game by sending an HTTP request to one of the API endpoints we have just created. The main endpoint, /hack is the main endpoint that the game client is listening to. The game detects when a new UUID has been added to the Object Store under that endpoints unique key. When a new UUID has been added, the powerup will spawn in the video game. This works since the game polls the /hack endpoint for any new update to the object store twice a second.

Here is a brief description of what each endpoint will do in the video game Mule Invaders:

*/hack*

This is the endpoint that the Mule Invaders client application listens to and polls to listen for a new update from the KV Store.

*/spawnshield*

SpawnShield gives the player invincibility for 5 seconds and protects the player from incoming vulnerabilities. You can see in-game that the text will flash “protected by Anypoint Security” when the shield is spawned.

*/powerlaser*

PowerLaser gives the player a spread laser cannon that changes the particle to red and allows you to kill enemies faster.

*/api-blaster*

API Blaster increases the fire rate of laser cannons. Pew pew pew.

*/spawnscore*

Spawn score gives you bonus +1000 points and creates the slot machine sound effect.

## Step 4: How to test the mule application

Now that you understand how the Mule application was built, and how each API endpoint affects the video game, go up to the top right of the Flow Designer window and click the Test button to try out the integration you just built.

To test the integration, insert the mocking service URL into a REST client such as Postman, and make a POST request to the endpoint URL that you just created (don't forget to add /hack on the end of the URL). Once you make the HTTP request, you should see is a null value response for each JSON key in all request body. This is because we currently have no values added in the Object Store under those key names.

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


To add a new value to the Object Store, change your request URL to /spawnscore instead of /hack and send an additional POST request.

Now once again change your request URL back from /spawnscore to /hack again, and you will notice that score now has a UUID assigned under that JSON key.

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

This JSON payload is how the game is able to detect if a new request has been made. Pretty simple right?



# Step 5: Download Mule Invaders

Next, we are going to download the Mule Invaders application for either Mac or PC. Each build will come in a compressed zip folder that you will have to unzip on your local machine.

[Mac Download](https://www.mulesoft.com/misc/assets/mule-invaders-mac.zip)
[Windows Download](https://www.mulesoft.com/misc/assets/mule-invaders-windows.zip)

For Mac, when you open the application, you will get a warning saying that this application is not verified. To run the application, navigate to your Mac's System Preferences, then click on Security and Privacy, then click on Open Anyway. On PC, simply just click Run Application.

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

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

# About the author

Jordan Schuetz is a Developer Advocate at MuleSoft
Email me: jordan.schuetz@mulesoft.com
LinkedIn: https://www.linkedin.com/in/jordanschuetz/

No purchase necessary.  Void where prohibited. Contest runs from October 22, 2020 at 9:00 am PT and ends on November 29, 2020 at 11:59 pm PT. Open to legal residents of the U.S. (incl. D.C.), Argentina, Australia, Belgium, Canada (excl. Quebec), France, Germany, India, Ireland, Japan, Luxembourg, Netherlands, New Zealand, Spain, South Africa, Ukraine, and the U.K. Must  be 18+ (20+ in Japan). Restrictions apply. See Official Rules.