In our previous blogpost, we introduced the ground of a blockchain app with Typescript & Deku.
For the next 30 minutes you will create the state machine for your own cookie-clicker game!
Each player will have a cookieBaker record type, which stores every counters:
- number of cookies
- number of cursors
- number of grandmas
There are three possible actions:
- mint a cookie => simply add one cookie in the current amount of cookies
- buy a cursor => if enough cookies to buy one, add one cursor to the current amount of cursors. Every second, one cursor will mint one cookie for the user
- buy a grandma => if enough cookies to buy one, add one grandma to the current amount of grandmas. Every second, one grandma will mint three cookies for the user.
The passive cookie minting from Cursors and Grandmas will be implemented in the third and last part of this tutorial, in which we will create a frontend application interacting with Deku-P HTTP APIs to perform actions on our state machine.
Define our actions
We will use a reducer pattern to dispatch the several actions. In this tutorial, we will only implement three of them:
- Increment amount of cookies
- Increment amount of Cursors
- Increment amount of Grandmas
Let’s create an enum holding all the possible actions:
Define our state
The state, which will be saved using the set("myState", nextValue); (operation explained in the following section), will look like:
Create a basic saveState function
The saveState function is just a syntactic sugar to avoid duplicated code lines, because we will save the state after each action.
Moreover, in this function, we will add some console.log to ensure everything is going well:
Our new transition function
First step for our new transition function is to update the state regarding the provided action
If you remember from the previous tutorial, we will need to get:
- source: the public address of the user
- operation: the provided payload
- state: the current state for this source
Everything is explained in the previous part, but here is how our transition function will look like:
Adding a cookie
Everything is ready, we can perform the correct business action from the “‘cookie’” payload provided by the user, basically, this will just add 1 to the current amount of cookies:
Simply call this function when user provided it, by adding a simple switch on the operation:
Let’s buy a Cursor or a Grandma! Of course, he needs to have enough cookies to perform this action!
When a user buys a Cursor or a grandma, there are four actions to perform:
- Add a new cursor
- Decrease the amount of cookies (because we buy cursor with cookies)
- Calculate the price of the next cursor (we used the cookie-clicker formula)
- Update the passive “cookie per second” given by the cursor
Hence, we need to update our state to store these new attributes:
Same as before, let’s create an addCursor function which will perform the correct action (adding one cursor, decreasing cookies, calculate new cost, update cookies per second of cursors):
I think you have guessed it, we need the addGrandma function now!
Here is its content, but I am sure you already figure out:
And finally, we need to add this case in our switch:
🎉 Voilà!! 👏
We now have a more complex example than a basic Hello World!
You can play with it using the following commands (remember to have your deku-cluster up and ready!):
The whole code of this state machine is available here, of course, we also added other buildings from the cookie-clicker game!
More complex action
From now on, we only use a flat string. But in the near future, we would like to support some more complex actions, like sending cookies to your friend, or eating some of your cookies.
Hence, we have to declare and use a more complex action:
It is not difficult at all, and you can access to the type field by doing:
Used by deku-cli, it becomes:
🎉 Voilà (bis)!! 👏
This is awesome, we managed to interact with the Deku blockchain and create our first “complex” state machine.
In the last tutorial, we will create a frontend application to interact with our state machine via the Deku HTTP APIs!
If you want to know more about Marigold, please follow us on social media (Twitter, Reddit, Linkedin)!