//Write your own contracts here. Currently compiles using solc v0.4.15+commit.bbb8e64f. pragma solidity ^0.4.18; /** * The goal is to keep looping and return the next user * For example; * 'add (accounts[1], 0)' and 'add (accounts[2], 0)' should result in * * newEntity.entityAddress = accounts[1] * newEntity.entityData = BetRound + 1 (in this case, 1) * * newEntity.entityAddress = accounts[2] * newEntity.entityData = BetRound + 1 (in this case, 1) * * The entityData field is the round from where the user is allowed to participate. * * Our loop should start to run, and if no candidate is suitable to participate, it should * go to the next round by setting the variable 'BetRound'++. * * If there are users who's entityData =< BetRound, we should return the index under wich they are stored. * For ease of testing, i adjusted this in returning the address itself. * If we found a user, we set the variable `SelectedUser` to the corresponder index and update the entityData++ to participate in the next round. * * Now the real issue is that at the end of each loop (when there are no more users, and the `BetRound` increases, * address 0x0 is always returned as first in line. The rest is working as expected, but i don't understand why it always returns 0x0. * */ contract arrayWithUniqueIds { uint public SelectedUser; uint public BetRound; struct EntityStruct { address entityAddress; uint entityData; // round } EntityStruct[] public entityStructs; mapping(address => bool) knownEntity; function isEntity(address entityAddress) public constant returns(bool isIndeed) { return knownEntity[entityAddress]; } function getEntityCount() public constant returns(uint entityCount) { return entityStructs.length; } function newEntity(address entityAddress, uint entityData) public returns(uint rowNumber) { if (isEntity(entityAddress)) throw; EntityStruct memory newEntity; newEntity.entityAddress = entityAddress; newEntity.entityData = entityData; knownEntity[entityAddress] = true; return entityStructs.push(newEntity) - 1; } function updateEntity(uint rowNumber) public returns(bool success) { entityStructs[rowNumber].entityData = BetRound + 1; return true; } function loop() public returns(address) { // If we reached the end of our array // it should mean we looped all users already // We need to increment the BetRound if (SelectedUser >= entityStructs.length) { BetRound++; SelectedUser = 0; } for (uint i = SelectedUser; i < entityStructs.length; i++) { if (entityStructs[i].entityData < BetRound) { updateEntity(i); SelectedUser = i; return entityStructs[i].entityAddress; } } BetRound++; } }
0.4.18