Create a cookie game Blockchain app with Deku-P

By Gauthier Sebille

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:

export enum actions {
    cookie = "cookie",
    cursor = "cursor",
    grandma = "grandma"
}

Define our state

The state, which will be saved using the set(“myState”, nextValue); (operation explained in the following section), will look like:

export type cookieBaker = {
    cookies: number,
    cursors: number,
    grandmas: number
}

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:

const saveState = (source: transaction, cookieBaker: cookieBaker) => {
    const cookieBakerJSON = JSON.stringify(cookieBaker);
    set(source, cookieBakerJSON);
    console.log("Successfully saved cookieBaker: " + cookieBakerJSON);
}

If you want to learn more about this tutorial, please read our blogpost on Marigold website :point_right: Create a cookie game Blockchain app with Deku-P
‍

2 Likes