Skip to content

Instantly share code, notes, and snippets.

@htfy96
Last active June 17, 2018 04:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save htfy96/141d43fb662dbcd3b89ce7592a80468f to your computer and use it in GitHub Desktop.
Save htfy96/141d43fb662dbcd3b89ce7592a80468f to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.4.24+commit.e67f0147.js&optimize=true&gist=
pragma solidity ^0.4.0;
contract IteratedRun {
struct City {
bytes32 name;
bool visited;
}
address owner;
City[] cities;
bytes32 startCity;
uint nextBlock;
int cur;
uint remain;
uint blockInterval;
bool returned;
constructor(bytes32 _startCity, bytes32[] names, uint _startBlock, uint _blockInterval) public {
owner = msg.sender;
startCity = _startCity;
nextBlock = _startBlock;
cur = -1; remain = names.length;
returned = false;
blockInterval = _blockInterval;
for (uint i=0; i<names.length; i++) {
cities.push(City({
name: names[i],
visited: false
}));
}
}
function decideReturn() public {
require(msg.sender == owner);
returned = true;
}
function updateToNextCity() public {
assert(cities.length > 0);
require(!returned);
if (nextBlock > block.number - 3) { // wait for 3 blocks
return;
}
log0(blockhash(nextBlock));
uint idx = uint(blockhash(nextBlock)) % remain + 1;
nextBlock = nextBlock + blockInterval;
remain--;
uint cnt = 0;
for (uint i=0; i<cities.length; i++)
if (!cities[i].visited) {
cnt++;
if (cnt == idx) {
cur = int(i);
cities[i].visited = true;
break;
}
}
}
function currentCity() view public returns (bytes32) {
if (returned) return startCity;
if (cur >= 0) {
return cities[uint(cur)].name;
} else {
return startCity;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment