Chainsafe Gaming web3.unity Calling a function Without metamask interaction

249 views Asked by At

I am making a WebGL game in unity in which two player can play match with each other. At the end of the game winner be decided by game. My game will call declareWinner function of my contract that you can see below.

The issue is that it will always need me to manually interact with metamask and proceed the transaction. I just want an automate process where game just call declareWinner function and it should do rest to the work. Note that i also have JoinRoom function that will create a new room and save all data of each room including the winning price.

I have deployed my Smart Contract on BSC testnet and I am using Chainsafe gaming web3.unity SDK https://docs.gaming.chainsafe.io/.

Currently I am using the latest version of Chainsafe gaming SDK(v2.1.0).

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.19;

contract FPS {
    struct Room {
        address player1;
        address player2;
        bool isFull;
        uint256 winnings;
        bool ended;
        address winner;
    }
 function declareWinner(string memory roomId, address winner)
        external
        onlyOwner
    {
        Room storage room = rooms[roomId];
        require(room.isFull, "Room is not full");
        require(
            winner == room.player1 || winner == room.player2,
            "Winner must be one of the players of this room"
        );
        require(!room.ended, "This room has already ended");
        room.ended = true;
        room.winner = winner;
        uint256 winnerAmount = room.winnings;
        payable(winner).transfer(winnerAmount);
    }

}

As I have tried this. This open Metamask for transaction to manually proceed.

public async void DeclareWinner()
    {
        string contractAbi = ContractDetails.ContractABI;
        string contractAddress = ContractDetails.ContractAddress;
        string method = "declareWinner";

        string roomId = RoomIDInput.text;
        string winner = Winner.text;
        string[] obj = { roomId, winner };

        string args = JsonConvert.SerializeObject(obj);

        string value = "0";
        string gasLimit = "200000";
        string gasPrice = "20000000000";
        try
        {
            string response = await Web3GL.SendContract(method, contractAbi, contractAddress, args, value, gasLimit, gasPrice);
            WinnerTxt.text=Winner.text;
        }
        catch (Exception e)
        {
            Debug.Log(e, this);
        }
    } 
0

There are 0 answers