When I was a teenager, web browser games started to be a thing; something that everyone played. Whether it was kingdom building game (Travian), space empire simulator (Ogame) or weird PvP / roleplaying hybrid (Bitefight) – there was a niche suited for everyone.
Nowadays mobile game have taken their place, although sometimes I am still bombarded with spam ads for these kind of games on Facebook.
Due to nostalgy for such games I thought a crazy idea – why not write a backbone for a web based RPG as an API?
The plan is simple – API should have endpoints allowing user to both retrieve and create in game characters, as well as items belonging to them.
I am utilising both GET and POST methods, and outline of endpoints looks like this:
- GET /character – fetch the entire list of characters
- GET /character/name – fetch info about specific character
- GET /character/name/item – fetch info about items in character’s inventory
- POST /character – create a new character
- POST /character/name/item – add an item to character’s inventory
To simplify things I have not added a database yet. Instead the info on characters is stored in a dictionary object. Of course it comes with a limitation – the characters we create with POST will only exist in applications memory and will only persists while the application runs, but I will address it in a later entry. For now I want something simple and working.
After creating endpoints in Flask the application can be run locally using following command:
flask run
Doing so the application will be run as an instance and published on default port 5000.
In the dictionary I’ve input two characters: Adelajda (named after my friend, my usual go-to for female characters in RPG games) and Tordek (dwarf warrior from D&D 3rd edition).
Let’s try to retrieve the list of these characters and see what we can get from Postman by hitting /characters endpoint:
{
"characters": [
{
"items": [
{
"name": "longbow",
"value": "100"
}
],
"level": "1",
"name": "Adelajda"
},
{
"items": [
{
"name": "battleaxe",
"value": "150"
},
{
"name": "platemail",
"value": "1000"
}
],
"level": "100",
"name": "Tordek"
}
]
}
We have two characters, one of them being lvl 1, while the other is lvl 100 and each of them has a different items in inventory. Pretty good start. However, the usual party in roleplaying games usually consists 4 players so let’s add two additional characters by using POST method. Let’s make sure to include „name” and „level” in request body, for example:
{
"name": "Strider",
"level": "50"
}
I’ve added one more character and now the output of GET /character looks the following:
{
"characters": [
{
"items": [
{
"name": "longbow",
"value": "100"
}
],
"level": "1",
"name": "Adelajda"
},
{
"items": [
{
"name": "battleaxe",
"value": "150"
},
{
"name": "platemail",
"value": "1000"
}
],
"level": "100",
"name": "Tordek"
},
{
"items": [],
"level": "50",
"name": "Strider"
},
{
"items": [],
"level": "20",
"name": "Evelynn"
}
]
}
Four characters make a decent party, however it looks like Strider and Evelynn came unprepared! Let’s try to fix that by using POST /character/name/item endpoint and add some item’s to inventory of these characters. Example request body:
{
"name": "robe",
"value": "150"
}
Let’s view only Evelynn’s inventory by using our last endpoint, GET /character/name/item and make sure that the newly added robe is indeed there. The output:
{
"items": [
{
"name": "robe",
"value": "150"
}
]
}
And here it is!
The application itself is simple and there are quite a few things that could be improved – one thing that comes to my mind is adding default values for level and items (in case those are not specified in POST request) and I might address them in the future.
For now let’s settle on what we have. You can find a full code of this application here.
In the upcoming blog post I will dockerize the application and deploy it in Azure Container Apps. See you next time!