# Games

The game contracts enable you to implement a play and earn mechanism around a game you or someone else deployed.&#x20;

Here is a step-by-step guide to creating your own game:

## Creating Your Game

* Go to the [Games page](https://payswap.org/games) and click on the **Create Game** button

<figure><img src="https://1098290585-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MgbtC1OtK6WPXJAWRcR%2Fuploads%2FDwvjl3PqM3drYjGfRqSN%2Fgame.png?alt=media&#x26;token=cd3a1cfc-a6a4-406f-9fde-4a3ca6676b80" alt=""><figcaption></figcaption></figure>

* Fill in the form that pops up and validate the transaction. This will create your game. If you're planning to use an API to process your users' scores, you should set the game contract address variable to that of PaySwap's primary address (the address that owns the [CanCan channel 1](https://payswap.org/cancan/collections/1)).&#x20;

<figure><img src="https://1098290585-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MgbtC1OtK6WPXJAWRcR%2Fuploads%2FXdc9BBKSCobFOV39aFnE%2Fgame2.png?alt=media&#x26;token=a3d5823b-0e08-43f6-8ec9-46a869a0afe8" alt=""><figcaption></figcaption></figure>

* Tick the **Mine Only** button to only list your games in order to find the game you just created faster. If you can't find your game, reload the page and try again.

<figure><img src="https://1098290585-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MgbtC1OtK6WPXJAWRcR%2Fuploads%2FzMdBjrs8Zk7zhhpe4H2d%2Fgame3.png?alt=media&#x26;token=be66e0a2-4535-4f66-8525-bb88a2737df6" alt=""><figcaption></figcaption></figure>

## Setting Up Your Game

* The next step is to update your game contract. Click on the **Control Panel** button in your betting contract's panel and then on the **Update Game** button. Fill in the form with the appropriate value for each parameter and validate the transaction

<div><figure><img src="https://1098290585-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MgbtC1OtK6WPXJAWRcR%2Fuploads%2FAjtkkRYTUaNK8edB8rjn%2Fgame4.png?alt=media&#x26;token=020bfdf8-9d0f-4b11-acac-cd483b6b3f4a" alt=""><figcaption></figcaption></figure> <figure><img src="https://1098290585-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MgbtC1OtK6WPXJAWRcR%2Fuploads%2FMzMPdZtSSEVP2xgeqmeJ%2Fgame5.png?alt=media&#x26;token=c92af776-7d80-4f5e-b750-7817b0779179" alt=""><figcaption></figcaption></figure></div>

* In the step after that, you need to update the link to play the game so users can play. In your control panel's menu, click on the **Update Info** button and fill in the form with the appropriate value for each field. Hovering your cursor over the help icon next to each parameter enables you to read a description of the parameter. Validate the form and confirm the transaction.

  <div><figure><img src="https://1098290585-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MgbtC1OtK6WPXJAWRcR%2Fuploads%2FAjtkkRYTUaNK8edB8rjn%2Fgame4.png?alt=media&#x26;token=020bfdf8-9d0f-4b11-acac-cd483b6b3f4a" alt=""><figcaption></figcaption></figure> <figure><img src="https://1098290585-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MgbtC1OtK6WPXJAWRcR%2Fuploads%2FyloMJSIErCxItmxnWR1q%2FScreenshot%20from%202024-01-24%2023-05-46.png?alt=media&#x26;token=faafefdd-0db3-4eea-9730-3883a988cde5" alt=""><figcaption></figcaption></figure></div>

  PaySwap uses the game API link to fetch scores by token ID so users can update their scores on the blockchain.

## How To Play

Here is a step-by-step guide to playing games:

* You first need to buy enough minutes to play. If you buy 30 minutes for instance, you have 30 minutes to get and submit a score from the game's site. Any score submitted after that will not be eligible for rewards from the game.
* To buy minutes, go to your game's panel and click on the **View Game** button to go to the game's page. Click on the **Mint new ticket** button and validate the transaction to mint a game NFT which is necessary to play your game and claim rewards

<figure><img src="https://1098290585-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MgbtC1OtK6WPXJAWRcR%2Fuploads%2FpAcuZXvWl7u6sK0Bs0ee%2Fgame7.png?alt=media&#x26;token=473bf267-5e58-4849-9479-b0027d8dc6a2" alt=""><figcaption></figcaption></figure>

* After minting your game NFT, the last line in the image below on your game page should change from You have 0 tickets to You have 1 ticket(s) followed by a list of your ticket ids. Remember your ticket id as you will be needing it for most interactions with your game. Our ticket ID in the image below is 2

<figure><img src="https://1098290585-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MgbtC1OtK6WPXJAWRcR%2Fuploads%2FIJBpy5FnjpbM4eUimL6j%2Fgame9.png?alt=media&#x26;token=332e6b88-75df-4f0a-89c3-1b88fa2a3af9" alt=""><figcaption></figcaption></figure>

* We can now buy minutes in our game, let's buy 30 minutes. To do so, click on the **Play to Win** button to go to the form in the image below. Fill in the form with enough tokens to buy the number of minutes you are willing to purchase and validate the transaction.

<figure><img src="https://1098290585-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MgbtC1OtK6WPXJAWRcR%2Fuploads%2F8aChW936lO4HS8ZxFsrY%2Fgame10.png?alt=media&#x26;token=950c367e-9263-46ac-b3ed-372c6ccf879d" alt=""><figcaption></figcaption></figure>

* We inputted our game NFT ID which is 2 and enough tokens to buy 30 minutes in the game. The text **Buy XX Minutes** changes as you change the number of tokens to send
* We now have to go play the game, get a score and submit that score before the 30 minutes expire or we would have lost our investment. At the top of the game page, you can view a **Play Game** button. Click on it to launch the game in a window&#x20;

<figure><img src="https://1098290585-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MgbtC1OtK6WPXJAWRcR%2Fuploads%2FIJBpy5FnjpbM4eUimL6j%2Fgame9.png?alt=media&#x26;token=332e6b88-75df-4f0a-89c3-1b88fa2a3af9" alt=""><figcaption></figcaption></figure>

* You can play the game from the pop-up or click on the link at the top of the pop-up to open the game in a new window

<figure><img src="https://1098290585-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MgbtC1OtK6WPXJAWRcR%2Fuploads%2FvhgAF3q0Py0rtNYhGKnv%2Fgame11.png?alt=media&#x26;token=119fdf59-eb05-44c0-b596-2cd960846362" alt=""><figcaption></figcaption></figure>

* Once you get a score you're satisfied with like in the image below, input your game NFT ID and click on the submit button from the game's page

<figure><img src="https://1098290585-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MgbtC1OtK6WPXJAWRcR%2Fuploads%2FY79nWkCJyt2wuyGQR47k%2Fgame12.png?alt=media&#x26;token=d5b1a4e2-3a10-483c-896a-e779b2e95a3e" alt=""><figcaption></figcaption></figure>

* As can be seen in the image above, once we got the best score we could get before the end of the 30 minutes we purchased, we entered our game NFT ID of 2 into the appropriate field on the game page and submitted it. We can keep playing until the 30 minutes expire and if we are able to get a better score we submit it as well to replace the previous one.
* You can figure out when your minutes expire by entering your game NFT ID in the form below on your game page. In our example below, we purchased our minutes at 19:12 (7:12 PM) so our minutes will end at 19:42 (7:42 PM); meaning we can try until that time to get a better score submitting each score every time to be safe.

<figure><img src="https://1098290585-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MgbtC1OtK6WPXJAWRcR%2Fuploads%2FPRpxEkjB7d8cwN6p9zsc%2Fgame13.png?alt=media&#x26;token=6b39a2f0-1831-48da-8d8b-9c87ddbddbab" alt=""><figcaption></figcaption></figure>

* After our minutes expire, we can wait for the right moment to process our score or we can process it straight away. Game contracts build up their prize pot with tokens collected from minute purchases and computes users' winnings based on their score.&#x20;

For our current scenario, our game's prize pot is 3 USD which you can view from the game's page

<figure><img src="https://1098290585-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MgbtC1OtK6WPXJAWRcR%2Fuploads%2FyhTW7TwrE6hGpKpuzqsg%2Fgame14.png?alt=media&#x26;token=ac074b85-b825-4c66-95b3-cc14c6cfbf3b" alt=""><figcaption></figcaption></figure>

Let's suppose we had a prize pot of 1000 USD and that the total addition of scores obtained in the game by users is 8000. We've gotten a score of 800 from playing our game so our winnings will be computed as such: 1000 \* 800 / 8000 = 100 USD

If we had claimed our winnings when the total scores obtained in the game was 5000 and the prize pot at 800 for instance, we would have had claimed: 800 \* 800 / 5000 = 128

So deciding when to claim your winnings is a strategic decision to make, you want to spot the moment when the total score obtained in the game is not too high but the prize pot is high. The total score is available at the top of the game page like in the image above (on the fifth line).

## How To Claim Your Winnings

* Go to the [Games page](https://payswap.org/games) and find your game
* Go to your game's control panel menu and click on the **Process Score** button. You cannot fill in the form; you just need to validate the transaction in order to write the score obtained in the game previously in the gaming contract. PaySwap will query your score from the game's API and fill in the form so you can validate it.

<div><figure><img src="https://1098290585-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MgbtC1OtK6WPXJAWRcR%2Fuploads%2FdpBqCkQD9PiTnjq0ZkGx%2Fgame15.png?alt=media&#x26;token=196f753d-67a8-46a8-9d64-06118db09a70" alt=""><figcaption></figcaption></figure> <figure><img src="https://1098290585-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MgbtC1OtK6WPXJAWRcR%2Fuploads%2FOfMJQiSm67cB7Jgx7Rab%2FScreenshot%20from%202024-01-24%2023-34-04.png?alt=media&#x26;token=64625051-7895-4b03-965e-91364985757b" alt=""><figcaption></figcaption></figure></div>

* You should be able to see the total score of the game updated with your score. The next step is to click on the **Claim Rewards** button in the control panel's menu. Validate the transaction to claim your rewards and transfer your earnings into your wallet.\
  You can either claim your rewards straight away or wait a bit for the prize pot to get bigger with more players joining the game and buying minutes. The bigger the prize pot, the more you can win but this is a bit tricky since if the new players are able to raise the average score of the game, that might reduce your share of the total earnings.\
  You basically have to pick the right time to claim your rewards, will you claim a bigger share of a smaller pot or a smaller share of a bigger pot?

## How To Turn Every Game Into A Play To Earn Game

* Add a form on the game site to enable users to update their scores. In the example image below, users can input their game token ID and click on the Save Score button whenever they have a score they are satisfied with and the game will register their score (saved under the name **score**) as well as the time of registration of the score (saved under the name **deadline**) and the time the user started playing the game (saved under the name **startime**) on the Firebase database (you can use any database you want). \
  Make sure your games keeps track of the moment your users start playing their games (**startime**) as this parameter is extremely important to make sure users can only register scores for game minutes they've purchased. Without it, a user can spend hours maximizing his/her score for a certain game, then just buy 1 minute from the games' page on PaySwap and use that to register the score. With a **startime** parameter, we would be able to detect the discrepancy between the minutes purchased and the actual minutes played.

<figure><img src="https://1098290585-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MgbtC1OtK6WPXJAWRcR%2Fuploads%2F9JGQ7MgjIn1rtOBF4wn0%2FScreenshot%20from%202024-01-24%2023-10-20.png?alt=media&#x26;token=b0c060a6-ed88-451c-b94e-292f43aad70c" alt=""><figcaption></figcaption></figure>

* Deploy the game contract following the steps above and make sure you update the game information with an API link that will return your users' scores and deadlines
* For our Mario game, our API link is: \
  [https://firestore.googleapis.com/v1/projects/tiktok-a2bdb/databases/(default)/documents/c4/](https://firestore.googleapis.com/v1/projects/tiktok-a2bdb/databases/\(default\)/documents/c4/1)
* This is what we will be updating our game with. To fetch the score and a deadline of token ID #1 for instance, PaySwap will just pad the token ID #1 to the API link that we've registered which will give this\
  <https://firestore.googleapis.com/v1/projects/tiktok-a2bdb/databases/(default)/documents/c4/1>\
  PaySwap is just padding the user's token ID to your API link so make sure that your saved API link has a trailing slash (/)\
  Querying this endpoint returns the score and deadline that has been registered for the token ID 1 by its owner in the following format. Make sure the startime & deadline parameters are saved in seconds and not milliseconds

<figure><img src="https://1098290585-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MgbtC1OtK6WPXJAWRcR%2Fuploads%2FJa30EY0KQqq54OSlyOdm%2F1.png?alt=media&#x26;token=9e3f8894-6b49-42ff-84a8-e616fd5e5189" alt=""><figcaption></figcaption></figure>

As long as your API follows the format above and returns an object with the format above, the system should have no problem updating your users' score information on the blockchain which is a necessary step for them to earn rewards.

Congratulations, your game now has a Play-to-earn component to it!!! Your users can now:

* &#x20;see your game from the [games page](https://payswap.org/games)&#x20;
* buy enough minutes in it for them to do their best scores
* &#x20;update their scores to your centralized database&#x20;
* use the PaySwap platform to report that score to the blockchain through an API that you've provided
* claim their earnings if their scores are high enough

[Get further support from the community here.](https://paychat.payswap.org/#/room/%23paycom%3Amatrix.org?via=matrix.org)
