1

How to check if data populates in mapping and how to get the value and display for checking?

     address[] public players;
   address public owner;
    struct Bet {
      uint256 amountBet;
      uint16 matchSelected;
      uint16 resultSelected;
    //   address[] players;
   }
   struct Player {
      uint256 amountBet;
      uint16 matchSelected;
      uint16 resultSelected;
      uint numFunders;
      mapping (uint => Bet) Bets;
   }
   mapping(uint16 => bool) matchBettingActive;

   mapping(address => Player) public playerInfo;

function for bet:

function bet(uint16 _matchSelected, uint16 _resultSelected) public payable {
  
  Player storage c = playerInfo[_matchSelected];
  
  require(matchBettingActive[_matchSelected], "Betting: match voting is disabled");
  //Check if the player already exist
  require(!checkIfPlayerExists(msg.sender, _matchSelected));

  //Check if the value sended by the player is higher than the min value
  require(msg.value >= minimumBet);
  
  //Set the player informations : amount of the bet, match and result selected
  playerInfo[msg.sender].amountBet = msg.value;
  playerInfo[msg.sender].matchSelected = _matchSelected;
  playerInfo[msg.sender].resultSelected = _resultSelected;
  c.Bets[c.numFunders++] = Bet({ amountBet:msg.value,matchSelected: _matchSelected,resultSelected: _resultSelected});
  
  //Add the address of the player to the players array
  players.push(msg.sender);

  //Finally increment the stakes of the team selected with the player bet
  if ( _resultSelected == 1){
      totalBetHome[_matchSelected] += msg.value;
  }
  else if( _resultSelected == 2){
      totalBetAway[_matchSelected] += msg.value;
  }
  else{
      totalBetDraw[_matchSelected] += msg.value;
  }

}

1 Answer 1

0

You can call the mapping variable with the correct Id and the check if an attribute of the struct you get is different than their "Zero" value. you can chose an attribute that you know is always different than "Zero" like numFunders.

you can do the following :

if(playerInfo[anAddress].numFunders > 0){
    // this means that the slot with the anAddress is populated. 
    // you can get the data the same way too.
}

here is an example of accessing the struct data inside the mapping :

uint256 amountBet = playerInfo[address].amountBet;

uint16 matchSelected = playerInfor[address].matchSelected;

6
  • So the function would be like this: function currentValue(address userAddress) public view{ if(playerInfo[userAddress].numFunders > 0){ // this means that the slot with the anAddress is populated. // you can get the data the same way too. } } Commented Aug 28, 2021 at 8:54
  • yeah, the condition i put in the answer is the condition that will tell you that you have populated the mapping slot or not. mapping have default "zero" values for their value (of the key value pair) in this case the value is a struct and it's zero equals a zero for all it's attributes. Commented Aug 28, 2021 at 9:18
  • oh see. Thank you, how to get the data from Player Struct to display the playerInfo? Is this the full function on displaying data of mapping? if(playerInfo[anAddress].numFunders > 0){ // this means that the slot with the anAddress is populated. // you can get the data the same way too. } Commented Aug 28, 2021 at 14:03
  • no, you need to access the info one attribute each time. Commented Aug 28, 2021 at 14:06
  • can you give an example for that? Commented Aug 28, 2021 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.