Skip to content

Latest commit

 

History

History
105 lines (67 loc) · 4.3 KB

File metadata and controls

105 lines (67 loc) · 4.3 KB

BuySystem Smart Contract

The BuySystem contract enables players in a match to purchase pieces within the game using a commit-reveal mechanism.

Commit-Reveal Mechanism

  1. Commit: Players initially commit to the pieces they wish to buy by encoding this information. Encoding ensures that other players cannot discern which pieces are being purchased.
  2. Reveal: Players subsequently reveal their encoded hash to confirm their purchase intentions for the game.

Contract Functionalities

  • Commit and Reveal Purchases: Allows players to securely commit to and reveal their intended purchases.
  • Generate Encoded Hashes: Provides the functionality to generate encoded hashes for the commit phase.
  • Abort Match: Includes the ability to abort a match if necessary.

commitBuy

function commitBuy(bytes32 _commitHash, bytes32 _matchEntity) public;

Allows a player to commit to buying pieces for a match by submitting a commit hash, securely recording their purchase intentions.

  • Allows a player to commit to buying pieces for a match by submitting a commit hash, securely recording their purchase intentions.

  • The function checks if the time control for committing has passed. If the time has passed, the commitment is not processed. It also verifies that the player's current spawn status allows committing. If the player's status is not None, indicating they are not in the correct state to commit, the function will revert with an error to prevent the transaction.

  • Parameters:

    • commitHash: The hash representing the committed pieces the player intends to buy.
    • matchEntity: The unique identifier of the match entity.
  • Sample Code:

bytes32 commitHash = 0xabc...;  // Example generated hash
bytes32 matchEntity = 0x000000000000000000000000000000000000000000000000000000000000beef;

world.tacticsWar__commitBuy(commitHash, matchEntity);

Note: The commit phase must be within the allowed time frame, or the commitment will not be processed.


revealBuy

 function revealBuy(
        bytes32 _matchEntity,
        uint256[] memory _pieceTypes,
        bytes32 _secret
 ) public
  • Allows a player to reveal their previously committed purchases for a match by providing the types of pieces and corresponding secret. This process finalizes the player's purchase intentions and updates their game state accordingly.

  • The function ensures that the reveal occurs within the permitted time frame. If the time control has expired, the reveal is not processed. Additionally, it verifies the validity of the provided secret and checks that the player has sufficient gold to cover the cost of the revealed pieces. If these conditions are not met, the function will revert to prevent the transaction.

  • Parameters:

  • matchEntity: The unique identifier of the match entity.

  • pieceTypes: An array of integers representing the types of pieces to be revealed.

  • secret: A single bytes32 value representing the secret associated with the committed pieces.

  • Sample Code:

bytes32 matchEntity = 0x000000000000000000000000000000000000000000000000000000000000beef;

uint256[] pieceTypes = new uint256[](2);
pieceTypes[0] = 1;
pieceTypes[1] = 2;

bytes32 secret = 0xabc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc1;

world.tacticsWar__revealBuy(matchEntity, pieceTypes, secret);

Note: The secret parameter is used to ensure the security of the reveal, tying it to the player's previous commitment.


abortBuy

function abortBuy(bytes32 _matchEntity) public;
  • Aborts the game if the preparation time control has passed for the specified match entity, ensuring that matches that exceed their preparation time are terminated.

  • The function checks whether the preparation time for the match entity has elapsed. If the preparation time has passed, the game is aborted to prevent further actions.

  • Parameters:

    • matchEntity: A bytes32 value representing the unique identifier of the match.
  • Sample Code

bytes32 matchEntity = 0x000000000000000000000000000000000000000000000000000000000000beef;

world.tacticsWar__abortBuy(matchEntity);

Note: The match can only be aborted if the preparation time control has passed. If the preparation time is still active, the function will not proceed