我正在统一制作一个 WebGL 游戏,两个玩家可以在其中进行比赛。在游戏结束时,获胜者由游戏决定。我的游戏将调用我的合约的 declareWinner 函数,您可以在下面看到它。
问题是它总是需要我手动与 metamask 交互并进行交易。我只想要一个自动化过程,其中游戏只调用 declareWinner 函数,它应该完成工作。请注意,我还有 JoinRoom 功能,它将创建一个新房间并保存每个房间的所有数据,包括中标价格。
我已经在 BSC 测试网上部署了我的智能合约,我正在使用 Chainsafe 游戏 web3.unity SDK https://docs.gaming.chainsafe.io/.
目前我使用的是最新版本的 Chainsafe 游戏 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);
}
}
因为我试过这个。这将打开 Metamask 以供手动进行交易。
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);
}
}