# Games

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

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://3481829468-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FbuuWgsRQF3QPmDvV7XJQ%2Fuploads%2Fgit-blob-2a3fc485d4b36c7a6e6d34375dda04c65f247cb2%2Fgame.png?alt=media" 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)).

<figure><img src="https://3481829468-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FbuuWgsRQF3QPmDvV7XJQ%2Fuploads%2Fgit-blob-e5112fa7ebc370dcff133457c9fca93e2e3caebd%2Fgame2.png?alt=media" 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://3481829468-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FbuuWgsRQF3QPmDvV7XJQ%2Fuploads%2Fgit-blob-223078f700945a55776947d2e793d2bc7bd61c1f%2Fgame3.png?alt=media" 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://3481829468-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FbuuWgsRQF3QPmDvV7XJQ%2Fuploads%2Fgit-blob-2f260a43732a3e1735a0b505a16413f8840afa81%2Fgame4.png?alt=media" alt=""><figcaption></figcaption></figure> <figure><img src="https://3481829468-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FbuuWgsRQF3QPmDvV7XJQ%2Fuploads%2Fgit-blob-c6e30048f4b446ae57fb22c15b2f18da9c69b63a%2Fgame5.png?alt=media" 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://3481829468-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FbuuWgsRQF3QPmDvV7XJQ%2Fuploads%2Fgit-blob-2f260a43732a3e1735a0b505a16413f8840afa81%2Fgame4.png?alt=media" alt=""><figcaption></figcaption></figure> <figure><img src="https://3481829468-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FbuuWgsRQF3QPmDvV7XJQ%2Fuploads%2Fgit-blob-663cb4d407e8614dda788aec0edcc285ee7130c8%2FScreenshot%20from%202024-01-24%2023-05-46.png?alt=media" 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://3481829468-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FbuuWgsRQF3QPmDvV7XJQ%2Fuploads%2Fgit-blob-99d4a1b49f2448d49e60cf8f0ce86c87077e086c%2Fgame7.png?alt=media" 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://3481829468-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FbuuWgsRQF3QPmDvV7XJQ%2Fuploads%2Fgit-blob-d8431e38bd3a9dfb727237f4178837ca74802684%2Fgame9.png?alt=media" 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://3481829468-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FbuuWgsRQF3QPmDvV7XJQ%2Fuploads%2Fgit-blob-d7eba8340822b19b738506e759b74392dc822d29%2Fgame10.png?alt=media" 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

<figure><img src="https://3481829468-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FbuuWgsRQF3QPmDvV7XJQ%2Fuploads%2Fgit-blob-d8431e38bd3a9dfb727237f4178837ca74802684%2Fgame9.png?alt=media" 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://3481829468-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FbuuWgsRQF3QPmDvV7XJQ%2Fuploads%2Fgit-blob-276386e6db609ef9930fd9416fa76bfb84fdf1d3%2Fgame11.png?alt=media" 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://3481829468-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FbuuWgsRQF3QPmDvV7XJQ%2Fuploads%2Fgit-blob-b62faae94c92c543a253698a98b46a0497c0112e%2Fgame12%20(1).png?alt=media" 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://3481829468-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FbuuWgsRQF3QPmDvV7XJQ%2Fuploads%2Fgit-blob-783eb100cef6315f7290705e2f3a29ebf7078dbe%2Fgame13.png?alt=media" 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.

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://3481829468-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FbuuWgsRQF3QPmDvV7XJQ%2Fuploads%2Fgit-blob-485052497b691598685ad0c9a4354e0917d43ca4%2Fgame14.png?alt=media" 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://3481829468-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FbuuWgsRQF3QPmDvV7XJQ%2Fuploads%2Fgit-blob-73b7dca0586d6f31de45c39e23578c17daf794ca%2Fgame15.png?alt=media" alt=""><figcaption></figcaption></figure> <figure><img src="https://3481829468-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FbuuWgsRQF3QPmDvV7XJQ%2Fuploads%2Fgit-blob-17e66b56d09b95fa3281781c2c997c0f33c3d07b%2FScreenshot%20from%202024-01-24%2023-34-04.png?alt=media" 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://3481829468-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FbuuWgsRQF3QPmDvV7XJQ%2Fuploads%2Fgit-blob-82ec47f39662a339a150a951e526904a60642271%2FScreenshot%20from%202024-01-24%2023-10-20.png?alt=media" 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://3481829468-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FbuuWgsRQF3QPmDvV7XJQ%2Fuploads%2Fgit-blob-9cad9c9dbc065f4bf4a16f759a84bf1255b953f6%2F1.png?alt=media" 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:

* see your game from the [games page](https://payswap.org/games)
* buy enough minutes in it for them to do their best scores
* update their scores to your centralized database
* 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)
