From Prototype To Demo: Building Out A Game-Changing Backend

code sample from middleware.js

When I ended the last game update, I was going to have to allow enemies to drop loot somehow. Ultimately, I want this loot to be a new NFT for the player. To do any minting or transferring of NFTs, I’ll need to set up a backend server to keep my API keys secured. So the next step is setting up the backend server!

Up until this point, the front end was responsible for handling game data while I was prototyping. Now that I’m happy with the way it’s behaving, I’m going to move that logic to my yet-to-be-built server’s API. I’ll also introduce MongoDB to the stack so I can persist player data. Persisting this information in the database will ensure that when a player selects their character, the character will start at whatever location they ended at in their last play session with any equipped items set.

Setting up the backend

I’m going to start by setting up a new folder in my project folder just called “middleware” and set up a file in there called middleware.js. I’m going to need some dependences so I’m going to “npm install” express, mongoose, and cors.

sample code from middleware.js
middleware.js set up to handle my requests

I don’t want to spill the beans about how all of my backend works for security and cheating protection, but you’ll see I have two routes set up. One will handle things related to the character and updating it in the database, while one route will handle battle logic. Here’s an example of characterRoutes.js to see what’s happening in there.

sample code from characterRoutes.js
/routes/characterRoutes.js

As you can see, we’re using express and express router to define our GET and POST endpoints. My model for Character is being imported to be used with my character routes and mongoose/MongoDB to persist data.

Defining the Character schema to be used in the database

Testing…

Now that I’m moving logic and data to the back end, I’m going to need new testing tools to make sure that I’m getting expected results from my APIs. I’m running these tests using a free app called Postcat which I downloaded from the MacOS App Store. I like it, it’s nice and light-weight and reminds me of when Postman was still just a simple free chrome extension. It’s also available as a web interface, but I haven’t used it myself. You can check it out at postcat.com.

Testing the attack api with Postcat
Testing the Attack API with Postcat

Demo

Now that I’ve refactored my data flows, I’m ready to show a short video demo of how things are coming together. With the backend logic implemented, and the front-end reconfigured, the user can select their character NFT, it loads the character into the last location they were the last time they played (or at 0,0 for a new game). Any equipment that was equipped the last time is still equipped, and the user can freely navigate the map. When an enemy is encountered the “attack” button is shown and damage is applied to both the user and the enemy.

There’s still a little work to do on the front end: state needs to be cleaned up a bit, enemy info needs to be displayed in a cleaner or more streamlined fashion including the enemy image, and I still need to add some style to the inventory screen.

New In-Game Items

While I’m working on this, I’m also starting to get some wearable defense items ready to be implemented into the game. Here’s an example image for a Fur Hat item that will be coming soon. Gear that the character will be able to use will feel familiar to the user assuming they’ve played any prior RPG games. Low level gear will be fur, cloth. As the character progresses, they will be able to equip leather armor, and eventually stronger materials such as iron and steel. That’s a bit away at the moment though.

Fur Helmet
Fur Helmet

That’s where things stand for now. I’ll be continuing to build out the back end for a little while longer while I work on moving from prototype to demo. Styles are going to be coming to the inventory screen, front end state is going to get cleaned up, loot still needs to be implemented, and I’m going to need to make some potions to replenish hitPoints. Until next time!

Leave a Reply

Your email address will not be published. Required fields are marked *