dossinstitute / sidequest

Apache License 2.0
0 stars 0 forks source link

Quest Completion Check #6

Open wvwatson opened 2 weeks ago

wvwatson commented 2 weeks ago

Ticket: Quest Completion Check

Description: Develop functionality to automatically check if a user has completed the required number of interactions for a quest. Once the requirement is met, mark the quest as completed for the user.

Acceptance Criteria:

  1. The contract should automatically verify if a user has met the required number of interactions for a quest.
  2. Once the required number of interactions is met, the quest should be marked as completed for that user.
  3. Ensure that the completion status can be retrieved for any user and quest.
**Solidity Smart Contract:**

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract EventAmbassadorQuest {

    address public admin;

    struct Quest {

        uint256 eventId;

        uint256 startDate;

        uint256 endDate;

        uint256 requiredInteractions;

        string rewardType; // "NFT" or "token"

    }

    struct UserInteraction {

        bool isRegistered;

        uint256 interactionsCount;

        bool isCompleted;

    }

    mapping(uint256 => Quest) public quests;

    mapping(uint256 => mapping(address => UserInteraction)) public userInteractions;

    event QuestCreated(uint256 eventId, uint256 startDate, uint256 endDate, uint256 requiredInteractions, string rewardType);

    event UserRegistered(uint256 eventId, address user);

    event QuestCompleted(uint256 eventId, address user);

    modifier onlyAdmin() {

        require(msg.sender == admin, "Only admin can perform this action");

        _;

    }

    constructor() {

        admin = msg.sender;

    }

    function createQuest(

        uint256 _eventId, 

        uint256 _startDate, 

        uint256 _endDate, 

        uint256 _requiredInteractions, 

        string memory _rewardType

    ) public onlyAdmin {

        require(_requiredInteractions >= 3, "Required interactions must be at least three");

        require(bytes(_rewardType).length > 0, "Reward type must be specified");

        quests[_eventId] = Quest({

            eventId: _eventId,

            startDate: _startDate,

            endDate: _endDate,

            requiredInteractions: _requiredInteractions,

            rewardType: _rewardType

        });

        emit QuestCreated(_eventId, _startDate, _endDate, _requiredInteractions, _rewardType);

    }

    function registerForQuest(uint256 _eventId) public {

        require(quests[_eventId].eventId != 0, "Quest does not exist");

        require(!userInteractions[_eventId][msg.sender].isRegistered, "User already registered");

        userInteractions[_eventId][msg.sender] = UserInteraction({

            isRegistered: true,

            interactionsCount: 0,

            isCompleted: false

        });

        emit UserRegistered(_eventId, msg.sender);

    }

    function submitInteraction(uint256 _eventId) public {

        require(userInteractions[_eventId][msg.sender].isRegistered, "User is not registered for this quest");

        require(!userInteractions[_eventId][msg.sender].isCompleted, "Quest already completed");

        userInteractions[_eventId][msg.sender].interactionsCount++;

        if (userInteractions[_eventId][msg.sender].interactionsCount >= quests[_eventId].requiredInteractions) {

            userInteractions[_eventId][msg.sender].isCompleted = true;

            emit QuestCompleted(_eventId, msg.sender);

        }

    }

    function isQuestCompleted(uint256 _eventId, address _user) public view returns (bool) {

        require(userInteractions[_eventId][_user].isRegistered, "User is not registered for this quest");

        return userInteractions[_eventId][_user].isCompleted;

    }

    function getUserInteractionCount(uint256 _eventId, address _user) public view returns (uint256) {

        require(userInteractions[_eventId][_user].isRegistered, "User is not registered for this quest");

        return userInteractions[_eventId][_user].interactionsCount;

    }

}

Acceptance Criteria Validation:

  1. Interaction Submission and Quest Completion Check:
    • The submitInteraction function allows users to submit an interaction.
    • Automatically checks if the user’s interaction count meets or exceeds the required number for quest completion.
    • Marks the quest as completed for the user if the requirement is met.
  2. Track Quest Completion:
    • The UserInteraction struct includes an isCompleted flag to indicate quest completion status.
    • The isQuestCompleted function allows retrieval of the quest completion status for any user and quest.
  3. Events:
    • The QuestCompleted event is emitted upon quest completion, providing transparency and tracking.
  4. Retrieve User Interaction Count:
    • The getUserInteractionCount function allows retrieval of the number of interactions a user has submitted for a specific quest.

This contract component ensures that the required number of interactions for quest completion is automatically checked and that the quest is marked as completed for the user upon meeting the requirement.

wvwatson commented 2 weeks ago

points: 1, 2, 3, 5, 8, 13, 21

@russlive215 8 @Fr0z0n3 13 @noireconnect2024 13 @wvwatson 8

points: 13