game-smart-contract

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract GamePoints {
    address public owner;
    address public relayer; // Background wallet that pays the gas fees

    struct Player {
        uint256 totalPoints;
        mapping(uint256 => uint256) gamePoints; // Mapping of game ID to points
    }

    mapping(address => Player) public players;

    event GamePointsUpdated(address indexed player, uint256 gameId, uint256 points, uint256 totalPoints);

    constructor(address _relayer) {
        owner = msg.sender;
        relayer = _relayer;
    }

    modifier onlyRelayer() {
        require(msg.sender == relayer, "Only the relayer can perform this action");
        _;
    }

    // Relayer updates game points, gameId, and total points for the user
    function updateGamePoints(
        address _player,
        uint256 _gameId,
        uint256 _points
    ) external onlyRelayer {
        players[_player].gamePoints[_gameId] = _points;
        players[_player].totalPoints += _points;

        emit GamePointsUpdated(_player, _gameId, _points, players[_player].totalPoints);
    }

    // Function to retrieve a player's points for a specific game
    function getGamePoints(address _player, uint256 _gameId) external view returns (uint256) {
        return players[_player].gamePoints[_gameId];
    }

    // Retrieve total points for a player
    function getTotalPoints(address _player) external view returns (uint256) {
        return players[_player].totalPoints;
    }

    // Allow the owner to change the relayer address
    function setRelayer(address _newRelayer) external {
        require(msg.sender == owner, "Only the owner can change the relayer");
        relayer = _newRelayer;
    }
}

Last updated