Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
134 lines (107 sloc) 3.76 KB
pragma lity >=1.2.6;
contract FairPlay {
address public owner;
struct Player {
uint ts;
string name;
string contact;
string mesg;
string confirm_mesg;
}
mapping(address => Player) players;
address[] player_addresses;
address[] winner_addresses;
address[] cache;
int8 public status; // 0: running; 1: declared winners
string public title;
string public desc;
string public image_url;
uint256 public number_of_winners;
uint cutoff_ts; // in the uint of seconds from unix epoch
modifier onlyOwner() {
assert(msg.sender == owner);
_;
}
constructor (string _title, string _desc, string _image_url, uint256 _number_of_winners, uint _cutoff_ts) public {
require(_number_of_winners > 0);
require(_cutoff_ts > now + 60); // must be more than a minute from now
owner = msg.sender;
title = _title;
desc = _desc;
image_url = _image_url;
number_of_winners = _number_of_winners;
cutoff_ts = _cutoff_ts;
status = 0;
}
function play (string _name, string _contact, string _mesg) public {
require (status == 0); // Must be running
require (now < cutoff_ts); // Not expired yet
if (players[msg.sender].ts == 0) {
player_addresses.push(msg.sender);
}
Player memory np = Player (now, _name, _contact, _mesg, "");
players[msg.sender] = np;
}
function draw () public {
require (status == 0); // Must be running
require (now > cutoff_ts); // Expired
if (player_addresses.length <= number_of_winners) {
winner_addresses = player_addresses;
status = 1;
return;
}
uint i = 0;
// Construct the winner_addresses array
for (i=0; i<player_addresses.length; i++) {
cache.push(player_addresses[i]);
}
for (i=0; i<number_of_winners; i++) {
uint r = rand() % cache.length;
winner_addresses.push(cache[r]);
cache[r] = cache[cache.length-1];
delete cache[cache.length-1];
cache.length--;
}
status = 1;
}
function confirm (string _confirm_mesg) public {
require (status == 1); // Must have declared
bool is_winner = false;
for (uint i=0; i<winner_addresses.length; i++) {
if (msg.sender == winner_addresses[i]) {
is_winner = true;
break;
}
}
require (is_winner);
players[msg.sender].confirm_mesg = _confirm_mesg;
}
function info () view public returns (int8, string, string, string, uint256, uint) {
return (status, title, desc, image_url, number_of_winners, cutoff_ts);
}
function playerInfo (address _addr) view public returns (bool, uint, string, string, string, string) {
Player memory p = players[_addr];
bool is_winner = false;
for (uint i=0; i<winner_addresses.length; i++) {
if (_addr == winner_addresses[i]) {
is_winner = true;
break;
}
}
return (is_winner, p.ts, p.name, p.contact, p.mesg, p.confirm_mesg);
}
function winner_addrs () view public returns (address[]) {
address[] memory res = new address[](winner_addresses.length);
for (uint i=0; i<winner_addresses.length; i++) {
res[i] = winner_addresses[i];
}
return res;
}
function player_addrs () view public returns (address[]) {
address[] memory res = new address[](player_addresses.length);
for (uint i=0; i<player_addresses.length; i++) {
res[i] = player_addresses[i];
}
return res;
}
}
You can’t perform that action at this time.