<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Hemant Singh Parmar</title>
    <description>The latest articles on DEV Community by Hemant Singh Parmar (@hemantcode625).</description>
    <link>https://dev.to/hemantcode625</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1479273%2Fedf6538f-3986-4349-b198-c4462d45a4fa.jpeg</url>
      <title>DEV Community: Hemant Singh Parmar</title>
      <link>https://dev.to/hemantcode625</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hemantcode625"/>
    <language>en</language>
    <item>
      <title>Know Backend of Stake’s Mine Game</title>
      <dc:creator>Hemant Singh Parmar</dc:creator>
      <pubDate>Sat, 13 Jul 2024 09:32:46 +0000</pubDate>
      <link>https://dev.to/hemantcode625/know-backend-of-stakes-mine-game-2d11</link>
      <guid>https://dev.to/hemantcode625/know-backend-of-stakes-mine-game-2d11</guid>
      <description>&lt;p&gt;Hey there, fellow game enthusiasts! 🎮&lt;/p&gt;

&lt;p&gt;I’ve recently built a Mine Game, and I’m excited to share how the backend is designed. If you’re curious about how such games work behind the scenes, you’re in the right place!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8rc9uvwzgr6slgb964qi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8rc9uvwzgr6slgb964qi.png" alt="Mine Game for Online Betting and Casino" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is a Mine Game?&lt;/strong&gt;&lt;br&gt;
First things first, let’s quickly recap what a Mine Game is. Inspired by Stake’s popular version, it’s a betting game where players reveal tiles on a grid, hoping to avoid mines. Each safe tile increases their winnings, but hitting a mine means losing the bet.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tech Stack&lt;/strong&gt;&lt;br&gt;
Frontend: Vite and React&lt;br&gt;
Styling: Tailwind CSS&lt;br&gt;
Backend: Node.js with Redis and GraphQL&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Designing the Backend&lt;/strong&gt;&lt;br&gt;
When a user starts a game, a unique game ID is generated. The game data includes fields like mineCount, mineField, bettingAmount, multiplierArray, and isGameOver. The mineField is not sent in queries but rather in mutations to prevent cheating or malpractices.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;GraphQL Resolvers&lt;/strong&gt;&lt;br&gt;
Here’s how I implemented the backend using GraphQL resolvers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const resolvers = {
  Query: {
    getGameResults: async (_, { gameId }) =&amp;gt; {
      const gameData = await redis.get(gameId);
      const game = JSON.parse(gameData);
      return {
        mineCount: game.mineCount,
        mineField: game.gameOver ? game.mineField : [],
        betAmount: game.betAmount,
        multiplier: game.multiplier,
        isWinner: game.betAmount * game.multiplier &amp;gt; 0 ? true : false,
        rounds: game.isWinner ? game.rounds : game.rounds + 1,
        winningAmount:
          game.multiplier == null ? 0 : game.betAmount * game.multiplier,
        updatedAt: game.updatedAt,
      };
    },
  },
  Mutation: {
    startGame: async (_, { betAmount, mineCount }) =&amp;gt; {
      const gameId = uuidv4();
      const mineField = generateMineField(mineCount);
      const multiplierArray = generatePayoutMultipliers(25, mineCount, 0.98);
      const updatedAt = new Date().toISOString();
      const gameData = {
        mineField,
        betAmount,
        mineCount,
        gameOver: false,
        rounds: -1, 
        positionSelected: [],
        multipliers: multiplierArray,
        updatedAt,
      };

      await redis.set(gameId, JSON.stringify(gameData));

      return { gameId, betAmount, mineCount, updatedAt };
    },
    cashoutResult: async (_, { gameId }) =&amp;gt; {
      const gameData = await redis.get(gameId);
      const game = JSON.parse(gameData);

      game.gameOver = true;
      await redis.set(gameId, JSON.stringify(game));
      return {
        mineCount: game.mineCount,
        mineField: game.gameOver ? game.mineField : [],
        betAmount: game.betAmount,
        multiplier: game.multiplier,
        isWinner: game.betAmount * game.multiplier &amp;gt; 0 ? true : false,
        rounds: game.isWinner ? game.rounds : game.rounds + 1,
        winningAmount:
          game.multiplier == null ? 0 : game.betAmount * game.multiplier,
        updatedAt: game.updatedAt,
      };
    },
    selectTile: async (_, { gameId, position }) =&amp;gt; {
      const gameData = await redis.get(gameId);
      const game = JSON.parse(gameData);

      if (game.gameOver) {
        return {
          multiplier: game.multiplier,
        };
      }

      if (!Array.isArray(game.positionSelected)) {
        game.positionSelected = [];
      }

      if (!game.positionSelected.includes(position)) {
        game.positionSelected.push(position);
        game.rounds += 1;
      }

      const isMine = game.mineField[position] === "M";
      const isGem = game.mineField[position] === "G";
      const updatedAt = new Date().toISOString();
      game.isMine = isMine;
      game.updatedAt = updatedAt;

      if (isMine) {
        game.gameOver = true;
        game.isWinner = false;
        game.multiplier = 0;
        game.winningAmount = 0;
      } else if (
        isGem &amp;amp;&amp;amp;
        !game.gameOver &amp;amp;&amp;amp;
        game.rounds &amp;lt; game.multipliers.length
      ) {
        game.multiplier = game.multipliers[game.rounds].payoutMultiplier;
        game.winningAmount = game.betAmount * game.multiplier;
      } else if (game.gameOver) {
        throw new Error("Game over. You can't select any more tiles.");
      } else {
        throw new Error("Invalid rounds index for multipliers array.");
      }

      await redis.set(gameId, JSON.stringify(game));

      return {
        isMine,
        multiplier: game.multiplier,
        winningAmount: game.winningAmount,
        updatedAt,
      };
    },
  },
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Wrapping Up&lt;/strong&gt;&lt;br&gt;
Building the backend for a Mine Game was a fantastic learning experience. If you’re interested in game development or just curious about how such games work, I hope this overview helps you get started!&lt;/p&gt;

&lt;p&gt;Soon gonna add a payment gateway to manage wallet and deploying it on AWS EKS.&lt;/p&gt;

&lt;p&gt;Try this out and feel free to comment down your queries! Stay tuned for more insights! 🚀&lt;/p&gt;

</description>
      <category>gamedev</category>
      <category>webdev</category>
      <category>redis</category>
      <category>graphql</category>
    </item>
  </channel>
</rss>
