I am fairly new to solidity and I am trying to create a lottery contract where every address needs to pay 1 ether to get added to the lottery pool and at the end, the owner would randomly select one winner. There are 2 conditions which are important
(a) If an address has already been added to the pool then he can't be added again for that round(i.e till a winner has been selected).
(b) However, once that round is over the whole contract will reset (i.e all the earlier details like the addresses which had entered the pool and the structure details mapped to those addresses would be deleted) and the earlier address can be added again to the new round.
My code below satisfies the condition 1 but the condition 2 is not working and once the contract resets itself I can't get the earlier players to be added again to the new round. I am assuming the struct data is not resetting. Any help would be greatly appreciated
contract Lottery
{
//Defining the Owner
address Owner;
constructor() public
{
Owner = msg.sender;
}
// Defining players
struct person
{
string name;
uint age;
bool played;
}
// One to one mapping of player and its address
mapping (address => person) players;
// creating a list/array of players
address payable[] playersAddressList;
// Tracking balance;
uint public balance=0;
// modifier for Owner Only
modifier ownerOnly(){
require(msg.sender==Owner);
_;
}
function addPlayer(string memory a, uint b) public payable
{
address payable creator = msg.sender;
// Checking if the player has played earlier or not for that particular round
require (players[creator].played == false);
// If has not played then adding it to the player array
playersAddressList.push(creator);
// Updating the balance the contract has
balance = balance + msg.value;
require (msg.value >= 1 ether);
// Entering the input for the structure
players[creator].name = a;
players[creator].age = b;
players[creator].played = true;
}
//Function to select a winner
function selectWinner() public ownerOnly {
uint a = random() % playersAddressList.length;
address payable winner;
winner = playersAddressList[a];
winner.transfer(balance);
balance = 0;
delete playersAddressList;
}
//displa the players list array length
function displayDetails(uint a) public view returns(uint, string memory, bool){
return(players[playersAddressList[a]].age,players[playersAddressList[a]].name,players[playersAddressList[a]].played);
}
//Generating a pseudo random number
function random() private view returns(uint){
return uint(keccak256(abi.encodePacked(block.difficulty, now, playersAddressList)));
}
}```
but the condition 2 is not working and once the contract resets itself I can get the earlier players to be added I again
- first of all, that's exactly what your 2nd requirement states, so I don't quite see what the problem is. Second, you need to explain whatthe contract resets itself
means to begin with! – goodvibration Oct 4 '20 at 12:13all the earlier details like the addresses which had entered the pool and the structure details mapped to those addresses would be deleted
- where exactly is that in your code??? – goodvibration Oct 4 '20 at 13:04