Mafia: A Serverless Multiplayer Game
Tags: programming, webdev, aws, serverless
Published: 2023-02-27 01:19:56

This is the first post (of hopefully many) where I will be documenting my journey creating a serverless multiplayer game; Mafia (name not final... unless???).

I'm going to use this first post to cover the tools I intend to use and give a general idea of what this game should look like at the end. So here we go...

### What is this game?
I'm basing this game heavily on the popular table top game by the same name. It's a [social deduction game](https://en.wikipedia.org/wiki/Social_deduction_game#:~:text=A%20social%20deduction%20game%20is,and%20another%20being%20%22bad%22.) in which a majority team, the Townies, try to root out the opposing minority team, the Mafia. 

Members of the Mafia are aware of their allies and must work together to mislead the Town and avoid suspicion. Town players are not aware of anyone else's role and must use their deductive reasoning to locate evildoers.

### Mechanics
The game isn't strictly turn based however it follows a similar principal. There are four states; Morning, Day, Evening, and Night. 

During the Day all members of the town (inclusive of the mafia members) gather to discuss what leads they have found on the Mafia. At this time they may also decide to remove a player under suspicion of being a member of the Mafia.

At Evening, the public forum closes and players are given time to process they day's conversation, and if possible with their role they can chose a player to target that night. For example, a Detective may chose to follow another player and see who they visit that night. Mafia members are able to converse amongst themselves during this time.

During the Night the game processes all the player actions.

In the Morning, the events of the previous Night are revealed to the Town.

The cycle repeats until there is a winner.

---
## Enough with the nerd stuff, tell me about the tech stack!
Heavy sarcasm :)

### Tools I'm using to make this
I want the game to be playable in the web browser at this stage, may change in future. Also primarily using AWS for the backend.

#### *Backend*
- [AWS ApiGateway](https://aws.amazon.com/api-gateway/) for player to backend communication
- [AWS DynamoDB](https://aws.amazon.com/dynamodb/) as a database following [Single Table Design](https://aws.amazon.com/blogs/compute/creating-a-single-table-design-with-amazon-dynamodb/) principles
- [AWS Cognito](https://aws.amazon.com/cognito/) for user authentication
- [AWS S3](https://aws.amazon.com/s3/) for medium/large file storage (images etc.)
- [AWS IoT Core](https://aws.amazon.com/iot/) for the pub/sub functionality (live updates)
- [Postman](https://www.postman.com/) to test and document the API

All application logic will be written in Python.

#### *Frontend*
The frontend will be made with [Quasar Framework](https://quasar.dev/) - a Vue.js wrapper. Quasar comes bundles with a wide array of prebuilt components and very thorough documentation. An additional benefit is that Quasar streamlines the process of packaging a single codebase into multiple deployment formats; web, mobile, and desktop. This sparks joy.

#### *The glue*
As I previously stated my goal with this project is 100% serverless, however there is another requirement that is just as important; 100% Infrastructure as Code. IaC is exactly as it reads, all infrastructure covered in the *Backend* section should be provisioned in code, with nothing needing to be created in AWS manually.

There are several benefits to this approach:
1. Changes to Infrastructure can be tracked with Git. Manual editing via the AWS console is un-trackable and generates no history.
2. The project can be deployed with a single command. If I deploy in Australia, and then have reason to deploy in North America, I must be able to do so with confidence and speed.
3. The project can be destroyed with a single command. This is extremely important. If I determine that the cost becomes unsustainable I must be able to completely eradicate all traces of it with no remnants remaining. Scorched earth.

The IaC framework I will be using for this project is [Serverless Stack (SST)](https://serverless-stack.com/). 

SST is a cloud architecture development kit that lets you provision Infrastructure as Code by leveraging the base [AWS CDK](https://docs.aws.amazon.com/cdk/v2/guide/home.html) with improvements geared towards serverless applications. If you've used any IaC providers in the past like Terraform, AWS CDK, Serverless, SAM etc. - I seriously recommend checking out SST. It's next level.

---
## Process, MVP, and Timeline
This project is my way of diving head first into serverless cloud development. I'm using something I'm passionate about to motivate and upskill myself into a field that I think is the future. For this reason I will be attempting to follow as many best practices as I can, and sharing them here as I adopt them.

Below is a flow chart with my rough architecture plan. This will no doubt change as the project matures.

![Architecture Diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/420qylj26hfojjmyzgtv.png)

### Process
User actions are sent via REST API to an AWS Api Gateway. This distributes requests to the appropriate lambda microservice, called *Controllers*. The Controllers handle all application logic between frontend and backend services such as the database. 

As there is no server I am not able to maintain continuous state. All user actions must be written to the database. During the Night sequence, all user actions will be read from the database and combined with data about the game (settings etc.).

The combined user actions and game settings will be resolved using my custom *MafiaEngine*. This will return the resolved game state to be communicated back to the users.

### Minimum Viable Product
The maximum number of players in a game is 15. A typical game will have 9 Town players, 3 Mafia players, and 3 Neutrals. A fully featured game will have large role diversity between the players, however as players can share the same role I will be aiming for the following.

Town: Citizen, Doctor, Bodyguard, Detective, Escort  
Mafia: Mafioso, Liaison (evil Escort)  
Neutral: Serial Killer, Survivor, Jester (of course)

The game will need a fully functioning lobby system, stage transitioning (morning/night/etc.), and state resolution.

### Timeline
I began routine work in early January 2023 and have maintained decent weekly progress. I hope to have a playable version of this game out within 2 months (end April). Between now and then I will aim for regular updates and cover technical roadblocks and solutions.

---
## Conclusion
I don't particularly want this series to be a generic game "devlog" thing. The real motivation behind this project was to get familiar with serverless cloud development and I think it's going to be a massive learning opportunity. 

What I'm more interested in writing about it showing how I've implemented some serverless feature and provide a legitimate use case that it is fulfilling. 

If this project sounds interesting I'd love to hear from you, especially if you see me running headlong into a classic pitfall. 

Note: I'm keeping somewhat-up-to-date technical documentation for the project at https://mafia-sdg.netlify.app/. 

Cheers,