Skip to content

JeremyLikness/DurableDungeon

Repository files navigation

DurableDungeon

A game designed to teach and learn serverless durable functions in C#.

📄 Read the related blog post, Stateful Serverless: Long-Running Workflows with Durable Functions.

Free Azure Account Get your Free Azure Account

Overview

The Durable Dungeon is a very simple game I wrote to illustrate a long-running serverless application architecture. It is entirely serverless and uses Table Storage as the database back end (or the preview Durable Entities). The general game flow works like this:

  1. New user assigned to game.
  2. A room is created, with a monster. A weapon is placed in the room and the monster holds the treasure.
  3. The user must confirm their commitment in 2 minutes or the character is killed.
  4. The user must pick up the weapon, slay the monster, and collect the treasure to win.

Commands are issued via end point "posts". A walkthrough below describes in more detail.

Get started

Clone the repository.

Optional :

There is a console app that monitors the queue. By default, it uses the Azure Storage Emulator. To use a real storage account, set STORAGE_CONNECTION to the connection string. Run this in a separate window to view "game play." Alternatively, you can monitor the queue directly. The app generates a lot of logging information so the console is useful for demoing steps in a clearer fashion.

Run the functions app locally from Visual Studio 2017 or later, Visual Studio Code (tasks and settings are included) or by using the functions runtime directly. Create a file in the root of the DurableDungeon project named local.settings.json and populate it with this to use the storage emulator:

{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet"
  }
}

There are two versions of code.DurableDungeon uses Durable Functions v1 and Table Storage. DungeonEntities surfaces the exact same API but uses the preview v2 and Durable Entities to track state.

Change AzureWebJobsStorage to a connection string if you wish to use real storage. You can also publish to Azure or run a command-line Zip deployment.

Game walkthrough

This is a simple walkthrough of the game. The application is designed to showcase all of the durable patterns and concepts. To issue "post" and "get" I recommend using the cross-platform HTTP REPL tool, but you can use any client of your choice.

If you want the easy button open index.html in the TestHarness folder. This is configured to connect with the local running instance (you can change the base URL as needed) and provides a rudimentary UI to interact with the game and monitor workflow status. If you experience Cross-Origin Resource Sharing (CORS) issues, consider adding this snippet as a peer to Values in your local.settings.json file:

    "Host": {
        "CORS": "*"
    }

Create User

The first step is to create a user. Assume a user named "Pat" for the following steps. If you are using HTTP REPL and running locally, you can set your base URL to:

set base https://localhost:7071/api

(Change the port if your functions run a different one).

Issue a POST to the NewUser endpoint:

{
   "name": "Pat" 
}

At this stage, you have 2 minutes to confirm. Tables named Inventory, Monster, Room, and User have been created and a monitor has started. You can view the tables with Azure Storage Explorer.

Check Monitor Status

You can check the confirmation status with:

GET CheckStatus/Pat/UserConfirmationWorkflow.

Check the monitor with:

GET CheckStatus/Pat/UserMonitorWorkflow.

Confirm

If you wish to confirm game play, issue a