Ethereum newbie here. I'm trying to implement a simple 4-number lottery game, based on the amount of Wei sent.
Example
Account 0 entered the lottery with 1234 Wei.
Account 1 entered the lottery with 5678 Wei.
If the manager ran pickWinner(1234), Account 0 will win and get the money since he entered with the winning number.
Problem
It seems like players = new Player[](0) is not allowed.
UnimplementedFeatureError: Copying of type struct Lottery.Player memory[] memory to storage not yet supported.
Is there any way to clear an array of structs? or is there a better way to approach this lottery mechanism? Thanks! :)
pragma solidity ^0.4.17;
contract Lottery {
address public manager;
struct Player {
address recipient;
uint value;
}
Player[] public players;
mapping(uint => address[]) recipientValue;
mapping(uint => uint) recipientValueCount;
function Lottery() public {
manager = msg.sender;
}
function enter() public payable {
Player memory newPlayer = Player({
recipient: msg.sender,
value: msg.value
});
players.push(newPlayer);
recipientValue[msg.value].push(msg.sender);
recipientValueCount[msg.value]++;
}
function pickWinner(uint index) public restricted {
address[] storage winners = recipientValue[index];
uint prize = this.balance / recipientValueCount[index];
for(uint i=0; i< winners.length; i++){
winners[i].transfer(prize);
}
players = new Player[](0);
}
function totalBalance() public view returns (uint) {
return this.balance;
}
modifier restricted() {
require(msg.sender == manager);
_;
}
}