## Tamagotchi battle Tamagotchi Battle is a thrilling multiplayer game that allows players to engage in exciting battles with their Tamagotchis. To participate in Tamagotchi Battle, players must first upload the smart contract of their tamagotchi onto the network. ### Deploying Tamagotchi Let's upload a Tamagotchi contract. Follow the [link](https://github.com/gear-dapps/workshop-tamagotchi) provided and clone or open the repository in Gitpod. Complete two tasks in the contract code located in the `src/lib.rs` file: - The first task is to set the Tamagotchi's name during initialization. ```rust #[no_mangle] unsafe extern "C" fn init() { // ⚠️ TODO: Change the tamagotchi name let name = String::from("Best-Tamagotchi"); ... } ``` - The second task is to add handling for a message requesting information about the Tamagotchi's age. ```rust #[no_mangle] extern "C" fn handle() { ... TmgAction::Age => { let age = exec::block_timestamp() - tmg.date_of_birth; // ⚠️ TODO: Send a reply about the Tamagotchi age // Hint: the message payload must be TmgReply::Age(age) } ... } ``` Once your project has been built, you will need two files to upload the contract to the network: - `tmg.opt.wasm` file located in the `target/wasm32-unknown-unknown/release` directory. This is the optimized Wasm file that should be uploaded to the blockchain. - `tmg.meta.txt` file located in the root of the project. It contains information about the smart contract interface and is necessary for interacting with the contract. Follow the steps described in the [link](https://wiki.gear-tech.io/docs/getting-started-in-5-minutes#deploy-your-smart-contract-to-the-testnet) and deploy the Tamagotchi to the network. As the game is hosted on the workshop node, you need to upload the contract to the `wss://node-workshop.gear.rs` node. ### Battle Once the smart contract has been uploaded, players can register their tamagotchis for the battle. The battle can be found at the following [link](http://ec2-54-219-144-107.us-west-1.compute.amazonaws.com:30696/). Copy the address of your Tamagotchi and paste it into the registration field. ![](https://i.imgur.com/Wqr12uj.png) When all the participants have registered, the admin starts the game: ![](https://i.imgur.com/ZyOWqAE.png) The contract divides the Tamagotchis into pairs and begins the game. The battles take place concurrently, and once all the battles have ended, the contract divides the winners into pairs again and starts a new round. The Tamagotchis continue competing until there is only one winner left. On the left side, there is a pop-up window displaying the ongoing battles, while on the right side, there is a leaderboard. ![](https://i.imgur.com/GxhyonO.png) Before the battle begins, Tamagotchis are given the same level of health. Additionally, each Tamagotchi has randomly generated levels of defense and attack. <center> <img src="https://i.imgur.com/uZjl50p.png" width=300> </center> A Tamagotchi battle is comprised of 5 rounds, with each round consisting of two turns - one for each Tamagotchi and its opponent. After each round, the levels of attack and defense are regenerated. Based on the levels of attack and defense of his own Tamagotchi and its opponent, the player chooses whether to attack or defend. Players do not know what move their opponent made. Moves are revealed after the round is complete. ![](https://i.imgur.com/ajSXy9z.png) Each turn is limited to one minute. If a player fails to make a move within the time limit, the turn is forfeited to the opponent. A Tamagotchi that misses its turn is penalized by receiving randomly generated damage to its health. Game automation is achieved through delayed messages. During the registration of players, the contract reserves a certain amount of gas in case a player misses their turn. When a player makes a move, a delayed message is sent that checks whether his opponent has taken his turn after one minute. If the opponent has not made a move, then the contract takes gas from his reservation and sends a delayed message using that gas. If the reserved gas runs out, it means that the Tamagotchi has missed enough turns and is considered to have lost the game. ![](https://i.imgur.com/UaZlVQW.jpg)